4 * Default wordbreaker module for full text indexing.
17 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
34 #include <libcitadel.h>
37 #include "sysdep_decls.h"
38 #include "citserver.h"
44 #include "ft_wordbreaker.h"
48 * Noise words are not included in search indices.
49 * NOTE: if the noise word list is altered in any way, the FT_WORDBREAKER_ID
50 * must also be changed, so that the index is rebuilt.
52 static char *noise_words[] = {
119 int intcmp(const void *rec1, const void *rec2) {
122 i1 = *(const int *)rec1;
123 i2 = *(const int *)rec2;
125 if (i1 > i2) return(1);
126 if (i1 < i2) return(-1);
131 void wordbreaker(char *text, int *num_tokens, int **tokens) {
133 int wb_num_tokens = 0;
134 int wb_num_alloc = 0;
135 int *wb_tokens = NULL;
146 if (text == NULL) { /* no NULL text please */
152 if (text[0] == 0) { /* no empty text either */
169 if ( (!isalnum(ch)) && (word_start) ) {
173 /* extract the word */
174 word_len = word_end - word_start;
175 if (word_len >= sizeof word) {
176 lprintf(CTDL_DEBUG, "Invalid word length: %d\n", word_len);
177 safestrncpy(word, word_start, sizeof word);
178 word[(sizeof word) - 1] = 0;
181 safestrncpy(word, word_start, word_len+1);
186 /* are we ok with the length? */
187 if ( (word_len >= WB_MIN)
188 && (word_len <= WB_MAX) ) {
189 for (i=0; i<word_len; ++i) {
190 word[i] = tolower(word[i]);
192 /* disqualify noise words */
193 for (i=0; i<(sizeof(noise_words)/sizeof(char *)); ++i) {
194 if (!strcmp(word, noise_words[i])) {
203 CalcCRC16Bytes(word_len, word);
206 if (wb_num_tokens > wb_num_alloc) {
208 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
210 wb_tokens[wb_num_tokens - 1] = word_crc;
215 /* sort and purge dups */
216 if (wb_num_tokens > 1) {
217 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
218 for (i=0; i<(wb_num_tokens-1); ++i) {
219 if (wb_tokens[i] == wb_tokens[i+1]) {
220 memmove(&wb_tokens[i], &wb_tokens[i+1],
221 ((wb_num_tokens - i - 1)*sizeof(int)));
228 *num_tokens = wb_num_tokens;