-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.cpp
353 lines (277 loc) · 9.47 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
#include "pinhawiki.h"
namespace indexer {
// ↓ Enables us to save space by using a single unordered_map for weights w[{ i, j }]
struct PairHash {
size_t operator()(const pair<int, int>& p) const {
return hash<int>{}(p.first) ^ hash<int>{}(p.second);
}
};
const int kRatioTitleToText = 30;
// ↓ Number of documents in the collection (assigned in LoadTitles)
size_t N;
// ↓ Number of terms in the collection (assigned in LoadTerms)
size_t M;
vector<string> titles; // Maps document id to title
unordered_map<string, int> title_to_id; // Maps title to document id (for checking exact matches)
// ↓ Unprocessed titles for returning results that can be used directly in links
vector<string> original_titles;
// ↓ Send only the final destination when returning results
unordered_map<string, string> redirections; // Maps alias to target title
unordered_map<string, int> term_to_id; // Maps term to id (0, 1, 2, ...)
vector<vector<IndexNode>> inverted_index;
vector<int> first_term_id_in_file, first_title_id_in_file;
// ↓ Resized and assigned in LoadTerms
vector<int> TF; // Term Frequency of term i in collection (not the local TF)
// ↓ Resized in LoadTerms, assigned in BuildIDF
vector<float> IDF; // Inverse Document Frequency of term i
vector<float> vector_norms;
inline float ComputeIDF(int i) {
return log2(float(N) / TF[i]); // In the book: page 38, formula 2.6
}
void BuildIDF() {
for (int i = 0; i < int(M); i++)
IDF[i] = ComputeIDF(i);
}
void LoadTitles() {
ifstream ifs(utility::Path("titles"));
string s;
titles.clear();
while (getline(ifs, s))
titles.push_back(utility::RemoveTrailingTrash(s));
ifs.close();
N = titles.size();
}
void LoadOriginalTitles() {
ifstream ifs(utility::Path("original_titles"));
string s;
original_titles.clear();
while (getline(ifs, s))
original_titles.push_back(utility::RemoveTrailingTrash(s));
ifs.close();
}
void LoadRedirections() {
ifstream ifs(utility::Path("redirections"));
string alias, target_title;
redirections.clear();
while (getline(ifs, alias) && getline(ifs, target_title))
redirections[utility::RemoveTrailingTrash(alias)] = utility::RemoveTrailingTrash(target_title);
unordered_map<string, string> to_add;
for (const auto& p : redirections) {
const string temp = preprocess::LowerAsciiSingleLine(p.first);
if (temp != p.second && !redirections.count(temp))
to_add[temp] = p.second;
}
for (const auto& p : to_add)
redirections.insert(p);
ifs.close();
}
void LoadTerms() {
M = utility::CountLines("terms");
TF.resize(M);
IDF.resize(M);
term_to_id.clear();
ifstream ifs(utility::Path("terms"));
string term;
int id = -1;
while (++id < int(M)) {
if (!(ifs >> term >> TF[id]))
break;
term_to_id[term] = id;
}
ifs.close();
BuildIDF();
}
void SaveTerms() {
LoadTitles();
cout << "Loaded " << N << " titles.\n";
ifstream ifs(utility::Path("articles"));
string article, term;
unordered_map<string, int> unique_terms;
while (getline(ifs, article)) {
stringstream ss(article);
while (ss >> term)
unique_terms[term]++;
}
ifs.close();
// ↓ For article title, keep all all-ascii terms
for (const string& title : titles) {
stringstream ss(title);
while (ss >> term)
if (utility::AllAscii(term)) {
const string processed_term = utility::NoParentheses(term);
if (processed_term.length() > 1)
unique_terms[processed_term]++;
}
}
// ↓ Get rid of extreme terms
for (auto& term : unique_terms)
if (term.second < 4 || term.second > 499999)
term.second = 0;
ofstream ofs(utility::Path("terms"));
for (auto& term : unique_terms)
if (term.second > 0)
ofs << term.first << " " << term.second << "\n";
ofs.close();
}
void LoadIndex() {
ifstream ifs(utility::Path("index"));
inverted_index.clear();
inverted_index.resize(M);
string line;
int sz, j, w;
for (int i = 0; i < int(M); i++) {
getline(ifs, line);
stringstream ss(line);
ss >> sz;
inverted_index[i].resize(sz);
for (int k = 0; k < sz; k++) {
ss >> j >> w;
inverted_index[i][k] = IndexNode{ j, w };
}
}
ifs.close();
}
void SaveIndex() {
ofstream ofs(utility::Path("index"));
for (int i = 0; i < int(M); i++) {
const size_t sz = inverted_index[i].size();
ofs << sz;
for (size_t k = 0; k < sz; k++) {
ofs << " " << inverted_index[i][k].j << " " << inverted_index[i][k].w;
}
ofs << "\n";
}
ofs.close();
}
void LoadNorms() {
ifstream ifs(utility::Path("vector_norms"));
vector_norms.resize(N);
for (int i = 0; i < int(N); i++)
ifs >> vector_norms[i];
ifs.close();
}
void SaveNorms() {
ofstream ofs(utility::Path("vector_norms"));
for (int i = 0; i < int(N); i++)
ofs << fixed << setprecision(2) << vector_norms[i] << "\n";
ofs.close();
}
void LoadFirstTermIdInFile() {
ifstream ifs(utility::Path("first_term_id_in_file"));
first_term_id_in_file.clear();
int x;
while (ifs >> x)
first_term_id_in_file.push_back(x);
first_term_id_in_file.push_back(INT32_MAX);
ifs.close();
}
void LoadFirstTitleIdInFile() {
ifstream ifs(utility::Path("first_title_id_in_file"));
first_title_id_in_file.clear();
int x;
while (ifs >> x)
first_title_id_in_file.push_back(x);
first_title_id_in_file.push_back(INT32_MAX);
ifs.close();
}
void BuildTitleToId() {
title_to_id.clear();
for (int i = 0; i < int(N); i++)
title_to_id[titles[i]] = i;
}
inline double PunishTinyDocs(int doc_word_count, double vector_norm) {
if (doc_word_count > 1500)
return vector_norm;
const double c = .6 - .002 * min(250, max(0, 1000 - doc_word_count));
const double m = (1. - c) / 1500.;
return pow(vector_norm, 1. / (m * doc_word_count + c));
}
void BuildIndex() {
LoadTitles();
cout << "Loaded " << N << " titles.\n";
LoadTerms();
cout << "Loaded " << M << " terms.\n";
// ↓ For term i, document j has weight w[{ i, j }]
unordered_map<pair<int, int>, int, PairHash> w;
vector<int> term_count_in_doc(N);
ifstream ifs(utility::Path("articles"));
string line, term;
for (int j = 0; j < int(N) && getline(ifs, line); j++) { // For each document j
stringstream ss(line), sst(titles[j]);
// ↓ Term Frequency for each term i in this document
unordered_map<int, int> document_TF;
// ↓ Whether each term i occurs is title
unordered_set<int> occurs_in_title;
// ↓ Temporary container for pairs of weights and ids (for sorting)
vector<pair<int, int>> w_and_id;
// ↓ Build title Term Frequencies
while (sst >> term)
if (utility::AllAscii(term)) {
term = utility::NoParentheses(term);
if (term.length() < 2)
continue;
if (term_to_id.count(term)) {
const int i = term_to_id[term];
occurs_in_title.insert(i);
document_TF[i] = 0;
}
}
// ↓ Build text Term Frequencies
while (ss >> term)
if (term_to_id.count(term)) {
const int i = term_to_id[term];
++document_TF[i];
++term_count_in_doc[j];
}
// ↓ Build weights for all terms for document j
for (const pair<int, int>& p : document_TF) {
const int i = p.first;
if (IDF[i] < 1) // Term is too frequent to matter
continue;
// ↓ Occurrences of term i in the text of document j
const int text_ct = p.second;
// ↓ Occurrences of term i in the title of document j (save memory by not adding extra zeroes)
const int weight = kRatioTitleToText * int(occurs_in_title.count(i)) + text_ct;
w_and_id.push_back({ weight, i });
}
// ↓ Consider only the top 80% heaviest terms for document j
const size_t threshold = .8 * w_and_id.size();
sort(begin(w_and_id), end(w_and_id), greater<pair<int, int>>());
for (size_t k = 0; k < threshold; k++) {
const int weight = w_and_id[k].first;
const int i = w_and_id[k].second;
w[{ i, j }] = weight;
}
}
ifs.close();
inverted_index.resize(M);
// ↓ Set inverted index sizes
vector<int> inverted_index_sizes(M);
for (const auto& p : w)
inverted_index_sizes[p.first.first]++;
for (int i = 0; i < int(M); i++)
inverted_index[i].resize(inverted_index_sizes[i]);
vector<int> inverted_index_current_position(M);
// ↓ Build the index and the vector norms
vector_norms.assign(N, 0);
for (const auto& p : w) {
const int i = p.first.first;
const int j = p.first.second;
const int weight = p.second;
const int idx = inverted_index_current_position[i];
inverted_index[i][idx] = IndexNode{ j, weight };
++inverted_index_current_position[i];
vector_norms[j] += (float)weight * weight;
}
for (int j = 0; j < int(vector_norms.size()); j++)
vector_norms[j] = sqrt(PunishTinyDocs(term_count_in_doc[j], vector_norms[j]));
}
void FullBuild() {
cout << "Building index.\n";
BuildIndex();
cout << "Saving index.\n";
SaveIndex();
cout << "Saving norms.\n";
SaveNorms();
}
}