-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.cpp
More file actions
234 lines (207 loc) · 7.38 KB
/
Dictionary.cpp
File metadata and controls
234 lines (207 loc) · 7.38 KB
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
#include "DictionaryTree.h"
const vector<char> Dictionary::ALPHABET({ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\'' });
Dictionary::~Dictionary() {
for (int i = 0; i < root.size(); i++) {
delete root[i];
}
}
void Dictionary::build(string file) {
ifstream myfile;
string word;
unsigned int size;
bool flag;
myfile.open(file, ios::in);
if (myfile.is_open()) {
while (getline(myfile, word)) {
//convert to lowercase
transform(word.begin(), word.end(), word.begin(), ::tolower);
//make sure word can be represented with Alphabet
flag = false;
for (char c : word) {
//test for good letter
if (inAlphabet(c) == -1) {
//letter does not exist
flag = true;
cout << "Error the charater " << c << " does not exist the the alphabet " << endl;
break;
}
}
if (!flag) {
size = word.size();
if (size >= 0 && size < root.size()) {
root[size]->build(word);
} else {
while (root.size() <= size) {
root.push_back(new LetterNode);
}
if (size >= 0) {
root[size]->build(word);
} else {
cout << "Did not add " << word << endl;
}
}
} else {
cout << "Did not add " << word << endl;
}
}
myfile.close();
} else {
cout << "File did not open: " << file << endl;
}
}
bool const Dictionary::ifExists(string word) {
transform(word.begin(), word.end(), word.begin(), ::tolower);
unsigned int size = word.size();
if (size >= 0 && size < root.size()) {
return root[size]->ifExists(word);
} else {
return false;
}
}
int Dictionary::inAlphabet(char c) {
c = tolower(c);
vector<char>::const_iterator f = find(Dictionary::ALPHABET.begin(), Dictionary::ALPHABET.end(), c);
if (f != Dictionary::ALPHABET.end()) {
//letter found in Alphabet
return distance(Dictionary::ALPHABET.begin(), f);
}
return -1;
}
vector<list<int>> Dictionary::possibleLetters(vector<CryptogramLetter*> word, vector<list<int>> *possibleLetters) const {
bool deadEnd = true;
//remove punctuation
for (unsigned int i = 0; i < word.size(); i++) {
if (word[i]->status == PUNCTUATION) {
if (word.size() - 1 == i) {
word.pop_back();
} else {
cout << "ERROR: Can't handle punctuation " << word[i]->punctuation << endl;
}
}
}
vector<list<int>> l(word.size());
if (word.size() >= 0 && word.size() < root.size()) {
deadEnd = root[word.size()]->possibleLetters(word, possibleLetters, 0, &l);
}
return l;
}
//***************************************************************************************************************************************************************************************************************************
//****************************************************************************************************LetterNode*************************************************************************************************************
//***************************************************************************************************************************************************************************************************************************
LetterNode::LetterNode() {
endOfWord = false;
for (unsigned int i = 0; i < Dictionary::ALPHABET.size(); i++) {
letters.push_back(NULL);
}
}
LetterNode::~LetterNode() {
for (unsigned int i = 0; i < Dictionary::ALPHABET.size(); i++) {
delete letters[i];
}
}
void LetterNode::build(string word) {
if (word.length() == 0) {
endOfWord = true;
return;
}
//test for good letter
int ndx = Dictionary::inAlphabet(word[0]);
if (ndx != -1) {
//letter found in Alphabet
if (letters[ndx] == NULL) {
letters[ndx] = new LetterNode();
}
letters[ndx]->build(word.substr(1));
} else {
cout << "Error the charater " << word[0] << " does not exist the the alphabet " << endl;
return;
}
}
void LetterNode::printDictionary(ostream &os, string l) const {
if (endOfWord) {
cout << l << endl;
}
for (unsigned int i = 0; i < letters.size(); i++) {
if (letters[i] != NULL) {
letters[i]->printDictionary(os, l + Dictionary::ALPHABET[i]);
}
}
}
bool const LetterNode::ifExists(string word) {
if (word.length() == 0) {
if (endOfWord) {
return true;
} else {
return false;
}
}
char l = word[0];
//test for good letter
int ndx = Dictionary::inAlphabet(word[0]);
if (ndx != -1) {
//letter found in Alphabet
if (letters[ndx] == NULL) {
letters[ndx] = new LetterNode();
}
return letters[ndx]->ifExists(word.substr(1));
} else {
return false;
}
}
bool LetterNode::possibleLetters(vector<CryptogramLetter *> word, vector<list<int>> *possibleLetters, unsigned int iteration, vector<list<int>> *l) const {
//exit clause
if (iteration == word.size()) {
return false;
}
bool flag;
bool deadEnd = true;
bool r;
if (word[iteration]->status == UNKNOWN) {
//cycle through letters
for (list<int>::const_iterator i = (*possibleLetters)[word[iteration]->encryptedLetter].begin(); i != (*possibleLetters)[word[iteration]->encryptedLetter].end(); i++) {
if (letters[*i] != NULL) {
if (iteration < word.size()) {
r = letters[*i]->possibleLetters(word, possibleLetters, iteration + 1, l);
if (deadEnd) {
deadEnd = r;
}
if (!r) {
//add letter
flag = false;
for (int j : (*l)[iteration]) {
if (j == *i) {
flag = true;
break;
}
}
if (!flag) {
(*l)[iteration].push_back(*i);
}
}
}
}
}
} else if (word[iteration]->status == KNOWN || word[iteration]->status == GUESS) {
if (letters[word[iteration]->uncryptedLetter] == NULL) {
return true;
}
r = letters[word[iteration]->uncryptedLetter]->possibleLetters(word, possibleLetters, iteration + 1, l);
if (deadEnd) {
deadEnd = r;
}
if (!r) {
//add letter
flag = false;
for (int j : (*l)[iteration]) {
if (j == word[iteration]->uncryptedLetter) {
flag = true;
break;
}
}
if (!flag) {
(*l)[iteration].push_back(word[iteration]->uncryptedLetter);
}
}
}
return deadEnd;
}