Repositories » TXT0
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Post uses an int for tf, otherwise comparisons in postcmp() turn out
to be wrong. p1->tf - p2->tf is what postcmp returns, and if it

operates on unsigned ints it will never return a negative value.

tokenizer() in tokenizer.c was fixed, had some minor bugs where tokens
were not being returned right.

ii1.c was refactored; buildii() and retrieve() now separates the two
actions. main() is shorter.

Changeset 043cfd6a1a21

Parent c75f9d817b1f

by Rup Palchowdhury

Changes to 5 files · Browse files at 043cfd6a1a21 Showing diff from parent c75f9d817b1f Diff from another changeset...

Change 1 of 5 Show Entire File ii1.c Stacked
 
11
12
13
14
15
 
 
16
17
18
 
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
 
94
95
96
97
98
 
 
99
100
101
 
102
103
 
104
105
106
107
 
 
 
108
109
110
 
144
145
146
147
148
 
 
149
150
151
 
160
161
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
164
165
 
 
 
166
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
170
171
 
11
12
13
 
 
14
15
16
17
18
 
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
 
124
125
126
 
 
127
128
129
130
 
131
132
 
133
134
 
 
 
135
136
137
138
139
140
 
174
175
176
 
 
177
178
179
180
181
 
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
@@ -11,8 +11,8 @@
 #include "tfile.h"  #include "doc.h"   -enum {NTERMS = 100003, NDOCS = 100003}; -/* enum {NTERMS = 10007, NDOCS = 4093}; */ +/* enum {NTERMS = 100003, NDOCS = 100003}; */ +enum {NTERMS = 10007, NDOCS = 4093};    void hprint(Node *htab[], unsigned size)  { @@ -49,39 +49,69 @@
  fprintf(stderr, "Node count: %d\n", sumlen);  }   +unsigned getquery(char q[100][100], FILE *fp) +{ + Tokenizer *tok; + static char *septxt = " ;,.:`'\"?!(){}[]<>~^&*_-+=#$%@|\\/"; + char buf[KB]; + unsigned n;   -int main(void) + if (fgets(buf, KB, fp) == NULL) { + if (!feof(fp)) + fprintf(stderr, "Error reading query file.\n"); + return 0; + } + n = 0; + tok = newtokenizer(septxt, LOWERCASE, NULL); + n = tokenize(q, buf, strlen(buf), tok); + return n; +} + +int postcmptf(const void *p1, const void *p2)  { - int i_; - TDoc *tdoc; + return ((Post *)p1)->tf - ((Post *)p2)->tf; +} + +void bubblesort(Post *p[NDOCS], int nel, int (*cmp)(const void*, const void*)) +{ + Post *tmp; + for (int i = 0; i < nel - 1; i++) { + for (int j = i + 1; j < nel; j++) { + if ((*cmp)((const void *)(p[i]), (const void *)(p[j])) > 0) { + tmp = p[i]; + p[i] = p[j]; + p[j] = tmp; + } + } + } +} + +void buildii(unsigned *n_doc, Node *ii[], Node *doctab[], Node *termtab[], FILE *fp) +{ + int j_; + unsigned n_term; + char *p_txt, *ts; + THeader *h; + TDoc *tdoc; + Doc *doc;   Post *post, *post_;   Term *term, *term_; - Doc *doc; - unsigned n_doc, n_term; - char *p_txt, *ts; - THeader *h; - Node *doctab[NDOCS], *termtab[NTERMS], *ii[NTERMS];   Node *npdoc, *npdoc_, *npterm, *npterm_, *npii, *npii_; - - for (int i = 0; i < NDOCS; i++) - doctab[i] = NULL; - for (int i = 0; i < NTERMS; i++) - termtab[i] = NULL; - for (int i = 0; i < NTERMS; i++) - ii[i] = NULL; - +   h = _newTHeader(TREC);   - if ((h = readTHeader(h, stdin)) == NULL) + if ((h = readTHeader(h, fp)) == NULL)   exit(0); - - for(n_doc = 1; n_doc <= h->n; n_doc++) { + + *n_doc = h->n; + + for(int i = 1; i <= h->n; i++) {   - fprintf(stderr, "%d/%d\r", n_doc, h->n); + fprintf(stderr, "\r%d/%d", i, h->n);     tdoc = newTDoc(TREC);   - if ((tdoc = readTDoc(tdoc, stdin)) == NULL) + if ((tdoc = readTDoc(tdoc, fp)) == NULL)   exit(0);     tdoc->id[tdoc->h->n_id] = '\0'; @@ -94,17 +124,17 @@
  free(doc);   free(npdoc_);   } - - i_ = 0; + + j_ = 0;   p_txt = tdoc->txt;   - for (int i = 0; i < tdoc->h->n_txt; i++) { + for (int j = 0; j < tdoc->h->n_txt; j++) {   - if (tdoc->txt[i] == ' ') { + if (tdoc->txt[j] == ' ') {   - ts = (char *)malloc(i - i_ + 1); - memcpy(ts, p_txt, i - i_); - ts[i - i_] = '\0'; + ts = (char *)malloc(j - j_ + 1); + memcpy(ts, p_txt, j - j_); + ts[j - j_] = '\0';     npterm_ = newnode(ts);   npterm = hlookup(termtab, npterm_, 1, hash, NTERMS, strcmp); @@ -144,8 +174,8 @@
  term_->ref = 0;   }   - i_ = i + 1; - p_txt = &(tdoc->txt[i + 1]); + j_ = j + 1; + p_txt = &(tdoc->txt[j + 1]);       if (post_->ref == 0) { @@ -160,12 +190,122 @@
    freeTDoc(tdoc);   } + _freeTHeader(h); + fprintf(stderr, "\n"); +} + +void retrieve(unsigned n_doc, Node *ii[], Node *doctab[], Node *termtab[]) +{ + FILE *qfp = fopen("q.txt", "r"); + char q[100][100]; + int n_qterm, n_q, n_d; + float idf; + Post *post, *post_; + Term *term, *term_; + Node *npterm, *npterm_, *npii, *npii_, *nppost, *nppost_; + Post *res[NDOCS]; + Node *posttab[NDOCS];   - fprintf(stderr, "%d/%d\n", n_doc - 1, h->n); - _freeTHeader(h); + n_q = 0; + + while ((n_qterm = getquery(q, qfp)) > 0) {   - /* hprint(ii, NTERMS); */ + for (int i = 0; i < NDOCS; i++) { + res[i] = NULL; + posttab[i] = NULL; + } + + n_q++; + n_d = 0; + + for (int i = 0; i < n_qterm; i++) { + npterm_ = newnode(q[i]); + npterm = hlookup(termtab, npterm_, 0, hash, NTERMS, strcmp); + free(npterm_); + if (npterm == NULL) + continue; + term_ = newterm((char *)(npterm->data), 0, NULL); + npii_ = newnode(term_); + npii = hlookup(ii, npii_, 0, hashterm, NTERMS, termcmp1); + free(term_); + free(npii_); + if (npii == NULL) + continue; + term = (Term *)(npii->data); + for (Node *np = term->plist; np != NULL; np = np->next) { + post_ = (Post *)(np->data); + nppost_ = newnode(post_); + nppost = hlookup(posttab, nppost_, 1, hashpost, NDOCS, postcmp); + if (nppost != nppost_) { + post = (Post *)(nppost->data); + post->tf += post_->tf; + free(nppost_); + } + else + res[n_d++] = post_; + } + } + + + /* results in a array of Posts */ + /* for (int i = 0; i < n_d; i++) */ + /* printf("%d %s %d\n", n_q, res[i]->id, res[i]->tf); */ + + + /* FIXIT: qsort isn't working */ + /* qsort(res, n_d, sizeof(Post *), postcmptf); */ + + bubblesort(res, n_d, postcmptf); + + for (int i = 0; i < n_d; i++) + printf("%d %s %d\n", n_q, res[i]->id, res[i]->tf); + + /* results in a list that points to nodes in the hash table */ + /* + for (Node *np = reslist; np != NULL; np = np->next) { + post = (Post *)(np->data); + printf("%d %s %d\n", n_q, post->id, post->tf); + } + */ + + /*TODO: figure out a way to free memory neatly*/ + /* freelist(reslist, NULL); */ + /* reslist = NULL; */ + + /* results in a hash table */ + /* for (int i = 0; i < NDOCS; i++) { */ + /* if (posttab[i] == NULL) */ + /* continue; */ + /* for (Node *np = posttab[i]; np != NULL; np = np->next) { */ + /* post = (Post *)(np->data); */ + /* /\* post->tf /= idf; *\/ /\*FIX: post->tf is int *\/ */ + /* printf("%d %s %d\n", n_q, post->id, post->tf); */ + /* } */ + /* } */ + + }   + fclose(qfp); +} + +int main(void) +{ + Node *doctab[NDOCS], *termtab[NTERMS], *ii[NTERMS]; + + unsigned n_doc; + + for (int i = 0; i < NDOCS; i++) + doctab[i] = NULL; + for (int i = 0; i < NTERMS; i++) { + termtab[i] = NULL; + ii[i] = NULL; + } + + buildii(&n_doc, ii, doctab, termtab, stdin); + /* hprint(ii, NTERMS); */ + /* exit(0); */ + retrieve(n_doc, ii, doctab, termtab); +   /* fprintf(stderr, "\n"); */   /* fprintf(stderr, "doctab[]\n"); */   /* hstats(doctab, NDOCS); */
Change 1 of 2 Show Entire File post.c Stacked
 
3
4
5
6
 
7
8
9
 
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
 
6
7
8
9
 
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@@ -3,7 +3,7 @@
 #include <string.h>  #include "post.h"   -Post *newpost(char *id, unsigned tf) +Post *newpost(char *id, int tf)  {   Post *p;   p = (Post *)malloc(sizeof(Post)); @@ -26,3 +26,14 @@
  fmt = (char *)arg;   printf(fmt, ((Post *)d)->id, ((Post *)d)->tf);  } + +unsigned hashpost(void *data, unsigned hsize) +{ + static unsigned MULTIPLIER = 31; + unsigned h; + char *p; + h = 0; + for (p = (char *)(((Post *)data)->id); *p != '\0'; p++) + h = MULTIPLIER * h + *p; + return h % hsize; +}
Change 1 of 1 Show Entire File post.h Stacked
 
2
3
4
5
 
6
7
8
9
 
10
11
 
 
2
3
4
 
5
6
7
8
 
9
10
11
12
@@ -2,10 +2,11 @@
   struct Post {   char* id; - unsigned tf; + int tf;   int ref;  };   -Post *newpost(char*, unsigned); +Post *newpost(char*, int);  int postcmp(void*, void*);  void printpost(void*, void*); +unsigned hashpost(void*, unsigned);
Change 1 of 1 Show Entire File tokenizer.c Stacked
 
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
 
107
108
109
 
110
111
 
 
 
 
 
 
 
 
 
 
112
113
114
115
116
117
118
119
120
121
122
123
 
 
 
 
 
 
124
125
126
127
128
 
129
130
@@ -107,27 +107,24 @@
  return n;  }   -int tokenize(char *dest, char *src, int size, Tokenizer *t) +int tokenize(char dest[100][100], char *src, unsigned size, Tokenizer *t)  { - int i, n; - static int len = 0; - n = 0; - for (i = 0; i < size; i++, src++) { - if (t->asciitab[(int)(*src)] == SEPCHAR) { - if (len > 0) { - *dest = ' '; - dest++; - len = 0; - n++; + int i, j, k; + char *s; + i = j = k = 0; + for (i = 0, s = src; i < size; i++, s++) { + if (t->asciitab[(int)(*s)] == SEPCHAR) { + if (k > 0) { + dest[j][k] = '\0'; + j++; + k = 0;   }   continue;   } - *dest = *src; - if (t->casechange == LOWERCASE && *src >= 65 && *src <= 90) - *dest += 32; - dest++; - len++; - n++; + dest[j][k] = *s; + if (t->casechange == LOWERCASE && *s >= 65 && *s <= 90) + dest[j][k] += 32; + k++;   } - return n; + return j;  }
Change 1 of 1 Show Entire File tokenizer.h Stacked
 
38
39
40
41
 
 
38
39
40
 
41
@@ -38,4 +38,4 @@
 Tokenizer *newtokenizer(char*, int, Stack*);  Token *newtoken(int);  int gettoken(Token*, FILE*, Tokenizer*); -int tokenize(char*, char*, int, Tokenizer*); +int tokenize(char[100][100], char*, unsigned, Tokenizer*);