2 * Parrot Picking Fortune Cookies aka Simple Text Associator
4 * (c) 2001 Martin Mares <mj@ucw.cz>
14 #define HASHSIZE 32768
17 #define MATCH_PTS_LOW 20
18 #define MATCH_PTS_HIGH 1000
26 static struct wd *hash[HASHSIZE];
30 static struct wd *input[TEXTLEN];
31 static int next[TEXTLEN];
43 for (w=hash[h]; w; w=w->next)
46 w = malloc(sizeof(struct wd) + strlen(x));
57 getword(FILE *f, char *buf)
65 (c = fgetc(f)) == '%' &&
66 (c = fgetc(f)) == '\n')
72 while (c != EOF && !isalnum(c));
81 while (c != EOF && isalnum(c) && --l);
93 while (getword(f, buf))
96 struct wd *w = findword(buf);
99 printf("Found %d words.\n", wcnt);
107 while (getword(f, buf))
110 struct wd *w = findword(buf);
125 fprintf(stderr, "Input too long!\n");
129 printf("Scanned input of %d words.\n", n);
135 off_t start = 0, best_start = 0;
139 int pts, xpos, p, pp, i, j;
149 // printf("%d pts\n", pp);
157 bzero(cost, n*sizeof(int));
158 memset(pos, -1, n*sizeof(int));
162 while (getword(f, buf))
167 struct wd *w = findword(buf);
168 for (i=w->ref; i >= 0; i=next[i])
183 p = SKIP_PTS*p + cost[j] + w->cnt;
198 printf("Best: %d points at %d\n", best_pts, best_start);
200 fseek(f, best_start, SEEK_SET);
201 while (getword(f, buf) > 0)
203 struct wd *w = findword(buf);
205 printf("<%s> ", buf);
210 i = ftell(f) - best_start;
211 fseek(f, best_start, SEEK_SET);
217 cmp(const void *a, const void *b)
219 struct wd *x = *(struct wd **)a;
220 struct wd *y = *(struct wd **)b;
221 return (x->cnt < y->cnt) - (x->cnt > y->cnt);
228 struct wd *w, *wa[wcnt];
231 for (i=0; i<HASHSIZE; i++)
232 for (w=hash[i]; w; w=w->next)
234 qsort(wa, wcnt, sizeof(struct wd *), cmp);
236 for (i=0; i<wcnt; i++)
239 printf("%5d %s\n", w->cnt, w->w);
242 for (i=0; i<wcnt/600; i++)
243 wa[i]->cnt = MATCH_PTS_LOW;
245 wa[i]->cnt = MATCH_PTS_HIGH;
249 main(int argc, char **argv)
255 fprintf(stderr, "Usage: parrot <fortune-file>\n");
258 fort = fopen(argv[1], "r");
261 fprintf(stderr, "%s: %m\n", argv[1]);