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

Forget everything.

Changeset 63220ad0c40e

Parent 7196fc40134c

by Rup Palchowdhury

Changes to 17 files · Browse files at 63220ad0c40e Showing diff from parent 7196fc40134c Diff from another changeset...

Change 1 of 1 Show Entire File Makefile Stacked
 
18
19
20
 
21
22
23
 
18
19
20
21
22
23
24
@@ -18,6 +18,7 @@
  tfile.c \   txt.c \   crc.c \ + parser.c \   porter.c    OBJ = $(patsubst %.c,$(O)/%.o,$(SRC))
Change 1 of 3 Show Entire File ii.c Stacked
 
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
 
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
 
410
411
412
413
414
415
416
417
418
419
 
 
 
 
 
 
 
420
421
422
423
424
425
426
427
428
429
430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
432
433
 
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
 
266
267
268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
270
 
271
272
273
 
274
275
276
277
 
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
@@ -1,336 +1,253 @@
 #include <stdio.h>  #include <stdlib.h>  #include <string.h> -#include <assert.h> +#include <kak/kcommon.h>  #include <kak/eprintf.h> -#include <kak/kcommon.h>  #include <kak/klist.h>  #include <kak/khash.h> +#include "txt.h"  #include "tfile.h" -#include "txt.h"   -enum {NTERMS = 100003, NDOCS = 100003, NHASH = 4093}; -/* enum {NTERMS = 10007, NDOCS = 4093, NHASH = 4093}; */ +Hash *buildii(TFile **tfc, FILE *fp) +{ + /* tfc will end up with the corpus' header, term-map, doc-map + and all the TDocHeaders in these place-holders, + respectively; + + TFile->h + TFile->ht + TFile->hd + TFile->hdh + + TFile->list stays empty, as TDocs will have been splintered + and shaped into the inverted index. + */ + + int n, size, bpack, pflag_, tflag_; + uint8_t *buf; + TDoc *tdoc; + Hash *ii; + Term *term_, *term; + Post *post_, *post; + Node *npt_, *npt, *npd_;   -void buildq(Hash *hq, FILE *fp) -{ - TFile *tfile; - TDoc *tdoc; - Post *post_; - Node *np, *npq_, *npq, *nppost_; - Query *q_, *q; - uint32_t k, k_, sumtf, uterm, tf, n_ts, c, e; - char *ts; + *tfc = newTFile(); + (*tfc)->hdh = newhash(NHASHD, cmpdoc, hashdoc); /* hash of TDocHeaders */   - c = e = 0; - n_ts = 64; - ts = malloc(sizeof(char) * n_ts); + /* dismantle the default hash table functions.*/ + (*tfc)->ht->cmp = cmptoken_id; + (*tfc)->ht->hash = hashtoken_id; + (*tfc)->hd->cmp = cmptoken_id; + (*tfc)->hd->hash = hashtoken_id; + + size = MB; + ii = newhash(NHASHT, cmpterm, hashterm); + buf = (uint8_t*)emalloc(size);   - /* read in the query file */ - - /* if ((tfile = readTFile(fp)) == NULL) */ - /* eprintf("buildq: readTFile() failed\n"); */ + /* get THeader */ + if ((n = fread(buf, BTHEADER, 1, fp)) != 1) + eprintf("ii(): fread() failed on %d byte THeader\n", + BTHEADER); + if (unpackTHeader((*tfc)->h, BTHEADER, buf) != BTHEADER) + eprintf("ii(): unpackTHeader() failed on %d byte THeader\n", + BTHEADER);   - if ((tfile = receiveTFile(fp)) == NULL) - eprintf("buildq: readTFile() failed\n"); + /* get ID maps */ + bpack = (*tfc)->h->bt > (*tfc)->h->bd ? (*tfc)->h->bt : (*tfc)->h->bd; + if (size < bpack) { + while ((size <<= 1) < bpack) + ; + buf = erealloc(buf, size); + } + if ((n = fread(buf, (*tfc)->h->bt, 1, fp)) != 1) + eprintf("ii(): fread() failed on %d byte tfile->h->bt\n", (*tfc)->h->bt); + if (unpack_idmap((*tfc)->ht, (*tfc)->h->bt, buf) != (*tfc)->h->bt) + eprintf("ii(): unpack_idmap() failed on %d byte tfile->ht", (*tfc)->h->bt); + if ((n = fread(buf, (*tfc)->h->bd, 1, fp)) != 1) + eprintf("ii(): fread() failed on %d byte tfile->h->bd\n", (*tfc)->h->bd); + if (unpack_idmap((*tfc)->hd, (*tfc)->h->bd, buf) != (*tfc)->h->bd) + eprintf("ii(): unpack_idmap() failed on %d byte tfile->hd", (*tfc)->h->bd);   - for (np = tfile->list; np != NULL; np = np->next) { /* for each query */ + /* get the documents */ + for (int i = 0; i < (*tfc)->h->nd; i++) { + tdoc = newTDoc();   - tdoc = np->data; + /* get TDocHeader */ + if ((n = fread(buf, BTDOCHEADER, 1, fp)) != 1) + eprintf("ii(): fread() failed on %d byte TDocHeader\n", + BTDOCHEADER); + if (unpackTDocHeader(tdoc->h, BTDOCHEADER, buf) != BTDOCHEADER) + eprintf("ii(): unpackTDocHeader() failed on %d byte TDocHeader\n", + BTDOCHEADER);   - if (tdoc->h->n_txt <= 0) { - e++; - weprintf("buildq: Skipped empty query %s", tdoc->id); - continue; + /* unpack the term info */ + bpack = 2 * tdoc->h->sumu * 4; + if (size < bpack) { + while ((size <<= 1) < bpack) + ; + buf = erealloc(buf, size);   } + if ((n = fread(buf, bpack, 1, fp)) != 1) + eprintf("ii(): fread() failed on %d byte TDoc payload\n", + bpack); + if (unpackTDoc_payload(tdoc, bpack, buf) != bpack) + eprintf("ii(): unpackTDoc_payload() failed on %d byte TDoc payload\n", + bpack);   - c++; - sscanf(tdoc->rsrc[0], "%u", &sumtf); - sscanf(tdoc->rsrc[1], "%u", &uterm); + /* go over the terms */ + for (int j = 0; j < tdoc->h->sumu; j++) {   - q_ = newquery(strdup(tdoc->id)); - q_->sumtf = sumtf; - q_->uterm = uterm; + /* build a payload */ + pflag_ = tflag_ = 0; + post_ = newpost(tdoc->h->id, tdoc->tf[j]); + term_ = newterm(tdoc->txt[j], 1, post_); + npt_ = newnode(term_); + + /* drop it on the inverted index */ + npt = hlookup(ii, npt_, 1); + if (npt != npt_) { + tflag_ = 1; + term = (Term *)(npt->data); + post = (Post *)(term->plist->data); + if (cmppost(post_, post) == 0) { + post->tf += post_->tf; + pflag_ = 1; + } + else { + term->plist = addfront(term->plist, term_->plist); + term->df++; + } + + } + if (pflag_) + freenode(term_->plist, free); + if (tflag_) + free(term_); + }   - npq_ = newnode(q_); - npq = hlookup(hq, npq_, 1); - q = (Query *)npq->data; - /* needn't check for duplicates, query IDs are unique */ + /* place the TDocHeader in (*tfc)->hdh */ + npd_ = newnode(tdoc->h); + hlookup((*tfc)->hdh, npd_, 1); + /* not checking for duplicates, documents in .t files + * are unique */   - k = k_ = 0; + /* reset */   - while (k < tdoc->h->n_txt) { /* for each 'term frequency' pair in the vector */ + /* careful not to call freeTDoc(tdoc), the the + * TDocHeader is persists in tfile->hdh */   - /* pick a term */ - for(; tdoc->txt[k] != ' '; k++) - ; - if (k - k_ >= n_ts) - ts = erealloc(ts, n_ts<<=1); - memcpy(ts, &(tdoc->txt[k_]), k - k_); - ts[k - k_] = '\0'; - k++; k_ = k; - - /* and then pick its frequency */ - for(; tdoc->txt[k] != ' '; k++) - ; - sscanf(&(tdoc->txt[k_]), "%u", &tf); - k++; k_ = k; - - /* pack them in a Post, attach to a list */ - post_ = newpost(ts, tf); - nppost_ = newnode(post_); - q->tlist = addfront(q->tlist, nppost_); - - /* needn't check for duplicates, terms are unique */ - } - - fprintf(stderr, "\rqueries built: %d/%d", c, tfile->h->n); + free(tdoc->txt); tdoc->txt = NULL; + free(tdoc->tf); tdoc->tf = NULL; + free(tdoc); + fprintf(stderr, "\rindexed: %d/%d", i+1, (*tfc)->h->nd);   }   fprintf(stderr, "\n"); - free(ts); - freeTFile(tfile, TRECQUERY); - weprintf("buildq: Skipped %d empty query(ies)\n", e); + free(buf); + return ii;  }   -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; +Hash *search(Hash *ii, TFile *tfc, TDoc *tdq, Model m) { + + /* tfc carries the following pieces; + TFile->h + TFile->ht + TFile->hd + TFile->hdh + */ + + Hash *res; + Term t__, *term; + Post *post; + Node n__, *npt, *nph_, *nph, *npd; + double tf, df, qtf, qdf, poc; + TDocHeader d__, *d; + Hit *hit_; + + res = newhash(NHASHD, cmphit, hashhit); + t__.id = 0; + t__.df = 0; + t__.plist = NULL; + npt = NULL; + + /* go over each query-term */ + for (int j = 0; j < tdq->h->sumu; j++) { + + /* look it up in the inverted index */ + t__.id = tdq->txt[j]; + n__.data = &t__; + if ((npt = hlookup(ii, &n__, 0)) == NULL) + continue; + term = (Term *)(npt->data); + + /* walk the term's list of docs; aka postings + * list */ + for (Node *npp = term->plist; npp != NULL; npp = npp->next) { + + post = (Post *)(npp->data); + + /* look up the doc's info */ + d__.id = post->id; + n__.data = &d__; + if ((npd = hlookup(tfc->hdh, &n__, 0)) == NULL) { + weprintf("search(): indexed doc %d not in corpus\n", + post->id); + continue; + } + d = (TDocHeader *)(npd->data); + + /* modulate the tf, df of a doc and a + * query */ + tf = (*m.tf)(post->tf, d->sumb, d->sumu, d->sumtf, + d->maxtf, d->sumsqtf, d->sumsqlogtf, + tfc->h->sumb, tfc->h->sumu, tfc->h->sumtf, tfc->h->nd); + df = (*m.df)(term->df, tfc->h->nd); + qtf = (*m.qtf)(tdq->tf[j], d->sumb, d->sumu, d->sumtf, + d->maxtf, d->sumsqtf, d->sumsqlogtf, + tfc->h->sumb, tfc->h->sumu, tfc->h->sumtf, tfc->h->nd); + qdf = (*m.qdf)(term->df, tfc->h->nd); + + /* compute the product of components of two vectors */ + poc = tf * df * qtf * qdf; + + /* sum the product; to partially + * complete the vector dot product */ + hit_ = newhit(post->id, poc); + nph_ = newnode(hit_); + nph = hlookup(res, nph_, 1); + if (nph != nph_) { + ((Hit *)(nph->data))->n += poc; + freenode(nph_, free);   }   }   } -} - -THeader *buildii(Hash *hdoc, Hash *hterm, FILE *fp) -{ - int pflag_, tflag_; - uint32_t k, k_, sumtf, uterm, sumsqtf, maxtf, tf, n_ts, c, e; - char *ts; - /* THeader *h; */ - TFile *tfile; - TDoc *tdoc; - Doc *doc; - Post *post, *post_; - Term *term, *term_; - Node *np, *npdoc, *npdoc_, *npterm, *npterm_; - - c = e = 0; - n_ts = 64; - ts = malloc(sizeof(char) * n_ts); - /* h = _newTHeader(TREC); */ - - if ((tfile = receiveTFile(fp)) == NULL) - eprintf("receiveTFile failed\n"); - - /* if ((h = readTHeader(h, fp)) == NULL) */ - /* eprintf("buildii: readTHeader failed\n"); */ - - /* for(int i = 1; i <= tfile->h->n; i++) { /\* for each doc *\/ */ - - /* tdoc = newTDoc(TREC); */ - - /* if ((tdoc = readTDoc(tdoc, fp, TREC)) == NULL) */ - /* eprintf("buildii: readTDoc failed\n"); */ - - for (np = tfile->list; np != NULL; np = np->next) { /* for each doc */ - - tdoc = np->data; - - if (tdoc->h->n_txt <= 0) { - e++; - weprintf("buildii: Skipped empty doc %s", tdoc->id); - freeTDoc(tdoc, TREC); - continue; - } - - c++; - sscanf(tdoc->rsrc[0], "%u", &sumtf); - sscanf(tdoc->rsrc[1], "%u", &uterm); - sscanf(tdoc->rsrc[2], "%u", &sumsqtf); - sscanf(tdoc->rsrc[3], "%u", &maxtf); - doc = newdoc(tdoc->id, tdoc->h->n_txt, sumtf, uterm, sumsqtf, maxtf); - npdoc_ = newnode(doc); - npdoc = hlookup(hdoc, npdoc_, 1); - if (npdoc != npdoc_) /* a repeating doc id */ - freenode(npdoc_, freedoc); - - k = k_ = 0; - - while (k < tdoc->h->n_txt) { /* for each 'term frequency' pair*/ - - /* pick a term */ - for(; tdoc->txt[k] != ' '; k++) - ; - if (k - k_ >= n_ts) - ts = erealloc(ts, n_ts<<=1); - memcpy(ts, &(tdoc->txt[k_]), k - k_); - ts[k - k_] = '\0'; - k++; k_ = k; - - /* and then pick its frequency */ - for(; tdoc->txt[k] != ' '; k++) - ; - sscanf(&(tdoc->txt[k_]), "%u", &tf); - k++; k_ = k; - - /* amass a payload */ - pflag_ = tflag_ = 0; - post_ = newpost_s(((Doc *)(npdoc->data))->id, tf); - term_ = newterm(ts, 1, post_); - npterm_ = newnode(term_); - npterm = hlookup(hterm, npterm_, 1); - - /* drop it on the inverted index */ - if (npterm != npterm_) { /* term exists */ - tflag_ = 1; /* mark to free */ - term = (Term *)(npterm->data); - post = (Post *)(term->plist->data); - if (cmppost_p(post_, post) == 0) { /* post exists */ - post->tf += post_->tf; - pflag_ = 1; /* mark to free */ - } - else { /* attach a new post */ - term->plist = addfront(term->plist, term_->plist); - term->df++; - } - } - - /* cleanup */ - if (pflag_) - freenode(term_->plist, freepost_s); - if (tflag_) - free(term_); - } - freeTDoc(tdoc, TREC); - fprintf(stderr, "\rindexed: %d/%d", c, tfile->h->n); - } - fprintf(stderr, "\n"); - free(ts); - - /*TODO: because tfile->h is needed, freeTFile() isn't called, - * but this isn't neat */ - - weprintf("buildii: Skipped %d empty docs\n", e); - return tfile->h; -} - -void search(Query *q, Hash *hdoc, Hash *hterm, THeader *h, Model m) -{ - double tf_, df_, qtf_, qdf_, wd_, wq_, dot; - Post *post_q, *post_d; - Term *term, term__; - Doc *doc, doc__; - Node *npterm, node__, *npdoc, *npscore, *npscore_; - Score *score, *score_; - Hash *hresult; - - hresult = newhash(NDOCS, cmpscore_p, hashscore); - - for (Node *np = q->tlist; np != NULL; np = np->next) { /* for a term */ - - post_q = (Post *)(np->data); - term__.s = post_q->id; - node__.data = &term__; - npterm = hlookup(hterm, &node__, 0); - - if (npterm == NULL) - continue; - - term = (Term *)(npterm->data); - - for (Node *np1 = term->plist; np1 != NULL; np1 = np1->next) { /* for a post */ - - post_d = (Post *)(np1->data); - doc__.id = post_d->id; - node__.data = &doc__; - hdoc->cmp = cmpdoc_p; /* dismantle cmp_fn */ - npdoc = hlookup(hdoc, &node__, 0); - hdoc->cmp = cmpdoc; /* put it back */ - - if (npdoc == NULL) - continue; - - doc = (Doc *)(npdoc->data); - - /* transform tf and df of the term (using some - * f(tf) and f(df)) and compute the two - * weights for the term; one in the document - * and one in the query and then compute the - * vector dot product */ - - /* transformations */ - tf_ = (*m.tf)(post_d->tf, doc->sumtf, doc->uterm, - doc->sumsqtf, doc->maxtf, doc->nb, - h->sumnb, h->sumnu, h->sumnt, - h->n); - df_ = (*m.df)(term->df, h->n); - qtf_ = (*m.qtf)(post_q->tf, doc->sumtf, doc->uterm, - doc->sumsqtf, doc->maxtf, doc->nb, - h->sumnb, h->sumnu, h->sumnt, - h->n); - qdf_ = (*m.qdf)(term->df, h->n); - - /* weight computation */ - wd_ = tf_ * df_; /* document term weight */ - wq_ = qtf_ * qdf_; /* query term weight */ - dot = wd_ * wq_; /* for the vector dot product */ - - score_ = newscore_s(post_d->id, dot); - npscore_ = newnode(score_); - npscore = hlookup(hresult, npscore_, 1); - - if (npscore != npscore_) { /* merge */ - score = (Score *)(npscore->data); - score->n += score_->n; - freenode(npscore_, freescore_s); - } - } - } - - /* print result */ - for (int i = 0; i < hresult->n; i++) { - for (Node *np = hresult->tab[i]; np != NULL; np = np->next) { - score = (Score *)(np->data); - fprintf(stdout, "%s %s %f\n", q->id, score->id, score->n); - } - } - - fflush(stdout); - freehash(hresult, freescore_s); - - /*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 (hpost[i] == NULL) */ - /* continue; */ - /* for (Node *np = hpost[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); */ - /* } */ - /* } */ + return res;  }    void usage(char *progname)  { - fprintf(stderr, "usage: %s [-s query] <doc.t\n", progname); - exit(1); + eprintf("usage: %s [-s query] [-m model] <doc.t\n", progname);  }    int main(int argc, char *argv[])  { - int opt_s, opt_m; - char qfile[KB], mstr[KB]; + int opt_s, opt_m; + char qfile[KB], mstr[KB]; + TFile *tfc, *tfq; + TDoc *tdq; + Hash *ii, *res; + FILE *fpq, *fplog; + Model m; + Hit *hit; + Token t__; + Node *npqt, *npdt, n__; + + esetprogname(estrdup(argv[0])); + fplog = fopen(strcat(estrdup(argv[0]), "-error.log"), "w"); + esetstream(fplog);   + tfc = NULL;   opt_s = opt_m = 0; +   for (int i = 1; i < argc; i++) {   if (strcmp(argv[i], "-s") == 0) {   if (i + 1 <= argc - 1) { @@ -349,40 +266,12 @@
  usage(argv[0]);   }   } - - Hash *hdoc, *hterm, *hq; - FILE *fpq, *fplog; - Model m; - THeader *h; - - esetprogname(estrdup(argv[0])); - fplog = fopen(strcat(estrdup(argv[0]), "-error.log"), "w"); - esetstream(fplog); - - hdoc = newhash(NDOCS, cmpdoc, hashdoc); - hterm = newhash(NTERMS, cmpterm, hashterm); - hq = newhash(NHASH, cmpquery, hashquery); - - h = buildii(hdoc, hterm, stdin); - - /* exit(0); /\* DEBUG *\/ */ - - /* fprinthash(stdout, hterm, fprintterm); */ - /* hstats(hdoc, NDOCS); */ - - if (opt_s) { - fpq = fopen(qfile, "r"); - buildq(hq, fpq); - fclose(fpq); - /* fprinthash(stdout, hq, fprintquery); */ - }     /* test model nnn.nnn, default */ -   m.tf = &SMART_n_n_tf;   m.df = &SMART__n__df;   m.qtf = &SMART_n_n_tf; - m.qdf = &SMART__n__df; + m.qdf = &SMART__n__df;     if (opt_m) {   if (strcmp(mstr, "SMART_dnb_dtn") == 0) { @@ -410,24 +299,59 @@
  m.qdf = &SMART__n__df;   }   } - - for (int i = 0; i < hq->n; i++) { - if (hq->tab[i] == NULL) - continue; - /* fprintquery(stdout, hq->tab[i]->data); */ - /* fprintf(stdout, "\n"); */ - search(hq->tab[i]->data, hdoc, hterm, h, m); + + ii = buildii(&tfc, stdin); + + if (!opt_s) { + freehash(ii, free); + freeTFile(tfc); + return 0;   }   - freehash(hq, freequery); - freehash(hterm, freeterm); - freehash(hdoc, freedoc); - - /* fprintf(stderr, "\n"); */ - /* fprintf(stderr, "doctab[]\n"); */ - /* hstats(doctab, NDOCS); */ - /* fprintf(stderr, "hterm[]\n"); */ - /* hstats(hterm, NTERMS); */ + tfq = newTFile(); + tfq->hd->cmp = cmptoken_id; + tfq->hd->hash = hashtoken_id; + tfq->ht->cmp = cmptoken_id; + tfq->ht->hash = hashtoken_id; + + fpq = fopen(qfile, "r"); + receive(fpq, tfq); + fclose(fpq); + + /* search */ + for (Node *npq = tfq->list; npq != NULL; npq = npq->next) { + tdq = (TDoc *)(npq->data); + res = search(ii, tfc, tdq, m); + for (int i = 0; i < res->n; i++) { + for (Node *np = res->tab[i]; np != NULL; np = np->next) { + hit = (Hit *)(np->data); + /* fprintf(stdout, "%u %u %+1.8e\n", */ + /* tdq->h->id, hit->id, hit->n); */ + + t__.id = tdq->h->id; + n__.data = &t__; + npqt = hlookup(tfq->hd, &n__, 0); + if (npqt == NULL) + eprintf("query %u missing in tfq->hd\n", + tdq->h->id); + t__.id = hit->id; + n__.data = &t__; + npdt = hlookup(tfc->hd, &n__, 0); + if (npdt == NULL) + eprintf("doc %u missing in tfc->hd\n", + hit->id); + fprintf(stdout, "%s %s %+1.8e\n", + ((Token *)(npqt->data))->str, + ((Token *)(npdt->data))->str, + hit->n); + } + } + freehash(res, free); + } + + freehash(ii, free); + freeTFile(tfc); + freeTFile(tfq);     fclose(fplog);   return 0;
Change 1 of 3 Show Entire File parser.c Stacked
 
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
 
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
 
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
 
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
 
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
 
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
@@ -1,38 +1,137 @@
 #include <stdio.h>  #include <stdlib.h>  #include <string.h> -#include <stdint.h> -#include <malloc/malloc.h> +#include <math.h>  #include <kak/eprintf.h>  #include <kak/kcommon.h>  #include <kak/klist.h>  #include <kak/khash.h> +#include "stemmer.h"  #include "crc.h" -#include "stemmer.h" -#include "tokenizer.h"  #include "tfile.h"  #include "txt.h"  #include "parser.h"   +const uint8_t TERM = 0x01; +const uint8_t OTAG = 0x02; +const uint8_t CTAG = 0x03;  static char *str = "width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680 name=\"CRC-32/POSIX\"";  static char crc_s[128];  static model_t crc_m;   +/* Tokenizer */ + +void reset(Stack *stk, int n) +{ + stk->p = -1; + stk->n = n; +} + +void push(Stack *stk, uint8_t *c) +{ + int i; + if (stk->p == (stk->n - 1)) { + for (i = 0; i < stk->p; i++) + stk->s[i] = stk->s[i+1]; + stk->p--; + } + stk->s[++stk->p] = *c; +} + +char pop(Stack *stk) +{ + if (stk->p >= 0) { + stk->p--; + return stk->s[stk->p + 1]; + } + return 0; +} + +void printstack(Stack *stk) +{ + int i; + for (i = 0; i <= stk->p; i++) + printf("%d:%d ", i, stk->s[i]); + printf("\n"); +} + +Tokenizer *newtokenizer(char *delimiters, int casechange, Stack *mem) +{ + Tokenizer *t; + int i; + char *c; + t = (Tokenizer *)emalloc(sizeof(Tokenizer)); + strcpy(t->delimiters, delimiters); + memset(t->asciitab, SEPCHAR, sizeof(int) * ASCII); + /* ASCII chars 0 - 31, 127 and those from the delimiter string + * are marked as separators and the rest as printable + * chars. */ + t->asciitab[127] = SEPCHAR; + for (i = 0; i <= 31; i++) + t->asciitab[i] = SEPCHAR; + for (i = 32; i <= 126; i++) + t->asciitab[i] = PRINTCHAR; + for (c = t->delimiters; *c; c++) + t->asciitab[(int)(*c)] = SEPCHAR; + t->mem = mem; + t->casechange = casechange; + return t; +} + +/* TODO: what could be done for portability ? */ + +int gettoken(Token *tok, FILE *fp, Tokenizer *t) +{ + uint32_t l; + uint8_t c, *s, c_[2]; + + c = ' '; + l = 0; + tok->t = TERM; + s = tok->str; + *s = '\0'; + while (fread(&c, 1, 1, fp) != 0) { + if (t->asciitab[(uint32_t)c] == SEPCHAR) { + push(t->mem, &c); + if (l == 0) + continue; + *s = '\0'; + if (c == '>') { + pop(t->mem); + c_[1] = pop(t->mem); + c_[0] = pop(t->mem); + if (c_[1] == '<') + tok->t = OTAG; + else if (c_[1] == '/' && c_[0] == '<') + tok->t = CTAG; + } + tok->l = l; + return 1; + } + if (t->casechange == LOWERCASE && c >= 65 && c <= 90) + c += 32; + *s = c; + s++; + l++; + } + if (ferror(fp)) + eprintf("In gettoken(), fread() failed\n"); + return 0; +} + +/* Parser */ +  static void __initcrc()  {   int ret;   strcpy(crc_s, str);   crc_m.name = NULL;   ret = read_model(&crc_m, crc_s); - if (ret == 2) { - fprintf(stderr, "crc: out of memory -- aborting\n"); - exit(1); - } - else if (ret == 1) { - fprintf(stderr, "crc: %s: -- unusable model\n", + if (ret == 2) + eprintf("__initcrc() out of memory\n"); + else if (ret == 1) + eprintf("__initcrc() %s: unusable model\n",   crc_m.name == NULL ? "<no name>" : crc_m.name); - exit(1); - }   crc_table_wordwise(&crc_m);  }   @@ -42,62 +141,52 @@
  crc_m.name = NULL;  }   -Parser *newparser(E_TDocType type, int c, int n, int x) +Parser *newparser(char type, char **cw, int c, int n, int x_min, int x_max)  { - /*TODO: path hard-wired, set it elsewhere */ -   Node *npterm_;   Parser *p; - FILE *fp; - char s[KB]; - char *cfile = "test/common_words.txt";     p = (Parser *)emalloc(sizeof(Parser));   p->type = type;   - switch (type) { - case TREC: + if (type == 'd') {   p->n_tag = 0; - strcpy(p->endtag, "doc"); + strcpy(p->doctag, "doc");   strcpy(p->idtag, "docno"); - break; - case WARC: - fprintf(stderr, "A parser for WARC isn't available.\n"); - exit(1); - break; - case TRECQUERY: + } + else if (type == 'q') {   p->n_tag = 0; - strcpy(p->endtag, "top"); + strcpy(p->doctag, "top");   strcpy(p->idtag, "num"); - break; - default: - fprintf(stderr, "Unknown parser type\n"); - exit(1);   } + else + eprintf("newparser(): unknown parser\n");   - p->c = 0; p->hterm = NULL; + p->c = 0; p->hcw = NULL;   p->n = 0; p->stemmer = NULL; - p->x = 0; + p->x_min = MINTERMLEN; + p->x_max = MAXTERMLEN;     if (c) {   p->c = c; - p->hterm = newhash(NHASH, _strcmp, _strhash); - fp = fopen(cfile, "r"); - while (fgets(s, KB, fp)) { - if (s[strlen(s)-1] == '\n') - s[strlen(s)-1] = '\0'; - npterm_ = newnode(estrdup(s)); - hlookup(p->hterm, npterm_, 1); - /* assuming incoming terms are unique */ + p->hcw = newhash(NHASH, _strcmp, _strhash); + int ncw; + sscanf(cw[0], "%d", &ncw); + for (int i = 1; i <= ncw; i++) { + npterm_ = newnode(cw[i]); + hlookup(p->hcw, npterm_, 1); + /* assuming incoming common-words are unique; + * not checking for repeatations. */   } - fclose(fp);   }   if (n) {   p->n = n;   p->stemmer = create_stemmer();   } - if (x) - p->x = x; + if (x_min) + p->x_min = x_min; + if (x_max) + p->x_max = x_max;     return p;  } @@ -106,207 +195,438 @@
 {   if (p == NULL)   return; - freehash(p->hterm, free); + for (int i = 0; i < p->hcw->n; i++) + freelist(p->hcw->tab[i], NULL);   free_stemmer(p->stemmer);   free(p);  }   -/* TODO: - parse() assumes a TREC-type TFile - use sizeof() var in stead of type -*/ -TFile* parse(Parser *parser, FILE *fp) +void __print_doc_buf(uint8_t *buf, uint32_t b)  { + uint32_t i; + uint8_t *bp; + fprintf(stderr, "%s.\n", buf); + for (bp = buf+MAXTERMLEN+1, i = MAXTERMLEN+1; i < b; i++, bp++) { + if (*bp == '\0') + fprintf(stderr, " "); + else + fprintf(stderr, "%c", *bp); + } + fprintf(stderr, ".\n"); +} + +uint32_t getdoc(uint8_t **buf, Parser *parser, FILE *fp) +{ + /* Return byte-size of a buffer, whose first MAXTERMLEN+1 + * bytes contain the NULL-terminated document ID and the rest + * of it contain NULL-terminated term strings. */ + + uint32_t size, indoc, b;   Tokenizer *toktxt, *tokid; - Token *token; - Stack mem; - TFile *tfile; - TDoc *tdoc; - unsigned lowmem, b_read; - uint32_t crc, sumtf, n_txt, uterm, sumsqtf, maxtf; - int n, n_txtbuf, m, intact, n_header, n_subheader; + Token t;   char *septxt = " ;,.:`'\"?!(){}[]<>~^&*_-+=#$%@|\\/";   char *sepid = " <>"; - Hash *hpost; - Node *nppost, *nppost_, *npterm, node__; - Post *post, *post_; + Stack mem; + + reset(&mem, 3); + + toktxt = newtokenizer(septxt, LOWERCASE, &mem); + tokid = newtokenizer(sepid, KEEPCASE, &mem); + + size = MB; + b = MAXTERMLEN + 1; + *buf = (uint8_t *)emalloc(size); + + t.str = *buf + b; + t.l = 0; + t.id = 0; + t.t = 0x00; + + indoc = 0; + + while(gettoken(&t, fp, toktxt) == 1) { + if ((t.t == OTAG) && (strcmp((char *)t.str, parser->doctag) == 0)) { + indoc = 1; + continue; + } + if (!indoc) + continue; + if ((t.t == OTAG) && (strcmp((char *)t.str, parser->idtag) == 0)) { + gettoken(&t, fp, tokid); + memcpy(*buf, t.str, t.l + 1); + continue; + } + if (t.t == TERM) { + if (size < (b + t.l + 1)) { + while ((size <<= 1) < (b + t.l + 1)) + ; + *buf = erealloc(*buf, size); + t.str = *buf + b; + } + t.str += t.l + 1; + /* *(t.str)++ = ' '; */ + b += t.l + 1; + continue; + } + if ((t.t == CTAG) && (strcmp((char *)t.str, parser->doctag) == 0)) { + /* *(t.str) = '\0'; */ + /* b++; */ + return b; + } + } + free(*buf); + return 0; +} + +TFile *parse(Parser *parser, FILE *fp) +{ + uint32_t b, tid, did, i, l, size, stemlen, e, crc; + uint8_t *buf, s[MAXTERMLEN+1], *bp, docid[MAXTERMLEN+1]; + TFile *tfile; + TDoc *tdoc; + Token *t_, t__; + Node n__, *npt_, *npt, *nppost, *nppost_; + Post *post, *post_; + double logtf; + Hash *hp; + + t__.str = s; + t__.l = 0; + t__.t = 0x00; + t__.id = 0; + + tid = 1; + did = 1; + e = 0; + size = MB;     __initcrc(); - reset(&mem, 3); - toktxt = newtokenizer(septxt, LOWERCASE, &mem); - tokid = newtokenizer(sepid, KEEPCASE, &mem); - hpost = newhash(NHASH, cmppost, hashpost); - token = newtoken(NULL, KB); - lowmem = KB; - n_txt = sumtf = uterm = sumsqtf = maxtf = b_read = 0; - tfile = newTFile(TREC); - tdoc = newTDoc(TREC); - n_txtbuf = TXTBUFSIZE; - n_header = sizeof(THeader); - n_subheader = sizeof(TSubHeader) - (MAX_RSRC - TREC_RSRC) * sizeof(uint32_t); - intact = 1; - - while((n = gettoken(token, fp, toktxt)) > 0) { + tfile = newTFile(); + + while ((b = getdoc(&buf, parser, fp)) != 0) { + + tdoc = newTDoc(); + hp = newhash(NHASH, cmppost, hashpost);   - b_read += n; + /* process the doc id: hash the docid string and + * assign a id */   - /* at end of a unit of document */ - if ((token->type == CTAG) && (strcmp(token->str, parser->endtag) == 0)) { + strcpy((char *)docid, (char *)buf); + t_ = newtoken(docid, strlen((char *)docid), 0x00, 0); + npt_ = newnode(t_); + npt = hlookup(tfile->hd, npt_, 1); + if (npt == npt_) { /* new doc, update headers */ + ((Token *)(npt->data))->id = did++; + tdoc->h->id = ((Token *)(npt->data))->id; + tfile->h->bd += ((Token *)(npt->data))->l + 1 + 4; + } + else + freenode(npt_, freetoken); + + /* process the tokens */ + + l = 0;   - /* at this point, tdoc->id & tdoc->h->n_id has - * been filled already */ + for(bp = buf+MAXTERMLEN+1, i = MAXTERMLEN+1; i < b; i++, bp++) {   - if (tdoc->h->n_id <= 0) - intact = 0; + if (*bp == '\0') { + + t__.str[l] = *bp; + t__.l = l; + l = 0;   - /* compute resources, fill tdoc->txt and empty - * the hash */ + /* drop a short or long token */ + if (t__.l < parser->x_min || t__.l > parser->x_max) + continue; + + /* drop too common a token */ + if (parser->c == 1) { + n__.data = t__.str; + npt = hlookup(parser->hcw, &n__, 0); + if (npt != NULL) + continue; + } + + /* normalize the token */ + if (parser->n == 1) { + stemlen = stem(parser->stemmer, (char *)t__.str, t__.l - 1); + t__.str[stemlen + 1] = '\0'; + t__.l = stemlen + 1; + } + + /* compute document's byte-length */ + tdoc->h->sumb += t__.l;   - n_txt = sumtf = uterm = sumsqtf = maxtf = 0; + /* hash the token and assign a id */ + t_ = newtoken(t__.str, t__.l, t__.t, 0); + npt_ = newnode(t_); + npt = hlookup(tfile->ht, npt_, 1); + if (npt == npt_) {/* new term */ + ((Token *)(npt->data))->id = tid++; + tfile->h->nt++; + tfile->h->bt += ((Token *)(npt->data))->l + 1 + 4; + } + else + freenode(npt_, freetoken); + + /* build Post, hash it to sum the tf */ + post_ = newpost(((Token *)(npt->data))->id, 1); + nppost_ = newnode(post_); + nppost = hlookup(hp, nppost_, 1); + if (nppost != nppost_) { /* repeating token */ + ((Post *)(nppost->data))->tf++; + freenode(nppost_, free); + }   - for (int i = 0; i < hpost->n; i++) { - for (Node *np = hpost->tab[i]; np != NULL; np = np->next) { - post = (Post *)(np->data); - m = sprintf(&(tdoc->txt[n_txt]), "%s %u ", - post->id, post->tf); - n_txt += m; - uterm++; - sumtf += post->tf; - sumsqtf += post->tf * post->tf; - if (post->tf > maxtf) - maxtf = post->tf; - if (n_txtbuf - n_txt < lowmem) - tdoc->txt = erealloc(tdoc->txt, n_txtbuf <<= 1); + continue; + } + + t__.str[l++] = *bp; + } + + /* empty the Posts hash 'hp' into TDoc's payload and + * header */ + + for (int j = 0; j < hp->n; j++) { + for (Node *np = hp->tab[j]; np != NULL; np = np->next) { + post = (Post *)(np->data); + tdoc->txt[tdoc->h->sumu] = post->id; + tdoc->tf[tdoc->h->sumu] = post->tf; + tdoc->h->sumu++; + tdoc->h->sumtf += post->tf; + if (post->tf > tdoc->h->maxtf) + tdoc->h->maxtf = post->tf; + tdoc->h->sumsqtf = post->tf * post->tf; + logtf = log((double)(post->tf)); + tdoc->h->sumsqlogtf += logtf * logtf; + if (size < (tdoc->h->sumu * 4)) { + while ((size <<= 1) < (tdoc->h->sumu * 4)) + ; + tdoc->txt = erealloc(tdoc->txt, size); + tdoc->tf = erealloc(tdoc->tf, size);   }   } - freehash(hpost, freepost); - hpost = newhash(NHASH, cmppost, hashpost); + }   - /* fill tdoc->h->n_txt */ - tdoc->h->n_txt = n_txt; + freehash(hp, free);   - if (tdoc->h->n_txt <= 0) - intact = 0; - - /* fill TDoc->rsrc[i] */ - - tdoc->h->n_rsrc[0] = sprintf(tdoc->rsrc[0], "%u", sumtf); - if (tdoc->h->n_rsrc[0] == -1) - tdoc->h->n_rsrc[0] = 0; - - tdoc->h->n_rsrc[1] = sprintf(tdoc->rsrc[1], "%u", uterm); - if (tdoc->h->n_rsrc[1] == -1) - tdoc->h->n_rsrc[1] = 0; - - tdoc->h->n_rsrc[2] = sprintf(tdoc->rsrc[2], "%u", sumsqtf); - if (tdoc->h->n_rsrc[2] == -1) - tdoc->h->n_rsrc[2] = 0; - - tdoc->h->n_rsrc[3] = sprintf(tdoc->rsrc[3], "%u", maxtf); - if (tdoc->h->n_rsrc[3] == -1) - tdoc->h->n_rsrc[3] = 0; - - /* fill remaining parts of TDoc->TSubHeader */ - crc = crc_wordwise(&crc_m, 0, NULL, 0); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)tdoc->txt, - tdoc->h->n_txt); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)tdoc->id, - tdoc->h->n_id); - for (int i = 0; i < tfile->h->r; i++) - crc = crc_wordwise(&crc_m, crc, - (unsigned char *)tdoc->rsrc[i], - tdoc->h->n_rsrc[i]); - tdoc->h->crc = crc; - - /* if the incoming document is intact attach - * the TDoc to TFile->list */ - - if (intact) { - tfile->list = addfront(tfile->list, newnode((void *)tdoc)); - - /* update TFile->THeader */ - tfile->h->n++; - tfile->h->sumnb += n_txt; - tfile->h->sumnu += uterm; - tfile->h->sumnt += sumtf; - tfile->h->b += n_subheader + tdoc->h->n_txt + tdoc->h->n_id; - for (int i = 0; i < tfile->h->r; i++) - tfile->h->b += tdoc->h->n_rsrc[i]; - } - else - freeTDoc(tdoc, parser->type); - + /* skip a broken document */ + if (tdoc->h->id == 0 || tdoc->h->sumu == 0) {   /* reset */ - intact = 1; - tdoc = newTDoc(TREC); - n_txtbuf = TXTBUFSIZE; - - fprintf(stderr, "\rparsed: %d", tfile->h->n); - + e++; + weprintf("broken doc %d: %s, id = %d, sumu = %d", + e, docid, tdoc->h->id, tdoc->h->sumu); + freeTDoc(tdoc);   continue;   } - if ((token->type == OTAG) && (strcmp(token->str, parser->idtag) == 0)) { - /* fill TDoc->id, update TDoc->TSubHeader */ - /* assuming that the next token is the id */ - n = gettoken(token, fp, tokid); - b_read += n; - strcpy(tdoc->id, token->str); - tdoc->h->n_id = token->l + 1; + + /* complete TDocHeader */ + crc = crc_wordwise(&crc_m, 0, NULL, 0); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tdoc->h, BTDOCHEADER); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tdoc->txt, tdoc->h->sumu * 4); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tdoc->tf, tdoc->h->sumu * 4); + tdoc->h->crc = crc; + + /* Attach TDoc to TFile->list */ + tfile->list = addfront(tfile->list, newnode((void *)tdoc)); + + /* update THeader */ + tfile->h->nd++; + tfile->h->b += BTDOCHEADER + 2 * tdoc->h->sumu * 4; + tfile->h->sumb += tdoc->h->sumb; + tfile->h->sumu += tdoc->h->sumu; + tfile->h->sumtf += tdoc->h->sumtf; + + fprintf(stderr, "\rdocs parsed %d", tfile->h->nd); + } + + fprintf(stderr, " skipped %d\n", e); + + tfile->h->b += BTHEADER + tfile->h->bt + tfile->h->bd; + + crc = crc_wordwise(&crc_m, 0, NULL, 0); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tfile->h, BTHEADER); + tfile->h->crc = crc; + + __deinitcrc(); + + return tfile; +} + +TFile *parseq(THeader *h, Hash *hv, Parser *parser, FILE *fp) +{ + uint32_t b, tid, did, i, l, size, stemlen, e, crc; + uint8_t *buf, s[MAXTERMLEN+1], *bp, docid[MAXTERMLEN+1]; + TFile *tfile; + TDoc *tdoc; + Token *t_, t__; + Node n__, *npt_, *npt, *nppost, *nppost_; + Post *post, *post_; + double logtf; + Hash *hp; + + t__.str = s; + t__.l = 0; + t__.t = 0x00; + t__.id = 0; + + tid = 1; + did = 1; + e = 0; + size = MB; + + __initcrc(); + + tfile = newTFile(); + + /* copy the global stats of the given vocabulary block and patch the + * new TFile pointer to point to this vocabulary */ + + tfile->h->nt = h->nt; + tfile->h->bt = h->bt; + freehash(tfile->ht, freetoken); + tfile->ht = hv; + + while ((b = getdoc(&buf, parser, fp)) != 0) { + + tdoc = newTDoc(); + hp = newhash(NHASH, cmppost, hashpost); + + /* process the doc id: hash the docid string and + * assign a id */ + + strcpy((char *)docid, (char *)buf); + t_ = newtoken(docid, strlen((char *)docid), 0x00, 0); + npt_ = newnode(t_); + npt = hlookup(tfile->hd, npt_, 1); + if (npt == npt_) { /* new doc, update headers */ + ((Token *)(npt->data))->id = did++; + tdoc->h->id = ((Token *)(npt->data))->id; + tfile->h->bd += ((Token *)(npt->data))->l + 1 + 4; + } + else + freenode(npt_, freetoken); + + /* process the tokens */ + + l = 0; + + for(bp = buf+MAXTERMLEN+1, i = MAXTERMLEN+1; i < b; i++, bp++) { + + if (*bp == '\0') { + + t__.str[l] = *bp; + t__.l = l; + l = 0; + + /* drop a short or long token */ + if (t__.l < parser->x_min || t__.l > parser->x_max) + continue; + + /* drop too common a token */ + if (parser->c == 1) { + n__.data = t__.str; + npt = hlookup(parser->hcw, &n__, 0); + if (npt != NULL) + continue; + } + + /* normalize the token */ + if (parser->n == 1) { + stemlen = stem(parser->stemmer, (char *)t__.str, t__.l - 1); + t__.str[stemlen + 1] = '\0'; + t__.l = stemlen + 1; + } + + /* compute document's byte-length */ + tdoc->h->sumb += t__.l; + + /* lookup the token and ignore it if + * it is not found */ + n__.data = &t__; + if ((npt = hlookup(tfile->ht, &n__, 0)) == NULL) + continue; + + /* build Post, hash it to sum the tf */ + post_ = newpost(((Token *)(npt->data))->id, 1); + nppost_ = newnode(post_); + nppost = hlookup(hp, nppost_, 1); + if (nppost != nppost_) { /* repeating token */ + ((Post *)(nppost->data))->tf++; + freenode(nppost_, free); + } + + continue; + } + + t__.str[l++] = *bp; + } + + /* empty the Posts hash 'hp' into TDoc's payload and + * header */ + + for (int j = 0; j < hp->n; j++) { + for (Node *np = hp->tab[j]; np != NULL; np = np->next) { + post = (Post *)(np->data); + tdoc->txt[tdoc->h->sumu] = post->id; + tdoc->tf[tdoc->h->sumu] = post->tf; + tdoc->h->sumu++; + tdoc->h->sumtf += post->tf; + if (post->tf > tdoc->h->maxtf) + tdoc->h->maxtf = post->tf; + tdoc->h->sumsqtf = post->tf * post->tf; + logtf = log((double)(post->tf)); + tdoc->h->sumsqlogtf += logtf * logtf; + if (size < (tdoc->h->sumu * 4)) { + while ((size <<= 1) < (tdoc->h->sumu * 4)) + ; + tdoc->txt = erealloc(tdoc->txt, size); + tdoc->tf = erealloc(tdoc->tf, size); + } + } + } + + freehash(hp, free); + + /* skip a broken document */ + if (tdoc->h->id == 0 || tdoc->h->sumu == 0) { + /* reset */ + e++; + weprintf("broken doc %d: %s, id = %d, sumu = %d", + e, docid, tdoc->h->id, tdoc->h->sumu); + freeTDoc(tdoc);   continue;   } - if (token->type == TERM) { /* place the term in a hash */ + + /* complete TDocHeader */ + crc = crc_wordwise(&crc_m, 0, NULL, 0); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tdoc->h, BTDOCHEADER); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tdoc->txt, tdoc->h->sumu * 4); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tdoc->tf, tdoc->h->sumu * 4); + tdoc->h->crc = crc; + + /* Attach TDoc to TFile->list */ + tfile->list = addfront(tfile->list, newnode((void *)tdoc));   - if (token->l <= parser->x) /* drop a short token */ - continue; - if (parser->c == 1) { /* drop a too-common token */ - node__.data = token->str; - npterm = hlookup(parser->hterm, &node__, 0); - if (npterm != NULL) - continue; - } - if (parser->n == 1) { /* normalize a token */ - int res = stem(parser->stemmer, token->str, token->l - 1); - token->str[res + 1] = '\0'; - token->l = res + 1; - } + /* update THeader */ + tfile->h->nd++; + tfile->h->b += BTDOCHEADER + 2 * tdoc->h->sumu * 4; + tfile->h->sumb += tdoc->h->sumb; + tfile->h->sumu += tdoc->h->sumu; + tfile->h->sumtf += tdoc->h->sumtf;   - post_ = newpost(token->str, 1); - nppost_ = newnode(post_); - nppost = hlookup(hpost, nppost_, 1); - if (nppost != nppost_) { /* repeating token */ - ((Post *)(nppost->data))->tf++; - freenode(nppost_, freepost); - } - } + fprintf(stderr, "\rdocs parsed %d", tfile->h->nd);   } + + fprintf(stderr, " skipped %d\n", e);   - fprintf(stderr, "\n"); - - b_read++; - - tfile->h->b += n_header; + tfile->h->b += BTHEADER + tfile->h->bt + tfile->h->bd;     crc = crc_wordwise(&crc_m, 0, NULL, 0); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->ver, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->type, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->b, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->n, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->sumnb, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->sumnu, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->sumnt, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->r, - sizeof(uint32_t)); - crc = crc_wordwise(&crc_m, crc, (unsigned char *)&tfile->h->bo, - sizeof(uint32_t)); + crc = crc_wordwise(&crc_m, crc, (uint8_t *)tfile->h, BTHEADER); + tfile->h->crc = crc;   - tfile->h->crc = crc;   __deinitcrc(); +   return tfile;  }
