-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
216 lines (179 loc) · 7.43 KB
/
main.cpp
File metadata and controls
216 lines (179 loc) · 7.43 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
#include "Trie.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <chrono>
bool loadDictionary(const std::string& filename, Trie& trie) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file '" << filename << "'" << std::endl;
return false;
}
std::string line;
int wordCount = 0;
while (std::getline(file, line)) {
if (line.empty()) continue;
std::istringstream iss(line);
std::string word;
int frequency;
if (iss >> word >> frequency) {
trie.insert(word, frequency);
wordCount++;
}
}
file.close();
std::cout << "Loaded " << wordCount << " words" << std::endl;
return wordCount > 0;
}
void handleAutocomplete(Trie& trie) {
std::string prefix;
std::cout << "\nEnter prefix: ";
std::cin >> prefix;
auto start = std::chrono::high_resolution_clock::now();
std::vector<std::string> suggestions = trie.autocomplete(prefix, 5);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "\n--- Results ---" << std::endl;
if (suggestions.empty()) {
std::cout << "No suggestions found" << std::endl;
} else {
std::cout << "Suggestions for '" << prefix << "':" << std::endl;
for (size_t i = 0; i < suggestions.size(); ++i) {
std::cout << " " << (i + 1) << ". " << suggestions[i]
<< " (freq: " << trie.getFrequency(suggestions[i]) << ")" << std::endl;
}
// Allow user to select a suggestion
std::cout << "\nSelect a suggestion (1-" << suggestions.size() << ") or 0 to skip: ";
int choice;
std::cin >> choice;
if (choice > 0 && choice <= static_cast<int>(suggestions.size())) {
std::string selectedWord = suggestions[choice - 1];
trie.incrementFrequency(selectedWord);
std::cout << "Selected: " << selectedWord
<< " (new freq: " << trie.getFrequency(selectedWord) << ")" << std::endl;
}
}
std::cout << "Time: " << duration.count() << " μs" << std::endl;
}
void handleSpellCheck(Trie& trie) {
std::string word;
std::cout << "\nEnter word: ";
std::cin >> word;
auto start = std::chrono::high_resolution_clock::now();
bool exists = trie.search(word);
if (exists) {
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "\n✓ Correct spelling!" << std::endl;
std::cout << "Frequency: " << trie.getFrequency(word) << std::endl;
std::cout << "Time: " << duration.count() << " μs" << std::endl;
// Increment frequency for the word being checked
trie.incrementFrequency(word);
std::cout << "Word used! New frequency: " << trie.getFrequency(word) << std::endl;
} else {
std::vector<std::string> suggestions = trie.getSuggestions(word, 2);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "\n✗ Not found" << std::endl;
if (suggestions.empty()) {
std::cout << "No suggestions" << std::endl;
} else {
std::cout << "Did you mean:" << std::endl;
int count = std::min(10, static_cast<int>(suggestions.size()));
for (int i = 0; i < count; ++i) {
std::cout << " " << (i + 1) << ". " << suggestions[i] << std::endl;
}
// Allow user to select a suggestion
std::cout << "\nSelect a suggestion (1-" << count << ") or 0 to add '"
<< word << "' to dictionary: ";
int choice;
std::cin >> choice;
if (choice > 0 && choice <= count) {
std::string selectedWord = suggestions[choice - 1];
trie.incrementFrequency(selectedWord);
std::cout << "Selected: " << selectedWord
<< " (new freq: " << trie.getFrequency(selectedWord) << ")" << std::endl;
} else if (choice == 0) {
// Add new word to dictionary
std::string lowerWord;
for (char c : word) {
lowerWord += std::tolower(c);
}
trie.insert(lowerWord, 1);
std::cout << "Added '" << lowerWord << "' to dictionary with frequency 1" << std::endl;
}
}
std::cout << "Time: " << duration.count() << " ms" << std::endl;
}
}
void handleAddWord(Trie& trie) {
std::string word;
std::cout << "\nEnter new word to add: ";
std::cin >> word;
if (word.empty()) {
std::cout << "Invalid word" << std::endl;
return;
}
std::string lowerWord;
for (char c : word) {
lowerWord += std::tolower(c);
}
if (trie.search(lowerWord)) {
std::cout << "Word '" << lowerWord << "' already exists with frequency: "
<< trie.getFrequency(lowerWord) << std::endl;
} else {
trie.insert(lowerWord, 1);
std::cout << "Successfully added '" << lowerWord << "' to dictionary!" << std::endl;
}
}
int main() {
std::cout << "=== Autocomplete & Spell Checker ===" << std::endl;
Trie trie;
std::string dictionaryFile = "dictionary.txt";
auto startLoad = std::chrono::high_resolution_clock::now();
if (!loadDictionary(dictionaryFile, trie)) {
std::cerr << "Failed to load dictionary" << std::endl;
return 1;
}
auto endLoad = std::chrono::high_resolution_clock::now();
auto loadTime = std::chrono::duration_cast<std::chrono::milliseconds>(endLoad - startLoad);
std::cout << "Load time: " << loadTime.count() << " ms\n" << std::endl;
bool running = true;
while (running) {
std::cout << "\n========================================" << std::endl;
std::cout << "1. Autocomplete" << std::endl;
std::cout << "2. Spell Checker" << std::endl;
std::cout << "3. Add New Word" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "Choice: ";
int choice;
std::cin >> choice;
std::cin.clear();
std::cin.ignore(10000, '\n');
switch (choice) {
case 1:
handleAutocomplete(trie);
break;
case 2:
handleSpellCheck(trie);
break;
case 3:
handleAddWord(trie);
break;
case 4:
std::cout << "\nSaving dictionary..." << std::endl;
if (trie.saveDictionary(dictionaryFile)) {
std::cout << "Dictionary saved successfully!" << std::endl;
} else {
std::cerr << "Failed to save dictionary!" << std::endl;
}
std::cout << "Goodbye!" << std::endl;
running = false;
break;
default:
std::cout << "Invalid choice" << std::endl;
}
}
return 0;
}