-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
769 lines (638 loc) · 15.5 KB
/
Source.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
class Doc_Info
{
public:
// Class Data Members
int DocID;
int term_frequency;
Doc_Info()// Default Constructor
{
DocID = 0;
term_frequency = 0;
}
Doc_Info(int d , int tf)// Parametrized Constructor
{
DocID = d;
term_frequency = tf;
}
void setDocID(int d)// Setter
{ DocID = d; }
void setterm_frequency(int tf)// Setter
{ term_frequency = tf; }
int getDocID()// Getter
{ return DocID; }
int getterm_frequency()// getter
{ return term_frequency; }
~Doc_Info()
{
term_frequency = 0;
DocID = 0;
}
};
template <class t>
class Node
{
public:
//Class Data Members
Node<t>* next;
t data;
Node()// Default Constructor
{ next = NULL; }
Node(t val1)// Parametrized Constructors
{
data = val1;
next=NULL;
}
~Node()// Destructor
{
next = NULL;
}
void setnext(Node<t>* s)// Setter
{ next = s; }
Node<t>* getnext()// getter
{ return next; }
t setdata(t x)// Setter
{ data = x; }
t getdata()// Getter
{ return data; }
};
template <class T>
class list
{
public:
// Class data Members
Node<T>* head;
Node<T>* tail;
int size;
list()// Default Constructor
{
head = NULL;
tail = NULL;
}
~list()// Destructor
{
head = NULL;
tail = NULL;
size = 0;
}
list(Node<T>* h)// Parametrized Constructor
{ head = h; }
Node<T>* gethead()// Getter
{ return head; }
Node<T>* gettail()// Getter
{ return tail; }
void sethead(Node<T>* h)// Setter
{ head = h; }
void settail(Node<T>* t)// Setter
{ tail = t; }
int getsize()// Getter
{ return size; }
void setsize(int s)// Setter
{ size = s; }
void InsertAtStart(Node<Doc_Info> x)// Insertionc
{
Node<T>* node1 = new Node<T>(x);
if (head == NULL)// If Empty
{
size = 1;
head = node1;
}
else if (head != NULL)// If Not Empty
{
node1->setnext(head);
head = node1;
size += 1;
}
}
void display()// Displayer
{
Node<T>* temp = head;
while(temp!=NULL)// Displaying Till Null
{
cout << " " ;
cout << temp->getdata();
temp=temp->getnext();
}
}
};
class Term_Info
{
public:
// Class data Members
string term;
list<Doc_Info> DI;
Term_Info()// Default Constructor
{
term = "";
DI = NULL;
}
Term_Info(string t)// Parametrized Constructor
{ term = t; }
void setterm(string t)// Setter
{ term = t; }
//void setDI(Doc_Info* di)
//{ DI = di; }
string getterm()// Getter
{ return term; }
//Doc_Info* getDI()
//{ return DI; }
~Term_Info()
{
term = "";
}
};
template <class T>
class AVLNode
{
public:
// Class data members
T val;
AVLNode* left;
AVLNode* right;
int height;
AVLNode()// Default Constructor
{
left = NULL;
right = NULL;
height=0;
}
~AVLNode()// Destructor
{
left = NULL;
right = NULL;
height = 0;
}
void setleft(AVLNode<T>* v)// Setter
{ left=v; }
void setright(AVLNode<T>* v)// Setter
{ right=v; }
AVLNode<T>* getleft()// Getter
{ return left; }
AVLNode<T>* getright()// Getter
{ return right; }
void setval(T s)// Setter
{ val=s; }
T getval()// Getter
{ return val; }
void seth(int s)// Setter
{ height=s; }
int geth()// Getter
{ return height; }
};
template <class T>
class AVLTree
{
// Class Data members
AVLNode<T>* root;
public:
AVLTree()// Default Constructor
{ root = NULL; }
~AVLTree()// Destructor
{ makeEmpty(root); }
AVLNode<T>* makeEmpty(AVLNode<T>* rt)// Emptier Function
{
if (rt == NULL)
return NULL;
{
makeEmpty(rt->getleft());
makeEmpty(rt->getright());
delete rt;
}
return NULL;
}
AVLNode<T>* newNode(int key,string s,int f)// New Node Creator
{
AVLNode<T>* node = new AVLNode<T>(key,s,f);
node->setleft(NULL);
node->setright(NULL);
node->seth(0);
return node;
}
int getBalance(AVLNode<T>* node)// balance Checker
{
if (node == NULL){
return 0;
}
return Height(node->getleft()) - Height(node->getright());
}
bool isBalance()//function to find if tree is balanced or not
{ return isBalanced(root); }
bool isBalanced(AVLNode<T>* rt)// Balance Checker's Implementation
{
int lH;
int rH;
if (rt == NULL)
return true;
lH = Height(rt->getleft());
rH = Height(rt->getright());
if ((lH - rH) <= 1 && isBalanced(rt->getleft()) && isBalanced(rt->getright()))
return true;
return false;
}
void insert(AVLNode<T>* no,string term)// insert function
{
root = Insert(root,no ,term);
}
AVLNode<T>* Insert(AVLNode<T>* node,AVLNode<T>* no,string s)// Insertion
{
if (node == NULL)
{ return no; }
if (s < node->val.term)
node->setleft( Insert(node->getleft(),no,s));
else if (s > node->val.term)
node->setright(Insert(node->getright(),no,s));
else
return node;
node->seth(1+ max(Height(node->getleft()), Height(node->getright()))) ;
int balance = getBalance(node);
//single rotations
if (balance > 1 && s < node->getleft()->val.term)
return rightRotate(node);
if (balance < -1 && s > node->getright()->val.term)
return leftRotate(node);
//double rotations
if (balance > 1 && s > node->getleft()->val.term)
{
node->setleft(leftRotate(node->getleft())) ;
return rightRotate(node);
}
else if (balance < -1 && s < node->getright()->val.term)
{
node->setright(rightRotate(node->getright())) ;
return leftRotate(node);
}
return node;
}
AVLNode<T>* rightRotate(AVLNode<T>* y)//function to rotate right
{
AVLNode<T>* x = y->getleft();
AVLNode<T>* T2 = x->getright();
// Performing rotation
x->setright(y);
y->setleft(T2);
y->seth(max(Height(y->getleft()),
Height(y->getright())) + 1) ;
x->seth(max(Height(x->getleft()),
Height(x->getright())) + 1);
return x;
}
AVLNode<T>* leftRotate(AVLNode<T>* x)//function to rotate left
{
AVLNode<T>* y = x->getright();
AVLNode<T>* T2 = y->getleft();
// Performing rotation
y->setleft(x);
x->setright(T2);
x->seth( max(Height(x->getleft()),
Height(x->getright())) + 1);
y->seth(max(Height(y->getleft()),
Height(y->getright())) + 1);
return y;
}
void displayInOrder()// Display Caller
{ inOrderDisplay(root); }
void inOrderDisplay(AVLNode<T>* rt)// Displays INorder
{
if (rt != NULL)
{
inOrderDisplay(rt->getleft());
cout << "term : "<<rt->val.term << endl;
while(rt->val.DI.head!=NULL)// While Empties
{
cout<<"Docid : "<<rt->val.DI.head->data.DocID<<endl;
cout<<"frequency : "<<rt->val.DI.head->data.term_frequency<<endl;
rt->val.DI.head=rt->val.DI.head->next;
}
cout<<endl<<endl;
inOrderDisplay(rt->getright());
}
}
T height()// height Checker Caller
{ return Height(root); }
int Height(AVLNode<T>* node)// Height Checker
{
if (node == NULL)
return -1;
else
return node->geth();
}
int Max(int a, int b)//max function
{ return (a > b) ? a : b; }
AVLNode<T>* minValueNode(AVLNode<T>* node) //function to find indorder successor
{
AVLNode<T>* currNode = node;
while (currNode && currNode->getleft() != NULL)//finding leftmost leaf
{ currNode = currNode->getleft(); }
return currNode;
}
};
class Search_Engine
{
// Class Data members
int c1;
AVLTree<Term_Info> *index;
AVLNode<Term_Info>* arr;
public:
Search_Engine()// Default Constructor
{
index = new AVLTree<Term_Info>[200];
}
~Search_Engine()// Destructor
{
index = NULL;
arr = NULL;
}
AVLNode<Term_Info>* Create_Index(char Docs[300][300] , int n)
{
// Declaring Variables
AVLNode<Term_Info> *z;
z = new AVLNode<Term_Info>[100];
arr = new AVLNode<Term_Info>[100];
int count=0;
for(int i = 0; i < n; i++)
{ Add_Doc_to_Index( Docs[i] , i+1,count); }
for (int i=0;i<count;i++)
{ z[i]=arr[i]; }
c1 = count;
for (int i=0;i<count;i++)
{
AVLNode<Term_Info> *p=&arr[i];
index->insert(p,arr[i].val.term);
}
index->displayInOrder();
return z;
}
void Add_Doc_to_Index(char fileName[256] , int id,int &count)
{
// Declaring Variables
int size = 0;
int size_U = 0;
int maxf=0;
string text = filehandling(fileName);
string *P = tokenizer(text , size);
string *U = new string[50];
int *F = frequency(P , U, size, size_U,maxf);
// Adding Documents To Indexes
for(int i = 0; i <size_U; i++)
{
Node<Doc_Info> new_node_temp;// Creating A Node Of LL
new_node_temp.data.term_frequency = F[i];
new_node_temp.data.DocID = id;
AVLNode<Term_Info> new_node;// Creating A Node Of AVLTree
new_node.val.term = U[i] ;
new_node.val.DI.InsertAtStart(new_node_temp);
int found=0;
if (count==0)
{
arr[count]=new_node;
count++;
}
else
{
for (int j=0;j<count;j++)
{
if (arr[j].val.term == new_node.val.term)
{
arr[j].val.DI.InsertAtStart(new_node_temp);
found=1;
break;
}
}
if (found==0)
{
arr[count] = new_node;
count++;
}
found = 0;
}
}
}
string filehandling(char fileName[256])
{
// Declaring Variables
string text;
string main = "";
ifstream myfile;
myfile.open (fileName);
// Collecting File Data
getline(myfile , main);
while (getline(myfile , text))
{
main = main + text;
}
myfile.close();
// Returning Data
return main;
}
string* tokenizer(string text , int &size)
{
// Declaring Variables
string *P = new string[100];
string temp = "";
int c=0,row = 0;
// Tokenizing On The Basis Of Spaces & Dots
for (int i = 0 ; text[i] != '\0'; i++)
{
if(text[i] == ' ' | text[i] == '.')
{
P[row] = temp;
row += 1;
c++;
temp = "";
}
else
{
temp = temp + text[i];
}
}
P[row] = temp;
size = ++row;
// Returning Tokenized Words
return P;
}
int* frequency(string *P , string *U , int size, int &size_U,int &maxf)
{
// Decalring Variables
int indexer = 0;
// Extarcting Unique Words
for (int i=0; i<=size+1; i++)
{
int j;
for (j=0; j<i; j++)
if (P[i] == P[j])
break;
if (i == j)
{
U[indexer]=P[i];
indexer++;
}
}
indexer--;
int *F = new int[indexer];
int count = 0;
// Counting Frequencies
for (int i = 0; i < indexer; i++ )
{
for(int j = 0; j < size; j++)
{
if(U[i] == P[j])
{
count++;
}
}
F[i] = count;
count = 0;
}
size_U = indexer;
// Returning Frequencies
return F;
}
void display_rules(int *frequencies , int *ids , int count_freqs , int count_ids)
{
// Declaring Variables
int indexer = 0;
int *Unique = new int[count_ids];
int * Unique_Freq = new int[count_ids];
int * Unique_Collective_Freq = new int[count_ids];
// Extracting Unique Documents
for (int i = 0; i <= count_ids; i++)
{
int j;
for (j = 0; j < i; j++)
{
if (ids[i] == ids[j])
break;
}
if (i == j)
{
Unique[indexer] = ids[i];
Unique_Freq[indexer] = 0;
Unique_Collective_Freq[indexer] = 0;
indexer++;
}
}
indexer--;
// Calculating Doc Frequencies and Collective Term Frequencies
for(int i = 0; i < indexer; i++)
{
for(int j = 0; j <= count_ids; j++)
{
if(Unique[i] == ids[j])
{
Unique_Freq[i] += 1;
Unique_Collective_Freq[i] += frequencies[j];
}
}
}
// Arranging According to 3 Display Rules
for(int x = 0; x < indexer; x++)
{
for(int y = x + 1; y < indexer; y++)
{
if(Unique_Freq[x] < Unique_Freq[y])// Rule 1
{
swap(Unique_Freq[x] , Unique_Freq[y]);
swap(Unique[x] , Unique[y]);
swap(Unique_Collective_Freq[x] , Unique_Collective_Freq[y]);
}
else if(Unique_Freq[x] == Unique_Freq[y])
{
if(Unique_Collective_Freq[y] > Unique_Collective_Freq[x])// Rule 2
{
swap(Unique_Freq[x] , Unique_Freq[y]);
swap(Unique[x] , Unique[y]);
swap(Unique_Collective_Freq[x] , Unique_Collective_Freq[y]);
}
else if(Unique_Collective_Freq[x] == Unique_Collective_Freq[y])// Rule 3
{
if(Unique[x] > Unique[y])
{
swap(Unique_Freq[x] , Unique_Freq[y]);
swap(Unique[x] , Unique[y]);
swap(Unique_Collective_Freq[x] , Unique_Collective_Freq[y]);
}
}
}
}
}
// Displaying Search Results
cout << "\t\t\t\t\t\t\t ================== \n";
cout << "\t\t\t\t\t\t\t | Search Results |\n";
cout << "\t\t\t\t\t\t\t ================== \n\n";
for(int i = 0; i < indexer; i++)
{
cout << "\t\t\t\t\t---------------------------------------------------------------\n";
cout << "\t\t\t\t\t| DOC-ID : " << Unique[i];
cout << " | DoC-Frequency : " << Unique_Freq[i];
cout << " | Collective Frequency : " << Unique_Collective_Freq[i] << " | " << endl;
}
cout << "\t\t\t\t\t---------------------------------------------------------------\n";
}
void searchquery(string *s , int row , AVLNode<Term_Info>*p)
{
// Decalring Variables
int *frequencies = new int[30];
int *ids = new int[30];
int count_freqs = 0;
int count_ids = 0;
cout << "\t\t\t\t\t\t\t ------------------ \n";
cout << "\t\t\t\t\t\t\t | Pre-Processing |\n";
cout << "\t\t\t\t\t\t\t ------------------ \n\n";
// Extracting DoC-IDs & Frequencies
for (int i = 0 ; i < row; i++)
{
for (int j = 0 ; j < c1 ; j++)
{
if (s[i] == p[j].val.term)
{
while (p[j].val.DI.head)
{
frequencies[count_freqs] = p[j].val.DI.head->data.term_frequency;
ids[count_ids] = p[j].val.DI.head->data.DocID;
count_freqs += 1;
count_ids += 1;
cout << "\t\t\t\t\t\t\t------------ ------------- \n";
cout << "\t\t\t\t\t\t\t|Frequency : "<< p[j].val.DI.head->data.term_frequency << " ";
cout << "Doc-ID :" << p[j].val.DI.head->data.DocID << " |\n";
p[j].val.DI.head = p[j].val.DI.head->next;
}
cout<<endl;
}
}
}
// Function That Displays According To Rules
cout << endl << endl << endl;
display_rules( frequencies , ids , count_freqs , count_ids);
}
};
int main()
{
// Filenames
char filenames[10][300] = {"Doc1.txt" , "Doc2.txt" , "Doc3.txt" , "Doc4.txt"};
// Displaying Search Engine
cout << "\t\t\t\t\t\t\t ================= \n";
cout << "\t\t\t\t\t\t\t | Search Engine |\n";
cout << "\t\t\t\t\t\t\t ================= \n\n";
// AVL Tree Creation
Search_Engine s;
AVLNode<Term_Info>* x = s.Create_Index(filenames , 4);
//Inserting Query Info
cout << "\t\t\t\t\t\t\t ================= \n";
cout << "\t\t\t\t\t\t\t | Query Section |\n";
cout << "\t\t\t\t\t\t\t ================= \n\n";
int size;
cout << endl << "Enter the size of query : " ;
cin>>size;
// Inserting Query
string *input=new string[size];
cout << endl << "Enter the query : " ;
for (int i=0;i<size;i++)
{ cin>>input[i]; }
// Search Engine
s.searchquery(input, size ,x);
return 0;
}