Change 1 of 1 Show Entire File parser.h Stacked
 
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
 
 
 
 
 
 
 
 
 
 
 
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
@@ -1,27 +1,43 @@
-/* #include <kak/kcommon.h> */ -/* #include <kak/khash.h> */ -/* #include "stemmer.h" */ - -#define BUFSIZE 10240 -#define NHASH 4093 - +typedef struct Stack Stack; +typedef struct Tokenizer Tokenizer;  typedef struct Parser Parser; -typedef struct Hash Hash;  typedef struct stemmer Stemmer;   +struct Stack { + uint8_t s[KB]; + int p; + int n; +}; + +struct Tokenizer { + char delimiters[ASCII]; + int asciitab[ASCII]; + Stack *mem; + int casechange; +}; +  struct Parser { - E_TDocType type; + char type;   int c;   int n; - int x; - Hash *hterm; + int x_min; + int x_max; + Hash *hcw; /* hash of common words */   Stemmer *stemmer;   int n_tag; - char endtag[64]; + char doctag[64];   char idtag[64];   char *tag[64];  };   -Parser *newparser(E_TDocType, int, int, int); +void reset(Stack*, int); +void push(Stack*, uint8_t*); +char pop(Stack*); +void printstack(Stack*); +Tokenizer *newtokenizer(char*, int, Stack*); +int gettoken(Token*, FILE*, Tokenizer*); +Parser *newparser(char, char**, int, int, int, int);  void freeparser(Parser*); -TFile* parse(Parser*, FILE*); +TFile *parse(Parser*, FILE*); +TFile *parseq(THeader*, Hash*, Parser*, FILE*); +uint32_t getdoc(uint8_t **buf, Parser *parser, FILE *fp);
Change 1 of 3 Show Entire File raw2t.c Stacked
 
