forked from aznszn/searchoverflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_lexicon.cpp
399 lines (345 loc) · 16.2 KB
/
build_lexicon.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "stemminglib/english_stem.h"
#include "utils/fetch_table.h"
#include "inverted_index.h"
//#include "utils/parseHTML.h"
#define WORDS_IN_FILE 500
#define MAX_WORD_LEN 17
#define TAGS_IMP 4
#define TITLE_IMP 10
#define BODY_IMP 1
#define LINECAP 200
#define QBODY_COL 6
#define QTITLE_COL 5
#define ANSBODY_COL 5
#define ANS_PID_COL 3
#define SCORE_COL 4
using namespace std;
using namespace filesystem;
void buildLexicon(unordered_map<wstring, int> &, vector<vector<wstring>>, vector<vector<wstring>>, vector<vector<wstring>>, int&, unordered_map<wstring, int> &);
void buildForwardIndex(unordered_map<wstring, int> &, unordered_map<wstring, int> &, int, const wstring&, unordered_map<wstring, wstring> &);
void utilLexiconFunction(unordered_map<wstring, int> &, unordered_map<wstring, int> &, wstringstream&, int &, unordered_map<wstring, wstring> &, int &, unordered_map<wstring, int> &);
int getAnswer(int &, vector<vector<wstring>> &, const wstring&);
int getTag(int &, vector<vector<wstring>>&, const wstring&);
void utilLexiconForTags(unordered_map<wstring, int> &, wstringstream &, int &, vector<wstring> &, unordered_map<wstring,int>&);
void buildForwardIndexTags(unordered_map<wstring, int> &, int, wstring &, vector<wstring> &);
void readStopWords(unordered_map<wstring, int> &);
void getLineNums();
//TODO: remove global
auto array_fi = new vector<wofstream>; //barrel file streams array
int main() {
{
//writes the number of characters in each .csv into metaData file
path x(current_path().string() + "/../dataset/questions_100k.csv");
path y(current_path().string() + "/../dataset/answers_100k.csv");
path z(current_path().string() + "/../dataset/tags_100k.csv");
wofstream metaData("../data_structures/metadata.txt");
metaData << file_size(x) << endl << file_size(y) << endl << file_size(z);
metaData.close();
//build stop words lexicon
unordered_map<wstring, int> stopWordsLexicon;
readStopWords(stopWordsLexicon);
cout << "Stop Words Lexicon made" << endl;
//lexicon map and file
unordered_map<wstring, int> lexicon;
wofstream lexicon_file("../data_structures/lexicon.txt", ios::out);
//incremental count for wordID
int i = 0;
//fetch questions table into 2D vector
wifstream questions("../dataset/questions_100k.csv");
vector<vector<wstring>> questions_table = fetch_table(questions);
cout << "questions table fetched" << endl;
//fetch answers table into 2D vector
wifstream answers("../dataset/answers_100k.csv");
vector<vector<wstring>> answers_table = fetch_table(answers);
cout << "answers table fetched" << endl;
//fetch tags into 2D vector
wifstream tags("../dataset/tags_100k.csv");
vector<vector<wstring>> tags_table = fetch_table(tags);
cout << "tags table fetched" << endl;
//parseHTML(questions_table, QBODY_COL);
//cout << "questions HTML parsed" << endl;
//parseHTML(answers_table, ANSBODY_COL);
//cout << "answers HTML parsed" << endl;
//build lexicon function
buildLexicon(lexicon, questions_table, answers_table, tags_table, i, stopWordsLexicon);
//close all barrels
for (auto &x: *array_fi)
x.close();
cout << (*array_fi).size() << endl;
delete array_fi;
//writing lexicon to file
for (auto &x: lexicon)
lexicon_file << x.first << L"," << x.second << "\n";
cout << "Lexicon file created" << endl;
lexicon_file.close();
}
//build inverted
buildInverted();
cout << "Built inverted" << endl;
//update line number file
getLineNums();
cout << "Got Line Num" << endl;
}
void buildLexicon(unordered_map<wstring, int> &lexicon, vector<vector<wstring>> questions, vector<vector<wstring>> answers, vector<vector<wstring>> tags, int& i, unordered_map<wstring, int> &stopWordsLexicon) {
int j = 0; // for questions counter
int k = 0; // for answers counter
int t = 0; // for tags counter
//documents list file
wofstream docList("../data_structures/docList.txt", ios::out);
while (j < questions.size()) {
int overallCharacterCount = 0; //stores the current word count in the document
unordered_map<wstring, int> storesCount; //stores the count of each word in the question title
unordered_map<wstring, int> storesQACount; //stores the count of each word in the body
unordered_map<wstring, wstring> hits; //unordered map for hits positions in question title
unordered_map<wstring, wstring> hitsQA; ////unordered map for hits positions in body
vector<wstring> wordsInTags; //vector to store all words in tags
// tags
while(1) {
int tagFound = getTag(t, tags, questions[j][0]); //get corresponding tag from table
if (tagFound == -1){
break;
}
auto row_stream = wstringstream(tags[tagFound][1]);
utilLexiconForTags(lexicon, row_stream, i, wordsInTags, stopWordsLexicon); //lexicon build for tags
}
buildForwardIndexTags(lexicon, TAGS_IMP, questions[j][0], wordsInTags); //forward index build for tags
// question titles
auto row_stream = wstringstream(questions[j][QTITLE_COL]);
utilLexiconFunction(lexicon, storesCount, row_stream, i, hits, overallCharacterCount, stopWordsLexicon); //lexicon build for question title
buildForwardIndex(lexicon, storesCount, TITLE_IMP, questions[j][0], hits); //forward index build for question title
// questions body
row_stream = wstringstream(questions[j][QBODY_COL]);
utilLexiconFunction(lexicon, storesQACount, row_stream, i, hitsQA, overallCharacterCount, stopWordsLexicon);
// answers for that specific question
int max = INT_MIN;
int answerCount = 0;
while (1) {
int ansFound = getAnswer(k, answers, questions[j][0]); //get corresponding answers for current question
if (ansFound == -1) {
break;
}
++answerCount;
//get max answer score
if (stoi(answers[ansFound][SCORE_COL]) > max){
max = stoi(answers[ansFound][SCORE_COL]);
}
row_stream = wstringstream(answers[ansFound][ANSBODY_COL]);
utilLexiconFunction(lexicon, storesQACount, row_stream, i, hitsQA, overallCharacterCount, stopWordsLexicon);
}
buildForwardIndex(lexicon, storesQACount, BODY_IMP, questions[j][0], hitsQA);
double rank = 0.46 * max + 0.33 * stoi(questions[j][SCORE_COL]) + 0.21 * answerCount; //calculate rank
docList << endl << questions[j][0] << L"," << overallCharacterCount << L"," << rank;
++j; //increment question counter
}
}
void utilLexiconFunction(unordered_map<wstring, int> &lexicon, unordered_map<wstring, int> &storesCount, wstringstream& row_stream, int &i, unordered_map<wstring, wstring> &hits, int &overallCharacterCount, unordered_map<wstring, int> &stopWordsLexicon){
wstring word;
stemming::english_stem<> stem; //for stemming
wchar_t c;
//read char by char
while (row_stream >> noskipws >> c) {
switch (c) {
case 'a' ... 'z':
case 'A' ... 'Z':
case '+':
case '#':
word.push_back(tolower(c));
break;
case '\'':
break;
default:
if (!word.empty()) {
stem(word);
if (word.length() <= MAX_WORD_LEN && !lexicon.count(word) && !stopWordsLexicon.count(word)) {
//open barrel
if (i % (WORDS_IN_FILE) == 0) {
array_fi->push_back(wofstream(
"../data_structures/barrels/" + to_string(i / (WORDS_IN_FILE )) + ".txt",
ios::out));
}
lexicon[word] = i++; //assign id to word
storesCount[word] = 1; //store word count
++overallCharacterCount; //increment total word count
//concat hit positions
if(hits[word].length() + to_wstring(overallCharacterCount).length() + 20 < LINECAP)
hits[word] = hits[word] + L"," + to_wstring(overallCharacterCount);
}
else if (word.length() <= MAX_WORD_LEN && storesCount.count(word) && !stopWordsLexicon.count(word)){
storesCount[word] += 1; //increment word count
++overallCharacterCount; //increment total word count
//concat hit positions
if(hits[word].length() + to_wstring(overallCharacterCount).length() + 20 < LINECAP)
hits[word] = hits[word] + L"," + to_wstring(overallCharacterCount);
}
else if (word.length() <= MAX_WORD_LEN && !stopWordsLexicon.count(word)){
storesCount[word] = 1;
++overallCharacterCount;
if(hits[word].length() + to_wstring(overallCharacterCount).length() + 20 < LINECAP)
hits[word] = hits[word] + L"," + to_wstring(overallCharacterCount);
}
word.clear();
}
}
}
//for last word
if (!word.empty()) {
stem(word);
if (word.length() <= MAX_WORD_LEN && !lexicon.count(word) && !stopWordsLexicon.count(word)) {
if (i % (WORDS_IN_FILE) == 0) {
array_fi->push_back(wofstream(
"../data_structures/barrels/" + to_string(i / (WORDS_IN_FILE)) + ".txt",
ios::out));
}
lexicon[word] = i++;
storesCount[word] = 1;
++overallCharacterCount;
if(hits[word].length() + to_wstring(overallCharacterCount).length() + 20 < LINECAP)
hits[word] = hits[word] + L"," + to_wstring(overallCharacterCount);
}
else if (word.length() <= MAX_WORD_LEN && storesCount.count(word) && !stopWordsLexicon.count(word)){
storesCount[word] += 1;
++overallCharacterCount;
if(hits[word].length() + to_wstring(overallCharacterCount).length() + 20 < LINECAP)
hits[word] = hits[word] + L"," + to_wstring(overallCharacterCount);
}
else if (word.length() <= MAX_WORD_LEN && !stopWordsLexicon.count(word)){
storesCount[word] += 1;
++overallCharacterCount;
if(hits[word].length() + to_wstring(overallCharacterCount).length() + 20 < LINECAP)
hits[word] = hits[word] + L"," + to_wstring(overallCharacterCount);
}
word.clear();
}
}
void utilLexiconForTags(unordered_map<wstring, int> &lexicon, wstringstream &row_stream, int &i, vector<wstring> &wordsInTags, unordered_map<wstring, int>& stopWordsLexicon){
wstring word;
stemming::english_stem<> stem; //for stemming
wchar_t c;
//go through each tag character by character
while (row_stream >> noskipws >> c) {
switch (c) {
case 'a' ... 'z':
case 'A' ... 'Z':
case '+':
case '#':
word.push_back(tolower(c));
break;
case '\'':
break;
default:
if (!word.empty()) {
stem(word); //stem word
if (word.length() <= MAX_WORD_LEN && !lexicon.count(word) && !stopWordsLexicon.count(word)) {
//open the barrel
if (i % (WORDS_IN_FILE) == 0) {
array_fi->push_back(wofstream(
"../data_structures/barrels/" + to_string(i / (WORDS_IN_FILE)) + ".txt",
ios::out));
}
lexicon[word] = i++;
wordsInTags.push_back(word);
}
word.clear();
}
}
}
//for last word
if (!word.empty()) {
stem(word);
if (word.length() <= MAX_WORD_LEN && !lexicon.count(word) && !stopWordsLexicon.count(word)) {
if (i % (WORDS_IN_FILE) == 0) {
array_fi->push_back(wofstream(
"../data_structures/barrels/" + to_string(i / (WORDS_IN_FILE)) + ".txt",
ios::out));
}
lexicon[word] = i++;
wordsInTags.push_back(word);
}
word.clear();
}
}
void buildForwardIndex(unordered_map<wstring, int> &lexicon, unordered_map<wstring, int> &storesCount, int importance, const wstring& id, unordered_map<wstring, wstring> &hits){
//documentId, wordId, importance, numberOfHits, HitPositions
for(auto &x: storesCount){
int wordId = lexicon[x.first];
wstringstream ss;
ss << endl << id << L"," << (wordId - (wordId/WORDS_IN_FILE)*WORDS_IN_FILE) << L"," << importance << L"," << x.second << hits[x.first];
array_fi->at(wordId / (WORDS_IN_FILE)) << ss.str() << setw(LINECAP - ss.str().length()) << " ";
}
}
void buildForwardIndexTags(unordered_map<wstring, int> &lexicon, int importance, wstring &id, vector<wstring> &wordsInTags){
//documentId, wordId, importance, numberOfHits, HitPosition(position is 0 for tags)
for (auto &x : wordsInTags){
int wordId = lexicon[x];
wstringstream ss;
ss << "\n" << id << L"," << (wordId - (wordId/WORDS_IN_FILE)*WORDS_IN_FILE) << L"," << importance << L',' << 1 << L"," << 0;
array_fi->at(wordId / (WORDS_IN_FILE)) << ss.str() << setw(LINECAP - ss.str().length()) << " ";
}
}
inline int getAnswer(int &k, vector<vector<wstring>>& answers, const wstring& id){
if(k < answers.size() && answers[k][ANS_PID_COL] == id)
return k++;
else
return -1;
}
inline int getTag(int &t, vector<vector<wstring>>& tags, const wstring& id){
if (t < tags.size() && tags[t][0] == id){
return t++;
}
else {
return -1;
}
}
// documentID, numberOfWords, question score, answers score max, number of answers
// answers score max 46%
// questions score 33%
// number of answers 21%
void readStopWords(unordered_map<wstring, int> &map){
wstring word;
stemming::english_stem<> stem;
wifstream stopWordsFile("../dataset/stop_words_english.txt");
while (!stopWordsFile.eof()){
getline(stopWordsFile, word);
stem(word);
map[word] = 0;
}
}
void getLineNums(){
wofstream lineNumberFile("../data_structures/lineNumbers.txt", ios::out); //open line number file
//get number of files in the /barrel dir
auto dirIter = std::filesystem::directory_iterator(current_path().string() + "/../data_structures/barrels");
int fileCount = count_if(
begin(dirIter),
end(dirIter),
[](auto& entry) { return entry.is_regular_file(); }
);
vector<wifstream> files;
files.reserve(fileCount);
for(int i = 0; i < fileCount; i++){
files.emplace_back("../data_structures/barrels/" + to_string(i) + ".txt", ios::in);
}
int i = 0;
// calculate the line count for each word
vector<wstring> row;
wstring line;
for(auto& file : files){
wstring cell;
wstring temp;
int ln = 1;
getline(file, line); //new
while(getline(file, line)){
ln++;
row.clear();
wstringstream linestream(line);
getline(linestream, cell, L',');
getline(linestream, cell, L',');
if(temp != cell) {
lineNumberFile << cell << L',' << ln << '\n';
}
temp = cell;
}
file.close();
i++;
}
lineNumberFile.close();
}