This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellchecker.cpp
134 lines (124 loc) · 4.43 KB
/
spellchecker.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
/**
* @file spellchecker.cpp
* @author Madison Solarana
* @brief The spell checker program.
* @details This program checks the spelling of user input strings using the default unix dictionary.
* @date Thu Mar 7, 2013
* @version 1.4
* @copyright Academic Free License ("AFL") v. 3.0
* Problem Description: http://www.twitch.tv/problems/spellcheck
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <chrono>
#include <cctype>
#include <cstdlib>
#include "node.h"
#include "trie.h"
using namespace std;
/**
* Function that determines whether or not user input is safe to search for
* @param input - the string the user input via stdin console
* @return returns true if the string just contains characters, false if otherwise
*/
bool isValidInput(const string& input);
int main()
{
Trie dictionary; //Trie in which the dictionary contents are stored in
bool exitChoice = false; //Boolean flag to control program termination
string reader = ""; //String that will store the contents of the dictionary file in temporarily
unsigned int numWords = 0; //Integer that holds the number of words in the dictionary
ifstream fileIn; //Input file stream interface
typedef std::chrono::high_resolution_clock highResClock; //typedef for convenience in writing certain expressions
typedef std::chrono::time_point<highResClock> time; //typedef for convenience in writing certain expressions
typedef std::chrono::milliseconds milliseconds; //typedef for convenience in writing certain expressions
typedef std::chrono::microseconds microseconds; //typedef for convenience in writing certain expressions
time dictionaryStartTime, dictionaryEndTime; //Time variables that will hold the starting and stopping time of the dictionary loading operation
cout << "Welcome to the Spell Checker." << endl;
fileIn.open("/usr/share/dict/words");
if(fileIn.fail())
{
cerr << "Unable to open /usr/share/dict/words!" << endl;
cerr << "Aborting program execution due to catastrophic failure..." << endl;
fileIn.close();
exit(EXIT_FAILURE);
}
dictionaryStartTime = highResClock::now();
while(!fileIn.eof())
{
fileIn >> reader;
std::transform(reader.begin(), reader.end(), reader.begin(), ::tolower); //Some words begin with uppercase letters, but we need to be case-insensitive
dictionary.addWord(reader);
++numWords;
}
dictionaryEndTime = highResClock::now();
fileIn.close();
auto elapsedDictionaryTime = std::chrono::duration_cast<milliseconds>(dictionaryEndTime - dictionaryStartTime).count(); //Store the elapsed time of the dictionary loading operation
cout << numWords << " word(s) loaded into " << dictionary.getNodeCount() << " node(s) in " << elapsedDictionaryTime << " millisecond(s)." << endl << endl;
do
{
time correctionStartTime, correctionEndTime; //Time variables that will hold the starting and stopping time if the correction operation
std::set<string> corrections;
string userInput = "";
cout << "Enter a word (\'-1\' to quit):" << endl;
cout << " > ";
cin >> userInput;
if(userInput == "-1")
{
exitChoice = true;
}
else if(isValidInput(userInput) == true)
{
correctionStartTime = highResClock::now();
corrections = dictionary.getCorrections(userInput);
correctionEndTime = highResClock::now();
auto elapsedCorrectionTime = std::chrono::duration_cast<microseconds>(correctionEndTime - correctionStartTime).count(); //Store the elapsed time of the correction operation
if(corrections.empty() != true)
{
cout << corrections.size() << " possible correction(s) found in " << elapsedCorrectionTime << " microsecond(s)." << endl;
cout << "Suggestion(s): ";
for(const string& s : corrections)
{
cout << s << " ";
}
cout << endl;
}
else
{
cout << "No Suggestions" << endl;
}
}
else
{
cout << "Invalid input! Please try again with a word containing only [a-z]." << endl;
}
}while(!exitChoice);
cout << "Terminating program execution..." << endl;
return 0;
}
bool isValidInput(const string& input)
{
if(input == "")
{
return false;
}
else
{
for(const char& c : input)
{
if(!isalpha(c))
{
return false;
}
}
if(input.size() > 32767) //Unsigned short max
{
return false;
}
}
return true;
}