1
2
3
4
5
6
 
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
 
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
 
1
 
 
2
3
4
 
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
 
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
@@ -1,6 +1,4 @@
 /* - Usage: raw2t <[in] >[out] -   Tokenizes all content within the <DOC> tags of a TREC document,   excluding the markup tags, and the document-id.   @@ -19,33 +17,31 @@
 #include <kak/khash.h>  #include "txt.h"  #include "tfile.h" +#include "parser.h"  #include "cw.h"    void usage(char *progname)  { - eprintf("usage: %s [-c] [-n] [-x] [-q] name\n", progname); + eprintf("usage: %s [-c] [-n] [-x] [-q corpus.t] <in.txt >out.t\n", progname);  }    int main(int argc, char *argv[])  { - FILE *fplog, *fpt, *fpd; - TFile *tfile; + FILE *fplog, *vfp; + TFile *tfile, *tfile_; + Hash *hv;   Parser *parser; - Hash *ht, *hd; - char type, name[11]; - int c, n, x_min, x_max, q; - - if (argc < 2) - usage(argv[0]); + char vfile[KB]; + int c, n, x_min, x_max, q, size, bpack; + uint8_t *buf;     esetprogname(estrdup(argv[0]));   fplog = fopen(strcat(estrdup(argv[0]), "-error.log"), "w");   esetstream(fplog);   - name[0] = '\0'; name[10] = '\0'; - - c = n = x_min = x_max = q = 0; - + c = n = x_min = x_max = q = bpack = 0; + size = MB; +   for (int i = 1; i < argc; i++) {   if (strcmp(argv[i], "-c") == 0)   c = 1; @@ -55,37 +51,62 @@
  x_min = 4;   x_max = 20;   } - else if (strcmp(argv[i], "-q") == 0) - q = 1; - else - strncpy(name, argv[i], 10); + else if (strcmp(argv[i], "-q") == 0) { + if (i + 1 <= argc - 1) { + strcpy(vfile, argv[++i]); + q = 1; + } + else + usage(argv[0]); + } + else { + /* process non-optional arguments here*/ + }   }   - type = 'd'; - if (q) - type = 'q'; + if (q) { + parser = newparser('q', cwSMART, c, n, x_min, x_max); + tfile_ = newTFile(); + hv = newhash(NHASHT, cmptoken_str, hashtoken_str); + buf = (uint8_t *)emalloc(size); + vfp = fopen(vfile, "r");   - ht = newhash(NHASHT, cmptoken_str, hashtoken_str); - hd = newhash(NHASHD, cmptoken_str, hashtoken_str); - parser = newparser(type, cwSMART, c, n, x_min, x_max); + + if ((n = fread(buf, BTHEADER, 1, vfp)) != 1) + eprintf("fread() failed on %d byte THeader\n", + BTHEADER); + if (unpackTHeader(tfile_->h, BTHEADER, buf) != BTHEADER) + eprintf("unpackTHeader() failed on %d byte THeader\n", + BTHEADER);   - tfile = parse(ht, hd, parser, stdin); + bpack = tfile_->h->bt > tfile_->h->bd ? tfile_->h->bt : tfile_->h->bd; + + if (size < bpack) { + while ((size <<= 1) < bpack); + buf = erealloc(buf, size); + } + + if ((n = fread(buf, tfile_->h->bt, 1, vfp)) != 1) + eprintf("fread() failed on %d byte tfile->h->bt\n", + tfile_->h->bt); + if (unpack_idmap(hv, tfile_->h->bt, buf) != tfile_->h->bt) + eprintf("unpack_idmap() failed on %d byte tfile->h->bt\n", + tfile_->h->bt);   - send(stdout, tfile); - fflush(stdout); + fclose(vfp); + free(buf); + + tfile = parseq(tfile_->h, hv, parser, stdin); + + freeTFile(tfile_); + } + else { + parser = newparser('d', cwSMART, c, n, x_min, x_max); + tfile = parse(parser, stdin); + } + + send(stdout, tfile); fflush(stdout);   freeTFile(tfile); - - /* write out {token, id} pairs */ - fpt = fopen(strcat(estrdup(name), "_vocab.txt"), "w"); - walkhash(ht, writetoken, fpt); - fflush(fpt); fclose(fpt); - freehash(ht, freetoken); - - /* write out {docid, id} pairs*/ - fpd = fopen(strcat(estrdup(name), "_docid.txt"), "w"); - walkhash(hd, writetoken, fpd); - fflush(fpd); fclose(fpd); - freehash(hd, freetoken);     fclose(fplog);  
