-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrieTree.h
205 lines (170 loc) · 3.6 KB
/
TrieTree.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
#include <stddef.h>
#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>
#include "Time.h"
struct TrieNode
{
TrieNode *_children[27];
double _freq;
bool isword;
};
class TrieTree
{
private:
TrieNode *_root;
public:
TrieTree(){
_root = new TrieNode();
_root -> isword = true;
for (int i = 0; i < 27; ++i)
{
_root -> _children[i] = NULL;
}
}
~TrieTree(){
deltree(_root);
}
void addWord(const char *,double);
double findWord(const char *);
void deltree(TrieNode *_root);
};
void TrieTree::addWord(const char *input,double freq){
char *pt;
TrieNode *tra = _root;
for (pt = (char *)input; *pt != '\0'; ++pt){
if(*pt != '\''){
if(tra -> _children[*pt - 'a'] == NULL)
tra -> _children[*pt - 'a'] = new TrieNode();
tra = tra -> _children[*pt - 'a'];
}else{
if(tra -> _children[26] == NULL)
tra -> _children[26] = new TrieNode();
tra = tra -> _children[26];
}
}
tra -> isword = true;
tra -> _freq = freq;
}
double TrieTree::findWord(const char *input){
char *pt = (char *)input;
TrieNode *tra = _root;
while(*pt != '\0' ){
if(*pt == '\''){
tra = tra -> _children[26];
}else{
tra = tra -> _children[*pt - 'a'];
}
if(!tra) return 0;
++pt;
}
if(tra -> isword)
return tra -> _freq;
else
return 0;
}
void TrieTree::deltree(TrieNode *root){
int i;
for(i = 0;i < 27; ++i){
if(root->_children[i] != NULL){
deltree(root->_children[i]);
}
}
delete root;
}
struct WordNode
{
WordNode *_children[26];
std::vector<std::string> words;
bool isword;
double unigram;
};
class WordTree
{
private:
WordNode *_root;
public:
WordTree(){
_root = new WordNode();
_root -> isword = true;
for (int i = 0; i < 26; ++i)
{
_root -> _children[i] = NULL;
}
}
~WordTree(){
deltree(_root);
}
void addWord(std::string ,const char *,double freq);
std::vector<std::string> findWord(const char *);
std::vector<std::string> dfsWord(const char *);
void deltree(WordNode *_root);
double finduni(const char *input){
std::vector<std::string> v;
char *pt = (char *)input;
WordNode *tra = _root;
while(*pt != '\0' ){
tra = tra -> _children[*pt - 'a'];
if(!tra) return 0;
++pt;
}
return tra -> unigram;
}
};
void WordTree::addWord(std::string word,const char *code,double freq){
char *pt;
WordNode *tra = _root;
for (pt = (char *)code; *pt != '\0'; ++pt){
if(*pt != '\''){
if(tra -> _children[*pt - 'a'] == NULL)
tra -> _children[*pt - 'a'] = new WordNode();
tra = tra -> _children[*pt - 'a'];
}
}
tra -> isword = true;
tra -> unigram = freq;
tra -> words.push_back(word);
}
std::vector<std::string> WordTree::findWord(const char *input){
std::vector<std::string> v;
char *pt = (char *)input;
WordNode *tra = _root;
while(*pt != '\0' ){
tra = tra -> _children[*pt - 'a'];
if(!tra) return v;
++pt;
}
return tra -> words;
}
void dfsword(std::string inp,WordNode *tra,std::vector<std::string> &v){
if(tra == NULL) return;
v.push_back(inp);
for (int i = 0; i < 26; ++i){
if(tra -> _children[i] != NULL){
dfsword(inp + (char)('a' + i), tra -> _children[i],v);
}
}
}
std::vector<std::string> WordTree::dfsWord(const char *input){
char *pt = (char *)input;
WordNode *tra = _root;
std::vector<std::string> v;
std::string inp(input);
while(*pt != '\0' ){
tra = tra -> _children[*pt - 'a'];
if(tra == NULL) return v;
++pt;
}
dfsword(inp,tra,v);
return v;
}
void WordTree::deltree(WordNode *root){
int i;
for(i = 0;i < 26; ++i){
if(root->_children[i] != NULL){
deltree(root->_children[i]);
}
}
delete root;
}