-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
259 lines (194 loc) · 7.16 KB
/
trie.cpp
File metadata and controls
259 lines (194 loc) · 7.16 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
#include "Trie.h"
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
// Forward declaration for edit distance function
int calculateEditDistance(const std::string& word1, const std::string& word2);
Trie::Trie() {
root = std::make_shared<TrieNode>();
}
Trie::~Trie() {
// Smart pointers handle cleanup
}
void Trie::insert(const std::string& word, int frequency) {
if (word.empty()) return;
std::shared_ptr<TrieNode> current = root;
for (char c : word) {
char lowerChar = std::tolower(c);
if (current->children.find(lowerChar) == current->children.end()) {
current->children[lowerChar] = std::make_shared<TrieNode>();
}
current = current->children[lowerChar];
}
current->isEndOfWord = true;
current->frequency = frequency;
}
bool Trie::search(const std::string& word) const {
if (word.empty()) return false;
std::shared_ptr<TrieNode> current = root;
for (char c : word) {
char lowerChar = std::tolower(c);
if (current->children.find(lowerChar) == current->children.end()) {
return false;
}
current = current->children[lowerChar];
}
return current->isEndOfWord;
}
bool Trie::startsWith(const std::string& prefix) const {
if (prefix.empty()) return true;
std::shared_ptr<TrieNode> current = root;
for (char c : prefix) {
char lowerChar = std::tolower(c);
if (current->children.find(lowerChar) == current->children.end()) {
return false;
}
current = current->children[lowerChar];
}
return true;
}
void Trie::collectWords(std::shared_ptr<TrieNode> node,
const std::string& prefix,
std::vector<std::pair<std::string, int>>& results) const {
if (!node) return;
if (node->isEndOfWord) {
results.push_back({prefix, node->frequency});
}
for (const auto& pair : node->children) {
collectWords(pair.second, prefix + pair.first, results);
}
}
std::vector<std::string> Trie::autocomplete(const std::string& prefix, int k) const {
std::vector<std::string> suggestions;
if (prefix.empty() || k <= 0) return suggestions;
std::shared_ptr<TrieNode> current = root;
std::string lowerPrefix;
for (char c : prefix) {
char lowerChar = std::tolower(c);
lowerPrefix += lowerChar;
if (current->children.find(lowerChar) == current->children.end()) {
return suggestions;
}
current = current->children[lowerChar];
}
// Collect all words with this prefix
std::vector<std::pair<std::string, int>> results;
collectWords(current, lowerPrefix, results);
// Sort by frequency
std::sort(results.begin(), results.end(),
[](const auto& a, const auto& b) {
return a.second > b.second;
});
int count = std::min(k, static_cast<int>(results.size()));
for (int i = 0; i < count; ++i) {
suggestions.push_back(results[i].first);
}
return suggestions;
}
void Trie::getAllWords(std::shared_ptr<TrieNode> node,
const std::string& currentWord,
std::vector<std::string>& allWords) const {
if (!node) return;
if (node->isEndOfWord) {
allWords.push_back(currentWord);
}
for (const auto& pair : node->children) {
getAllWords(pair.second, currentWord + pair.first, allWords);
}
}
std::vector<std::string> Trie::getSuggestions(const std::string& word,
int maxEditDistance) const {
std::vector<std::string> suggestions;
if (word.empty()) return suggestions;
std::string lowerWord;
for (char c : word) {
lowerWord += std::tolower(c);
}
if (search(lowerWord)) {
suggestions.push_back(lowerWord);
return suggestions;
}
// Get all dictionary words
std::vector<std::string> allWords;
getAllWords(root, "", allWords);
// Find words within edit distance threshold
std::vector<std::pair<std::string, int>> candidatesWithDistance;
for (const std::string& dictWord : allWords) {
int distance = calculateEditDistance(lowerWord, dictWord);
if (distance <= maxEditDistance) {
candidatesWithDistance.push_back({dictWord, distance});
}
}
// Sort by edit distance
std::sort(candidatesWithDistance.begin(), candidatesWithDistance.end(),
[](const auto& a, const auto& b) {
return a.second < b.second;
});
for (const auto& pair : candidatesWithDistance) {
suggestions.push_back(pair.first);
}
return suggestions;
}
int Trie::getFrequency(const std::string& word) const {
if (word.empty()) return 0;
std::shared_ptr<TrieNode> current = root;
for (char c : word) {
char lowerChar = std::tolower(c);
if (current->children.find(lowerChar) == current->children.end()) {
return 0;
}
current = current->children[lowerChar];
}
return current->isEndOfWord ? current->frequency : 0;
}
void Trie::incrementFrequency(const std::string& word) {
if (word.empty()) return;
std::shared_ptr<TrieNode> current = root;
std::string lowerWord;
for (char c : word) {
char lowerChar = std::tolower(c);
lowerWord += lowerChar;
if (current->children.find(lowerChar) == current->children.end()) {
return; // Word doesn't exist
}
current = current->children[lowerChar];
}
if (current->isEndOfWord) {
current->frequency++;
}
}
void Trie::collectAllWordsWithFrequency(std::shared_ptr<TrieNode> node,
const std::string& currentWord,
std::vector<std::pair<std::string, int>>& words) const {
if (!node) return;
if (node->isEndOfWord) {
words.push_back({currentWord, node->frequency});
}
for (const auto& pair : node->children) {
collectAllWordsWithFrequency(pair.second, currentWord + pair.first, words);
}
}
std::vector<std::pair<std::string, int>> Trie::getAllWordsWithFrequency() const {
std::vector<std::pair<std::string, int>> words;
collectAllWordsWithFrequency(root, "", words);
// Sort alphabetically for consistent output
std::sort(words.begin(), words.end(),
[](const auto& a, const auto& b) {
return a.first < b.first;
});
return words;
}
bool Trie::saveDictionary(const std::string& filename) const {
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file '" << filename << "' for writing" << std::endl;
return false;
}
std::vector<std::pair<std::string, int>> words = getAllWordsWithFrequency();
for (const auto& pair : words) {
file << pair.first << " " << pair.second << "\n";
}
file.close();
return true;
}