-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoptree.cpp
374 lines (306 loc) · 8.03 KB
/
stoptree.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "stoptree.h"
#include "terminal.h"
#include <QTextStream>
#include <QTime>
#include <QDebug>
#define TRIE_NULL_CHAR ' '
#define MAX 50
StopTree::StopTree()
{
}
StopTreeObject::StopTreeObject()
{
stopTree = NULL;
}
StopTreeObject &StopTreeObject::getInstace()
{
static StopTreeObject instance;
return instance;
}
void StopTreeObject::setStopWordFilePath(QString filePath)
{
QFile file(filePath);
if(file.exists())
{
Terminal::getInstance().writeLine("File \"" + filePath + "\" is set for stop words file successfully.");
this->filePath = filePath;
}
else
{
Terminal::getInstance().writeLine("err : File \"" + filePath + "\" is not exists.");
}
}
void StopTreeObject::buildBST()
{
stopTree = new BSTStop;
}
void StopTreeObject::buildTrie()
{
stopTree = new TrieStop;
}
void StopTreeObject::buildTST()
{
stopTree = new TSTStop;
}
void StopTreeObject::buildTree()
{
QTime timer; // Timer for measuring the time elapsed for adding stop words to their tree
QFile file(filePath);
if(file.open(QIODevice::ReadOnly))
{
Terminal::getInstance().writeLine("Reading from file : \"" + filePath + "\" ...");
QTextStream in(&file);
timer.start(); // Start the timer
int wordCound = 0;
while(!in.atEnd())
{
QString line = in.readLine();
QStringList list = line.split(QRegExp("\\W+"), QString::SkipEmptyParts);
for(int i = 0; i < list.size(); i++)
{
stopTree->add(QString(list[i]).toLower());
wordCound ++;
}
}
int time = timer.elapsed();
Terminal::getInstance().writeLine(QString("%1 word(s) added to tree in %2 ms.").arg(wordCound).arg(time));
file.close();
Terminal::getInstance().writeLine("File : \"" + filePath + "\" closed.");
}
else
{
Terminal::getInstance().writeLine("Unable to open the file : \"" + filePath + "\".");
}
}
bool StopTreeObject::isExisted(QString word)
{
return this->stopTree->isExisted(word);
}
StopTreeObject::~StopTreeObject()
{
delete stopTree;
}
BSTStopNode::BSTStopNode(QString &word)
{
this->value = word;
leftChild = NULL;
rightChild = NULL;
}
BSTStop::BSTStop()
{
this->root = NULL;
}
void BSTStop::add(QString word)
{
BSTStopNode *newNode = new BSTStopNode(word);
if(this->root != NULL)
{
BSTStopNode *pointer = this->root;
BSTStopNode *parent = NULL;
while(pointer)
{
parent = pointer;
if(QString::compare(pointer->value, word, Qt::CaseInsensitive) == 0)
{
return;
}
else if(QString::compare(pointer->value, word, Qt::CaseInsensitive) > 0)
{
pointer = pointer->leftChild;
}
else if(QString::compare(pointer->value, word, Qt::CaseInsensitive) < 0)
{
pointer = pointer->rightChild;
}
}
if(QString::compare(parent->value, word, Qt::CaseInsensitive) > 0)
parent->leftChild = newNode;
else if(QString::compare(parent->value, word, Qt::CaseInsensitive) < 0)
parent->rightChild = newNode;
}
else
{
root = newNode;
}
}
bool BSTStop::isExisted(QString word)
{
BSTStopNode *pointer = this->root;
while(pointer)
{
if(QString::compare(pointer->value, word, Qt::CaseInsensitive) == 0)
{
return true;
}
else if(QString::compare(pointer->value, word, Qt::CaseInsensitive) > 0)
{
pointer = pointer->leftChild;
}
else if(QString::compare(pointer->value, word, Qt::CaseInsensitive) < 0)
{
pointer = pointer->rightChild;
}
}
return false;
}
void TSTStop::add(QString word)
{
insert(word.toLatin1().data());
}
bool TSTStop::isExisted(QString word)
{
return search(word.toLatin1().data());
}
TSTStop::TSTStop()
{
root = NULL;
}
int TSTStop::search(char *word)
{
return searchTST(root, word);
}
// A utility function to create a new ternary search tree node
TSTStopNode* TSTStop::newNode(char data)
{
TSTStopNode* temp = (TSTStopNode *) malloc(sizeof(TSTStopNode));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
}
void TSTStop::insert(char *word)
{
insertUtil(&root, word);
}
// Function to insert a new word in a Ternary Search Tree
void TSTStop::insertUtil(TSTStopNode** root, char *word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word);
// If current character of word is smaller than root's character,
// then insert this word in left subtree of root
if ((*word) < (*root)->data)
insertUtil(&( (*root)->left ), word);
// If current character of word is greate than root's character,
// then insert this word in right subtree of root
else if ((*word) > (*root)->data)
insertUtil(&( (*root)->right ), word);
// If current character of word is same as root's character,
else
{
if (*(word+1))
insertUtil(&( (*root)->eq ), word+1);
// the last character of the word
else
(*root)->isEndOfString = 1;
}
}
// A recursive function to traverse Ternary Search Tree
void TSTStop::traverseTSTUtil(TSTStopNode* root, char* buffer, int depth)
{
if (root)
{
// First traverse the left subtree
traverseTSTUtil(root->left, buffer, depth);
// Store the character of this node
buffer[depth] = root->data;
if (root->isEndOfString)
{
buffer[depth+1] = '\0';
printf( "%s\n", buffer);
}
// Traverse the subtree using equal pointer (middle subtree)
traverseTSTUtil(root->eq, buffer, depth + 1);
// Finally Traverse the right subtree
traverseTSTUtil(root->right, buffer, depth);
}
}
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void TSTStop::traverseTST()
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
// Function to search a given word in TST
int TSTStop::searchTST(TSTStopNode *root, char *word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else
{
if (*(word+1) == '\0')
return root->isEndOfString;
return searchTST(root->eq, word+1);
}
}
TrieStopNode* TrieStopNode::findChild(char c)
{
for ( int i = 0; i < mChildren.size(); i++ )
{
TrieStopNode* tmp = mChildren.at(i);
if ( tmp->content() == c )
{
return tmp;
}
}
return NULL;
}
TrieStop::TrieStop()
{
root = new TrieStopNode();
}
TrieStop::~TrieStop()
{
// Free memory
}
void TrieStop::add(QString s)
{
TrieStopNode* current = root;
if ( s.length() == 0 )
{
current->setWordMarker(); // an empty word
return;
}
for ( int i = 0; i < s.length(); i++ )
{
TrieStopNode* child = current->findChild(s.at(i).toLatin1());
if ( child != NULL )
{
current = child;
}
else
{
TrieStopNode* tmp = new TrieStopNode();
tmp->setContent(s.at(i).toLatin1());
current->appendChild(tmp);
current = tmp;
}
if ( i == s.length() - 1 )
current->setWordMarker();
}
}
bool TrieStop::isExisted(QString s)
{
TrieStopNode* current = root;
while ( current != NULL )
{
for ( int i = 0; i < s.length(); i++ )
{
TrieStopNode* tmp = current->findChild(s.at(i).toLatin1());
if ( tmp == NULL )
return false;
current = tmp;
}
if ( current->wordMarker() )
return true;
else
return false;
}
return false;
}