Change 1 of 1 Show Entire File t2mem.c Stacked
 
8
9
10
 
 
11
12
13
14
 
 
15
16
17
18
19
20
 
 
 
21
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
24
25
 
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
@@ -8,18 +8,43 @@
 #include "txt.h"  #include "tfile.h"   +/* TODO: pass a optional parameter to name the vocab and docid + * files? */  int main(int argc, char *argv[])  {   TFile *tfile; - FILE *fplog; + FILE *fplog, *fpv, *fpd; + Token *t;     esetprogname(estrdup(argv[0]));   fplog = fopen(strcat(estrdup(argv[0]), "-error.log"), "w");   esetstream(fplog);   - tfile = receive(stdin); + tfile = newTFile(); + + receive(stdin, tfile);   printTFile(tfile, stdout);   + fpv = fopen("vocab.txt", "w"); + for (int i = 0; i < tfile->ht->n; i++) { + for (Node *np = tfile->ht->tab[i]; np != NULL; np = np->next) { + t = (Token *)(np->data); + fprintf(fpv, "%s %u\n", t->str, t->id); + } + } + fflush(fpv); fclose(fpv); + + fpd = fopen("docid.txt", "w"); + for (int i = 0; i < tfile->hd->n; i++) { + for (Node *np = tfile->hd->tab[i]; np != NULL; np = np->next) { + t = (Token *)(np->data); + fprintf(fpd, "%s %u\n", t->str, t->id); + } + } + fflush(fpd); fclose(fpd); + + freeTFile(tfile); +   fclose(fplog);   return 0;  }
