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

A hash table version of the inverted index.

ii1.c: In stead of a BST, the inverted index is stored in a hashtable
here. This has the advantage of quicker build times but the ordering
of terms is not available.

Some unused bits were removed from Term and Post.

Makefile updated to build ii1.

Changeset 769c9c738c1b

Parent 9f7191bdc9a6

by Rup Palchowdhury

Changes to 5 files · Browse files at 769c9c738c1b Showing diff from parent 9f7191bdc9a6 Diff from another changeset...

Change 1 of 2 Show Entire File Makefile Stacked
 
24
25
26
27
 
28
29
30
 
35
36
37
 
 
 
38
39
40
 
24
25
26
 
27
28
29
30
 
35
36
37
38
39
40
41
42
43
@@ -24,7 +24,7 @@
   OBJ = $(patsubst %.c,$(O)/%.o,$(SRC))   -all: raw2t t2mem ii +all: raw2t t2mem ii ii1    raw2t: $(O)/raw2t.o $(OBJ)   $(CC) $(CFLAGS) $(INCLUDES) $(LDFLAGS) -o $@ $^ $(LIBS) @@ -35,6 +35,9 @@
 ii: $(O)/ii.o $(OBJ)   $(CC) $(CFLAGS) $(INCLUDES) $(LDFLAGS) -o $@ $^ $(LIBS)   +ii1: $(O)/ii1.o $(OBJ) + $(CC) $(CFLAGS) $(INCLUDES) $(LDFLAGS) -o $@ $^ $(LIBS) +  $(O)/%.o: %.c $(DEPDIR)/%.d   $(CC) $(DEPFLAGS) $(CFLAGS) $(INCLUDES) -c $< -o $@   $(POSTCOMPILE)
Change 1 of 1 Show Entire File ii.c Stacked
 
11
12
13
14
15
 
 
16
17
18
 
11
12
13
 
 
14
15
16
17
18
@@ -11,8 +11,8 @@
 #include "tokenizer.h"  #include "tfile.h"   -/* enum {NTERMS = 100003, NDOCS = 100003}; */ -enum {NTERMS = 10007, NDOCS = 4093}; +enum {NTERMS = 100003, NDOCS = 100003}; +/* enum {NTERMS = 10007, NDOCS = 4093}; */    int main(void)  {
