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"
29 #include "jenkins_hash.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;
56 struct termlist *termlist_create(NMEM nmem, int numterms, int highscore_size)
62 // Calculate a hash size smallest power of 2 larger than 50% of expected numterms
63 halfnumterms = numterms >> 1;
66 while (hashsize < halfnumterms)
68 res = nmem_malloc(nmem, sizeof(struct termlist));
69 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct termlist_bucket*));
70 memset(res->hashtable, 0, hashsize * sizeof(struct termlist_bucket*));
71 res->hashtable_size = hashsize;
73 res->hashmask = hashsize - 1; // Creates a bitmask
75 res->highscore = nmem_malloc(nmem, highscore_size * sizeof(struct termlist_score *));
76 res->highscore_size = highscore_size;
77 res->highscore_num = 0;
78 res->highscore_min = 0;
83 static void update_highscore(struct termlist *tl, struct termlist_score *t)
89 if (tl->highscore_num > tl->highscore_size && t->frequency < tl->highscore_min)
93 for (i = 0; i < tl->highscore_num; i++)
95 if (tl->highscore[i]->frequency < tl->highscore[smallest]->frequency)
97 if (tl->highscore[i] == t)
100 if (tl->highscore_num)
101 tl->highscore_min = tl->highscore[smallest]->frequency;
102 if (t->frequency < tl->highscore_min)
103 tl->highscore_min = t->frequency;
106 if (tl->highscore_num < tl->highscore_size)
108 tl->highscore[tl->highscore_num++] = t;
109 if (t->frequency < tl->highscore_min)
110 tl->highscore_min = t->frequency;
114 if (t->frequency > tl->highscore[smallest]->frequency)
116 tl->highscore[smallest] = t;
121 void termlist_insert(struct termlist *tl, const char *term)
124 struct termlist_bucket **p;
127 if (strlen(term) > 255)
131 for (cp = buf + strlen(buf); cp != buf && strchr(",. -", cp[-1]); cp--)
134 bucket = jenkins_hash((unsigned char *)buf) & tl->hashmask;
135 for (p = &tl->hashtable[bucket]; *p; p = &(*p)->next)
137 if (!strcmp(buf, (*p)->term.term))
139 (*p)->term.frequency++;
140 update_highscore(tl, &((*p)->term));
144 if (!*p) // We made it to the end of the bucket without finding match
146 struct termlist_bucket *new = nmem_malloc(tl->nmem,
147 sizeof(struct termlist_bucket));
148 new->term.term = nmem_strdup(tl->nmem, buf);
149 new->term.frequency = 1;
152 update_highscore(tl, &new->term);
156 static int compare(const void *s1, const void *s2)
158 struct termlist_score **p1 = (struct termlist_score**) s1, **p2 = (struct termlist_score **) s2;
159 return (*p2)->frequency - (*p1)->frequency;
162 struct termlist_score **termlist_highscore(struct termlist *tl, int *len)
164 qsort(tl->highscore, tl->highscore_num, sizeof(struct termlist_score*), compare);
165 *len = tl->highscore_num;
166 return tl->highscore;
172 * c-file-style: "Stroustrup"
173 * indent-tabs-mode: nil
175 * vim: shiftwidth=4 tabstop=8 expandtab