Change 1 of 1 Show Entire File test/​alice.mem Stacked
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@@ -1,26 +1,14 @@
-MG CRC V T BYTES N SUMNB SUMNU SUMNT R BO -41 136804978 1 0 562 3 407 52 57 4 44 -CRC TXT ID RSRC0 RSRC1 RSRC2 RSRC3 -134067299 159 2 2 2 2 1 -tdoc->id: A. -tdoc->txt: rude 1 open 1 curios 1 cut 1 alic 2 learn 1 hear 1 desk 1 remark 1 great 1 hair 1 sever 1 wide 1 hatter 2 speech 1 write 1 ey 1 person 1 time 1 raven 1 make 1 . -tdoc->rsrc[0]: 23. -tdoc->rsrc[1]: 21. -tdoc->rsrc[2]: 27. -tdoc->rsrc[3]: 2. -CRC TXT ID RSRC0 RSRC1 RSRC2 RSRC3 -4054011989 153 2 2 2 2 1 -tdoc->id: B. -tdoc->txt: adventur 1 issu 1 wrong 2 alic 1 1897 1 nevar 1 front 2 carrol 1 propos 1 final 1 flat 1 note 1 earli 1 spell 1 lewi 1 answer 1 produc 1 raven 1 revis 2 . -tdoc->rsrc[0]: 22. -tdoc->rsrc[1]: 19. -tdoc->rsrc[2]: 28. -tdoc->rsrc[3]: 2. -CRC TXT ID RSRC0 RSRC1 RSRC2 RSRC3 -2606170364 95 2 2 2 2 1 -tdoc->id: C. -tdoc->txt: alic 1 dip 1 gave 1 1990 1 martin 1 gardner 1 flap 1 answer 1 quill 1 reader 1 annot 1 slope 1 . -tdoc->rsrc[0]: 12. -tdoc->rsrc[1]: 12. -tdoc->rsrc[2]: 12. -tdoc->rsrc[3]: 1. +T BO CRC B NT BT ND BD SUMB SUMU SUMTF +1 38 1090320213 1082 48 472 3 18 276 52 57 +T BO CRC ID SUMB SUMU SUMTF MAXTF SUMSQTF SUMSQLOGTF +2 38 933851374 1 106 21 23 2 1 +9.60906028e-01 +ID 1 +1:1 2:1 3:2 4:2 5:1 6:1 7:1 8:1 9:1 10:1 11:1 12:1 13:1 14:1 15:1 16:1 17:1 18:1 19:1 20:1 21:1 +T BO CRC ID SUMB SUMU SUMTF MAXTF SUMSQTF SUMSQLOGTF +2 38 1284414819 2 111 19 22 2 1 +1.44135904e+00 +ID 2 +4:1 19:1 22:1 23:1 24:1 25:1 26:1 27:1 28:2 29:1 30:1 31:1 32:1 33:2 34:2 35:1 36:1 37:1 38:1 +T BO CRC ID SUMB SUMU SUMTF MAXTF SUMSQTF SUMSQLOGTF +2 38 3033863290 3 59 12 12 1 1 +0.00000000e+00 +ID 3 +4:1 25:1 39:1 40:1 41:1 42:1 43:1 44:1 45:1 46:1 47:1 48:1
Change 1 of 1 Show Entire File test/​alice.res Stacked
 
1
2
3
4
5
6
7
8
9
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
@@ -1,12 +1,12 @@
-1 A 7.000000 -1 B 3.000000 -1 C 1.000000 -2 A 2.000000 -2 B 3.000000 -2 C 1.000000 -3 A 6.000000 -3 B 2.000000 -3 C 2.000000 -4 A 2.000000 -4 B 1.000000 -4 C 4.000000 +1 A +7.00000000e+00 +1 B +3.00000000e+00 +1 C +1.00000000e+00 +2 A +2.00000000e+00 +2 B +3.00000000e+00 +2 C +1.00000000e+00 +3 A +6.00000000e+00 +3 B +2.00000000e+00 +3 C +2.00000000e+00 +4 A +2.00000000e+00 +4 B +1.00000000e+00 +4 C +4.00000000e+00
 
 
Show Entire File test/​ap.mem Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File test/​ap.res Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File test/​ap.run Stacked
This file's diff was not loaded because this changeset is very large. Load changes
 
 
Change 1 of 11 Show Entire File tfile.c Stacked
 
