1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 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"
30 #include "jenkins_hash.h"
33 // As terms are found in incoming records, they are added to (or updated in) a
34 // Hash table. When term records are updated, a frequency value is updated. At
35 // the same time, a highscore is maintained for the most frequent terms.
37 struct termlist_bucket
39 struct termlist_score term;
40 struct termlist_bucket *next;
45 struct termlist_bucket **hashtable;
52 struct termlist *termlist_create(NMEM nmem)
54 struct termlist *res = nmem_malloc(nmem, sizeof(struct termlist));
57 nmem_malloc(nmem, res->hash_size * sizeof(struct termlist_bucket*));
58 memset(res->hashtable, 0, res->hash_size * sizeof(struct termlist_bucket*));
64 void termlist_insert(struct termlist *tl, const char *display_term,
65 const char *norm_term, int freq)
68 struct termlist_bucket **p;
71 if (strlen(norm_term) > 255)
73 strcpy(buf, norm_term);
74 bucket = jenkins_hash((unsigned char *)buf) % tl->hash_size;
75 for (p = &tl->hashtable[bucket]; *p; p = &(*p)->next)
77 if (!strcmp(buf, (*p)->term.norm_term))
79 (*p)->term.frequency += freq;
83 if (!*p) // We made it to the end of the bucket without finding match
85 struct termlist_bucket *new = nmem_malloc(tl->nmem,
86 sizeof(struct termlist_bucket));
87 new->term.norm_term = nmem_strdup(tl->nmem, buf);
88 new->term.display_term = *display_term ?
89 nmem_strdup(tl->nmem, display_term) : new->term.norm_term;
90 new->term.frequency = freq;
97 static int compare(const void *s1, const void *s2)
99 struct termlist_score **p1 = (struct termlist_score **) s1;
100 struct termlist_score **p2 = (struct termlist_score **) s2;
101 int d = (*p2)->frequency - (*p1)->frequency;
104 return strcmp((*p1)->display_term, (*p2)->display_term);
107 struct termlist_score **termlist_highscore(struct termlist *tl, int *len,
110 struct termlist_score **highscore =
111 (struct termlist_score **)
112 nmem_malloc(nmem, tl->no_entries * sizeof(*highscore));
116 for (bucket = 0; bucket < tl->hash_size; bucket++)
118 struct termlist_bucket *p;
119 for (p = tl->hashtable[bucket]; p; p = p->next)
120 highscore[no++] = &p->term;
122 assert(no == tl->no_entries);
123 qsort(highscore, tl->no_entries, sizeof(struct termlist_score*), compare);
124 *len = tl->no_entries;
131 * c-file-style: "Stroustrup"
132 * indent-tabs-mode: nil
134 * vim: shiftwidth=4 tabstop=8 expandtab