-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.cpp
459 lines (397 loc) · 17.1 KB
/
update.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <iostream>
#include "bits/stdc++.h"
#include "utils/fetch_table.h"
#include "utils/parseHTML.h"
#include "inverted_index.h"
#include "stemminglib/english_stem.h"
using namespace std;
using namespace filesystem;
#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
unordered_map<wstring, vector<wstring>> getDocsInfo();
unordered_map<wstring, int> getLexicon(int &);
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();
auto array_fi = new vector<wofstream>;
set<int, greater<int>> barrelsToUpdate;
int main() {
//get all barrels and total files count
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(); }
);
array_fi->reserve(fileCount);
for(int i = 0; i < fileCount; i++){
array_fi->emplace_back("../data_structures/barrels/" + to_string(i) + ".txt", ios::app);
}
//stop words lexicon
unordered_map<wstring, int> stopWordsLexicon;
readStopWords(stopWordsLexicon);
cout << "Stop Words Lexicon made" << endl;
int maxId = INT_MIN; //last update max wordID
vector<wstring> newRecords;
unordered_map<wstring, vector<wstring>> docsInfo = getDocsInfo();
unordered_map<wstring, int> lexicon = getLexicon(maxId);
maxId++;
cout << maxId << endl;
//open all dataset
wifstream questions("../dataset/questions_100k.csv");
wifstream answers("../dataset/answers_100k.csv");
wifstream tags("../dataset/tags_100k.csv");
//open metadata file
wifstream metaData("../data_structures/metadata.txt");
wstring questionsSize;
getline(metaData, questionsSize);
wstring answersSize;
getline(metaData, answersSize);
wstring tagsSize;
getline(metaData, tagsSize);
metaData.close();
//seek to new records
questions.seekg(stoi(questionsSize), ios::beg);
answers.seekg(stoi(answersSize), ios::beg);
tags.seekg(stoi(tagsSize), ios::beg);
//get new records
vector<vector<wstring>> questions_table = fetch_table(questions);
cout << "questions table fetched" << endl;
//get new records
vector<vector<wstring>> answers_table = fetch_table(answers);
cout << "answers table fetched" << endl;
//get new records
vector<vector<wstring>> tags_table = fetch_table(tags);
cout << "tags table fetched" << endl;
//parse table
//parseHTML(questions_table, QBODY_COL);
//cout << "questions HTML parsed" << endl;
//parseHTML(answers_table, ANSBODY_COL);
//cout << "answers HTML parsed" << endl;
//build lexicon
buildLexicon(lexicon, questions_table, answers_table, tags_table, maxId, stopWordsLexicon);
//close all barrels
for (auto &x: *array_fi)
x.close();
cout << (*array_fi).size() << endl;
delete array_fi;
//writing lexicon to file
wofstream lexicon_file("../data_structures/lexicon.txt", ios::out);
for (auto &x: lexicon)
lexicon_file << x.first << L"," << x.second << "\n";
cout << "Lexicon file created" << endl;
lexicon_file.close();
updateInverted(barrelsToUpdate);
cout << "Built inverted" << endl;
questions.close();
answers.close();
tags.close();
//update 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 updateMetaData("../data_structures/metadata.txt");
updateMetaData << file_size(x) << endl << file_size(y) << endl << file_size(z);
updateMetaData.close();
cout << "Updated Meta Data File" << endl;
getLineNums();
cout << "Updated Line Number File" << 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
wofstream docList("../data_structures/docList.txt", ios::app);
while (j < questions.size()) {
int overallCharacterCount = 0;
unordered_map<wstring, int> storesCount;
unordered_map<wstring, int> storesQACount;
unordered_map<wstring, wstring> hits;
unordered_map<wstring, wstring> hitsQA;
vector<wstring> wordsInTags;
// tags
while(1) {
int tagFound = getTag(t, tags, questions[j][0]);
if (tagFound == -1){
break;
}
auto row_stream = wstringstream(tags[tagFound][1]);
utilLexiconForTags(lexicon, row_stream, i, wordsInTags, stopWordsLexicon);
}
buildForwardIndexTags(lexicon, TAGS_IMP, questions[j][0], wordsInTags);
// question titles
auto row_stream = wstringstream(questions[j][QTITLE_COL]);
utilLexiconFunction(lexicon, storesCount, row_stream, i, hits, overallCharacterCount, stopWordsLexicon);
buildForwardIndex(lexicon, storesCount, TITLE_IMP, questions[j][0], hits);
// 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]);
if (ansFound == -1) {
break;
}
++answerCount;
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;
docList << endl << questions[j][0] << L"," << overallCharacterCount << L"," << rank; // change to answerCount
++j;
}
}
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;
wchar_t c;
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)) {
if (i % (WORDS_IN_FILE) == 0) {
array_fi->push_back(wofstream(
"../data_structures/barrels/" + to_string(i / (WORDS_IN_FILE )) + ".txt",
ios::app));
}
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();
}
}
}
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::app));
}
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;
wchar_t c;
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)) {
if (i % (WORDS_IN_FILE) == 0) {
array_fi->push_back(wofstream(
"../data_structures/barrels/" + to_string(i / (WORDS_IN_FILE)) + ".txt",
ios::app));
}
lexicon[word] = i++;
wordsInTags.push_back(word);
}
word.clear();
}
}
}
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::app));
}
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, hits
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()) << " ";
barrelsToUpdate.insert(wordId/ WORDS_IN_FILE);
}
}
void buildForwardIndexTags(unordered_map<wstring, int> &lexicon, int importance, wstring &id, vector<wstring> &wordsInTags){
//documentId, wordId, importance
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()) << " ";
barrelsToUpdate.insert(wordId/ WORDS_IN_FILE);
}
}
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;
}
}
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;
}
}
unordered_map<wstring, vector<wstring>> getDocsInfo() {
unordered_map<wstring, vector<wstring>> docs_info;
wifstream doclist("../data_structures/doclist.txt");
docs_info.reserve(100000);
wstring word;
wstring str;
getline(doclist, word);
word.clear();
while(getline(doclist, word, L',')){
vector<wstring> info;
getline(doclist, str, L',');
info.push_back(str);
getline(doclist, str);
info.push_back(str);
docs_info[word] = info;
}
doclist.close();
return docs_info;
}
unordered_map<wstring, int> getLexicon(int &maxId) {
wifstream lexicon_in("../data_structures/lexicon.txt");
unordered_map<wstring, int> lexicon;
wstring word;
int wordId;
lexicon.reserve(156000);
while(getline(lexicon_in, word, L',')){
lexicon_in >> wordId;
if (wordId > maxId){maxId = wordId;}
lexicon[word] = wordId;
getline(lexicon_in, word);
}
lexicon_in.close();
return lexicon;
}
void getLineNums(){
wofstream lineNumberFile("../data_structures/lineNumbers.txt", ios::out);
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;
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();
}