-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzy_search.c
622 lines (530 loc) · 13.7 KB
/
fuzzy_search.c
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 256
#include <time.h>
typedef struct use
{
char* st;
int index;
int n; //indicates length of suffux array to which the string belongs to
char* doc_title;
}use;
typedef struct rev
{
int doc_no;
int score;
}rev;
use** destroy_and_remake(use** arr_of_suffix_arrs);
int search_str(char* pattern, use* suf_arr, int n);
char* get_text_title(int i,char** heading);
char* search_lcs(char* a,use** arr_of_suf_arr, int g, int* index);
int no_of_occurances(char* pattern, use* suf_arr, int n);
use* create_suf_arr(char* t,char* title)
{
use* suf_arr=(use*)malloc(sizeof(use)*(strlen(t)));
int i;
char* text=t;
int n=strlen(t);
for(i=0;i<strlen(t);i++){
suf_arr[i].n=strlen(t);
suf_arr[i].st=(char*)malloc(sizeof(char)*(n-i+1));
strcpy(suf_arr[i].st,text);
suf_arr[i].doc_title=(char*)malloc(sizeof(char)*(strlen(title)+1));
strcpy(suf_arr[i].doc_title,title);
text++;
// printf("%d\t%d\n",strlen(text),(n-i));
suf_arr[i].index=i+1;
}
/*
for(i=0;i<n;i++){
printf("%d\t%s\n",suf_arr[i].index,suf_arr[i].st);
}
*/
return suf_arr;
//free(suf_arr);
}
int compare_string(char* a, char* b)
{
int m=strlen(a);
int n=strlen(b);
int x;
if(m>n){
x=n;
}
else{
x=m;
}
int i;
for(i=0;i<x;i++){
if(a[i]<b[i]){return -1;}
else if(a[i]>b[i]){return 1;}
else{}
}
if(m>n){
return 1;
}
else if(m<n){
return -1;
}
else{
return 0;
}
}
int compare_k_string(char* a, char* b)
{
int m=strlen(a);
int n=strlen(b);
int x;
if(m>n){
x=n;
}
else{
x=m;
}
int i;
for(i=0;i<x;i++){
if(a[i]<b[i]){return -1;}
else if(a[i]>b[i]){return 1;}
else{}
}
if(m>=n){
return 0;
}
else{
return -1;
}
}
void merge(use* suf_arr,int k, int n)
{
use* temp_arr=(use*)malloc(sizeof(use)*n);
int i=0,j=k,l=0,p=0;
//char* te;
while(i<k && j<n){
if(compare_string(suf_arr[i].st,suf_arr[j].st)==-1){
temp_arr[l].index=suf_arr[i].index;
temp_arr[l].n=suf_arr[i].n;
temp_arr[l].st=(char*)malloc(sizeof(char)*(strlen(suf_arr[i].st)+1));
strcpy(temp_arr[l].st,suf_arr[i].st);
temp_arr[l].doc_title=(char*)malloc(sizeof(char)*(strlen(suf_arr[i].doc_title)+1));
strcpy(temp_arr[l].doc_title,suf_arr[i].doc_title);
i++;
}
else{
temp_arr[l].index=suf_arr[j].index;
temp_arr[l].n=suf_arr[j].n;
temp_arr[l].st=(char*)malloc(sizeof(char)*(strlen(suf_arr[j].st)+1));
strcpy(temp_arr[l].st,suf_arr[j].st);
temp_arr[l].doc_title=(char*)malloc(sizeof(char)*(strlen(suf_arr[j].doc_title)+1));
strcpy(temp_arr[l].doc_title,suf_arr[j].doc_title);
j++;
}
l++;
}
if(i==k){
while(j<n){
temp_arr[l].index=suf_arr[j].index;
temp_arr[l].n=suf_arr[j].n;
temp_arr[l].st=(char*)malloc(sizeof(char)*(strlen(suf_arr[j].st)+1));
strcpy(temp_arr[l].st,suf_arr[j].st);
temp_arr[l].doc_title=(char*)malloc(sizeof(char)*(strlen(suf_arr[j].doc_title)+1));
strcpy(temp_arr[l].doc_title,suf_arr[j].doc_title);
j++;l++;
}
}
else{
while(i<k){
temp_arr[l].index=suf_arr[i].index;
temp_arr[l].n=suf_arr[i].n;
temp_arr[l].st=(char*)malloc(sizeof(char)*(strlen(suf_arr[i].st)+1));
strcpy(temp_arr[l].st,suf_arr[i].st);
temp_arr[l].doc_title=(char*)malloc(sizeof(char)*(strlen(suf_arr[i].doc_title)+1));
strcpy(temp_arr[l].doc_title,suf_arr[i].doc_title);
i++;l++;
}
}
//printf("%d\t%d\t%d\n",l,n,k);
for(p=0;p<n;p++){
suf_arr[p].index=temp_arr[p].index;
suf_arr[p].n=temp_arr[p].n;
suf_arr[p].st=(char*)realloc(suf_arr[p].st,sizeof(char)*(strlen(temp_arr[p].st)+1));
strcpy(suf_arr[p].st,temp_arr[p].st);
suf_arr[p].doc_title=(char*)realloc(suf_arr[p].doc_title,sizeof(char)*(strlen(temp_arr[p].doc_title)+1));
strcpy(suf_arr[p].doc_title,temp_arr[p].doc_title);
free(temp_arr[p].st);
free(temp_arr[p].doc_title);
}
free(temp_arr);
}
void sort_arr(use* suf_arr,int a)
{
//printf("%d\n",a);
fflush(stdout);
int l=0;
//int h=sizeof(suf_arr)/sizeof(use);
//int in_temp;
//char* temp;
//int t;
if(a<=1){
//return
}
else{
int k=a/2;
sort_arr(suf_arr,k);
sort_arr(suf_arr+k,a-k);
merge(suf_arr,k,a);
}
}
int main()
{
struct timespec start;
struct timespec end;
FILE *fp;
fp=fopen("AesopTales.txt","r");
char ch;
char* buf=(char*)malloc(sizeof(char)*100000000);
int flag=0;
int i=0;
float tb=0;
char* title;
int fhead=0;
//clock_gettime(CLOCK_REALTIME,&start);
while(!feof(fp)){
ch=fgetc(fp);
if(ch==13 || ch==10){
if(fhead>5 && fhead<1000){
fhead=1000;
//buf[i]='\0';
title=(char*)malloc(sizeof(char)*(strlen(buf)+1));
strcpy(title,buf);
}
flag++;
//printf("%d\n",ch);
//printf("%c\t newline\t%d",ch,flag);
if(flag==6 || i>=1000){
break;
}
buf[i]=ch;
i++;
}
else{
fhead++;
buf[i]=ch;
i++;
flag=0;
}
}
buf[i]='\0';
int n=strlen(buf);
char* t=(char*)malloc(sizeof(char)*(n+1));
strcpy(t,buf);
free(buf);
use** arr_of_suffix_arrs=(use**)malloc(sizeof(use*)*10); //creating array of suffix trees for the first 10 documents in the file
arr_of_suffix_arrs[0]=create_suf_arr(t,title);
sort_arr(arr_of_suffix_arrs[0],strlen(t));
char* text;
char* heading;
for(i=2;i<=10;i++){
text=get_text_title(i,&heading);
//printf("%shello\n%s",text,heading);
//fflush(stdout);
arr_of_suffix_arrs[i-1]=create_suf_arr(text,heading);
sort_arr(arr_of_suffix_arrs[i-1],strlen(text));
free(text);
free(heading);
}
//The query string has not been scanned from user input. If needed, the query string can be changed in the following lines as required.
for(i=1;i<=10;i++){
int answer=search_str("meeting",arr_of_suffix_arrs[i-1],arr_of_suffix_arrs[i-1][0].n); //searching for the 1st occurances of the query string in the first 10 documents
if(answer!=-1){
printf("%d\n",answer);
}
else{
char* lcs=search_lcs("meeting",arr_of_suffix_arrs,i-1,&answer); //searching for the longest substring of the query string in the first 10 documents
printf("%d\t%s\n",answer,lcs);
}
}
printf("\n\n\n");
//free(arr_of_suffix_arrs);
arr_of_suffix_arrs=destroy_and_remake(arr_of_suffix_arrs);
free(t);
free(title);
//printf("%d\n",arr_of_suffix_arrs[1][0].n);
fflush(stdout);
rev* relevance=(rev*)malloc(sizeof(rev)*10); //creating a relevance array for the 10 documents to quantify and store the relevance of the documents to the query string
for(i=1;i<=10;i++){relevance[i-1].doc_no=i-1;
relevance[i-1].score=0;}
for(i=1;i<=10;i++){
int answer=search_str("meeting",arr_of_suffix_arrs[i-1],arr_of_suffix_arrs[i-1][0].n);
int count;
char* lcs;
if(answer!=-1){
if(answer<=strlen(arr_of_suffix_arrs[i-1][0].doc_title)){ //if the query strng is present in the title, it its more relevant and receives a higher score
relevance[i-1].score+=10000;
}
count=no_of_occurances("meeting",arr_of_suffix_arrs[i-1],arr_of_suffix_arrs[i-1][0].n); //Higher the number of occurances of the query string, higher the score
relevance[i-1].score+=count*1000;
}
else{
lcs=search_lcs("meeting",arr_of_suffix_arrs,i-1,&answer); //Longer the substring of the query string matched, higher the score
arr_of_suffix_arrs=destroy_and_remake(arr_of_suffix_arrs);
relevance[i-1].score+=100*strlen(lcs);
count=no_of_occurances(lcs,arr_of_suffix_arrs[i-1],arr_of_suffix_arrs[i-1][1].n); //Higher the number of occurances of the substring of query string, higher the score
relevance[i-1].score+=10*count;
}
}
int j;
rev temp;
//for(i=0;i<10;i++){printf("%d\t",relevance[i].score);}printf("\n");
for(i=0;i<10;i++){
for(j=0;j<9-i;j++){
if(relevance[j].score<relevance[j+1].score){
temp=relevance[j];
relevance[j]=relevance[j+1];
relevance[j+1]=temp;
}
}
}
//for(i=0;i<10;i++){printf("%d\t",relevance[i].score);}
for(i=0;i<10;i++){
printf("%s\n",arr_of_suffix_arrs[relevance[i].doc_no][0].doc_title);
}
printf("\nThe relevance is determined as follows:\nif the query string is present in the title of the document, then it is more relevant.\nif the query string is present more number of times in the document, it is more relevant\nif the query string is partially present, then the document with the longer substring of the query string is more relevant.\nFinally, the more number of times the substring occurs in the document, the more relevant it is.\n");
}
use** destroy_and_remake(use** arr_of_suffix_arrs)
{
free(arr_of_suffix_arrs);
arr_of_suffix_arrs=(use**)malloc(sizeof(use*)*10);
char* text;
char* heading;
int i;
for(i=1;i<=10;i++){
text=get_text_title(i,&heading);
//printf("%shello\n%s",text,heading);
//fflush(stdout);
arr_of_suffix_arrs[i-1]=create_suf_arr(text,heading);
sort_arr(arr_of_suffix_arrs[i-1],strlen(text));
free(text);
free(heading);
}
return arr_of_suffix_arrs;
}
int search_prefix(char* a, char* b)
{
int n=strlen(a);
int m=strlen(b);
int x;
if(n<m){x=n;}
else{x=m;}
int i;
int k=0;
for(i=0;i<x;i++){
if(a[i]==b[i]){
k++;
}
else{
break;
}
}
return k;
}
char* search_lcs(char* a,use** arr_of_suf_arr,int g,int* ans)
{
char* b=a;
int i;
int lcs=0;
int fi=-1;
int ii=-1;
use* suf_arr=arr_of_suf_arr[g];
suf_arr=(use*)realloc(suf_arr,sizeof(use)*(suf_arr[0].n+strlen(a)));
for(i=0;i<strlen(a);i++){
suf_arr[i+suf_arr[0].n].st=(char*)malloc(sizeof(char)*(strlen(a)-i));
strcpy(suf_arr[i+suf_arr[0].n].st,b);
suf_arr[i+suf_arr[0].n].doc_title=(char*)malloc(sizeof(char)*10);
strcpy(suf_arr[i+suf_arr[0].n].doc_title,"noheading\0");
b++;
suf_arr[i+suf_arr[0].n].index=-1;
}
sort_arr(suf_arr,suf_arr[0].n+strlen(a));
int k;
for(i=0;i<strlen(a)+suf_arr[0].n-1;i++){
k=search_prefix(suf_arr[i].st,suf_arr[i+1].st);
if(k>lcs && ((suf_arr[i].index==-1 && suf_arr[i+1].index!=-1) || (suf_arr[i+1].index==-1 && suf_arr[i].index!=-1))){
lcs=k;
if(suf_arr[i+1].index==-1){fi=suf_arr[i].index;ii=i;}
else{fi=suf_arr[i+1].index;ii=i;}
}
}
//printf("%dhello",fi);
char* common_substr;
if(fi!=-1){
common_substr=(char*)malloc(sizeof(char)*lcs);
for(i=0;i<lcs;i++){
common_substr[i]=suf_arr[ii].st[i];
//printf("%c\n",suf_arr[ii].st[i]);
}
common_substr[i]='\0';
}
int nempty=1;
//printf("testing");
//int d=0;
for(i=0;i<suf_arr[0].n+strlen(a);i++){
if(suf_arr[i].index==-1){//d++;
suf_arr[i].st=(char*)realloc(suf_arr[i].st,sizeof(char)*strlen(suf_arr[suf_arr[0].n+strlen(a)-nempty].st)+1);
strcpy(suf_arr[i].st,suf_arr[suf_arr[0].n+strlen(a)-nempty].st);
suf_arr[i].doc_title=(char*)realloc(suf_arr[i].st,sizeof(char)*strlen(suf_arr[suf_arr[0].n+strlen(a)-nempty].doc_title)+1);
strcpy(suf_arr[i].doc_title,suf_arr[suf_arr[0].n+strlen(a)-nempty].doc_title);
suf_arr[i].n=suf_arr[suf_arr[0].n+strlen(a)-nempty].n;
free(suf_arr[suf_arr[0].n+strlen(a)-nempty].st);
free(suf_arr[suf_arr[0].n+strlen(a)-nempty].doc_title);
nempty++;
}
}
fflush(stdout);
//printf("%d\n",d);
suf_arr=(use*)realloc(suf_arr,sizeof(use)*(suf_arr[0].n));
//printf("testing");
//sort_arr(suf_arr,suf_arr[0].n);
*ans=fi;
return common_substr;
}
int search_str(char* pattern, use* suf_arr, int n)
{
//printf("\n\n\n");
int l=0;
int h=n;
int m,k;
int i;
int ans_index=1000000;
while(l<=h){
m=(l+h)/2;
//printf("Searching\t%d\t%d\t%d\t%s\n",l,h,m,suf_arr[m].st);
k=compare_k_string(suf_arr[m].st,pattern);
if(k==0){
int z=m;
//printf("%d hello",suf_arr[m].index);
while(compare_k_string(suf_arr[m].st,pattern)==0 && (m>0 && m<n)){
if(suf_arr[m].index<ans_index){ans_index=suf_arr[m].index;}
m++;
}
m=z;
while(compare_k_string(suf_arr[m].st,pattern)==0 && (m>0 && m<n)){
if(suf_arr[m].index<ans_index){ans_index=suf_arr[m].index;}
m--;
}
return ans_index;
}
else if(k>0){
h=m-1;
}
else{
l=m+1;
}
}
return -1;
}
int no_of_occurances(char* pattern, use* suf_arr, int n)
{
int l=0;
int h=n;
int m,k;
int count=0;
int i;
int ans_index=1000000;
while(l<=h){
m=(l+h)/2;
//printf("Searching\t%d\t%d\t%d\t%s\n",l,h,m,suf_arr[m].st);
k=compare_k_string(suf_arr[m].st,pattern);
if(k==0){
int z=m;
//printf("%d hello",suf_arr[m].index);
while(compare_k_string(suf_arr[m].st,pattern)==0 && (m>0 && m<n)){
if(suf_arr[m].index<ans_index){ans_index=suf_arr[m].index;}
m++;
count++;
}
m=z;
while(compare_k_string(suf_arr[m].st,pattern)==0 && (m>0 && m<n)){
if(suf_arr[m].index<ans_index){ans_index=suf_arr[m].index;}
m--;
count++;
}
count--;
return count;
}
else if(k>0){
h=m-1;
}
else{
l=m+1;
}
}
return 0;
}
char* get_text_title(int doc_id,char** heading)
{
FILE *fp;
fp=fopen("AesopTales.txt","r");
char* buf=(char*)malloc(sizeof(char)*100000000);
char ch;
int flag=0,f2=0;
int i=0;
int count=1;
int fhead=0;
char* text;
while(!feof(fp)){
if(count==doc_id){
//printf("%c\t%d\t%d\t%d\n",ch,ch,f2,count);
ch=fgetc(fp);
//printf("%c\t%d\n",ch,ch);
if(ch==13 || ch==10 || (ch==32 && f2>0)){
if(fhead>5 && fhead<1000){
fhead=1000;
buf[i]='\0';
*heading=(char*)malloc(sizeof(char)*(strlen(buf)+1));
strcpy(*heading,buf);
//printf("%s",*heading);
fflush(stdout);
}
f2++;
//printf("%d\n",ch);
//printf("%c\t newline\t%d",ch,flag);
if(f2>=6){
break;
}
buf[i]=ch;
i++;
}
else{
fhead++;
buf[i]=ch;
i++;
f2=0;
}
}
else{
ch=fgetc(fp);
//printf("%c\t%d\t%d\t%d\n",ch,ch,flag,count);
if(ch==13 || ch==10 || (ch==32 && flag>0)){
flag++;
//printf("%d\n",ch);
//printf("%c\t newline\t%d",ch,flag);
if(flag>=6){
count++;
flag=0;
}
}
else{
flag=0;
}
}
}
buf[i]='\0';
int n=strlen(buf);
//printf("%s",n);
text=(char*)malloc(sizeof(char)*(n+1));
strcpy(text,buf);
//printf("%s",*text);
free(buf);
return (text);
}