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;
153 int intcmp(const void *rec1, const void *rec2) {
156 i1 = *(const int *)rec1;
157 i2 = *(const int *)rec2;
159 if (i1 > i2) return(1);
160 if (i1 < i2) return(-1);
165 void wordbreaker(const char *text, int *num_tokens, int **tokens) {
167 int wb_num_tokens = 0;
168 int wb_num_alloc = 0;
169 int *wb_tokens = NULL;
172 const char *word_start;
173 const char *word_end;
182 if (text == NULL) { /* no NULL text please */
188 if (text[0] == 0) { /* no empty text either */
205 if ( (!isalnum(ch)) && (word_start) ) {
208 /* extract the word */
209 word_len = word_end - word_start;
210 if (word_len >= sizeof word) {
211 syslog(LOG_DEBUG, "wordbreaker: invalid word length: %d", word_len);
212 safestrncpy(word, word_start, sizeof word);
213 word[(sizeof word) - 1] = 0;
216 safestrncpy(word, word_start, word_len+1);
221 /* are we ok with the length? */
222 if ( (word_len >= WB_MIN)
223 && (word_len <= WB_MAX) ) {
224 for (i=0; i<word_len; ++i) {
225 word[i] = tolower(word[i]);
227 /* disqualify noise words */
228 noise = noise_words[(int) (word[0]-'a')];
231 if (noise->len == word_len)
233 if (!strcmp(word, noise->word))
245 CalcCRC16Bytes(word_len, word);
248 if (wb_num_tokens > wb_num_alloc) {
250 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
252 wb_tokens[wb_num_tokens - 1] = word_crc;
257 /* sort and purge dups */
258 if (wb_num_tokens > 1) {
259 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
260 for (i=0; i<(wb_num_tokens-1); ++i) {
261 if (wb_tokens[i] == wb_tokens[i+1]) {
262 memmove(&wb_tokens[i], &wb_tokens[i+1],
263 ((wb_num_tokens - i - 1)*sizeof(int)));
270 *num_tokens = wb_num_tokens;