-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
164 lines (157 loc) · 5.21 KB
/
main.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
#include<time.h>
#include "include.h"
#define INPUT_LIMIT 500
int main()
{
char c;
clock_t timer;
long stop_db_size;//number of words in stop database
char stop_db_path[]="SE-db/stop-words.txt";
char index_path[]="SE-db/index.txt";
char dbindex_path[] = "SE-db/document-index.txt";
char** stop_db_arr=import_stopdb_tomem(&stop_db_size,stop_db_path);
long size_index; //import index structure
struct index_word* index = import_index_tomem(&size_index, index_path);
long size_dbindex;
struct docdet* dbindex = import_dbindex(&size_dbindex,dbindex_path);
char raw_uin_str[INPUT_LIMIT];
char raw_uin_str_cp[INPUT_LIMIT];
printf("Search for: ");
fgets(raw_uin_str,INPUT_LIMIT,stdin);
printf("Running search for: %s\n", raw_uin_str);
timer = clock();
raw_uin_str[strcspn(raw_uin_str, "\n")] = 0;
strcpy(raw_uin_str_cp, raw_uin_str);
tolowerstr(raw_uin_str_cp);
int kwsize;
int i = 0;
int owordsize;
int temp;
char** uin_wtable = extract_keywords(stop_db_size, stop_db_arr, raw_uin_str_cp,&kwsize);
int* keywords = (int*)malloc(kwsize*sizeof(int));
int* owordsdocfreq = (int*)calloc(kwsize,sizeof(int));
int* swordsdocfreq = (int*)calloc(kwsize,sizeof(int));
int* swordsfreq = (int*)calloc(kwsize,sizeof(int));
float* oworddoclist = (float*)calloc(size_dbindex,sizeof(float));
float* sworddoclist = (float*)calloc(size_dbindex,sizeof(float));
char** stemmed_uin_wtable = (char**)malloc((kwsize+1)*sizeof(char*));
stemmed_uin_wtable[kwsize]=NULL;
int odocfreq = 0;
int odoclen = 0;
int sdocfreq = 0;
int sdoclen = 0;
int rwflag;
for(i = 0;uin_wtable[i]!= NULL;i++)
{
stemmed_uin_wtable[i]=(char*)malloc((strlen(uin_wtable[i])+1)*sizeof(char));
strcpy(stemmed_uin_wtable[i],uin_wtable[i]);
porter_stemmer(stemmed_uin_wtable[i]);
keywords[i]=bin_search_struct(size_index,index,stemmed_uin_wtable[i],-1);
if(keywords[i]<0)
continue;
for(owordsize = 0; index[keywords[i]].doc_data[owordsize].orword != NULL; owordsize++)
{
rwflag = 0;
temp = bin_search_struct_2(size_dbindex,dbindex,index[keywords[i]].doc_data[owordsize].docname);
if(temp<0)
return 3;//database error
if(!strcmp(uin_wtable[i],index[keywords[i]].doc_data[owordsize].orword))
{
owordsdocfreq[i]++;
if(!oworddoclist[temp])
{
rwflag = 1;
odocfreq++;
odoclen += dbindex[temp].length;
oworddoclist[temp]=1;
}
}
swordsdocfreq[i]++;
if(!sworddoclist[temp]&&!rwflag)
{
sdocfreq++;
sdoclen += dbindex[temp].length;
swordsfreq[i]+= index[keywords[i]].doc_data[owordsize].freq;
sworddoclist[temp]=1;
}
}
}
for(int j = 0; j<size_dbindex;j++)
{
if(oworddoclist[j])
{
//sworddoclist[j] = 0;
for(i = 0;uin_wtable[i]!=NULL;i++)
{
if(keywords[i]>=0)
for(int iter = 0; index[keywords[i]].doc_data[iter].orword!=NULL;iter++)
if(!strcmp(index[keywords[i]].doc_data[iter].orword,uin_wtable[i])&&!strcmp(index[keywords[i]].doc_data[iter].docname,dbindex[j].name))
{
oworddoclist[j]+=rank(get_idf(odocfreq,owordsdocfreq[i]),index[keywords[i]].doc_data[iter].freq,dbindex[j].length,(odoclen/odocfreq));
break;
}
}
}
if(sworddoclist[j])
for(i = 0;uin_wtable[i]!=NULL;i++)
{
sworddoclist[j]+=rank(get_idf(sdocfreq,swordsdocfreq[i]),swordsfreq[i],dbindex[j].length,(sdoclen/sdocfreq));
break;
}
}
int res;
int loopctr = 1;
int* filematch = (int*)malloc(size_dbindex*sizeof(int));
memset(filematch,-1,size_dbindex*sizeof(int));
while((res=pop_max(oworddoclist,sworddoclist,size_dbindex))>=0)
{
if(loopctr==1)
printf("Best match: %s\n", dbindex[res].name);
else
printf("%d%s match: %s\n",loopctr,(loopctr==1)?"st":((loopctr==2)?"nd":((loopctr==3)?"rd":"th")),dbindex[res].name);
filematch[loopctr-1]=res;
loopctr++;
}
timer = clock() - timer;
double time = ((double)timer)/(CLOCKS_PER_SEC/1000000);
if(loopctr==1)
printf("Your search '%s' did not match any known documents\n", raw_uin_str_cp);
else
{
printf("\nTo open any of the matched documents(should have atom installed), type its position number or press enter to quit:");
int nflag = 0;
int chr;
char ch;
while(!nflag)
{
ch = getchar();
chr = ch-'0'-1;
if(ch!='\n')
{
getchar();
while(ch<49||ch>57||chr>=(loopctr-1))
{
printf("\nYou have entered invalid index number, try again:");
ch = getchar();
if(ch == '\n')
break;
getchar();
chr = ch-'0'-1;
}
if(ch != '\n')
{
char* openpath = (char*)malloc((strlen(dbindex[filematch[chr]].name)+12)*sizeof(char));
strcpy(openpath,"atom source/");
strcat(openpath,dbindex[filematch[chr]].name);
printf("Opening %s\n", openpath);
system(openpath);
printf("Open another file?(enter key to quit):");
free(openpath);
}
}
else
nflag = 1;
}
}
printf("\nSimple search took %.1lf microseconds to return %d results\n", time, loopctr-1);
}