-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentSpellCheck.cpp
137 lines (118 loc) · 2.71 KB
/
StudentSpellCheck.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
135
136
137
#include "StudentSpellCheck.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
SpellCheck* createSpellCheck()
{
return new StudentSpellCheck;
}
StudentSpellCheck::~StudentSpellCheck() {
}
bool StudentSpellCheck::load(std::string dictionaryFile) {
ifstream dictionaryText(dictionaryFile);
if (!dictionaryText)
return false;
trieRoot = new Node;
string nextWord;
Node* n;
while (getline(dictionaryText, nextWord))
{
n = trieRoot;
for (int i = 0; i < nextWord.length(); i++)
{
bool nextLetterFound = false;
for (vector<Node*>::iterator it = n->nextLetter.begin(); it != n->nextLetter.end(); it++)
if ((*it)->letter == nextWord[i])
{
nextLetterFound = true;
n = *it;
break;
}
if (!nextLetterFound)
{
Node* newLetter = new Node;
newLetter->letter = nextWord[i];
n->nextLetter.push_back(newLetter);
n = newLetter;
}
}
Node* endOfWord = new Node;
endOfWord->letter = '.';
n->nextLetter.push_back(endOfWord);
}
return true;
}
bool StudentSpellCheck::spellCheck(std::string word, int max_suggestions, std::vector<std::string>& suggestions) {
if (inDictionary(word))
return true;
//at this point, the word is not in dictionary
suggestions.clear();
for (int i = 0; i < word.length(); i++)
{
string newWord;
for (char ch = 'a'; ch <= 'z'; ch++)
{
newWord = word;
newWord[i] = ch;
if (inDictionary(newWord))
{
suggestions.push_back(newWord);
}
}
if (suggestions.size() == max_suggestions)
break;
}
return false;
}
void StudentSpellCheck::spellCheckLine(const std::string& line, std::vector<SpellCheck::Position>& problems) {
int i = 0;
while (i < line.length())
{
while (i < line.length() && !partOfWord(line[i]))
i++;
Position wordPos;
wordPos.start = i;
while (i < line.length() && partOfWord(line[i]))
i++;
wordPos.end = i - 1;
if (!inDictionary(line.substr(wordPos.start, wordPos.end - wordPos.start + 1)))
problems.push_back(wordPos);
}
}
bool StudentSpellCheck::inDictionary(string word)
{
//set word to lowercase
for (int i = 0; i < word.length(); i++)
word[i] = tolower(word[i]);
Node* n = trieRoot;
bool inDictionary = true;
for (int i = 0; i < word.length(); i++)
{
bool nextLetterFound = false;
for (vector<Node*>::iterator it = n->nextLetter.begin(); it != n->nextLetter.end(); it++)
{
if (word[i] == (*it)->letter)
{
nextLetterFound = true;
n = *it;
break;
}
}
if (!nextLetterFound)
{
inDictionary = false;
break;
}
}
if (inDictionary)
{
for (vector<Node*>::iterator it = n->nextLetter.begin(); it != n->nextLetter.end(); it++)
{
if ((*it)->letter == '.')
return true;
}
}
return false;
}