forked from chinaltang/mySearchEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexer.cpp
360 lines (286 loc) · 8.86 KB
/
Indexer.cpp
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
/*******************************Copyright (c)*********************************************
**Copyright Owner: Cory Tang
**Department: University of Electronic Science and Technology of China
**Email: chong.tang@live.cn
**--------------------------- File Info ---------------------------------------------------
** File name: Crawler.cpp
** Last modified Date: 2013-08-29
** Last Version: 0.1
** Descriptions: Definition of the class Indexer
** Based on Linux 2.6.36.
**------------------------------------------------------------------------------------------
** Created by: Cory Tang
** Created date: 2013-08-05
**------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
*******************************************************************************************/
#include "Indexer.h"
Indexer::Indexer(const string& crawledDir, const bool& isPureEnglishIndex):m_crawledDir(crawledDir)//,m_isPureEnglishIndex(isPureEnglishIndex)
{
}
int Indexer::get_doc_set(const string& crawledDir, DOC_SET& docSet)
{
string savedUrlLogFile;
if(*crawledDir.end() == '/')
{
savedUrlLogFile.append(crawledDir).append("saved_url_list.lst");
}
else
{
savedUrlLogFile.append(crawledDir).append("/").append("saved_url_list.lst");
}
fstream fsUrlList;
fsUrlList.open(savedUrlLogFile.c_str(), ios_base::in);
if(fsUrlList.fail())
{
fsUrlList.close();
cout<<savedUrlLogFile<<" Open Failed"<<endl;
return -1;
}
string tmpStringLen;
string tmpUrl;
string tmpFileName;
size_t docId = 0;
while(getline(fsUrlList, tmpStringLen))
{
// stringstream tmpStreamLen(tmpStringLen);
stringstream tmpStreamLen(tmpStringLen);
if(!(tmpStreamLen >> tmpUrl))
{
cout<<savedUrlLogFile<<" Format Error, Can't Get the URL" <<endl;
continue;
}
if(!(tmpStreamLen >> tmpFileName))
{
cout<<savedUrlLogFile<<" Format Error, Can't Get the FileName" <<endl;
continue;
}
++docId;
docSet.insert(pair<DOC_ID, pair<DOC_URL, DOC_FILENAME> >(docId, pair<DOC_URL, DOC_FILENAME>(tmpUrl, tmpFileName)));
}
return docId;
}
int Indexer::get_text_from_html(const string& fileAbsolutePath, string& text)
{
string fileContent;
fstream fsFileContent;
fsFileContent.open(fileAbsolutePath.c_str(), ios_base::in);
if(fsFileContent.fail())
{
fsFileContent.close();
cout<<fileAbsolutePath<<" Open Failed"<<endl;
fsFileContent.close();
return -1;
}
istreambuf_iterator<char> beginIter(fsFileContent), endIter;
fileContent = string(beginIter, endIter);
string tagBefore, tagAfter;
size_t posTagBefore, posTagAfter, posStart = 0;
if((posTagBefore = find_html_tag(fileContent, tagBefore, posStart)) == string::npos)
{
return -1;
}
posStart = posTagBefore + tagBefore.size();
while((posTagAfter = find_html_tag(fileContent, tagAfter, posStart)) != string::npos)
{
if(posTagAfter - posTagBefore >= tagBefore.size() + 1 && tagBefore.find("<script") == string::npos && tagBefore.find("<style") == string::npos)
{
string textSegment = fileContent.substr(posTagBefore + tagBefore.size(), posTagAfter - (posTagBefore + tagBefore.size()));
text.append(textSegment).append(" ");
}
posTagBefore = posTagAfter;
tagBefore = tagAfter;
posStart = posTagBefore + tagBefore.size();
}
fsFileContent.close();
return 0;
}
int Indexer::create_index(const string& crawledDir, const DOC_SET& docSet, POSTING_INDEX& postingIndex)
{
string tmpCrawledDir = crawledDir;
if(*(tmpCrawledDir.end() - 1) != '/')
{
tmpCrawledDir.append("/");
}
boost::char_separator<char> sep(",.-|@\"\"\'\' \n");
DOC_SET::const_iterator docSetIter = docSet.begin();
while(docSetIter != docSet.end())
{
string tmpFileAbsolutePath;
tmpFileAbsolutePath.append(tmpCrawledDir).append((docSetIter->second).second);
cout<<tmpFileAbsolutePath<<endl;
string docText;
if(get_text_from_html(tmpFileAbsolutePath, docText) < 0)
{
docSetIter++;
continue;
}
boost::tokenizer<boost::char_separator<char> > tokens(docText, sep);
for(boost::tokenizer<boost::char_separator<char> >::iterator tokIter = tokens.begin(); tokIter != tokens.end(); ++tokIter)
{
POSTING_INDEX::iterator postingIndexIter = postingIndex.find(*tokIter);
if(postingIndexIter != postingIndex.end()) //Not a new word
{
POSTING_LIST::iterator postingListIter = (postingIndexIter->second).find(docSetIter->first);
if(postingListIter != (postingIndexIter->second).end()) //Not a new word and Not a new doc, just modify the TF
{
(postingListIter->second)++;
}
else
{
(postingIndexIter->second).insert(POSTING_ITEM(docSetIter->first, 1)); //Not a new word, but it appear in a new doc, add new posting item
}
}
else //A new word
{
POSTING_LIST tmpPostingList;
tmpPostingList.insert(POSTING_ITEM(docSetIter->first, 1));
postingIndex.insert(pair<WORD, POSTING_LIST>(*tokIter, tmpPostingList));
}
}//end for
docSetIter++;
}//end while
return 0;
}
void Indexer::get_time_string(string& timeStr)
{
time_t t;
time(&t);
timeStr = string(ctime(&t));
string::iterator iter = timeStr.begin();
while(iter != timeStr.end())
{
if(*iter == ' ' || *iter == ':')
{
*iter = '_';
}
else if(*iter == '\n')
{
iter = timeStr.erase(iter);
}
else
{
iter++;
}
}
}
size_t Indexer::find_html_tag(const string& pageContent, string& tag, const size_t position)
{
size_t tagStart, tagEnd;
if((tagStart = pageContent.find('<', position)) == string::npos)
{
return string::npos;
}
if((tagEnd = pageContent.find('>', tagStart + 1)) == string::npos)
{
return string::npos;
}
tag = pageContent.substr(tagStart, tagEnd - tagStart + 1);
return tagStart;
}
int Indexer::start_index()
{
if(get_doc_set(m_crawledDir, m_docSet) < 0)
{
return -1;
}
return create_index(m_crawledDir, m_docSet, m_postingIndex);
}
int Indexer::save_index(const string& indexedDir)
{
string tmpIndexDir = indexedDir;
if(*(tmpIndexDir.end() - 1) != '/')
{
tmpIndexDir.append("/");
}
string timeStr;
get_time_string(timeStr);
string postingIndexFileAbsolutePath;
postingIndexFileAbsolutePath.append(tmpIndexDir).append(timeStr).append("_indexed").append(".idx");
string docSetFileAbsolutePath;
docSetFileAbsolutePath.append(tmpIndexDir).append(timeStr).append("_indexed").append(".dst");
fstream fsPostingIndex;
fstream fsDocSet;
fsPostingIndex.open(postingIndexFileAbsolutePath.c_str(), ios_base::out);
if(fsPostingIndex.fail())
{
fsPostingIndex.close();
cout<<postingIndexFileAbsolutePath<<" Open Failed"<<endl;
return -1;
}
fsDocSet.open(docSetFileAbsolutePath.c_str(), ios_base::out);
if(fsDocSet.fail())
{
fsDocSet.close();
cout<<docSetFileAbsolutePath<<" Open Failed"<<endl;
return -1;
}
boost::archive::text_oarchive oaPostingIndex(fsPostingIndex);
boost::archive::text_oarchive oaDocSet(fsDocSet);
oaPostingIndex<<m_postingIndex;
oaDocSet<<m_docSet;
fsPostingIndex.close();
fsDocSet.close();
return 0;
}
int Indexer::load_index(const string& idxFileAbsolutePath, const string& dstFileAbsolutePath)
{
postingIndexFileAbsolutePath = idxFileAbsolutePath;
docSetFileAbsolutePath = dstFileAbsolutePath;
fstream fsPostingIndex;
fstream fsDocSet;
fsPostingIndex.open(postingIndexFileAbsolutePath.c_str(), ios_base::in);
if(fsPostingIndex.fail())
{
fsPostingIndex.close();
cout<<postingIndexFileAbsolutePath<<" Open Failed"<<endl;
return -1;
}
fsDocSet.open(docSetFileAbsolutePath.c_str(), ios_base::in);
if(fsDocSet.fail())
{
fsDocSet.close();
cout<<docSetFileAbsolutePath<<" Open Failed"<<endl;
return -1;
}
boost::archive::text_iarchive oaPostingIndex(fsPostingIndex);
boost::archive::text_iarchive oaDocSet(fsDocSet);
oaPostingIndex>>m_postingIndex;
oaDocSet>>m_docSet;
fsPostingIndex.close();
fsDocSet.close();
return 0;
}
Indexer::POSTING_INDEX::iterator Indexer::do_query(const string& keyWord)
{
POSTING_INDEX::iterator postingIndexIter = m_postingIndex.find(keyWord);
return postingIndexIter;
}
int Indexer::query(const string& keyWord) //This user interface can be redefine to support different indexing method, such as simple bool methnd.
{
POSTING_INDEX::iterator postingIndexIter = do_query(keyWord);
if(postingIndexIter == m_postingIndex.end())
{
return -1;
}
POSTING_LIST::iterator postingListIter;
DOC_SET::iterator docSetIter;
cout<<"Qeury Result: The following URL contained the key word -"<<keyWord<<endl;
size_t countUrl = 0;
for(postingListIter = (postingIndexIter->second).begin(); postingListIter != (postingIndexIter->second).end(); postingListIter++)
{
docSetIter = m_docSet.find(postingListIter->first);
countUrl++;
cout<<countUrl<<": "<<(docSetIter->second).first<<endl;
}
return (postingIndexIter->second).size();
}
int Indexer::load_index(const string& indexedDir)
{
//To be realized
cout<<"Not realized"<<endl;
return -1;
}