-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
282 lines (198 loc) · 6.1 KB
/
app.cpp
File metadata and controls
282 lines (198 loc) · 6.1 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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
// DATA STRUCTURES
typedef pair<string, int> Query;
vector<Query> searchData;
const int TOP_K = 5;
void printDivider();
// UTILITIES
string toLowerCase(string str) {
for (char& c : str) c = tolower(c);
return str;
}
string trim(string str) {
size_t start = str.find_first_not_of(" \t\n\r");
if (start == string::npos) return "";
size_t end = str.find_last_not_of(" \t\n\r");
return str.substr(start, end - start + 1);
}
// EDIT DISTANCE ( SEARCH)
int editDistance(string a, string b) {
int n = a.size(), m = b.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for (int i = 0; i <= n; i++) dp[i][0] = i;
for (int j = 0; j <= m; j++) dp[0][j] = j;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = 1 + min({
dp[i - 1][j],
dp[i][j - 1],
dp[i - 1][j - 1]
});
}
}
return dp[n][m];
}
// DATA LOADING
void loadData() {
ifstream file("data.txt");
if (!file.is_open()) {
cerr << "Error: Unable to open data.txt\n";
exit(1);
}
string line;
while (getline(file, line)) {
size_t commaPos = line.find(',');
if (commaPos == string::npos || commaPos == 0) continue;
string query = trim(line.substr(0, commaPos));
string freqStr = trim(line.substr(commaPos + 1));
try {
int frequency = stoi(freqStr);
query = toLowerCase(query);
searchData.push_back({query, frequency});
} catch (...) {
continue;
}
}
file.close();
if (searchData.empty()) {
cerr << "Error: No data loaded\n";
exit(1);
}
sort(searchData.begin(), searchData.end(),
[](const Query& a, const Query& b) {
return a.first < b.first;
});
cout << "✓ Loaded " << searchData.size() << " queries\n";
}
// BINARY SEARCH (PREFIX)
int binarySearchPrefix(const string& prefix) {
int left = 0, right = searchData.size() - 1;
int result = -1;
while (left <= right) {
int mid = (left + right) / 2;
int cmp = searchData[mid].first.compare(0, prefix.size(), prefix);
if (cmp == 0) {
result = mid;
right = mid - 1;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// HIGHLIGHT MATCH
string highlightMatch(string text, string prefix) {
size_t pos = text.find(prefix);
if (pos == string::npos) return text;
return text.substr(0, pos) +
"[" + text.substr(pos, prefix.length()) + "]" +
text.substr(pos + prefix.length());
}
// AUTOCOMPLETE ENGINE (HYBRID + FUZZY)
vector<Query> autocomplete(string prefix) {
vector<Query> results;
if (prefix.empty()) return results;
prefix = toLowerCase(prefix);
// STEP 1: Prefix match (Binary Search)
int startIdx = binarySearchPrefix(prefix);
if (startIdx != -1) {
for (int i = startIdx; i < searchData.size(); i++) {
if (searchData[i].first.compare(0, prefix.size(), prefix) == 0) {
results.push_back(searchData[i]);
} else break;
}
}
// STEP 2: Substring fallback
if (results.empty()) {
for (const auto& item : searchData) {
if (item.first.find(prefix) != string::npos) {
results.push_back(item);
}
}
}
// STEP 3: Fuzzy matching (typo tolerance)
if (results.empty()) {
for (const auto& item : searchData) {
string part = item.first.substr(0, prefix.size());
if (editDistance(part, prefix) <= 1) {
results.push_back(item);
}
}
}
// STEP 4: Ranking
sort(results.begin(), results.end(),
[](const Query& a, const Query& b) {
if (a.second != b.second)
return a.second > b.second;
return a.first < b.first;
});
if (results.size() > TOP_K)
results.resize(TOP_K);
return results;
}
// DISPLAY (PREMIUM MINIMAL)
void printDivider() {
cout << "────────────────────────────────────────────\n";
}
string trimDisplay(string text, int width = 38) {
if (text.length() > width)
return text.substr(0, width - 3) + "...";
return text;
}
void displaySuggestions(const vector<Query>& results, const string& searchTerm) {
printDivider();
cout << "🔍 Query: " << toLowerCase(searchTerm) << "\n\n";
if (results.empty()) {
cout << "No suggestions found.\n";
printDivider();
return;
}
cout << "Top Suggestions\n";
cout << "───────────────\n";
string lowerQuery = toLowerCase(searchTerm);
for (const auto& r : results) {
string highlighted = highlightMatch(r.first, lowerQuery);
cout << "• "
<< left << setw(40)
<< trimDisplay(highlighted)
<< r.second << "\n";
}
printDivider();
}
// MAIN
int main() {
cout << "\nSearchCraft — Autocomplete Engine\n";
cout << "Type 'exit' to quit\n";
printDivider();
cout << "Loading data...\n";
loadData();
string userInput;
while (true) {
cout << "\nEnter search: ";
getline(cin, userInput);
userInput = trim(userInput);
if (userInput == "exit" || userInput == "quit" || userInput == "q") {
cout << "\nGoodbye.\n";
break;
}
if (userInput.empty()) {
cout << "Please enter a valid query.\n";
continue;
}
vector<Query> suggestions = autocomplete(userInput);
displaySuggestions(suggestions, userInput);
}
return 0;
}