-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinverted_index.c
More file actions
307 lines (286 loc) · 6.9 KB
/
inverted_index.c
File metadata and controls
307 lines (286 loc) · 6.9 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
#pragma once
#pragma execution_character_set("UTF-8")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "inverted_index.h"
#include "Chinese_word_segment.h"
#include "htmlparser.h"
//djb2 is one of the best string hash function
uint inverted_index_hash(const void* ptr, int lenth)
{
uint hash = 5381;
int i;
uchar* p = (uchar*)ptr;
for (i=0; i < lenth; ++i)
hash += ((hash << 5) +hash+ p[i]);
return hash;
}
//find a prime not less than bound for words' hashtable
uint find_prime(uint bound)
{
uint val;
uint sr, i;
if(bound <= 2)
return 2;
for(val=bound; ;++val)
{
sr = sqrt(val*1.0);
for(i=2; i<=sr; ++i)
if(val%i == 0)
continue;
return val;
}
}
//make a new word node with a given word
WORDNODE* inverted_index_new_wordnode(const char* src)
{
char* pword=NULL;
WORDNODE* pwordnode=NULL;
if(src==NULL || !*src)
return NULL;
pword = (char*)calloc(strlen(src)+1, sizeof(char));
pwordnode = (WORDNODE*)calloc(1, sizeof(WORDNODE));
if(pword==NULL || pwordnode==NULL)
{
fprintf(stderr, "Error allocating memory\n");
free(pword);
free(pwordnode);
return NULL;
}
strcpy(pword, src);
pwordnode->pword = pword;
pwordnode->phead = NULL;
pwordnode->pnext = NULL;
pwordnode->idf = 0;
return pwordnode;
}
//make a new document node with given url
DOCNODE* inverted_index_new_docnode(int doc_id)
{
DOCNODE* pdocnode = NULL;
pdocnode = (DOCNODE*)calloc(1,sizeof(DOCNODE));
if(pdocnode==NULL)
{
fprintf(stderr, " Error allocating memory\n");
return NULL;
}
pdocnode->docid = doc_id;
pdocnode->pnext = NULL;
pdocnode->tf = 0;
return pdocnode;
}
INDEXTABLE* inverted_index_initialize(const DICTIONARY* pdict)
{
INDEXTABLE* ptable;
INDEXITEM *ptr;
uint word_num=0;
if(pdict==NULL || !pdict->dic_capacity)
return NULL;
ptable = (INDEXTABLE*)calloc(1, sizeof(INDEXTABLE));
if(ptable==NULL)
{
fprintf(stderr, "Error allocating memory\n");
return NULL;
}
word_num = pdict->dic_capacity;//number of words in dictionary
ptable->prime_num = find_prime(word_num);
ptr = (INDEXITEM*)calloc(ptable->prime_num, sizeof(INDEXITEM));
if(ptr==NULL)
{
fprintf(stderr, "Error allocating memory\n");
free(ptable);
return NULL;
}
ptable->pindex = ptr;
return ptable;
}
//insert a word into the inverted index
void inverted_index_insert_word(INDEXTABLE* ptable, const char* pword)
{
uint hashcode=0;
const char* ptr = pword;
WORDNODE* pwordnode=NULL;
WORDNODE *pwordpre=NULL, *pwordcur=NULL;
uint pos=0;
if(ptable==NULL || pword==NULL || !*pword)
return;
while(*ptr)
++ptr;
//compute the hash code for this word
hashcode = inverted_index_hash(pword, ptr-pword);
//find the place to insert the word node
pos = hashcode%(ptable->prime_num);
//no word has been inserted in this position
if(ptable->pindex[pos].phead == NULL)
{
//make a new word node
pwordnode = inverted_index_new_wordnode(pword);
if(pwordnode==NULL)
return;
ptable->pindex[pos].phead = pwordnode;
return;
}
//find the place to insert the word
pwordcur = ptable->pindex[pos].phead;
if(!strcmp(pwordcur->pword, pword))//the word is identical with the first node
return;
while(pwordcur && strcmp(pwordcur->pword,pword)<0)
{
pwordpre = pwordcur;
pwordcur = pwordcur->pnext;
}
//the word doesn't exists
if(pwordcur==NULL || strcmp(pwordcur->pword,pword))
{
//make a new word and insert it
pwordnode = inverted_index_new_wordnode(pword);
if(pwordnode==NULL)
return;
pwordnode->pnext = pwordcur;
pwordpre->pnext = pwordnode;
}
}
//insert multiple words stored in a trie-tree
void inverted_index_insert_words(INDEXTABLE* ptable, const TRIETREE root, char* buf, int depth)
{
uchar i;
if(root==NULL)
return;
buf[depth] = root->keyword;
if(root->stat == STAT_WORD)
{
buf[depth+1] = 0;
inverted_index_insert_word(ptable, buf);
}
for(i=0; i<root->numchild; ++i)
inverted_index_insert_words(ptable, root->childptr+i, buf, depth+1);
}
//insert the whole words in dictionary into inverted index
void inverted_index_insert_dictionary(INDEXTABLE* ptable, const DICTIONARY* pdict)
{
uchar i;
const int MAX_DEPTH=100;
char* buf;
if(ptable==NULL || pdict==NULL)
return;
buf = (char*)malloc(sizeof(char)*MAX_DEPTH);
if(buf==NULL)
{
fprintf(stderr, "Error allocating memory\n");
return;
}
memset(buf, 0, sizeof(char)*MAX_DEPTH);
for(i=0; i<0xff; ++i)
if(pdict->ptrees[i])
inverted_index_insert_words(ptable, pdict->ptrees[i], buf, 0);
free(buf);
}
void inverted_index_save(const INDEXTABLE* ptable, const char* outfilename)
{
WORDNODE* pwordnode=NULL;
DOCNODE* pdocnode=NULL;
unsigned int i=0, cnt=0;
FILE* fp=NULL;
fp = fopen(outfilename,"wt");
if(fp==NULL)
{
fprintf(stderr, "Can't write to file %s", outfilename);
return;
}
fprintf(fp, "%d\n", ptable->prime_num);
for(i=0; i<ptable->prime_num; ++i)
{
pwordnode = ptable->pindex[i].phead;
while(pwordnode)
{
fprintf(fp, "%u %s %f", i, pwordnode->pword, pwordnode->idf);
//write the number of documents containing this word into file
cnt=0;
pdocnode = pwordnode->phead;
while(pdocnode)
{
++cnt;
pdocnode = pdocnode->pnext;
}
fprintf(fp," %u", cnt);
//write the id of documents
pdocnode = pwordnode->phead;
while(pdocnode)
{
fprintf(fp, " %u %f", pdocnode->docid, pdocnode->tf);
pdocnode = pdocnode->pnext;
}
fprintf(fp, "\n");
pwordnode = pwordnode->pnext;
}
}
fclose(fp);
}
//FUNCTION:load inverted index from file
INDEXTABLE* inverted_index_load(const char* infilename)
{
INDEXTABLE* ptable=NULL;
INDEXITEM* pitem=NULL;
WORDNODE* pwordnode=NULL, *pwordlast=NULL;
DOCNODE* pdocnode=NULL, *pdoclast=NULL;
unsigned int primenum=0, cnt=0, i=0, doc_id;
unsigned int hashcode=0;
char pword[150]={0};
double idf, tf;
FILE* fp=NULL;
fp = fopen(infilename, "rt");
if(fp==NULL)
{
fprintf(stderr, "Cant't read file %s\n", infilename);
return NULL;
}
fscanf(fp, "%u", &primenum);
ptable = (INDEXTABLE*)calloc(1, sizeof(INDEXTABLE));
pitem = (INDEXITEM*)calloc(primenum, sizeof(INDEXITEM));
if(ptable==NULL || pitem==NULL)
{
fprintf(stderr, "Error allocating memory\n");
fclose(fp);
free(ptable);
free(pitem);
return NULL;
}
while(4 == fscanf(fp, "%u %s %lf %u", &hashcode, pword, &idf, &cnt))
{
pwordnode = inverted_index_new_wordnode(pword);
pwordnode->idf = idf;
if(pitem[hashcode].phead == NULL)
{
pitem[hashcode].hashcode = hashcode;
pitem[hashcode].phead = pwordnode;
pwordlast = pwordnode;
}
else
{
pwordlast->pnext = pwordnode;
pwordlast = pwordnode;
}
for(i=0; i<cnt; ++i)
{
fscanf(fp, "%u %lf", &doc_id, &tf);
pdocnode = inverted_index_new_docnode(doc_id);
pdocnode->tf = tf;
if(pwordnode->phead == NULL)
{
pwordnode->phead = pdocnode;
pdoclast = pdocnode;
}
else
{
pdoclast->pnext = pdocnode;
pdoclast = pdocnode;
}
}
}
fclose(fp);
ptable->prime_num = primenum;
ptable->pindex = pitem;
return ptable;
}