-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_algorithm.cpp
81 lines (68 loc) · 2.29 KB
/
search_algorithm.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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
#include <algorithm>
// Document structure to store document data
struct Document {
int id;
std::string text;
float rank; // Rank is assigned during the search
// Constructor for convenience
Document(int id, const std::string& text, float rank)
: id(id), text(text), rank(rank) {}
};
// Search engine class
class SearchEngine {
private:
std::vector<Document> documents;
std::unordered_map<std::string, std::vector<int>> invertedIndex;
public:
void addDocument(int id, const std::string& text) {
documents.push_back(Document(id, text, 0.0f)); // Add document
std::istringstream stream(text);
std::string word;
while (stream >> word) {
invertedIndex[word].push_back(id); // Build inverted index
}
}
std::vector<Document> search(const std::string& query) {
std::unordered_map<int, float> scores;
std::istringstream stream(query);
std::string word;
while (stream >> word) {
if (invertedIndex.find(word) != invertedIndex.end()) {
for (int id : invertedIndex[word]) {
scores[id] += 1.0f; // Increment score for matching documents
}
}
}
std::vector<Document> results;
for (auto& score : scores) {
auto& doc = documents[score.first];
doc.rank = score.second; // Update document rank
results.push_back(doc);
}
// Sort results by rank in descending order
std::sort(results.begin(), results.end(),
[](const Document& a, const Document& b) {
return a.rank > b.rank;
});
return results;
}
};
// Main function
int main() {
SearchEngine engine;
engine.addDocument(0, "Python crawler for NLP tasks");
engine.addDocument(1, "C++ implementation of search algorithms");
engine.addDocument(2, "NLP techniques and tools");
auto results = engine.search("NLP");
for (const auto& doc : results) {
std::cout << "Document ID: " << doc.id
<< ", Text: " << doc.text
<< ", Rank: " << doc.rank << std::endl;
}
return 0;
}