Change 1 of 1 Show Entire File ii1.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
@@ -0,0 +1,161 @@
+#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <kak/klog.h> +#include <kak/klist.h> +#include <kak/khash.h> +#include "post.h" +#include "term.h" +#include "tokenizer.h" +#include "tfile.h" + +enum {NTERMS = 100003, NDOCS = 100003}; +/* enum {NTERMS = 10007, NDOCS = 4093}; */ + +void hprint(Node *htab[], unsigned size) +{ + float fill, avglen; + int c, maxlen, sumlen, chainlen; + Node *np, *np1; + Term *t; + Post *p; + c = maxlen = sumlen = 0; + for (int i = 0; i < size; i++) { + if (htab[i] != NULL) { + c++; + chainlen = 0; + for (np = htab[i]; np != NULL; np = np->next) { + t = (Term *)(np->data); + chainlen++; + printf("%s:%d ", t->s, t->df); + for (np1 = t->plist; np1 != NULL; np1 = np1->next) { + p = (Post *)(np1->data); + printf("%s:%d ", p->id, p->tf); + } + printf("\n"); + } + sumlen += chainlen; + if (chainlen > maxlen) maxlen = chainlen; + } + } + + fill = (float)c / (float)size * 100; + avglen = (float)sumlen / (float)c; + fprintf(stderr, "Fill: %5.2f%% (%d/%d)\n", fill, c, size); + fprintf(stderr, "Avg chain length: %5.2f\n", avglen); + fprintf(stderr, "Max chain length: %d\n", maxlen); + fprintf(stderr, "Node count: %d\n", sumlen); +} + + +int main(void) +{ + int i_; + TDoc *tdoc; + Post *post, *post_; + Term *term, *term_; + unsigned n_doc; + char *p_txt, *ts; + THeader *h; + Node *idtab[NDOCS], *termtab[NTERMS], *ii[NTERMS]; + Node *npid, *npt, *npii, *npii_; + + + for (int i = 0; i < NDOCS; i++) + idtab[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) + exit(0); + + for(n_doc = 1; n_doc <= h->n; n_doc++) { + + fprintf(stderr, "%d/%d\r", n_doc, h->n); + + tdoc = newTDoc(TREC); + + if ((tdoc = readTDoc(tdoc, stdin)) == NULL) + exit(0); + + tdoc->id[tdoc->h->n_id] = '\0'; + npid = hlookup(idtab, newnode(strdup(tdoc->id)), 1, hash, NDOCS, strcmp); + i_ = 0; + p_txt = tdoc->txt; + + for (int i = 0; i < tdoc->h->n_txt; i++) { + + if (tdoc->txt[i] == ' ') { + + ts = (char *)malloc(i - i_ + 1); + memcpy(ts, p_txt, i - i_); + ts[i - i_] = '\0'; + + npt = hlookup(termtab, newnode(ts), 1, hash, NTERMS, strcmp); + + post = newpost((char *)(npid->data), 1); + term = newterm((char *)(npt->data), 1, post); + npii = newnode(term); + + /* if term exists + if post exists at front of list + post->tf++ + else + add new post at front of list + term->df++ + else + add term to hash table + */ + + npii_ = hlookup(ii, npii, 1, hashterm, NTERMS, termcmp1); + + if (npii != npii_) { /* a new Term wasn't added to the table */ + term_ = (Term *)(npii_->data); + post_ = (Post *)(term_->plist->data); + + if (postcmp(post, post_) == 0) { + post_->tf++; + post->ref = 0; + } + else { + term_->plist = addfront(term_->plist, term->plist); + term_->df++; + } + term->ref = 0; + } + + i_ = i + 1; + p_txt = &(tdoc->txt[i + 1]); + + + if (post->ref == 0) { + free(post); + free(term->plist); + } + if (term->ref == 0) { + free(term); + } + } + } + + freeTDoc(tdoc); + } + + fprintf(stderr, "%d/%d\n", n_doc - 1, h->n); + _freeTHeader(h); + + /* hprint(ii, NTERMS); */ + + /* fprintf(stderr, "\n"); */ + /* fprintf(stderr, "idtab[]\n"); */ + /* hstats(idtab, NDOCS); */ + /* fprintf(stderr, "termtab[]\n"); */ + /* hstats(termtab, NTERMS); */ + + return 0; +}
Change 1 of 2 Show Entire File term.c Stacked
 
16
17
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
20
21
 
71
72
73
74
75
76
77
78
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
89
90
91
 
 
 
 
 
@@ -16,6 +16,24 @@
  return t;  }   +unsigned hashterm(void *data, unsigned hsize) +{ + static unsigned MULTIPLIER = 31; + unsigned h; + char *p; + h = 0; + for (p = (char *)(((Term *)data)->s); *p != '\0'; p++) + h = MULTIPLIER * h + *p; + return h % hsize; +} + +int termcmp1(void *d1, void *d2) +{ + if (((Term *)d1)->s == ((Term *)d2)->s) + return 0; + return 1; +} +  int termcmp(void *d1, void *d2)  {   if (((Term *)d1)->s == ((Term *)d2)->s) @@ -71,8 +89,3 @@
  apply(((Term *)d)->plist, printpost, "%s:%d ");   printf("\n");  } - -unsigned int hashterm(unsigned int (*fn)(char*, int), void *data, int modulo) -{ - return (*fn)(((Term *)data)->s, modulo); -}
Change 1 of 1 Show Entire File term.h Stacked
 
7
8
9
 
10
11
 
12
13
14
15
16
 
7
8
9
10
11
12
13
14
15
16
17
 
@@ -7,10 +7,11 @@
  int ref;  };   +unsigned hashterm(void*, unsigned);  Term *newterm(char*, unsigned, Post*);  int termcmp(void*, void*); +int termcmp1(void*, void*);  int term_match_handler(void*, void*);  void freeterm(void*);  void printterm(void*, void*);  void printtree(void*, void*); -unsigned int hashterm(unsigned (*fn)(char*, int), void*, int);