#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>
#include "dictionary.h"
using namespace std;
void lower(string& s);
string strip_punct(const string& s);
void check_spelling(ifstream& in, Dictionary& dict);
int main(int argc, char* argv[]) {
// Output usage message if improper command line args were given.
if (argc != 3) {
cerr << "Usage: " << argv[0] << " wordlist_filename input_file\n";
return EXIT_FAILURE;
}
ifstream inf(argv[2]);
if (!inf) {
cerr << "Could not open " << argv[2] << "\n";
return EXIT_FAILURE;
}
// Read dictionary, but let user know what we are working on.
cout << "Loading dictionary, this may take awhile...\n";
Dictionary d(argv[1]);
check_spelling(inf, d);
inf.close();
return EXIT_SUCCESS;
}
void check_spelling(ifstream& in, Dictionary& dict) {
int line_number = 0;
while (in) {
line_number++;
string line;
getline(in, line);
stringstream ss(stringstream::in | stringstream::out);
ss << line;
string word;
while (ss >> word) {
string abc = "qwertyuiopasdfghjklzxcvbnm";
lower(word);
word = strip_punct(word);
if (!dict.search(word)){
vector<string> sugg; // array of suggestions
string w = word; //word
// transposing of adjacent letters
for (int i = 0; i < word.size() - 1; i++){
swap(w[i], w[i + 1]);
if (dict.search(w)){
sugg.push_back(w);
}
swap(w[i], w[i + 1]);
}
// TODO: Complete the spell check of each word
// removal of each letter
for (int x = 0; x < word.size() - 1; x++){
w.erase(w.begin() + x); // delete of each letter with function ERASE
if (dict.search(w)){
sugg.push_back(w);
}
}
// replacement of each letter
for (int z = 0; z < word.size() - 1; z++){
for (int j = 0; j < abc.size(); j++){
string zz = " ";
zz[0] = abc[j];
w.insert(z, zz); //inserting the word for each letter
if (dict.search(w)){
sugg.push_back(w);
}
}
w = word;
}
// inserting any letter at any position
for (int z = 0; z < word.size() - 1; z++){
for (int j = 0; j < abc.size(); j++){
w[z] = abc[j];
if (dict.search(w)){
sugg.push_back(w);
}
}
w = word;
}
cout << "Line" << line_number << " " << word << endl;
cout << "Suggestion:" << endl;
for (vector<string>::iterator it = sugg.begin(); it != sugg.end(); it++){
cout << *it << endl;
}
}
}
}
}
void lower(string& s) {
// Ensures that a word is lowercase
for (unsigned int i = 0; i < s.length(); i++) {
s[i] = tolower(s[i]);
}
}
string strip_punct(const string& s) {
// Remove any single trailing
// punctuation character from a word.
if (ispunct(s[s.length() - 1])) {
return s.substr(0, s.length() - 1);
}
else {
return s;
}
}