This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
did_you_mean_optimized.cpp
160 lines (133 loc) · 3.34 KB
/
did_you_mean_optimized.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
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
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
int levenstain(string str1, string str2);
void possible_var(string words_base[], int size_of_words_base, string inputed_string);
int main() {
string input;
while (true)
{
cout << "Enter the word or sentence" << endl;
getline(cin, input);
string path = "20k.txt"; //input the path to your file with words
//in my case the directory of my file is the same with this project directory
string words[20001];
int i = 0;
ifstream fin;
fin.open(path);
int words_size = sizeof(words) / sizeof(words[0]);
string check_msg = !fin.is_open() ? "Error" : "File successfully opened";
cout << check_msg << endl;
string temp;
while (!fin.eof())
{
temp = " ";
fin >> temp;
words[i] = temp;
i++;
}
cout << "Did you mean: ";
possible_var(words, words_size, input);
cout << endl;
fin.close();
}
return 0;
}
#pragma region Functions
int levenstain(string str1, string str2) {
int height = str1.length() + 1;
int length = str2.length() + 1;
int** matrix = new int*[height];
for (int i = 0; i < height; i++) {
matrix[i] = new int[length];
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (i == 0 && j == 0) {
matrix[i][j] = 0;
}
else if (i == 0) {
matrix[i][j] = matrix[i][j - 1] + 1;
}
else if (j == 0) {
matrix[i][j] = matrix[i - 1][j] + 1;
}
else {
if (str1[i - 1] == str2[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1];
}
else {
matrix[i][j] = 1 + min(matrix[i - 1][j - 1], min(matrix[i - 1][j], matrix[i][j - 1]));
}
}
}
}
return matrix[height - 1][length - 1];
}
void possible_var(string words_base[], int size_of_words_base, string inputed_string) {
string temp;
int iwa_size = 100;
string* inputed_words_arr = new string[iwa_size];
int last_point = 0, i = 0, j = 0, k = 0, iter = 0;
//transfer all words from inputed string to array of words
do
{
if (!(isspace(inputed_string[k])) || inputed_string[k] == '\0')
{
if (inputed_string[j] == ' ') {
j++;
}
else {
j = last_point;
}
while (inputed_string[j] != ' ' && j < inputed_string.length())
{
temp += inputed_string[j];
j++;
}
}
last_point = j;
k = last_point + 1;
inputed_words_arr[iter] = temp;
i++;
iter++;
temp = "";
} while (inputed_string[j] != '\0');
iwa_size = iter;
int index_of_min = 0;
string output, checking_word;
//compare each word in our array of words with words base
for (int i = 0; i < iwa_size; i++)
{
int min = INT_MAX;
int comparison_result = 0;
for (int j = 0; j < size_of_words_base; j++)
{
comparison_result = levenstain(inputed_words_arr[i], words_base[j]);
if (comparison_result == 0) //same word
{
output += inputed_words_arr[i];
output += " ";
break;
}
else
{
if (comparison_result < min)
{
min = comparison_result;
index_of_min = j;
}
}
}
if (checking_word != words_base[index_of_min] && comparison_result != 0)
{
output += words_base[index_of_min];
output += " ";
}
checking_word = words_base[index_of_min];
}
cout << output << endl;
}
#pragma endregion