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>
29 #include <libcitadel.h>
32 #include "sysdep_decls.h"
33 #include "citserver.h"
39 #include "ft_wordbreaker.h"
41 #include "ctdl_module.h"
44 * Noise words are not included in search indices.
45 * NOTE: if the noise word list is altered in any way, the FT_WORDBREAKER_ID
46 * must also be changed, so that the index is rebuilt.
48 static char *noise_words[] = {
111 #define NUM_NOISE (sizeof(noise_words) / sizeof(char *))
117 int intcmp(const void *rec1, const void *rec2) {
120 i1 = *(const int *)rec1;
121 i2 = *(const int *)rec2;
123 if (i1 > i2) return(1);
124 if (i1 < i2) return(-1);
129 void wordbreaker(const char *text, int *num_tokens, int **tokens) {
131 int wb_num_tokens = 0;
132 int wb_num_alloc = 0;
133 int *wb_tokens = NULL;
136 const char *word_start;
137 const char *word_end;
144 if (text == NULL) { /* no NULL text please */
150 if (text[0] == 0) { /* no empty text either */
167 if ( (!isalnum(ch)) && (word_start) ) {
170 /* extract the word */
171 word_len = word_end - word_start;
172 if (word_len >= sizeof word) {
173 syslog(LOG_DEBUG, "wordbreaker: invalid word length: %d", word_len);
174 safestrncpy(word, word_start, sizeof word);
175 word[(sizeof word) - 1] = 0;
178 safestrncpy(word, word_start, word_len+1);
183 /* are we ok with the length? */
184 if ( (word_len >= WB_MIN) && (word_len <= WB_MAX) ) {
185 for (i=0; i<word_len; ++i) {
186 word[i] = tolower(word[i]);
188 /* disqualify noise words */
189 for (i=0; i<NUM_NOISE; ++i) {
190 if (!strcmp(word, noise_words[i])) {
199 word_crc = (int) CalcCRC16Bytes(word_len, word);
202 if (wb_num_tokens > wb_num_alloc) {
204 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
206 wb_tokens[wb_num_tokens - 1] = word_crc;
211 /* sort and purge dups */
212 if (wb_num_tokens > 1) {
213 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
214 for (i=0; i<(wb_num_tokens-1); ++i) {
215 if (wb_tokens[i] == wb_tokens[i+1]) {
216 memmove(&wb_tokens[i], &wb_tokens[i+1],
217 ((wb_num_tokens - i - 1)*sizeof(int)));
224 *num_tokens = wb_num_tokens;