-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
218 lines (188 loc) · 6.88 KB
/
Trie.cpp
File metadata and controls
218 lines (188 loc) · 6.88 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
#include "Headers.h"
#include "Trie.h"
using namespace std;
//Initialization of head node in Constructor
Trie::Trie() {
rootNode = newNode();
};
//Destructor to deallocate memory
//Trie::~Trie() {
// delete rootNode;
//};
// creating a new node
struct TrieNode* Trie::newNode() {
struct TrieNode* node = new TrieNode;
node->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
node->children[i] = NULL;
return node;
}
void Trie::insertNode(string dictionaryWord) {
struct TrieNode* iteratorNode = rootNode;
for (int i = 0; i < dictionaryWord.size(); i++) {
int alphabetIndex = dictionaryWord[i];
if (iteratorNode->children[alphabetIndex] == NULL)
iteratorNode->children[alphabetIndex] = newNode();
iteratorNode = iteratorNode->children[alphabetIndex];
}
// marking last character node as the end of word
iteratorNode->isEndOfWord = true;
}
bool Trie::searchWord(string wordToSearch) {
struct TrieNode* iteratorNode = rootNode;
for (int i = 0; i < wordToSearch.size(); i++) {
int alphabetIndex = wordToSearch[i];
if (iteratorNode->children[alphabetIndex] == NULL) {
return false;
}
iteratorNode = iteratorNode->children[alphabetIndex];
}
return (iteratorNode != NULL && iteratorNode->isEndOfWord);
}
map<int, int> Trie::findWordDetails(string wordToPrint) {
struct TrieNode* iteratorNode = rootNode;
// cout<<"word to search " << wordToPrint << endl;
map<int,int> res;
res.clear();
for (int i = 0; i < wordToPrint.size(); i++) {
int alphabetIndex = wordToPrint[i];
iteratorNode = iteratorNode->children[alphabetIndex];
}
if (iteratorNode != NULL && iteratorNode->isEndOfWord) {
res = iteratorNode->fileWithCount;
// map<int, int>::iterator it = fileWithCount.begin();
// cout << "Details for the word " << wordToPrint << "are : " << endl;
//
// while (it != fileWithCount.end()) {
// cout << "Printing positions in file no. " << it->first << " with count = " << it->second << endl;
// //vector<int> listOfPositions = it->second;
// /*for (int i = 0; i < listOfPositions.size(); i++) {
// cout << listOfPositions[i] << " ";
// }*/
// //cout << endl;
// it++;
// }
}
return res;
}
void Trie::insertPosition(string fileWord, int fileIndex, int position) {
struct TrieNode* iteratorNode = rootNode;
for (int i = 0; i < fileWord.size(); i++) {
int alphabetIndex = fileWord[i];
if (iteratorNode->children[alphabetIndex] == NULL) {
return;
}
iteratorNode = iteratorNode->children[alphabetIndex];
}
if (iteratorNode->isEndOfWord) {
/*if (iteratorNode->fileWithPosition.find(fileIndex) == iteratorNode->fileWithPosition.end()) {
iteratorNode->fileWithPosition[fileIndex] = vector<int>();
}
iteratorNode->fileWithPosition[fileIndex].push_back(position);*/
}
}
void Trie::incrementCount(string fileWord, int fileIndex) {
//cout<<"inserting word: "<<fileWord <<" from file:"<< fileIndex << endl;
struct TrieNode* iteratorNode = rootNode;
for (int i = 0; i < fileWord.size(); i++) {
int alphabetIndex = fileWord[i];
if (iteratorNode->children[alphabetIndex] == NULL) {
return;
}
iteratorNode = iteratorNode->children[alphabetIndex];
}
if (iteratorNode->isEndOfWord) {
// cout<<fileWord<<" is inserted succesfully from file:"<< fileIndex << endl;
if (iteratorNode->fileWithCount.find(fileIndex) == iteratorNode->fileWithCount.end()) {
iteratorNode->fileWithCount[fileIndex] = 0;
}
iteratorNode->fileWithCount[fileIndex]++;
}
}
void Trie::insertDictionaryWords(string pathOfDictionary, string type) {
ifstream wordsFile;
wordsFile.open(pathOfDictionary);
string word, wordReversed;
// reading the words in the file and inserting
if (wordsFile.is_open()) {
while (!wordsFile.eof()) {
wordsFile >> word;
transform(word.begin(), word.end(), word.begin(), ::tolower);
if (type == "normal") {
insertNode(word);
}
else {
reverse(word.begin(), word.end());
insertNode(word);
}
}
/*if(type == "normal")
cout << "\nNormal Trie has been created successfully" << endl;
else
cout << "\nReverse Trie has been created successfully" << endl;*/
}
else {
cout << "File not found" << endl;
}
wordsFile.close();
}
void Trie::insertCountInFiles(map<int,string> files_index, string type) {
// Now, traversing the files to check the positions of the dictionary words
ifstream wordsFile;
string word;
// cout << "Traversing the files index" << endl;
map<int, string>::iterator it = files_index.begin();
while (it != files_index.end()) {
int fileIndex = it->first;
string filePath = it->second;
wordsFile.open(filePath);
if (wordsFile.is_open()) {
while (!wordsFile.eof()) {
wordsFile >> word;
for (int i = 0; i < word.size(); i++) {
if (ispunct(word[i]) && word[i] != '\'' && word[i] != '&' && word[i] != '/')
{
word.erase(i--, 1);
}
}
transform(word.begin(), word.end(), word.begin(), ::tolower);
if (type == "normal") {
incrementCount(word, fileIndex);
}
else {
reverse(word.begin(), word.end());
incrementCount(word, fileIndex);
}
}
// cout << "All word positions for file" << fileIndex << " has been inserted" << endl;
}
wordsFile.close();
it++;
}
}
void Trie::findValidWordsHelper(TrieNode* node, string currWord, vector<string>& validWords) {
if (node->isEndOfWord) {
validWords.push_back(currWord);
}
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (node->children[i] != NULL) {
findValidWordsHelper(node->children[i], currWord + (char)i, validWords);
}
}
}
vector<string> Trie::findValidWords(string query) {
vector<string> validWords;
bool isPresent = true;
struct TrieNode* iteratorNode = rootNode;
for (int i = 0; i < query.size(); i++) {
int alphabetIndex = query[i];
if (iteratorNode->children[alphabetIndex] == NULL) {
isPresent = false;
break;
}
iteratorNode = iteratorNode->children[alphabetIndex];
}
if (isPresent)
findValidWordsHelper(iteratorNode, query, validWords);
return validWords;
}