2 * Default wordbreaker module for full text indexing.
4 * Copyright (c) 2005-2017 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
23 #include <sys/types.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
40 #include <libcitadel.h>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
50 #include "ft_wordbreaker.h"
52 #include "ctdl_module.h"
55 * Noise words are not included in search indices.
56 * NOTE: if the noise word list is altered in any way, the FT_WORDBREAKER_ID
57 * must also be changed, so that the index is rebuilt.
60 noise_word *noise_words[26];
62 static char *noise_words_init[] = {
127 void initialize_noise_words(void)
134 memset (noise_words, 0, sizeof(noise_words));
136 for (i=0; i<(sizeof(noise_words_init)/sizeof(char *)); ++i)
138 ch = noise_words_init[i][0] - 'a';
139 len = strlen(noise_words_init[i]);
141 next = malloc(sizeof(noise_word));
143 next->word = strdup(noise_words_init[i]);
144 next->next = noise_words[ch];
145 noise_words[ch] = next;
150 void noise_word_cleanup(void)
153 noise_word *cur, *next;
155 syslog(LOG_INFO, "wordbreaker: cleaning up fulltext noise words");
157 for (i = 0 ; i < 26 ; i++)
159 cur = noise_words[i];
173 int intcmp(const void *rec1, const void *rec2) {
176 i1 = *(const int *)rec1;
177 i2 = *(const int *)rec2;
179 if (i1 > i2) return(1);
180 if (i1 < i2) return(-1);
185 void wordbreaker(const char *text, int *num_tokens, int **tokens) {
187 int wb_num_tokens = 0;
188 int wb_num_alloc = 0;
189 int *wb_tokens = NULL;
192 const char *word_start;
193 const char *word_end;
202 if (text == NULL) { /* no NULL text please */
208 if (text[0] == 0) { /* no empty text either */
225 if ( (!isalnum(ch)) && (word_start) ) {
228 /* extract the word */
229 word_len = word_end - word_start;
230 if (word_len >= sizeof word) {
231 syslog(LOG_DEBUG, "wordbreaker: invalid word length: %d", word_len);
232 safestrncpy(word, word_start, sizeof word);
233 word[(sizeof word) - 1] = 0;
236 safestrncpy(word, word_start, word_len+1);
241 /* are we ok with the length? */
242 if ( (word_len >= WB_MIN)
243 && (word_len <= WB_MAX) ) {
244 for (i=0; i<word_len; ++i) {
245 word[i] = tolower(word[i]);
247 /* disqualify noise words */
248 noise = noise_words[(int) (word[0]-'a')];
251 if (noise->len == word_len)
253 if (!strcmp(word, noise->word))
265 CalcCRC16Bytes(word_len, word);
268 if (wb_num_tokens > wb_num_alloc) {
270 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
272 wb_tokens[wb_num_tokens - 1] = word_crc;
277 /* sort and purge dups */
278 if (wb_num_tokens > 1) {
279 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
280 for (i=0; i<(wb_num_tokens-1); ++i) {
281 if (wb_tokens[i] == wb_tokens[i+1]) {
282 memmove(&wb_tokens[i], &wb_tokens[i+1],
283 ((wb_num_tokens - i - 1)*sizeof(int)));
290 *num_tokens = wb_num_tokens;