-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuto_Correct.cpp
115 lines (104 loc) · 4.27 KB
/
Auto_Correct.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
//Kyle Skompinski kyleskom
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <math.h>
std::string dict(std::string &,std::map<std::string, int>&);
int main(int argc, char* argv[]){
std::map<std::string, int> m;
if (argc < 2) {
std::cout << "error" << std::endl;
return -1;
}
std::ifstream in(argv[1]);
std::string s;
while(in >> s) {
std::string y;
in >> y;
int x = std::stoi(y);
m.insert(std::pair<std::string,int>(s,x));
}
in.close();
while (std::cin >> s) {
std::string print;
print = dict(s,m);
std::cout << print << std::endl;
}
}
std::string dict(std::string &s,std::map<std::string, int> &m){
std::string word;
int frequency;
auto search = m.find(s);
std::map<std::string, int> m2;
if (search != m.end()) {
word = s;
frequency = search->second;
++m[word];
return word + " " + std::to_string(frequency);
}else {
int i = 0;
int distance;
for (std::map<std::string, int>::iterator lit = m.begin(); lit!=m.end(); ++lit) {
int count = 0;
distance = abs((lit->first.length() - s.length()));
if (lit->first.length() == s.length()) {
for (auto i = 0; i < s.length(); i++) {
if ((lit->first)[i] != s.at(i)) {
count++;
}
if (count == 2) {
break;
}
}
if (count < 2) {
m2.insert(std::pair<std::string,int>(lit->first,lit->second));
}
}
else if (distance == 1) {
std::string longer;
std::string shorter;
if (s.length() > lit->first.length()) {
longer = s;
shorter = lit->first;
}
if(s.length() < lit->first.length()) {
longer = lit->first;
shorter = s;
}
int f = 0;
for (int g = 0; g < longer.length(); g++) {
if (shorter[f] == longer[g]) {
f++;
if (f == shorter.length()) {
break;
}
}
else {
count++;
if (count == 2) {
break;
}
}
}
if (count < 2) {
m2.insert(std::pair<std::string,int>(lit->first,lit->second));
}
}
}
if (m2.empty()) {
m.insert(std::pair<std::string,int>(s,1));
return "-";
}
}
std::map<std::string,int>::iterator it=m2.begin();
std::string answer = it->first;
int best = it->second;
for (; it!=m2.end(); it++) {
if (it->second > best) {
answer = it->first;
best = it->second;
}
}
return answer + " " + std::to_string(best);
}