1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <yaz/yaz-util.h>
28 #include "termlists.h"
31 // As terms are found in incoming records, they are added to (or updated in) a
32 // Hash table. When term records are updated, a frequency value is updated. At
33 // the same time, a highscore is maintained for the most frequent terms.
35 struct termlist_bucket
37 struct termlist_score term;
38 struct termlist_bucket *next;
43 struct termlist_bucket **hashtable;
47 struct termlist_score **highscore;
56 // Jenkins one-at-a-time hash (from wikipedia)
57 static unsigned int hash(const unsigned char *key)
59 unsigned int hash = 0;
73 struct termlist *termlist_create(NMEM nmem, int numterms, int highscore_size)
79 // Calculate a hash size smallest power of 2 larger than 50% of expected numterms
80 halfnumterms = numterms >> 1;
83 while (hashsize < halfnumterms)
85 res = nmem_malloc(nmem, sizeof(struct termlist));
86 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct termlist_bucket*));
87 memset(res->hashtable, 0, hashsize * sizeof(struct termlist_bucket*));
88 res->hashtable_size = hashsize;
90 res->hashmask = hashsize - 1; // Creates a bitmask
92 res->highscore = nmem_malloc(nmem, highscore_size * sizeof(struct termlist_score *));
93 res->highscore_size = highscore_size;
94 res->highscore_num = 0;
95 res->highscore_min = 0;
100 static void update_highscore(struct termlist *tl, struct termlist_score *t)
106 if (tl->highscore_num > tl->highscore_size && t->frequency < tl->highscore_min)
110 for (i = 0; i < tl->highscore_num; i++)
112 if (tl->highscore[i]->frequency < tl->highscore[smallest]->frequency)
114 if (tl->highscore[i] == t)
117 if (tl->highscore_num)
118 tl->highscore_min = tl->highscore[smallest]->frequency;
119 if (t->frequency < tl->highscore_min)
120 tl->highscore_min = t->frequency;
123 if (tl->highscore_num < tl->highscore_size)
125 tl->highscore[tl->highscore_num++] = t;
126 if (t->frequency < tl->highscore_min)
127 tl->highscore_min = t->frequency;
131 if (t->frequency > tl->highscore[smallest]->frequency)
133 tl->highscore[smallest] = t;
138 void termlist_insert(struct termlist *tl, const char *term)
141 struct termlist_bucket **p;
144 if (strlen(term) > 255)
148 for (cp = buf + strlen(buf); cp != buf && strchr(",. -", cp[-1]); cp--)
151 bucket = hash((unsigned char *)buf) & tl->hashmask;
152 for (p = &tl->hashtable[bucket]; *p; p = &(*p)->next)
154 if (!strcmp(buf, (*p)->term.term))
156 (*p)->term.frequency++;
157 update_highscore(tl, &((*p)->term));
161 if (!*p) // We made it to the end of the bucket without finding match
163 struct termlist_bucket *new = nmem_malloc(tl->nmem,
164 sizeof(struct termlist_bucket));
165 new->term.term = nmem_strdup(tl->nmem, buf);
166 new->term.frequency = 1;
169 update_highscore(tl, &new->term);
173 static int compare(const void *s1, const void *s2)
175 struct termlist_score **p1 = (struct termlist_score**) s1, **p2 = (struct termlist_score **) s2;
176 return (*p2)->frequency - (*p1)->frequency;
179 struct termlist_score **termlist_highscore(struct termlist *tl, int *len)
181 qsort(tl->highscore, tl->highscore_num, sizeof(struct termlist_score*), compare);
182 *len = tl->highscore_num;
183 return tl->highscore;
189 * indent-tabs-mode: nil
191 * vim: shiftwidth=4 tabstop=8 expandtab