-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARSDocument.cpp
More file actions
383 lines (361 loc) · 13.1 KB
/
ARSDocument.cpp
File metadata and controls
383 lines (361 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// ARSDocument.cpp: implementation of the CARSDocument class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IR.h"
#include "ARS.h"
#include "KeyWord.h"
#include "ARSDocument.h"
#include "IndexTermsInDoc.h"
#include "FilePrepare.h"
#include "DBManpulation.h"
#include "Indexer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//////////////////Defintion of the STATIC Members in file Scope///////
int CARSDocument::nTotalDocs ;
//CMapStringToOb CARSDocument::WordDFiCollection;
//CMapStringToOb CARSDocument::WordCountCollection;
///////////////////////////////////////////////////////////////////////
extern CDBManpulation ARSdb;
extern CIndexer ARSIndexer;
extern CKeyWord ARSKWords;
//////////////////////////////////////////////////////////////////////
CFilePrepare fpDocs;
IMPLEMENT_DYNAMIC( WordCountStruct, CObject )
IMPLEMENT_DYNAMIC( WordDFI_Struct, CObject )
IMPLEMENT_DYNAMIC( WordStruct, CObject )
CARSDocument::CARSDocument()
{
}
CARSDocument::~CARSDocument()
{
}
BOOL CARSDocument::PrepareDocument(CStringList* DocPathList)
{
WordDFiCollection.RemoveAll();
nTotalDocs = 0;
CString sCurrentDoc;
ARSdb.AddNewDoc(DocPathList); // for recording documents that been processed
// in the ARS database ...
for(POSITION pos = DocPathList->GetHeadPosition(); pos != NULL;)
{
sCurrentDoc = DocPathList->GetNext(pos);
CountFreq(sCurrentDoc);
}
CalculateWeight(DocPathList);
return TRUE;
}
void CARSDocument::CountFreq(CString DocPath)
{
WordCountStruct* pWC;
WordDFI_Struct* pWD;
WordStruct* pWS;
char * token;
CMapStringToOb WordUpdateList;
CMapStringToOb WordCountCollection;
WordCountCollection.RemoveAll(); // for every collection of documents ...
if( fpDocs.OpenFile(DocPath.GetBuffer(DocPath.GetLength())) )
{
token = ::strtok(fpDocs.pFileContent, (char*)fpDocs.seps);
while(token != NULL && strlen(token) > 14)
token = ::strtok(NULL, (char*)fpDocs.seps);
while(token != NULL)
{
// check if word found increment Frequency and nDFi
// else add it in word map put 1 in Frequency and nDFi++
// take care of not resetting nDFi, only Frequency...
if( !ARSdb.MatchCommonWord(token) )
{ // if the token not in the StopList do ...
if(WordCountCollection.Lookup((const char*)token, ( CObject*& )pWC))
{
WordCountCollection.SetAt(pWC->WordValue, new WordCountStruct(++pWC->Frequency, pWC->WordValue));
delete pWC;
}
else
{
WordCountCollection.SetAt((const char*)token, new WordCountStruct(1, (char*)token));
}
if( !WordUpdateList.Lookup((const char*)token, ( CObject*& )pWS) )
WordUpdateList.SetAt((const char*)token, new WordStruct((char*)token));
}
do{
token = ::strtok(NULL, (char*)fpDocs.seps);
}
while(token != NULL && strlen(token) > 14);
}
nTotalDocs++;
{ // this block for updating the DFI of the words extracted from current doc. ...
CString key;
for(POSITION pos = WordUpdateList.GetStartPosition();pos!=NULL;)
{
WordUpdateList.GetNextAssoc( pos, key, (CObject*&)pWS);
WordCountCollection.Lookup((const char*)pWS->WordValue, ( CObject*& )pWC);
if( WordDFiCollection.Lookup((const char*)pWS->WordValue, ( CObject*& )pWD) )
{
WordDFiCollection.SetAt((const char*)pWS->WordValue, new WordDFI_Struct(++pWD->nDFi, pWC->Frequency + pWD->nGFi, (char*)pWD->WordValue,0,0,FALSE));
delete pWD;
}
else
{
WordDFiCollection.SetAt((const char*)pWS->WordValue, new WordDFI_Struct(1, pWC->Frequency,(char*)pWS->WordValue, 0,0,FALSE));
}
}
}//////////////////////////////////////////////////////////////////////////////////
/*//////////////////////////////////////////////////////////////////////*/
{ // block for writing results of WordCountCollection for the current Doc ...
WordCountStruct* pWC1;
CString key;
char strLine[80];
sprintf(strLine,"C:\\temp\\%d.txt",nTotalDocs);
CStdioFile constfile( strLine, CFile::modeCreate | CFile::modeWrite | CFile::typeText);
//int CollectionCount = WordCountCollection.GetCount();
for(POSITION pos = WordCountCollection.GetStartPosition();pos!=NULL;)
{
WordCountCollection.GetNextAssoc( pos,key , (CObject*&)pWC1);
sprintf(strLine,"%d-%s\r", pWC1->Frequency, pWC1->WordValue);
constfile.Write(strLine, 80);
WordCountCollection.RemoveKey(key);
delete pWC1;
}
constfile.Close();
}
/*///////////////////////////////////////////////////////////////////////*/
}
else
{
AfxMessageBox( "Error, Couldn't open file" );
}
/*//////////////////////////////////////////////////////////////////////*/
/*
{ // block for testing the output data from every phase
WordCountStruct* pWC1;
CString key;
CStdioFile constfile("_testToken.txt", CFile::modeCreate | CFile::modeWrite | CFile::typeText);
char strLine[80];
//int CollectionCount = WordCountCollection.GetCount();
for(POSITION pos = WordCountCollection.GetStartPosition();pos!=NULL;)
{
WordCountCollection.GetNextAssoc( pos, key, (CObject*&)pWC1);
wsprintf(strLine,"%s\t%d\n",pWC1->WordValue, pWC1->Frequency);
constfile.WriteString (strLine);
}
constfile.Close();
}
/*///////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////*/
/*
{ // block for testing the output data from DFI
WordDFI_Struct* pWD1;
CString key;
CStdioFile constfile("_testDFI.txt", CFile::modeCreate | CFile::modeWrite | CFile::typeText);
char strLine[80];
//int CollectionCount = WordCountCollection.GetCount();
for(POSITION pos = WordDFiCollection.GetStartPosition();pos!=NULL;)
{
WordDFiCollection.GetNextAssoc( pos, key, (CObject*&)pWD1);
wsprintf(strLine,"%s\t%d\t%d\n",pWD1->WordValue, pWD1->nGFi,pWD1->nDFi);
constfile.WriteString (strLine);
}
constfile.Close();
}
/*///////////////////////////////////////////////////////////////////////*/
///////////////////////////////// Avoiding memory leak ///////////////
{
CString key;
for(POSITION pos = WordUpdateList.GetStartPosition();pos!=NULL;)
{
WordUpdateList.GetNextAssoc( pos,key , (CObject*&)pWS);
WordUpdateList.RemoveKey(key);
delete pWS;
}
}
////////////////////////////////////////////////////////////////////////
//WordUpdateList.RemoveAll();
} //end of CountFreq()
void CARSDocument::CalculateWeight(CStringList* DocPathList)
{
char strLine[80];
WordCountStruct WC;
WordDFI_Struct* pWD;
CStdioFile constfile;
double fPGiVal;
long lTnum;
CIndexTermsInDoc ARSIndexTerm;
for(int nFileNum = 1; nFileNum <= nTotalDocs; nFileNum++)
{ // this Iteration on files for calculating the partial Global weight ...
sprintf(strLine,"C:\\temp\\%d.txt",nFileNum);
constfile.Open( strLine, CFile::modeRead | CFile::typeText);
//strLine = NULL;
while( constfile.Read( strLine, 80 ) )
{
sscanf(strLine,"%d-%s\r", &WC.Frequency, &WC.WordValue);
if( WordDFiCollection.Lookup((const char*)&WC.WordValue, ( CObject*& )pWD) )
{
fPGiVal = ((double)WC.Frequency/pWD->nGFi) * ( (log10((double)WC.Frequency/pWD->nGFi)/log10(2)) );
WordDFiCollection.SetAt((const char*)pWD->WordValue, new WordDFI_Struct(pWD->nDFi, pWD->nGFi, (char*)pWD->WordValue, pWD->fPGi + fPGiVal,0,FALSE) );
delete pWD;
}
}
constfile.Close();
}
double fLw, fGw, fWeight;
CString sCurrentDoc;
CARSDocument dCurrentDocInfo;
double fWThreshold = ARSIndexer.GetThreShold();
//**/CStdioFile constfile1("_Weights274.txt", CFile::modeCreate | CFile::modeWrite | CFile::typeText);
//////////// here get the real document # from Document Table ////////
POSITION pos;
for(pos = DocPathList->GetHeadPosition(),nFileNum = 1; pos != NULL;nFileNum++)
{
sCurrentDoc = DocPathList->GetNext(pos);
ARSdb.GetDocumentInfoByName( sCurrentDoc.GetBuffer(sCurrentDoc.GetLength()), dCurrentDocInfo );
// this Iteration on files for calculating the weight of the term ...
sprintf(strLine,"C:\\temp\\%d.txt",nFileNum);
constfile.Open( strLine, CFile::modeRead | CFile::typeBinary);
//strLine = NULL;
while( constfile.Read( strLine, 80 ) )
{
sscanf(strLine,"%d-%s", &WC.Frequency, &WC.WordValue);
// if(!strcmp(WC.WordValue, "ÅÌãÇáí"))
// int xx = 0;
if( WordDFiCollection.Lookup((const char*)&WC.WordValue, ( CObject*& )pWD) )
{
fLw = ( log10(WC.Frequency + 1)/ log10(2) );
fGw = 1 - ( pWD->fPGi/( log10(nTotalDocs)/log10(2) ) );
fWeight = fLw * fGw;
if( fWeight > fWThreshold )
{
// here add the pWD->WordValue and fGw to KeyWord Table ...
if(!pWD->bMarked)
{
//pWD->bMarked = 1;
strcpy(ARSKWords.m_TermValue, WC.WordValue);
ARSKWords.m_GlobalWeight = float(fGw);
ARSKWords.m_Gfi = pWD->nGFi;
ARSdb.AddNewTerm(ARSKWords);
if(!ARSdb.GetTermInfoByName(pWD->WordValue, lTnum))
::MessageBox( NULL, "No records,NO TermInfo, some thing wrong in the db", "", MB_OK );
WordDFiCollection.SetAt(
(const char*)pWD->WordValue,
new WordDFI_Struct(pWD->nDFi, pWD->nGFi, (char*)pWD->WordValue, pWD->fPGi, lTnum, TRUE)
);
delete pWD;
if( !WordDFiCollection.Lookup((const char*)&WC.WordValue, ( CObject*& )pWD) )
AfxMessageBox("ERROR in processing Terms(bMarked)");
}
if(!pWD->TNum)
AfxMessageBox("ERROR in processing Terms");
ARSIndexTerm.SetIndexTermInfo(pWD->TNum,
dCurrentDocInfo.m_DocNumber,
fWeight,
WC.Frequency);
ARSdb.AddIndexTerm(ARSIndexTerm);
/*//////////////////////////////////////////////////////////////////////*/
/* // block for testing the output data from every phase
char strLine[80];
sprintf(strLine,"%s,%d%f,%f,%f\n",pWD->WordValue, pWD->TNum, fLw, fGw, fWeight);
constfile1.WriteString (strLine);
/*///////////////////////////////////////////////////////////////////////*/
///////////////////////////////////////////////////////////////////////////////
}
}
}
constfile.Close();
}
/////////////////////////////////////////////////////////////////////
////////////////////////old block have been updated above ///////////
/* for(nFileNum = 1; nFileNum <= nTotalDocs; nFileNum++)
{ // this Iteration on files for calculating the weight of the term ...
sprintf(strLine,"C:\\temp\\%d.txt",nFileNum);
constfile.Open( strLine, CFile::modeRead | CFile::typeBinary);
//strLine = NULL;
while( constfile.Read( strLine, 80 ) )
{
sscanf(strLine,"%d-%s", &WC.Frequency, &WC.WordValue);
// if(!strcmp(WC.WordValue, "ÅÌãÇáí"))
// int xx = 0;
if( WordDFiCollection.Lookup((const char*)&WC.WordValue, ( CObject*& )pWD) )
{
fLw = ( log10(WC.Frequency + 1)/ log10(2) );
fGw = 1 - ( pWD->fPGi/( log10(nTotalDocs)/log10(2) ) );
fWeight = fLw * fGw;
if( fWeight >= ARSIndexer.GetThreShold() )
{
// here add the pWD->WordValue and fGw to KeyWord Table ...
///////////////////////////////////////////////////////////////////////////////
}
/*//////////////////////////////////////////////////////////////////////*/
/* // block for testing the output data from every phase
char strLine[80];
sprintf(strLine,"%s,%f,%f,%f\n",WC.WordValue, fLw, fGw, fWeight);
constfile1.WriteString (strLine);
/*///////////////////////////////////////////////////////////////////////*/
/* }
}
constfile.Close();
}
/**///constfile1.Close();
/////////////////////////////////////end of old block /////////////
///////////////////////////////// Avoiding memory leak ///////////////
{
CString key;
for(POSITION pos = WordDFiCollection.GetStartPosition();pos!=NULL;)
{
WordDFiCollection.GetNextAssoc( pos,key , (CObject*&)pWD);
WordDFiCollection.RemoveKey(key);
delete pWD;
}
}
/////////////////////////////////////////////////////////////////////////
}// end of CaluclateWeight()
WordCountStruct::WordCountStruct()
{
}
WordCountStruct::WordCountStruct(int Freq , char* WVlaue)
{
Frequency = Freq;
strcpy(WordValue, WVlaue);
}
WordCountStruct::~WordCountStruct()
{
}
WordDFI_Struct::WordDFI_Struct()
{
}
WordDFI_Struct::WordDFI_Struct(int nFreq, int nGFreq, char *WordVal, double fPGiVal=0, long lTnum = 0, BOOL bMark = FALSE)
{
nDFi = nFreq;
nGFi = nGFreq;
fPGi = fPGiVal;
strcpy(WordValue, WordVal);
bMarked = bMark;
TNum = lTnum;
}
WordDFI_Struct::~WordDFI_Struct()
{
}
WordStruct::WordStruct()
{
}
WordStruct::WordStruct(char* WVlaue)
{
strcpy(WordValue, WVlaue);
}
WordStruct::~WordStruct()
{
}
BOOL CARSDocument::SetIgnored(BOOL bIgnored, int DocNum)
{
return ARSdb.UpdateDoc(bIgnored, DocNum);
}
BOOL CARSDocument::ReSetIgnored(BOOL bIgnored, int DocNum)
{
return ARSdb.UpdateDoc(bIgnored, DocNum);
}