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
/
trie.h
268 lines (253 loc) · 8.72 KB
/
trie.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* @file trie.h
* @author Madison Solarana
* @brief The Trie data structure.
* @details This class is the definition of a Trie used by the spell checker to hold the dictionary.
* @date Thu Mar 7, 2013
* @version 1.6
* @copyright Academic Free License ("AFL") v. 3.0
*/
#ifndef TRIE_H
#define TRIE_H
#include <set>
#include <string>
#include <utility>
#include <cctype>
#include "node.h"
extern const unsigned short ALPHABET; ///Constant for the number of alphabetic characters defined in node.h
class Trie
{
public:
/**
* Constructor that initializes the root of the Trie and sets the initial node count
*/
Trie()
{
root = new Node();
nodeCount = 0;
}
/**
* Move Constructor that defines move semantics (rvalue reference) so that we can detect rvalue arguments via function overloading
* @param t - a rvalue reference to a Trie
*/
Trie(Trie&& t) : Trie()
{
std::swap(*this, t);
}
//Copy Constructor
Trie(const Trie& t)
{
this->nodeCount = t.nodeCount;
*this->root = *t.root;
}
/**
* Assignment Operator Overload that defines how we set a Trie equal to another Trie using the Copy-and-Swap Idiom
* @param t - the Trie that we are setting the other Trie equal to
* @return returns a pointer to the instance of the Trie calling the assignment operator
*/
Trie& operator=(const Trie& t)
{
if(this != &t) //Verify that this and t are not the same object
{
Trie temp(t);
std::swap(*this, temp);
}
return *this;
}
/**
* Destructor that deletes the dynamic memory contained in the Trie
*/
~Trie()
{
delete root;
}
/**
* Function that returns the number of nodes currently in the Trie
* @return returns an unsigned integer that contains the number of nodes in the Trie
*/
unsigned int getNodeCount() const
{
return nodeCount;
}
/**
* Function that adds a word to the Trie. Duplicate insertion is allowed, but no new nodes are created
* @param word - the word to be added into the tree
*/
void addWord(const std::string& word)
{
Node *currentNode = root;
std::string builder = "";
for(const char& c : word)
{
if(!isalpha(c))
{
continue; //Ignore non-alphabetic characters
}
if(currentNode->getChild(c) == nullptr)
{
currentNode->getChild(c) = new Node();
++nodeCount;
}
currentNode = currentNode->getChild(c);
builder += c;
currentNode->word = builder;
}
currentNode->isEndpoint = true;
}
/**
* Function that searches the Trie for a specific word
* @param word - the word that we are searching for
* @return returns true or false if the word is in the Trie
*/
bool search(const std::string& word) const
{
Node *currentNode = root;
for(const char& c : word)
{
if(!isalpha(c))
{
continue; //Ignore non-alphabetic characters
}
if(currentNode->getChild(c) == nullptr)
{
return false;
}
currentNode = currentNode->getChild(c);
}
return currentNode->isEndpoint;
}
/**
* Function that returns the possible corrections for a word. If there are no corrections, the empty set is returned
* @param word - the word that needs to be corrected
* @return returns a set of strings that are possible corrections for the word. Returns the empty set if no corrections are found
*/
std::set<std::string> getCorrections(const std::string& word) const
{
std::set<std::string> results;
std::string temp = word;
std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower); //Convert to lowercase to eliminate case errors
if(search(temp) == true)
{
results.insert(temp);
return results;
}
fuzzySearch(temp, root, results);
return results;
}
private:
/**
* Function that determines if a character is a vowel
* @param c - the character that we are examining
* @return returns a boolean flag that represents whether or not the character is a vowel
*/
bool isVowel(const char& c) const
{
return ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u'));
}
/**
* Function that determines possible corrections for a word based off of the known error algorithm (duplicate letters, substituted vowels)
* @param word - the word that we are determining corrections for
* @param currentNode - a pointer to the node we are currently at in the Trie
* @param results - the set of results that we store corrections in
*/
void fuzzySearch(const std::string& word, Node *currentNode, std::set<std::string>& results) const
{
unsigned short wordSize = static_cast<unsigned short>(word.size());
if(wordSize == 0)
{
if(currentNode->isEndpoint == true) //Only add a word if it is flagged as a complete word
{
results.insert(currentNode->word);
}
return;
}
char c = word[0];
if(currentNode->getChild(c) != nullptr)
{
if(!isVowel(c)) //Don't need branching behavior for consonants
{
fuzzySearch(word.substr(1), currentNode->getChild(c), results); //Recursively continue searching
if(wordSize < 2)
{
return;
}
c = word[1];
if((c == word[0]) && (currentNode->getChild(c) != nullptr))
{
fuzzySearch(word.substr(2), currentNode->getChild(c), results); //Skip a duplicate letter
}
}
else
{
fuzzySearch(word.substr(1), currentNode->getChild(c), results); //Recursively continue searching
if(currentNode->getChild('a') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('a'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('e') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('e'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('i') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('i'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('o') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('o'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('u') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('u'), results); //Recursively continue searching with a vowel permutation
}
if(wordSize < 2)
{
return;
}
c = word[1];
if((c == word[0]) && (currentNode->getChild(c) != nullptr))
{
fuzzySearch(word.substr(2), currentNode->getChild(c), results); //Skip a duplicate letter
}
}
}
else
{
if(isVowel(c) == true) //Only branch for vowels
{
if(currentNode->getChild('a') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('a'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('e') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('e'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('i') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('i'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('o') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('o'), results); //Recursively continue searching with a vowel permutation
}
if(currentNode->getChild('u') != nullptr)
{
fuzzySearch(word.substr(1), currentNode->getChild('u'), results); //Recursively continue searching with a vowel permutation
}
if(wordSize < 2)
{
return;
}
c = word[1];
if((c == word[0]) && (currentNode->getChild(c) != nullptr))
{
fuzzySearch(word.substr(2), currentNode->getChild(c), results); //Skip a duplicate letter
}
}
}
}
Node *root; ///Top Node of the Trie
unsigned int nodeCount; ///Number of nodes (not counting the root as it has no data)
};
#endif