19
20
21
22
 
 
 
 
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
39
40
41
42
43
44
45
46
 
50
51
52
53
 
54
55
 
 
 
 
 
 
56
57
58
 
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
 
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
 
182
183
184
185
 
 
 
186
187
188
189
190
191
192
193
194
 
 
 
 
 
 
 
 
 
195
196
197
 
229
230
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
233
234
 
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
 
281
282
283
284
 
285
286
287
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
290
291
 
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
 
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
 
19
20
21
 
22
23
24
25
26
27
28
29
30
31
 
 
 
 
 
32
33
34
 
37
38
39
 
 
40
41
42
 
46
47
48
 
49
50
 
51
52
53
54
55
56
57
58
59
 
74
75
76
 
77
78
79
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
82
83
84
 
85
86
87
88
89
90
91
 
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
 
165
166
167
 
168
169
170
171
 
 
 
 
 
 
 
 
172
173
174
175
176
177
178
179
180
181
182
183
 
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
 
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
 
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
 
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
 
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
@@ -19,18 +19,16 @@
  h->bo = THEADER;   h->crc = 0;   h->b = 0; - h->n = 0; + h->nt = 0; + h->nd = 0; + h->bt = 0; + h->bd = 0;   h->sumb = 0;   h->sumu = 0;   h->sumtf = 0;   return h;  }   -void _freeTHeader(void *d) -{ - free((THeader *)d); -} -  TDocHeader *_newTDocHeader(void)  {   TDocHeader *h; @@ -39,8 +37,6 @@
  h->bo = TDOCHEADER;   h->crc = 0;   h->id = 0; - h->b_txt = 0; - h->b_tf = 0;   h->sumb = 0;   h->sumu = 0;   h->sumtf = 0; @@ -50,9 +46,14 @@
  return h;  }   -void _freeTDocHeader(void *d) +int cmpdoc(void *d1, void *d2)  { - free((TDocHeader *)d); + return ((TDocHeader *)d1)->id - ((TDocHeader *)d2)->id; +} + +uint32_t hashdoc(void *data, uint32_t hsize) +{ + return _inthash(&(((TDocHeader *)data)->id), hsize);  }    TDoc *newTDoc(void) @@ -73,45 +74,18 @@
  tdoc = (TDoc *)d;   free(tdoc->tf);   free(tdoc->txt); - _freeTDocHeader(tdoc->h); + free(tdoc->h);   free(tdoc);  }   -/* updates TDoc using Post data */ -void updateTDoc(void *data, void *arg) -{ - static uint32_t size = sizeof(uint32_t) * NTERM; - double logtf; - Post *post; - TDoc *tdoc; - - post = (Post *)data; - tdoc = (TDoc *)arg; - - tdoc->h->b_txt += sizeof(uint32_t); - tdoc->h->b_tf += sizeof(uint32_t); - if (size - tdoc->h->b_txt < sizeof(uint32_t)) { - size <<= 1; - tdoc->txt = erealloc(tdoc->txt, size); - tdoc->tf = erealloc(tdoc->tf, size); - - } - tdoc->txt[tdoc->h->sumu] = post->id; - tdoc->tf[tdoc->h->sumu] = post->tf; - tdoc->h->sumu++; - tdoc->h->sumtf += post->tf; - if (post->tf > tdoc->h->maxtf) - tdoc->h->maxtf = post->tf; - tdoc->h->sumsqtf = post->tf * post->tf; - logtf = log((double)(post->tf)); - tdoc->h->sumsqlogtf += logtf * logtf; -} -  TFile *newTFile(void)  {   TFile *t;   t = (TFile *)emalloc(sizeof(TFile)); - t->h = _newTHeader(); + t->h = _newTHeader(); + t->ht = newhash(NHASHT, cmptoken_str, hashtoken_str); + t->hd = newhash(NHASHD, cmptoken_str, hashtoken_str); + t->hdh = NULL;   t->list = NULL;   return t;  } @@ -120,49 +94,58 @@
 {   if (d == NULL)   return; - TFile *tfile; + TFile *t;   Node *np, *next; - tfile = (TFile *)d; - np = tfile->list; + t = (TFile *)d; + + np = t->list;   for (; np != NULL; np = next) {   next = np->next;   freeTDoc(np->data);   free(np);   } - _freeTHeader(tfile->h); - free(tfile); + + freehash(t->hd, freetoken); + freehash(t->ht, freetoken); + freehash(t->hdh, free); + free(t->h); + free(t);  }    void _printTHeader(THeader *h, FILE *stream)  {   fprintf(stream, - "%-2s %-2s %-10s %-10s %-10s %-10s %-10s %-10s\n", - "T", "BO", "CRC", "B", "N", + "%-2s %-2s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", + "T", "BO", "CRC", "B", + "NT", "BT", "ND", "BD",   "SUMB", "SUMU", "SUMTF");   fprintf(stream, - "%-2u %-2u %-10u %-10u %-10u %-10u %-10u %-10u\n", - h->type, h->bo, h->crc, h->b, h->n, + "%-2u %-2u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u\n", + h->type, h->bo, h->crc, h->b, + h->nt, h->bt, h->nd, h->bd,   h->sumb, h->sumu, h->sumtf);  }    void _printTDocHeader(TDocHeader *h, FILE *stream)  {   fprintf(stream, - "%-2s %-2s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-15s\n", - "T", "BO", "CRC", "ID", "B_TXT", "B_TF", - "SUMB", "SUMU", "SUMTF", "MAXTF", "SUMSQTF", "SUMSQLOGTF"); + "%-2s %-2s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-16s\n", + "T", "BO", "CRC", "ID", + "SUMB", "SUMU", "SUMTF", "MAXTF", + "SUMSQTF", "SUMSQLOGTF");   fprintf(stream, - "%-2u %-2u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %+1.8le\n", - h->type, h->bo, h->crc, h->id, h->b_txt, h->b_tf, - h->sumb, h->sumu, h->sumtf, h->maxtf, h->sumsqtf, h->sumsqlogtf); + "%-2u %-2u %-10u %-10u %-10u %-10u %-10u %-10u %-10u %+1.8le\n", + h->type, h->bo, h->crc, h->id, + h->sumb, h->sumu, h->sumtf, h->maxtf, + h->sumsqtf, h->sumsqlogtf);  }    void _printTDoc(TDoc *tdoc, FILE *stream)  { - char tid[11], tf[11]; /* max 10 digits in uint32_t */ + char tid[11], tf[11]; /* max 10 digits uint32_t */   _printTDocHeader(tdoc->h, stream);   fprintf(stream, "ID %u\n", tdoc->h->id); - for (int i = 0; i < tdoc->h->b_txt / sizeof(uint32_t); i++) { + for (int i = 0; i < tdoc->h->sumu; i++) {   sprintf(tid, "%u", tdoc->txt[i]);   sprintf(tf, "%u", tdoc->tf[i]);   fprintf(stream, "%s:%s ", tid, tf); @@ -182,16 +165,19 @@
  }  }   -int _pack(uchar *buf, char *fmt, ...) +/* TODO: check if fmt length and parameter count matches */ + +int _pack(uint8_t *buf, char *fmt, ...)  { - va_list args; - char *p; - uchar *bp; - uint16_t s; - uint32_t i; - uint64_t l; - double d; - char fstr[BFLOAT]; + va_list args; + char *p; + uint8_t *bp, *pc; + uint16_t *ps, s; + uint32_t *pi, i; + uint64_t *pl, l; + double *pd, d; + char fstr[BFLOAT+1]; + int n;     bp = buf;   va_start(args, fmt); @@ -229,6 +215,53 @@
  memcpy(bp, fstr, BFLOAT);   bp += BFLOAT;   break; + case 'C': + pc = va_arg(args, uint8_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) + *bp++ = *(pc+j); + break; + case 'S': + ps = va_arg(args, uint16_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + *bp++ = *(ps+j) >> 8; + *bp++ = *(ps+j); + } + break; + case 'I': + pi = va_arg(args, uint32_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + *bp++ = *(pi+j) >> 24; + *bp++ = *(pi+j) >> 16; + *bp++ = *(pi+j) >> 8; + *bp++ = *(pi+j); + } + break; + case 'L': + pl = va_arg(args, uint64_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + *bp++ = *(pl+j) >> 56; + *bp++ = *(pl+j) >> 48; + *bp++ = *(pl+j) >> 40; + *bp++ = *(pl+j) >> 32; + *bp++ = *(pl+j) >> 24; + *bp++ = *(pl+j) >> 16; + *bp++ = *(pl+j) >> 8; + *bp++ = *(pl+j); + } + break; + case 'F': + pd = va_arg(args, double *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + sprintf(fstr, "%+1.8le", *(pd+j)); + memcpy(bp, fstr, BFLOAT); + bp += BFLOAT; + } + break;   default:   va_end(args);   return -1; @@ -238,39 +271,56 @@
  return bp - buf;  }   -int _unpack(uchar *buf, char *fmt, ...) +/* TODO: check if fmt length and parameter count matches */ + +int _unpack(uint8_t *buf, char *fmt, ...)  {   va_list args;   char *p; - uchar *bp, *pc; + uint8_t *bp, *pc;   uint16_t *ps;   uint32_t *pi;   uint64_t *pl;   double *pd; - char fstr[BFLOAT]; + char fstr[BFLOAT+1]; + int n;   + fstr[BFLOAT] = '\0'; /* BFLOAT = 17, say. Here is a safe guard + * placed in the 17th. byte; in case the + * floating point string produced during + * packing was 16 bytes long. If it was, + * then the 16 bytes being unpacked from + * the buffer will not have a trailing + * NULL terminator. Otherwise, if it was + * a 15-byte string, then it will have + * had the NULL terminator in the + * 16th. byte, because the string + * representation was produced by a + * sprint(buf, '+1.8e', d) which appends + * one at the end. */ +   bp = buf;   va_start(args, fmt);   for (p = fmt; *p != '\0'; p++) {   switch (*p) {   case 'c': /* char or uint8_t */ - pc = va_arg(args, uchar*); + pc = va_arg(args, uint8_t *);   *pc = *bp++;   break;   case 's': /* short or uint16_t */ - ps = va_arg(args, uint16_t*); + ps = va_arg(args, uint16_t *);   *ps = (uint16_t)*bp++ << 8;   *ps |= (uint16_t)*bp++;   break;   case 'i': /* int or uint32_t */ - pi = va_arg(args, uint32_t*); + pi = va_arg(args, uint32_t *);   *pi = (uint32_t)*bp++ << 24;   *pi |= (uint32_t)*bp++ << 16;   *pi |= (uint32_t)*bp++ << 8;   *pi |= (uint32_t)*bp++;   break;   case 'l': /* long or uint64_t */ - pl = va_arg(args, uint64_t*); + pl = va_arg(args, uint64_t *);   *pl = (uint64_t)*bp++ << 56;   *pl |= (uint64_t)*bp++ << 48;   *pl |= (uint64_t)*bp++ << 40; @@ -281,11 +331,58 @@
  *pl |= (uint64_t)*bp++;   break;   case 'f': /* float */ - pd = va_arg(args, double*); + pd = va_arg(args, double *);   memcpy(fstr, bp, BFLOAT);   bp += BFLOAT;   sscanf(fstr, "%le", pd);   break; + case 'C': /* char or uint8_t */ + pc = va_arg(args, uint8_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) + *(pc+j) = *bp++; + break; + case 'S': /* short or uint16_t */ + ps = va_arg(args, uint16_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + *(ps+j) = (uint16_t)*bp++ << 8; + *(ps+j) |= (uint16_t)*bp++; + } + break; + case 'I': /* int or uint32_t */ + pi = va_arg(args, uint32_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + *(pi+j) = (uint32_t)*bp++ << 24; + *(pi+j) |= (uint32_t)*bp++ << 16; + *(pi+j) |= (uint32_t)*bp++ << 8; + *(pi+j) |= (uint32_t)*bp++; + } + break; + case 'L': /* long or uint64_t */ + pl = va_arg(args, uint64_t *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + *(pl+j) = (uint64_t)*bp++ << 56; + *(pl+j) |= (uint64_t)*bp++ << 48; + *(pl+j) |= (uint64_t)*bp++ << 40; + *(pl+j) |= (uint64_t)*bp++ << 32; + *(pl+j) |= (uint64_t)*bp++ << 24; + *(pl+j) |= (uint64_t)*bp++ << 16; + *(pl+j) |= (uint64_t)*bp++ << 8; + *(pl+j) |= (uint64_t)*bp++; + } + break; + case 'F': /* float */ + pd = va_arg(args, double *); + n = va_arg(args, int); + for (int j = 0; j < n; j++) { + memcpy(fstr, bp, BFLOAT); + bp += BFLOAT; + sscanf(fstr, "%le", pd+j); + } + break;   default:   va_end(args);   return -1; @@ -297,134 +394,199 @@
   int packTHeader(uchar *buf, THeader *h)  { - return _pack(buf, "cciiiiii", - h->type, h->bo, h->crc, h->b, h->n, + return _pack(buf, "cciiiiiiiii", + h->type, h->bo, h->crc, h->b, + h->nt, h->nd, h->bt, h->bd,   h->sumb, h->sumu, h->sumtf);  }   +int pack_idmap(uint8_t *buf, Hash *h) +{ + /* A n-byte string, a NULL terminator '\0', and a 4-byte int + * is packed into n+1+4 bytes. */ + + uint8_t *bp; + Token *token; + bp = buf; + for (int i = 0; i < h->n; i++) { + for (Node *np = h->tab[i]; np != NULL; np = np->next) { + token = (Token *)(np->data); + memcpy(bp, token->str, token->l + 1); /* include '\0' */ + bp += token->l + 1; + *bp++ = token->id >> 24; + *bp++ = token->id >> 16; + *bp++ = token->id >> 8; + *bp++ = token->id; + } + } + return bp - buf; +} +  int packTDocHeader(uchar *buf, TDocHeader *h)  { - return _pack(buf, "cciiiiiiiiif", - h->type, h->bo, h->crc, h->id, h->b_tf, - h->b_txt, h->sumb, h->sumu, h->sumtf, h->maxtf, + return _pack(buf, "cciiiiiiif", + h->type, h->bo, h->crc, h->id, + h->sumb, h->sumu, h->sumtf, h->maxtf,   h->sumsqtf, h->sumsqlogtf);  }    int packTDoc_payload(uchar *buf, TDoc *tdoc)  { - int m, m_; - m = m_ = 0; - for (int i = 0; i < tdoc->h->sumu; i++) { - if ((m_ = _pack(buf + m, "i", &(tdoc->txt[i]))) == -1) - eprintf("_pack failed on tdoc->txt[%d] = %d\n", i, tdoc->txt[i]); - m += m_; - } - for (int i = 0; i < tdoc->h->b_tf / sizeof(uint32_t); i++) { - if ((m_ = _pack(buf + m, "i", &(tdoc->tf[i]))) == -1) - eprintf("_pack failed on tdoc->tf[%d] = %d", i, tdoc->tf[i]); - m += m_; - } - return m; + return _pack(buf, "II", + tdoc->txt, tdoc->h->sumu, + tdoc->tf, tdoc->h->sumu);  }   -int unpackTHeader(THeader *h, int n, uchar *buf) +int unpackTHeader(THeader *h, int n, uint8_t *buf)  {   int m; - if ((m = _unpack(buf, "cciiiiii", - &h->type, &h->bo, &h->crc, &h->b, &h->n, + if ((m = _unpack(buf, "cciiiiiiiii", + &h->type, &h->bo, &h->crc, &h->b, + &h->nt, &h->nd, &h->bt, &h->bd,   &h->sumb, &h->sumu, &h->sumtf)) != n)   return -1;   assert(h->type == 0x01);   return m;  }   -int unpackTDocHeader(TDocHeader *h, int n, uchar *buf) +int unpack_idmap(Hash *h, int n, uint8_t *buf)  { + /* A n-byte string, a NULL terminator '\0', and a 4-byte int + * is packed into n+1+4 bytes. */ + + uint8_t *bp; + uint8_t s[MAXTERMLEN + 1]; + uint32_t l;   int m; - if ((m = _unpack(buf, "cciiiiiiiiif", - &h->type, &h->bo, &h->crc, &h->id, &h->b_tf, - &h->b_txt, &h->sumb, &h->sumu, &h->sumtf, &h->maxtf, - &h->sumsqtf, &h->sumsqlogtf)) != n) - return m; - assert(h->type == 0x02); - return m; -} + Token token__, *token_; + Node *nptoken_;   -int unpackTDoc_payload(TDoc *tdoc, int n, uchar *buf) -{ - int m, m_; - m = m_ = 0; - for (int i = 0; i < tdoc->h->sumu; i++) { - if ((m_ = _unpack(buf + m, "i", &(tdoc->txt[i]))) == -1) - eprintf("_unpack failed on tdoc->txt[%d] = %d\n", i, tdoc->txt[i]); - m += m_; - } - for (int i = 0; i < (tdoc->h->b_tf / sizeof(uint32_t)); i++) { - if ((m_ = _unpack(buf + m, "i", &(tdoc->tf[i]))) == -1) - eprintf("_unpack failed on tdoc->tf[%d] = %d\n", i, tdoc->tf[i]); - m += m_; + token__.str = s; + token__.l = 0; + token__.id = 0; + token__.t = 0; + bp = buf; + l = 0; + m = 0; + for(int i = 0; i < n; i++) { + if (*(bp+i) == '\0') { + /* unpack from buffer into local memory */ + token__.str[l] = *(bp + i++); + token__.l = l; + token__.id = (uint32_t)*(bp + i++) << 24; + token__.id |= (uint32_t)*(bp + i++) << 16; + token__.id |= (uint32_t)*(bp + i++) << 8; + token__.id |= (uint32_t)*(bp + i); + m += l + 1 + 4; + l = 0; + /* build Token and insert it into hash table */ + token_ = newtoken(token__.str, token__.l, token__.t, token__.id); + nptoken_ = newnode(token_); + hlookup(h, nptoken_, 1); + continue; /* FIXIT: continue even if i has been incremented? */ + } + token__.str[l++] = *(bp+i);   }   if (m != n)   return -1;   return m;  }   +int unpackTDocHeader(TDocHeader *h, int n, uint8_t *buf) +{ + int m; + if ((m = _unpack(buf, "cciiiiiiif", + &h->type, &h->bo, &h->crc, &h->id, + &h->sumb, &h->sumu, &h->sumtf, &h->maxtf, + &h->sumsqtf, &h->sumsqlogtf)) != n) + return -1; + assert(h->type == 0x02); + return m; +} + +int unpackTDoc_payload(TDoc *tdoc, int n, uint8_t *buf) +{ + int m; + if ((m = _unpack(buf, "II", + tdoc->txt, tdoc->h->sumu, + tdoc->tf, tdoc->h->sumu)) != n) + return -1; + return m; +} +  void send(FILE *fp, TFile *tfile)  { - int n, c, size, b_payload; - Node *np; - TDoc *tdoc; + int n, c, size, bpack; + Node *np; + TDoc *tdoc;   uchar *buf;     size = MB;   buf = (uchar *)emalloc(size);     c = 0; - + + /* THeader */   if ((n = packTHeader(buf, tfile->h)) == -1)   eprintf("packTheader() failed on THeader\n");   if (fwrite(buf, n, 1, fp) != 1)   eprintf("fwrite() failed on %d byte THeader\n", n);   + /* tfile->ht and tfile->hd */ + bpack = tfile->h->bt > tfile->h->bd ? tfile->h->bt : tfile->h->bd; + if (size < bpack) { + while ((size <<= 1) < bpack); + buf = erealloc(buf, size); + } + if ((n = pack_idmap(buf, tfile->ht)) == -1) + eprintf("pack_idmap() failed on tfile->ht\n"); + if (fwrite(buf, n, 1, fp) != 1) + eprintf("fwrite() failed on %d byte, packed, tfile->ht\n", n); + if ((n = pack_idmap(buf, tfile->hd)) == -1) + eprintf("pack_idmap() failed on tfile->hd\n"); + if (fwrite(buf, n, 1, fp) != 1) + eprintf("fwrite() failed on %d byte, packed, tfile->hd\n", n); + + /* list of TDocs */   for (np = tfile->list; np != NULL; np = np->next) {     tdoc = np->data;   + /* TDocHeader */   if ((n = packTDocHeader(buf, tdoc->h)) == -1)   eprintf("packTDocHeader() failed\n");   if (fwrite(buf, n, 1, fp) != 1)   eprintf("fwrite() failed on %d byte TDocHeader\n", n);   - b_payload = tdoc->h->id + tdoc->h->b_txt + tdoc->h->b_tf; - - if (size < b_payload) { - size = b_payload; - buf = erealloc(buf, b_payload); + /* TDoc */ + bpack = 2 * tdoc->h->sumu * 4; + if (size < bpack) { + while ((size <<= 1) < bpack); + buf = erealloc(buf, size);   } -   if ((n = packTDoc_payload(buf, tdoc)) == -1) - eprintf("packTDoc_payload() failed on %d byte TDoc payload\n", b_payload); + eprintf("packTDoc_payload() failed on %d byte TDoc payload\n", bpack);   if (fwrite(buf, n, 1, fp) != 1)   eprintf("fwrite() failed on %d byte TDoc payload\n", n);     c++; - fprintf(stderr, "\rTDocs sent: %d/%u", c, tfile->h->n); + fprintf(stderr, "\rTDocs sent: %d/%u", c, tfile->h->nd);   }     free(buf); - if (c < tfile->h->n) - fprintf(stderr, "; TDocs skipped: %d", tfile->h->n - c); + if (c < tfile->h->nd) + fprintf(stderr, "; TDocs skipped: %d", tfile->h->nd - c);   fprintf(stderr, "\n");  }   -TFile *receive(FILE *fp) +TFile *receive(FILE *fp, TFile *tfile)  { - int n, c, size, b_payload; - TFile *tfile; + int n, c, size, bpack; + /* TFile *tfile; */   TDoc *tdoc;   uchar *buf;   - tfile = newTFile(); + /* tfile = newTFile(); */     size = MB;   buf = (uchar *)emalloc(size); @@ -432,41 +594,66 @@
  c = 0;     if ((n = fread(buf, BTHEADER, 1, fp)) != 1) - eprintf("fread() failed on %d byte THeader\n", BTHEADER); + eprintf("fread() failed on %d byte THeader\n", + BTHEADER);   if (unpackTHeader(tfile->h, BTHEADER, buf) != BTHEADER) - eprintf("unpackTHeader() failed on %d byte THeader\n", BTHEADER); + eprintf("unpackTHeader() failed on %d byte THeader\n", + BTHEADER); + + bpack = tfile->h->bt > tfile->h->bd ? tfile->h->bt : tfile->h->bd; + if (size < bpack) { + while ((size <<= 1) < bpack); + buf = erealloc(buf, size); + } + + if ((n = fread(buf, tfile->h->bt, 1, fp)) != 1) + eprintf("fread() failed on %d byte tfile->h->bt\n", + tfile->h->bt); + if (unpack_idmap(tfile->ht, tfile->h->bt, buf) != tfile->h->bt) + eprintf("unpack_idmap() failed on %d byte tfile->h->bt\n", + tfile->h->bt); + if ((n = fread(buf, tfile->h->bd, 1, fp)) != 1) + eprintf("fread() failed on %d byte tfile->h->bd\n", + tfile->h->bd); + if (unpack_idmap(tfile->hd, tfile->h->bd, buf) != tfile->h->bd) + eprintf("unpack_idmap() failed on %d byte tfile->h->bd\n", + tfile->h->bd);   - for (int i = 0; i < tfile->h->n; i++) { + for (int i = 0; i < tfile->h->nd; i++) {     tdoc = newTDoc();     if ((n = fread(buf, BTDOCHEADER, 1, fp)) != 1) - eprintf("fread() failed on %d bytes TDocHeader\n", BTDOCHEADER); + eprintf("fread() failed on %d bytes TDocHeader for doc %d of %d\n", + BTDOCHEADER, i, tfile->h->nd);   if ((n = unpackTDocHeader(tdoc->h, BTDOCHEADER, buf)) != BTDOCHEADER) - eprintf("unpackTDocHeader() failed on %d byte TDocHeader, n = %d\n", BTDOCHEADER, n); + eprintf("unpackTDocHeader() failed on %d byte TDocHeader for doc %d of %d\n", + BTDOCHEADER, i, tfile->h->nd);   - b_payload = tdoc->h->b_txt + tdoc->h->b_tf; + bpack = 2 * tdoc->h->sumu * 4;   - if (size < b_payload) { - size = b_payload; - buf = erealloc(buf, b_payload); + if (size < bpack) { + while ((size <<= 1) < bpack); + buf = erealloc(buf, size);   }   - if ((n = fread(buf, b_payload, 1, fp)) != 1) - eprintf("fread() failed on %d byte payload\n", b_payload); - if ((n = unpackTDoc_payload(tdoc, b_payload, buf)) != b_payload) - eprintf("unpackTDoc_payload() failed on %d byte payload\n", b_payload); + if ((n = fread(buf, bpack, 1, fp)) != 1) + eprintf("fread() failed on %d byte payload\n", + bpack); + if ((n = unpackTDoc_payload(tdoc, bpack, buf)) != bpack) + eprintf("unpackTDoc_payload() failed on %d byte payload\n", + bpack);     /* add payload to list */   tfile->list = addfront(tfile->list, newnode((void *)tdoc));     c++; - fprintf(stderr, "\rTDocs read: %d/%d", i+1, tfile->h->n); + fprintf(stderr, "\rTDocs read: %d/%d", i+1, tfile->h->nd);   }     free(buf); - if (c < tfile->h->n) - fprintf(stderr, "; TDocs skipped: %d", tfile->h->n - c); + if (c < tfile->h->nd) + fprintf(stderr, "; TDocs skipped: %d", tfile->h->nd - c);   fprintf(stderr, "\n");     return tfile;
Change 1 of 5 Show Entire File tfile.h Stacked
 
1
2
3
4
5
6
 
 
 
 
 
 
 
 
7
8
9
 
17
18
19
20
 
 
 
 
21
22
23
 
28
29
30
31
32
33
34
35
 
40
41
42
43
44
 
 
45
46
47
48
49
 
 
 
 
50
51
52
53
 
54
55
 
 
56
57
58
 
66
67
68
69
70
 
 
71
72
 
73
74
 
75
76
77
78
 
 
 
 
79
80
81
 
 
1
 
 
 
 
 
2
3
4
5
6
7
8
9
10
11
12
 
20
21
22
 
23
24
25
26
27
28
29
 
34
35
36
 
 
37
38
39
 
44
45
46
 
 
47
48
49
50
51
52
 
53
54
55
56
57
58
 
 
59
60
 
61
62
63
64
65
 
73
74
75
 
 
76
77
78
79
80
81
 
82
83
 
 
 
84
85
86
87
88
89
 
90
@@ -1,9 +1,12 @@
 #define NTERM 10000 -#define BTHEADER 26 /* packed bytes for THeader; 8 elements; 1*2 + 4*6 */ -#define THEADER 26 /* bytes in memory */ -#define BTDOCHEADER 54 /* packed bytes for TDocHeader; 12 elements; 1*2 + 4*9 + 16*1 */ -#define TDOCHEADER 46 /* bytes in memory; 1*2 + 4*9 + 8*1 */ -#define BFLOAT 16 /* a double formatted as +1.12345678e+01, using +1.8e */ +#define BTHEADER 38 /* packed bytes for THeader; 8 elements; 1*2 + 4*9 */ +#define THEADER 38 /* bytes in memory */ +#define BTDOCHEADER 46 /* packed bytes for TDocHeader; 10 elements; 1*2 + 4*7 + 16*1 */ +#define TDOCHEADER 38 /* bytes in memory; 1*2 + 4*7 + 8*1 */ + +#define BFLOAT 16 /* a double formatted as +1.12345678e+01, using + * +1.8e is 15 bytes long. One extra byte for + * 3-digit exponent. (Why 3?)*/    typedef unsigned char uchar;   @@ -17,7 +20,10 @@
  uint8_t bo;   uint32_t crc;   uint32_t b; - uint32_t n; + uint32_t nt; + uint32_t nd; + uint32_t bt; + uint32_t bd;   uint32_t sumb;   uint32_t sumu;   uint32_t sumtf; @@ -28,8 +34,6 @@
  uint8_t bo;   uint32_t crc;   uint32_t id; - uint32_t b_txt; - uint32_t b_tf;   uint32_t sumb;   uint32_t sumu;   uint32_t sumtf; @@ -40,19 +44,22 @@
   struct TDoc {   TDocHeader *h; - uint32_t *txt; - uint32_t *tf; + uint32_t *txt; + uint32_t *tf;  };    struct TFile {   THeader *h; - Node *list; + Hash *ht; + Hash *hd; + Hash *hdh; + Node *list;  };   -THeader *_newTHeader(void); -void _freeTHeader(void*); +THeader *_newTHeader(void);  TDocHeader *_newTDocHeader(void); -void _freeTDocHeader(void*); +int cmpdoc(void*, void*); +uint32_t hashdoc(void*, uint32_t);    TDoc *newTDoc(void);  void freeTDoc(void*); @@ -66,16 +73,18 @@
 void _printTDoc(TDoc*, FILE*);  void printTFile(TFile*, FILE*);   -int _pack(uchar *buf, char *fmt, ...); -int _unpack(uchar *buf, char *fmt, ...); +int _pack(uint8_t *buf, char *fmt, ...); +int _unpack(uint8_t *buf, char *fmt, ...);    int packTHeader(uint8_t *buf, THeader *h); +int pack_idmap(uint8_t *buf, Hash *h);  int packTDocHeader(uint8_t *buf, TDocHeader *h); -int packTDoc_payload(uchar *buf, TDoc *tdoc); +int packTDoc_payload(uint8_t *buf, TDoc *tdoc);   -int unpackTHeader(THeader *h, int n, uchar *buf); -int unpackTDocHeader(TDocHeader *h, int n, uchar *buf); -int unpackTDoc_payload(TDoc *tdoc, int n, uchar *buf); +int unpackTHeader(THeader *h, int n, uint8_t *buf); +int unpack_idmap(Hash *h, int n, uint8_t *buf); +int unpackTDocHeader(TDocHeader *h, int n, uint8_t *buf); +int unpackTDoc_payload(TDoc *tdoc, int n, uint8_t *buf);    void send(FILE *fp, TFile *tfile); -TFile *receive(FILE *fp); +TFile *receive(FILE *fp, TFile *tfile);
Show Entire File txt.c Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Change 1 of 2 Show Entire File txt.h Stacked
 
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
 
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
 
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
 
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
@@ -11,95 +11,44 @@
 #define NHASHD 100003    /* typedef struct Doc Doc; */ -typedef struct TFile TFile; -typedef struct stemmer Stemmer; -typedef struct Stack Stack; -typedef struct Tokenizer Tokenizer;  typedef struct Token Token; -typedef struct Parser Parser; -typedef struct Query Query;  typedef struct Term Term;  typedef struct Post Post; -typedef struct Score Score; +typedef struct Hit Hit;  typedef struct Model Model;   -typedef double (tf_fn)(uint32_t, uint32_t, uint32_t, - uint32_t, uint32_t, uint32_t, - uint32_t, uint32_t, uint32_t, - uint32_t); +typedef double (tf_fn)(uint32_t, uint32_t, uint32_t, uint32_t, + uint32_t, uint32_t, double, + uint32_t, uint32_t, uint32_t, uint32_t); +  typedef double (df_fn)(uint32_t, uint32_t);    enum {KEEPCASE, LOWERCASE};    typedef enum {CTRLCHAR = 1, PRINTCHAR, SEPCHAR} E_asciitype;   - -/* struct Doc { */ -/* uint32_t id; */ -/* uint32_t sumb; */ -/* uint32_t sumu; */ -/* uint32_t sumtf; */ -/* uint32_t maxtf; */ -/* char *s; */ -/* }; */ - -/* struct Query { */ -/* uint32_t id; */ -/* uint32_t sumtf; */ -/* uint32_t uterm; */ -/* char *s; */ -/* Node *tlist; */ -/* }; */ - -/* struct Term { */ -/* uint32_t id; */ -/* uint32_t df; */ -/* Node *plist; */ -/* }; */ - -struct Stack { - char s[KB]; - int p; - int n; -}; - -struct Tokenizer { - char delimiters[ASCII]; - int asciitab[ASCII]; - Stack *mem; - int casechange; -}; -  struct Token { - char *str; + uint8_t *str;   uint32_t l;   uint32_t id;   uint8_t t;  };   -struct Parser { - char type; - int c; - int n; - int x_min; - int x_max; - Hash *hcw; /* hash of common words */ - Stemmer *stemmer; - int n_tag; - char endtag[64]; - char idtag[64]; - char *tag[64]; -}; -  struct Post {   uint32_t id;   uint32_t tf;  };   -/* struct Score { */ -/* uint32_t id; */ -/* double n; */ -/* }; */ +struct Term { + uint32_t id; + uint32_t df; + Node *plist; +}; + +struct Hit { + uint32_t id; + double n; +};    struct Model {   tf_fn *tf; @@ -111,67 +60,41 @@
 /* TODO: move to libk */  void walkhash(Hash*, void (*f)(void*, void*), void*);  uint32_t _inthash(void*, uint32_t); -  int cmp_p(void*, void*);    int cmpdoc_id(void*, void*);   -/* Tokenizer */ -void reset(Stack*, int); -void push(Stack*, char*); -char pop(Stack*); -void printstack(Stack*); -Tokenizer *newtokenizer(char*, int, Stack*); -  /* Token */ -Token *newtoken(char*, uint32_t, uint8_t, uint32_t); +Token *newtoken(uint8_t*, uint32_t, uint8_t, uint32_t);  void freetoken(void*);  int cmptoken_str(void*, void*);  int cmptoken_id(void*, void*);  uint32_t hashtoken_str(void*, uint32_t);  uint32_t hashtoken_id(void*, uint32_t); -int gettoken(Token*, FILE*, Tokenizer*); -void writetoken(void*, void*);    /* Post */  Post *newpost(uint32_t, uint32_t); +uint32_t hashpost(void*, uint32_t);  int cmppost(void*, void*);  int cmppost_tf(const void*, const void*); -uint32_t hashpost(void*, uint32_t); +void fprintpost(void*, FILE*);   -/* Parser */ -Parser *newparser(char, char**, int, int, int, int); -void freeparser(Parser*); -TFile *parse(Hash*, Hash*, Parser*, FILE*); +/* Term */ +Term *newterm(uint32_t, uint32_t, Post*); +void freeterm(void*); +uint32_t hashterm(void*, uint32_t); +int cmpterm(void*, void*); +int term_match_handler(void*, void*); +void fprintterm(void*, FILE*);   -/* Query *newquery(char*); */ -/* void freequery(void*); */ -/* int cmpquery(void*, void*); */ -/* uint32_t hashquery(void*, uint32_t); */ -/* void fprintquery(FILE*, void*); */ - -/* Term *newterm(char*, uint32_t, Post*); */ -/* void freeterm(void*); */ -/* int cmpterm(void*, void*); */ -/* int cmptermp(void*, void*); */ -/* uint32_t hashterm(void*, uint32_t); */ -/* int term_match_handler(void*, void*); */ -/* void fprintterm(FILE*, void*); */ -/* void printtree(void*, void*); */ - -/* Score */ -/* Score *newscore(char*, double); */ -/* void freescore(void*); */ -/* Score *newscore_s(char*, double); */ -/* void freescore_s(void*); */ -/* int cmpscore(void*, void*); */ -/* int cmpscore_p(void*, void*); */ -/* int cmpscore_n(void*, void*); */ -/* uint32_t hashscore(void*, uint32_t); */ +/* Hit */ +Hit *newhit(uint32_t, double); +uint32_t hashhit(void*, uint32_t); +int cmphit(void*, void*); +void fprinthit(void*, FILE*);    /* Models */   -  /** SMART **/    tf_fn SMART_n_n_tf;