-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieFinal.cpp
More file actions
316 lines (261 loc) · 7.37 KB
/
Copy pathTrieFinal.cpp
File metadata and controls
316 lines (261 loc) · 7.37 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
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
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<algorithm>
#include<vector>
#include <ctime> //for clocking the code
//===========================================================================
#include <cstring> //because dirent stores dir names in char arrays...i.e. c type strings
#include <dirent.h> //format of directory entries
#include <sstream> //string stream
DIR *dpdf;
struct dirent *epdf;
//==================================================================================================
#define NUM_ALPHABET 26
#define ChartoIndex(c) ((int)c-(int)'a')
const int NUM_FILES=100;
//---------trie node--------============================================================================
class trienode
{
public:
//int is_end;
int freq[NUM_FILES];
trienode *child[NUM_ALPHABET];
//constructor
trienode()
{
//is_end=0;
for(int i=0;i<NUM_FILES;i++)
freq[i]=0;
for(int i=0;i<NUM_ALPHABET;i++)
child[i]=NULL;
}
};
//create a new node
trienode* getnode()
{
trienode* pnode=new trienode;
//pnode->is_end=1;
for(int i=0;i<NUM_FILES;i++)
pnode->freq[i]=0;
for(int i=0;i<NUM_ALPHABET;i++)
pnode->child[i]=NULL;
return pnode;
}
// trie ADT
class trie
{
public:
trienode *root;
//int count;
trie()
{
root=getnode();
//count=0;
}
//inserting into trie(if word not present, insert. if present,increase freq and mark as leaf)
void insert(std::string key,int file_index)
{
int depth;
int length=key.length();
int index;
trienode* pCrawl;//crawler
//ptrie->count++;
pCrawl=root;
for(depth=0;depth<length;depth++)
{
index=ChartoIndex(key[depth]);
if(pCrawl->child[index]==NULL)
pCrawl->child[index]=getnode();
pCrawl->child[index]->freq[file_index]++;//increase the frequency for that letter
pCrawl=pCrawl->child[index];
}
//mark lastnode as leaf
//pCrawl->is_end=ptrie->count;
}
//search the trie for the key. return zero if not found. if foing, return the frequency.
int* search(std::string key)
{
int depth;
int length=key.length();
int index;
trienode* pCrawl;//crawler
pCrawl=root;
for(depth=0;depth<length;depth++)
{
index=ChartoIndex(key[depth]);
if(pCrawl->child[index]==NULL)
return 0;
pCrawl=pCrawl->child[index];
}
return((pCrawl->freq));
}
void maketrie(std::string *filename,int file_index)
{
// A buffer to store one word at a time
std::string buffer;
std::ifstream fileptr;
fileptr.open(filename->c_str());
while(!fileptr.eof())//loop till end of file
{
fileptr>>buffer;//read one word to buffer
//remove punctuations...
buffer.erase (std::remove_if (buffer.begin (), buffer.end (), ispunct), buffer.end ());
//make lover case
transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower);
//insert in trie
insert(buffer,file_index);
}
}
};
//=====================================================================
//=====================================================================
class BTreeNode
{
public:
int filenum;
long int freq;
BTreeNode* left;
BTreeNode* right;
BTreeNode()
{
filenum=0;
freq=0;
left=NULL;
right=NULL;
}
};
class BTree
{
public:
BTreeNode* root;
BTree(){root=NULL;}
void insertnode(int Fnum,long int Ffreq)
{
//make the node
BTreeNode* pnode=new BTreeNode;
pnode->filenum=Fnum;
pnode->freq=Ffreq;
pnode->left=NULL;
pnode->right=NULL;
//insert node
BTreeNode* x,*y;
x=root;
y=NULL;
while(x!=NULL)
{
y=x;
if(x->freq > pnode->freq)
x=x->left;
else
x=x->right;
}
if(y==NULL)
root=pnode;
else
{
if(y->freq>pnode->freq)
y->left=pnode;
else y->right=pnode;
}
}
void BTreeSort(BTreeNode *root,std::vector<std::string> filenames)
{
BTreeNode* x=root;
if(x!=NULL)
{
BTreeSort(x->right,filenames);
if(x->freq)//don't print if frequency zero
std::cout<<"File name: "<<filenames[x->filenum]<<"\tfrequency: "<<x->freq<<"\n";
BTreeSort(x->left,filenames);
//stop if frequency 0
//if(x->freq==0) return 0;
}
}
};
//driver
int main()
{
//==============get the file names of files in the directory=============
/* cout<<"Enter the path of the directory in which you wish to search"<<endl;
string dir_path;
cin>>dir_path;
cout<<"The target directory is...\n"<<dir_path.c_str()<<endl;*/
clock_t begin = clock();
//reading all the files in the directory
std::cout<<"Getting all files in the desired directory.....\n";
dpdf = opendir("./files/"); //opening the current directory
std::vector<std::string> filenames;
//printing the file names
if (dpdf != NULL)
{
while (epdf = readdir(dpdf))
{
filenames.push_back(epdf->d_name);
}
}
//======================================
//std::vector<std::string> filenames;
//filenames.push_back("a.txt");
//filenames.push_back("b.txt");
//filenames.push_back("c.txt");
int num_files=filenames.size();
//NUM_FILES=num_files;gives error
//========making trie for files==========
std::ifstream fileptr;
std::string filename;
trie trie1;
std::size_t found;
for(int i=0;i<num_files;i++)
{
found = filenames[i].find(".txt");
if (found!=std::string::npos)
{
filename="./files/"+filenames[i];
std::cout<<"inserting in trie the file: "<<filename.c_str()<<" .............\n";
fileptr.open(filename.c_str());
if (!fileptr.is_open())
{
printf ("File does not exist \n");
continue;
}
else
trie1.maketrie(&filename,i);}}
clock_t end = clock();
double elapsed_secs1 = double(end - begin) / CLOCKS_PER_SEC;
std::cout<<"total time elapsed in making the trie is "<<elapsed_secs1<<"\n";
//======================Searching==============================
char flag='y';//to control flow for multiple search
while(flag!='n')
{std::string search_string;
std::cout<<"enter the string you want to search for\n";
std::cin>>search_string;
transform(search_string.begin(), search_string.end(), search_string.begin(), ::tolower);
clock_t begin1=clock();
// Search for string
BTree *BST=new BTree;
int* FREQ=NULL;
FREQ=trie1.search(search_string);
if(FREQ==NULL)//word not found
std::cout<<"word not found\ntry another word!\n";
else
{
for(int i=0;i<num_files;i++)
BST->insertnode(i,FREQ[i]);
//Print result
std::cout<<"search result....\n";
BST->BTreeSort(BST->root,filenames);
std::cout<<"\nNo more results to display.";
}
clock_t end1 = clock();
double elapsed_secs = double(end1 - begin1) / CLOCKS_PER_SEC;
std::cout<<"\n time elapsed in searching for this string is "<<elapsed_secs<<" \n";
//prompt for another search
std::cout<<"\n\ntry another search?(y/n)\n";
std::cin>>flag;}
std::cout<<"\n\nSearch has concluded. End of program.\n";
//deallocating memory
return 0;
}