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"
46 #include "ctdl_module.h"
49 * Noise words are not included in search indices.
50 * NOTE: if the noise word list is altered in any way, the FT_WORDBREAKER_ID
51 * must also be changed, so that the index is rebuilt.
53 static char *noise_words[] = {
120 int intcmp(const void *rec1, const void *rec2) {
123 i1 = *(const int *)rec1;
124 i2 = *(const int *)rec2;
126 if (i1 > i2) return(1);
127 if (i1 < i2) return(-1);
132 void wordbreaker(char *text, int *num_tokens, int **tokens) {
134 int wb_num_tokens = 0;
135 int wb_num_alloc = 0;
136 int *wb_tokens = NULL;
147 if (text == NULL) { /* no NULL text please */
153 if (text[0] == 0) { /* no empty text either */
170 if ( (!isalnum(ch)) && (word_start) ) {
174 /* extract the word */
175 word_len = word_end - word_start;
176 if (word_len >= sizeof word) {
177 CtdlLogPrintf(CTDL_DEBUG, "Invalid word length: %d\n", word_len);
178 safestrncpy(word, word_start, sizeof word);
179 word[(sizeof word) - 1] = 0;
182 safestrncpy(word, word_start, word_len+1);
187 /* are we ok with the length? */
188 if ( (word_len >= WB_MIN)
189 && (word_len <= WB_MAX) ) {
190 for (i=0; i<word_len; ++i) {
191 word[i] = tolower(word[i]);
193 /* disqualify noise words */
194 for (i=0; i<(sizeof(noise_words)/sizeof(char *)); ++i) {
195 if (!strcmp(word, noise_words[i])) {
204 CalcCRC16Bytes(word_len, word);
207 if (wb_num_tokens > wb_num_alloc) {
209 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
211 wb_tokens[wb_num_tokens - 1] = word_crc;
216 /* sort and purge dups */
217 if (wb_num_tokens > 1) {
218 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
219 for (i=0; i<(wb_num_tokens-1); ++i) {
220 if (wb_tokens[i] == wb_tokens[i+1]) {
221 memmove(&wb_tokens[i], &wb_tokens[i+1],
222 ((wb_num_tokens - i - 1)*sizeof(int)));
229 *num_tokens = wb_num_tokens;