Created
November 29, 2018 00:34
-
-
Save Khalefa/0fc22b79b2d645927258c083f8a8a858 to your computer and use it in GitHub Desktop.
SpellChecker.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include<unordered_map> | |
#include <string> | |
using namespace std; | |
; | |
vector<string> exchange_two_chars(string &s); | |
vector<string> add_one_char(string &s); | |
vector<string> remove_one_char(string &s); | |
string normalize(string &s) { | |
s.erase(s.find_last_not_of(" \n\r\t") + 1); | |
for (int i = 0; i < s.size(); i++) { | |
s[i] = tolower(s[i]); | |
} | |
return s; | |
} | |
bool readfile(unordered_map<string, int> &dictionary, string filename, int limit = -1) { | |
vector<string> org_words; | |
string line; | |
ifstream myfile(filename); | |
if (myfile.is_open()) | |
{ | |
while (getline(myfile, line)) | |
{ | |
if (limit == 0) break; | |
limit--; | |
string str = normalize(line); | |
org_words.push_back(str); | |
for (string s : add_one_char(str)) { | |
dictionary[s] = 1; | |
} | |
for (string s : remove_one_char(str)) { | |
dictionary[s] = 1; | |
} | |
for (string s : exchange_two_chars(str)) { | |
dictionary[s] = 1; | |
} | |
} | |
for(auto word: org_words) | |
{ | |
dictionary[word] = 0; | |
} | |
myfile.close(); | |
} | |
else { | |
cout << "Failed to open the file"; | |
return false; } | |
return true; | |
} | |
vector<string> add_one_char(string &s) { | |
vector<string> vect; | |
string x = ""; | |
string y = s; | |
for (int l = 0; l < s.size() + 1; l++) { | |
for (int i = 0; i < 26; i++) | |
{ | |
char ch = 'a' + i; | |
string ss = x + ch + y; | |
vect.push_back(ss); | |
} | |
x = x + y[0]; | |
y = y.erase(0, 1); | |
} | |
return vect; | |
} | |
vector<string> remove_one_char(string &s) { | |
vector<string> vect; | |
for (int i = 0; i < s.size(); i++) { | |
string c = s; | |
c.erase(i, 1); | |
vect.push_back(c); | |
} | |
return vect; | |
} | |
vector<string> exchange_two_chars(string &s) { | |
vector<string> vect; | |
for (int i = 0; i < s.size() - 1; i++) { | |
string c = s; | |
swap(c[i], c[i + 1]); | |
vect.push_back(c); | |
} | |
return vect; | |
} | |
void search(unordered_map<string, int> &dictionary, string &s) { | |
auto itr = dictionary.find(s); | |
if (itr == dictionary.end()) { | |
cout << "not found" << endl; | |
} | |
else { | |
cout << "found " << itr->second << endl; | |
} | |
} | |
int main() { | |
unordered_map<string, int> dictionary; | |
if (!readfile(dictionary, "w.txt",1000)) exit(1); | |
else { | |
cout << "Dictionary Loaded" << endl; | |
} | |
string search_string; | |
cin >> search_string; | |
search(dictionary,search_string); | |
//exchange_two_chars(search); | |
/* | |
if (dictionary.find(search) == dictionary.end()) | |
cout << "not found"; | |
else cout << "found";*/ | |
/*for (auto item : dictionary) { | |
cout << item.first << " " << item.second << endl; | |
}*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include<unordered_map> | |
#include <string> | |
using namespace std; | |
; | |
vector<string> exchange_two_chars(string &s); | |
vector<string> add_one_char(string &s); | |
vector<string> remove_one_char(string &s); | |
string normalize(string &s) { | |
s.erase(s.find_last_not_of(" \n\r\t") + 1); | |
for (int i = 0; i < s.size(); i++) { | |
s[i] = tolower(s[i]); | |
} | |
return s; | |
} | |
bool readfile(unordered_map<string, int> &dictionary, string filename, int limit = -1) { | |
int cnt = 0; | |
vector<string> org_words; | |
string line; | |
ifstream myfile(filename); | |
if (myfile.is_open()) | |
{ | |
while (getline(myfile, line)) | |
{ | |
if (limit == 0) break; | |
limit--; | |
cnt++; | |
if (cnt% 10000==0)cout << "."; | |
string str = normalize(line); | |
org_words.push_back(str); | |
/* for (string s : add_one_char(str)) { | |
dictionary[s] = 1; | |
}*/ | |
for (string s : remove_one_char(str)) { | |
dictionary[s] = 1; | |
} | |
for (string s : exchange_two_chars(str)) { | |
dictionary[s] = 1; | |
} | |
} | |
for(auto word: org_words) | |
{ | |
dictionary[word] = 0; | |
} | |
myfile.close(); | |
} | |
else { | |
cout << "Failed to open the file"; | |
return false; } | |
return true; | |
} | |
vector<string> add_one_char(string &s) { | |
vector<string> vect; | |
string x = ""; | |
string y = s; | |
for (int l = 0; l < s.size() + 1; l++) { | |
for (int i = 0; i < 26; i++) | |
{ | |
char ch = 'a' + i; | |
string ss = x + ch + y; | |
vect.push_back(ss); | |
} | |
x = x + y[0]; | |
y = y.erase(0, 1); | |
} | |
return vect; | |
} | |
vector<string> remove_one_char(string &s) { | |
vector<string> vect; | |
for (int i = 0; i < s.size(); i++) { | |
string c = s; | |
c.erase(i, 1); | |
vect.push_back(c); | |
} | |
return vect; | |
} | |
vector<string> exchange_two_chars(string &s) { | |
vector<string> vect; | |
for (int i = 0; i < s.size() - 1; i++) { | |
string c = s; | |
swap(c[i], c[i + 1]); | |
vect.push_back(c); | |
} | |
return vect; | |
} | |
pair<string,int> search(unordered_map<string, int> &dictionary, string &s) { | |
bool found = false; | |
string found_string; | |
int difference; | |
auto itr = dictionary.find(s); | |
if (itr == dictionary.end()) { | |
for (string str : remove_one_char(s)) { | |
cout << str << endl; | |
auto it = dictionary.find(str); | |
if (it != dictionary.end()) { | |
found = true; | |
found_string = it->first; | |
difference = it->second + 1; | |
break; | |
} | |
} | |
} | |
else { | |
found = true; | |
found_string = itr->first; | |
difference = itr->second; | |
} | |
if (found) { | |
cout << "found " << s << " " << found_string << " " << difference << endl; | |
return make_pair(found_string, difference); | |
} | |
else | |
return make_pair("NOT FOUND", -1); | |
} | |
int main() { | |
unordered_map<string, int> dictionary; | |
if (!readfile(dictionary, "words.txt")) exit(1); | |
else { | |
cout << "Dictionary Loaded" << endl; | |
} | |
string search_string; | |
cin >> search_string; | |
while (search_string != "end!") { | |
search(dictionary, search_string); | |
cin >> search_string; | |
} | |
//exchange_two_chars(search); | |
/* | |
if (dictionary.find(search) == dictionary.end()) | |
cout << "not found"; | |
else cout << "found";*/ | |
/*for (auto item : dictionary) { | |
cout << item.first << " " << item.second << endl; | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment