-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordTree.cpp
More file actions
193 lines (170 loc) · 4.73 KB
/
WordTree.cpp
File metadata and controls
193 lines (170 loc) · 4.73 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
#include "WordTree.hpp"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <locale>
#include <queue>
#include <string>
#include <vector>
TreeNode::TreeNode()
{
}
WordTree::WordTree()
{
}
void WordTree::add(std::string word)
{
if (word.size() == 0)
{
return;
}
// If all of the characters in given word are not punctuation...
if (std::all_of(word.begin(), word.end(), [](unsigned char c)
{
return std::isalpha(c);
}))
{
std::transform(word.begin(), word.end(), word.begin(), [](char c)
{
return static_cast<char>(std::tolower(c));
});
}
else
{
return;
}
// Loop through each node, one character at a time.
std::shared_ptr<TreeNode> root = m_root;
for (unsigned int i = 0; i < word.size(); i++)
{
// Converstion to find placement in vector.
unsigned int value = int(word[i]) - 97;
if (root->children.at(value) == nullptr)
{
std::shared_ptr<TreeNode> node = std::make_shared<TreeNode>();
root->children.at(value) = node;
}
root = root->children.at(value);
if (i == word.size() - 1)
{
if (!root->endOfWord)
{
root->endOfWord = true;
m_sizeOfTree += 1;
}
}
}
}
bool WordTree::find(std::string word)
{
if (word.size() == 0)
{
return false;
}
// If all of the characters in given word are not punctuation...
if (std::all_of(word.begin(), word.end(), [](unsigned char c)
{
return std::isalpha(c);
}))
{
std::transform(word.begin(), word.end(), word.begin(), [](char c)
{
return static_cast<char>(std::tolower(c));
});
}
else
{
return false;
}
std::shared_ptr<TreeNode> root = m_root;
for (unsigned int i = 0; i < word.size(); i++)
{
// Converstion to find placement in vector.
unsigned int value = int(word[i]) - 97;
if (root->children.at(value) == nullptr)
{
return false;
}
root = root->children.at(value);
if (i == word.size() - 1)
{
if (root->endOfWord)
{
return true;
}
return false;
}
}
return false;
}
std::vector<std::string> WordTree::predict(std::string partial, std::uint8_t howMany)
{
if (partial.size() == 0)
{
return std::vector<std::string>();
}
// If all of the characters in given word are not punctuation...
if (std::all_of(partial.begin(), partial.end(), [](unsigned char c)
{
return std::isalpha(c);
}))
{
std::transform(partial.begin(), partial.end(), partial.begin(), [](char c)
{
return static_cast<char>(std::tolower(c));
});
}
else
{
return std::vector<std::string>();
}
std::vector<std::string> prediction;
std::shared_ptr<TreeNode> root = m_root;
for (unsigned int i = 0; i < partial.size(); i++)
{
// Converstion to find placement in vector.
unsigned int value = int(partial[i]) - 97;
if (root->children.at(value) == nullptr)
{
return std::vector<std::string>();
}
root = root->children.at(value);
}
// Queues for TreeNodes and word.
std::queue<std::shared_ptr<TreeNode>> queue;
std::queue<std::string> wordQueue;
std::string tempWord;
queue.push(root);
wordQueue.push(partial);
while (queue.size() != 0)
{
root = queue.front();
tempWord = wordQueue.front();
wordQueue.pop();
queue.pop();
for (unsigned int j = 0; j < root->children.size(); j++)
{
if (root->children.at(j) != nullptr)
{
// Current 'letter' that we are at.
char ascii = '0' + (static_cast<char>(j) + static_cast<char>(49));
if (root->children.at(j)->endOfWord)
{
prediction.push_back(tempWord + ascii);
howMany -= 1;
if (howMany == 0)
{
return prediction;
}
}
wordQueue.push(tempWord + ascii);
queue.push(root->children.at(j));
}
}
}
return prediction;
}
std::size_t WordTree::size()
{
return m_sizeOfTree;
}