Repository navigation
Expand file tree
/
Copy pathengine.cpp
More file actions
495 lines (414 loc) · 13.6 KB
/
Copy pathengine.cpp
File metadata and controls
495 lines (414 loc) · 13.6 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "engine.hpp"
#include <cmath>
#include <sstream>
#include <algorithm>
// --- Helper Functions ---
std::string cleanWord(const std::string& word) {
std::string cleaned;
for (char c : word) {
if (std::isalnum(static_cast<unsigned char>(c))) {
cleaned += std::tolower(static_cast<unsigned char>(c));
}
}
return cleaned;
}
std::vector<std::string> tokenize(const std::string& text) {
std::vector<std::string> tokens;
std::stringstream ss(text);
std::string word;
while (ss >> word) {
std::string cleaned = cleanWord(word);
if (!cleaned.empty()) {
tokens.push_back(cleaned);
}
}
return tokens;
}
// --- Trie Implementation ---
Trie::Trie() {
root = std::make_shared<TrieNode>();
}
void Trie::insert(const std::string& word) {
auto current = root;
for (char c : word) {
if (current->children.find(c) == current->children.end()) {
current->children[c] = std::make_shared<TrieNode>();
}
current = current->children[c];
}
current->isEndOfWord = true;
}
bool Trie::search(const std::string& word) const {
auto current = root;
for (char c : word) {
if (current->children.find(c) == current->children.end()) {
return false;
}
current = current->children[c];
}
return current->isEndOfWord;
}
void Trie::collectWords(const std::shared_ptr<TrieNode>& node, std::string currentPrefix, std::vector<std::string>& results) const {
if (!node) return;
if (node->isEndOfWord) {
results.push_back(currentPrefix);
}
// Sort keys alphabetically so autocomplete suggestions are returned in alphabetical order
std::vector<char> keys;
for (const auto& pair : node->children) {
keys.push_back(pair.first);
}
std::sort(keys.begin(), keys.end());
for (char c : keys) {
collectWords(node->children.at(c), currentPrefix + c, results);
}
}
std::vector<std::string> Trie::getWordsWithPrefix(const std::string& prefix) const {
std::vector<std::string> results;
auto current = root;
for (char c : prefix) {
if (current->children.find(c) == current->children.end()) {
return results; // No words match this prefix
}
current = current->children[c];
}
collectWords(current, prefix, results);
return results;
}
std::vector<std::string> Trie::getVocabulary() const {
return getWordsWithPrefix("");
}
// --- Inverted Index Implementation ---
void InvertedIndex::addDocument(int docId, const std::string& content) {
std::vector<std::string> tokens = tokenize(content);
docLengths[docId] = tokens.size();
// Count term frequencies within this document
std::unordered_map<std::string, int> termCounts;
for (const auto& token : tokens) {
termCounts[token]++;
vocabularyTrie.insert(token);
}
// Insert into Inverted Index
for (const auto& pair : termCounts) {
const std::string& term = pair.first;
int freq = pair.second;
index[term].push_back(Posting{docId, freq});
}
}
std::vector<Posting> InvertedIndex::searchWord(const std::string& word) const {
std::string cleaned = cleanWord(word);
auto it = index.find(cleaned);
if (it != index.end()) {
return it->second;
}
return {};
}
std::vector<int> InvertedIndex::searchAnd(const std::vector<std::string>& words) const {
if (words.empty()) return {};
std::vector<std::vector<int>> docLists;
for (const auto& w : words) {
std::string cleaned = cleanWord(w);
auto it = index.find(cleaned);
if (it == index.end()) {
// One of the query terms has no documents, so intersection must be empty
return {};
}
std::vector<int> docs;
for (const auto& posting : it->second) {
docs.push_back(posting.docId);
}
docLists.push_back(docs);
}
// Sort document lists by size (ascending) to optimize intersection
std::sort(docLists.begin(), docLists.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
return a.size() < b.size();
});
std::vector<int> result = docLists[0];
for (size_t i = 1; i < docLists.size(); ++i) {
std::vector<int> temp;
const auto& currentList = docLists[i];
size_t p1 = 0, p2 = 0;
while (p1 < result.size() && p2 < currentList.size()) {
if (result[p1] == currentList[p2]) {
temp.push_back(result[p1]);
p1++;
p2++;
} else if (result[p1] < currentList[p2]) {
p1++;
} else {
p2++;
}
}
result = std::move(temp);
if (result.empty()) break;
}
return result;
}
std::vector<int> InvertedIndex::searchOr(const std::vector<std::string>& words) const {
std::unordered_set<int> uniqueDocs;
for (const auto& w : words) {
std::string cleaned = cleanWord(w);
auto it = index.find(cleaned);
if (it != index.end()) {
for (const auto& posting : it->second) {
uniqueDocs.insert(posting.docId);
}
}
}
std::vector<int> result(uniqueDocs.begin(), uniqueDocs.end());
std::sort(result.begin(), result.end());
return result;
}
// --- Spell Corrector Implementation ---
int SpellCorrector::levenshteinDistance(const std::string& s1, const std::string& s2) {
size_t m = s1.length();
size_t n = s2.length();
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1));
for (size_t i = 0; i <= m; ++i) dp[i][0] = i;
for (size_t j = 0; j <= n; ++j) dp[0][j] = j;
for (size_t i = 1; i <= m; ++i) {
for (size_t j = 1; j <= n; ++j) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + std::min({
dp[i - 1][j], // Deletion
dp[i][j - 1], // Insertion
dp[i - 1][j - 1] // Substitution
});
}
}
}
return dp[m][n];
}
std::vector<std::string> SpellCorrector::getSuggestions(const std::string& word, const std::vector<std::string>& vocabulary, int maxDistance) {
std::string cleaned = cleanWord(word);
if (cleaned.empty()) return {};
std::vector<std::pair<std::string, int>> candidates;
for (const auto& vocabWord : vocabulary) {
// Quick length check optimization: if length difference is greater than maxDistance,
// edit distance is guaranteed to be greater than maxDistance.
if (std::abs(static_cast<int>(cleaned.length()) - static_cast<int>(vocabWord.length())) > maxDistance) {
continue;
}
int dist = levenshteinDistance(cleaned, vocabWord);
if (dist <= maxDistance) {
candidates.push_back({vocabWord, dist});
}
}
// Sort suggestions by distance (ascending)
std::sort(candidates.begin(), candidates.end(), [](const std::pair<std::string, int>& a, const std::pair<std::string, int>& b) {
if (a.second != b.second) {
return a.second < b.second; // lower edit distance first
}
return a.first < b.first; // alphabetical second
});
std::vector<std::string> suggestions;
for (const auto& cand : candidates) {
suggestions.push_back(cand.first);
}
return suggestions;
}
// --- BM25 Ranking Implementation ---
std::vector<SearchResult> BM25::rankDocuments(
const std::vector<std::string>& queryWords,
const std::vector<int>& matchingDocIds,
const InvertedIndex& invertedIndex,
int totalDocuments
) {
std::vector<SearchResult> results;
if (matchingDocIds.empty() || queryWords.empty() || totalDocuments == 0)
return results;
const auto& rawIndex = invertedIndex.getRawIndex();
const auto& docLengths = invertedIndex.getDocLengths();
// BM25 parameters
const double k1 = 1.5;
const double b = 0.75;
// Calculate average document length
double avgDocLength = 0.0;
for (const auto& doc : docLengths) {
avgDocLength += doc.second;
}
avgDocLength /= totalDocuments;
// Calculate BM25 IDF for each query word
std::unordered_map<std::string, double> idfs;
for (const auto& w : queryWords) {
std::string cleaned = cleanWord(w);
auto it = rawIndex.find(cleaned);
int df = 0;
if (it != rawIndex.end())
df = static_cast<int>(it->second.size());
if (df > 0) {
idfs[cleaned] =
std::log(
1.0 +
((totalDocuments - df + 0.5) /
(df + 0.5))
);
}
else {
idfs[cleaned] = 0.0;
}
}
// Score every matching document
for (int docId : matchingDocIds) {
double score = 0.0;
std::unordered_map<std::string,double> bm25Breakdown;
int docLength = 1;
auto lengthIt = docLengths.find(docId);
if(lengthIt != docLengths.end())
docLength = lengthIt->second;
for(const auto& w : queryWords){
std::string cleaned = cleanWord(w);
auto indexIt = rawIndex.find(cleaned);
if(indexIt == rawIndex.end()){
bm25Breakdown[cleaned] = 0.0;
continue;
}
int tf = 0;
for(const auto& posting : indexIt->second){
if(posting.docId == docId){
tf = posting.termFrequency;
break;
}
}
if(tf==0){
bm25Breakdown[cleaned]=0.0;
continue;
}
double idf=idfs[cleaned];
double numerator=tf*(k1+1.0);
double denominator=
tf+
k1*
(
1.0-b+
b*
(
static_cast<double>(docLength)/avgDocLength
)
);
double bm25=idf*(numerator/denominator);
score+=bm25;
bm25Breakdown[cleaned]=bm25;
}
results.push_back(
SearchResult{
docId,
score,
bm25Breakdown
}
);
}
std::sort(
results.begin(),
results.end(),
[](const SearchResult& a,const SearchResult& b){
return a.score>b.score;
}
);
return results;
}
// --- LRU Cache Implementation ---
LRUCache::LRUCache(int cap) : capacity(cap), size(0) {
// Create dummy head and tail sentinel nodes
// Real nodes are inserted between them
// head <-> [most recent] <-> ... <-> [least recent] <-> tail
head = new LRUNode();
tail = new LRUNode();
head->next = tail;
tail->prev = head;
}
LRUCache::~LRUCache() {
clear();
delete head;
delete tail;
}
void LRUCache::removeNode(LRUNode* node) {
// Detach node from its current position in the doubly linked list
// Before: A <-> node <-> B
// After: A <-> B
node->prev->next = node->next;
node->next->prev = node->prev;
}
void LRUCache::insertAtFront(LRUNode* node) {
// Insert node right after the dummy head
// Before: head <-> oldFirst <-> ...
// After: head <-> node <-> oldFirst <-> ...
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
}
void LRUCache::moveToFront(LRUNode* node) {
// Step 1: Remove from current position
removeNode(node);
// Step 2: Insert at front (most recently used)
insertAtFront(node);
}
void LRUCache::evictLRU() {
// The least recently used node is right before the dummy tail
LRUNode* lruNode = tail->prev;
if (lruNode == head) return; // Cache is empty, nothing to evict
// Remove from linked list
removeNode(lruNode);
// Remove from hash map
hashMap.erase(lruNode->key);
// Free memory
delete lruNode;
size--;
}
std::string LRUCache::get(const std::string& key) {
auto it = hashMap.find(key);
if (it == hashMap.end()) {
// Cache MISS — key not found
return "";
}
// Cache HIT — promote to most recently used
LRUNode* node = it->second;
moveToFront(node);
return node->value;
}
void LRUCache::put(const std::string& key, const std::string& value) {
auto it = hashMap.find(key);
if (it != hashMap.end()) {
// Key already exists — update value and promote to front
LRUNode* existingNode = it->second;
existingNode->value = value;
moveToFront(existingNode);
} else {
// New key — check if we need to evict
if (size >= capacity) {
evictLRU();
}
// Create new node and insert at front
LRUNode* newNode = new LRUNode(key, value);
insertAtFront(newNode);
hashMap[key] = newNode;
size++;
}
}
void LRUCache::clear() {
// Walk the linked list and delete all real nodes
LRUNode* current = head->next;
while (current != tail) {
LRUNode* nextNode = current->next;
delete current;
current = nextNode;
}
// Reset list to empty: head <-> tail
head->next = tail;
tail->prev = head;
hashMap.clear();
size = 0;
}
std::vector<std::string> LRUCache::getKeysInOrder() const {
// Walk from head->next (most recent) to tail (least recent)
std::vector<std::string> keys;
LRUNode* current = head->next;
while (current != tail) {
keys.push_back(current->key);
current = current->next;
}
return keys;
}