1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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
27 #include <yaz/yaz-util.h>
29 #include "termlists.h"
32 // As terms are found in incoming records, they are added to (or updated in) a
33 // Hash table. When term records are updated, a frequency value is updated. At
34 // the same time, a highscore is maintained for the most frequent terms.
36 struct termlist_bucket
38 struct termlist_score term;
39 struct termlist_bucket *next;
44 struct termlist_bucket **hashtable;
48 struct termlist_score **highscore;
57 // Jenkins one-at-a-time hash (from wikipedia)
58 static unsigned int hash(const unsigned char *key)
60 unsigned int hash = 0;
74 struct termlist *termlist_create(NMEM nmem, int numterms, int highscore_size)
80 // Calculate a hash size smallest power of 2 larger than 50% of expected numterms
81 halfnumterms = numterms >> 1;
84 while (hashsize < halfnumterms)
86 res = nmem_malloc(nmem, sizeof(struct termlist));
87 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct termlist_bucket*));
88 memset(res->hashtable, 0, hashsize * sizeof(struct termlist_bucket*));
89 res->hashtable_size = hashsize;
91 res->hashmask = hashsize - 1; // Creates a bitmask
93 res->highscore = nmem_malloc(nmem, highscore_size * sizeof(struct termlist_score *));
94 res->highscore_size = highscore_size;
95 res->highscore_num = 0;
96 res->highscore_min = 0;
101 static void update_highscore(struct termlist *tl, struct termlist_score *t)
107 if (tl->highscore_num > tl->highscore_size && t->frequency < tl->highscore_min)
111 for (i = 0; i < tl->highscore_num; i++)
113 if (tl->highscore[i]->frequency < tl->highscore[smallest]->frequency)
115 if (tl->highscore[i] == t)
118 if (tl->highscore_num)
119 tl->highscore_min = tl->highscore[smallest]->frequency;
120 if (t->frequency < tl->highscore_min)
121 tl->highscore_min = t->frequency;
124 if (tl->highscore_num < tl->highscore_size)
126 tl->highscore[tl->highscore_num++] = t;
127 if (t->frequency < tl->highscore_min)
128 tl->highscore_min = t->frequency;
132 if (t->frequency > tl->highscore[smallest]->frequency)
134 tl->highscore[smallest] = t;
139 void termlist_insert(struct termlist *tl, const char *term)
142 struct termlist_bucket **p;
145 if (strlen(term) > 255)
149 for (cp = buf + strlen(buf); cp != buf && strchr(",. -", cp[-1]); cp--)
152 bucket = hash((unsigned char *)buf) & tl->hashmask;
153 for (p = &tl->hashtable[bucket]; *p; p = &(*p)->next)
155 if (!strcmp(buf, (*p)->term.term))
157 (*p)->term.frequency++;
158 update_highscore(tl, &((*p)->term));
162 if (!*p) // We made it to the end of the bucket without finding match
164 struct termlist_bucket *new = nmem_malloc(tl->nmem,
165 sizeof(struct termlist_bucket));
166 new->term.term = nmem_strdup(tl->nmem, buf);
167 new->term.frequency = 1;
170 update_highscore(tl, &new->term);
174 static int compare(const void *s1, const void *s2)
176 struct termlist_score **p1 = (struct termlist_score**) s1, **p2 = (struct termlist_score **) s2;
177 return (*p2)->frequency - (*p1)->frequency;
180 struct termlist_score **termlist_highscore(struct termlist *tl, int *len)
182 qsort(tl->highscore, tl->highscore_num, sizeof(struct termlist_score*), compare);
183 *len = tl->highscore_num;
184 return tl->highscore;
190 * indent-tabs-mode: nil
192 * vim: shiftwidth=4 tabstop=8 expandtab