Repository navigation
Expand file tree
/
Copy pathSearchEngine.cpp
More file actions
147 lines (118 loc) · 3.92 KB
/
Copy pathSearchEngine.cpp
File metadata and controls
147 lines (118 loc) · 3.92 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
#include "SearchEngine.h"
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <print>
#include "Lexer.h"
#include "lib/pugixml/pugixml.hpp"
#define SEARCH_LIMIT 10
SearchEngine::SearchEngine() {
this->filePaths = {};
this->termInDocCount = {};
this->allDocTermFreq = {};
}
void SearchEngine::compile() {
if (this->filePaths.size() == 0) {
std::print("No document to compile");
return;
}
std::println("Indexing documents...");
this->parseAllDocAndGetTermFreq();
}
void SearchEngine::addDocument(const std::string &filePath) {
this->filePaths.emplace_back(filePath);
}
void SearchEngine::addDir(const std::string &path) {
std::filesystem::directory_iterator dirIter =
std::filesystem::directory_iterator(path);
for (const auto &filePath : dirIter) {
this->filePaths.emplace_back(filePath.path().string());
}
}
TermFreq SearchEngine::getTermFreq(std::string &content) {
Lexer lexer(content);
std::vector<std::string> terms = lexer.process();
TermFreq result = {};
result.reserve(int(terms.size() / 2));
for (std::string &term : terms) {
this->makeLowercase(term);
if (result.find(term) == result.end())
result[term] = 1;
else
result[term] += 1;
}
for (auto &[word, freq] : result) {
freq /= terms.size();
if (this->termInDocCount.find(word) == this->termInDocCount.end())
this->termInDocCount[word] = 1;
else
this->termInDocCount[word]++;
}
return result;
}
double SearchEngine::getTermInverseDocumentFreq(const std::string &term) {
int documentCount = 1;
if (this->termInDocCount.find(term) != this->termInDocCount.end())
documentCount = this->termInDocCount[term];
return std::log(this->filePaths.size() / documentCount);
}
double SearchEngine::calculateTFIDF(
const std::string &term, const std::string &path
) {
return this->allDocTermFreq[path][term] *
this->getTermInverseDocumentFreq(term);
}
void SearchEngine::parseAllDocAndGetTermFreq() {
for (const std::string &path : this->filePaths) {
std::string content = this->parseFile(path);
this->allDocTermFreq[path] = this->getTermFreq(content);
}
}
std::string SearchEngine::parseFile(const std::string &filePath) {
pugi::xml_document doc;
doc.load_file(filePath.data());
return this->collectText(doc);
}
std::string SearchEngine::collectText(const pugi::xml_node &node) {
std::string content = "";
for (pugi::xml_node child : node.children()) {
if (child.type() == pugi::node_pcdata ||
child.type() == pugi::node_cdata) {
content += " ";
content += child.value();
}
content += collectText(child);
}
return content;
}
void SearchEngine::makeLowercase(std::string &term) {
std::transform(term.begin(), term.end(), term.begin(), [](unsigned char c) {
return std::tolower(c);
});
}
void SearchEngine::find(std::string &keyword) {
Lexer lexer(keyword);
std::vector<std::string> terms = lexer.process();
std::vector<std::pair<std::string, double>> ranking;
ranking.reserve(this->allDocTermFreq.size());
for (auto &[path, termFreq] : this->allDocTermFreq) {
double rankingScore = 0.0;
for (std::string &term : terms) {
this->makeLowercase(term);
rankingScore += this->calculateTFIDF(term, path);
}
ranking.emplace_back(path, rankingScore);
}
std::sort(
ranking.begin(),
ranking.end(),
[=](std::pair<std::string, double> &first,
std::pair<std::string, double> &second) {
return first.second > second.second;
}
);
int limit = ranking.size() > 10 ? SEARCH_LIMIT : ranking.size();
for (int i = 0; i < limit; i++) {
std::println("{} -> {}", ranking[i].first, ranking[i].second);
}
}