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>
36 #include "sysdep_decls.h"
37 #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[] = {
163 int intcmp(const void *rec1, const void *rec2) {
166 i1 = *(const int *)rec1;
167 i2 = *(const int *)rec2;
169 if (i1 > i2) return(1);
170 if (i1 < i2) return(-1);
175 void wordbreaker(char *text, int *num_tokens, int **tokens) {
177 int wb_num_tokens = 0;
178 int wb_num_alloc = 0;
179 int *wb_tokens = NULL;
190 if (text == NULL) { /* no NULL text please */
196 if (text[0] == 0) { /* no empty text either */
213 if ( (!isalnum(ch)) && (word_start) ) {
217 /* extract the word */
218 word_len = word_end - word_start + 1;
219 safestrncpy(word, word_start, sizeof word);
220 if (word_len >= sizeof word) {
221 lprintf(CTDL_DEBUG, "Invalid word length: %d\n", word_len);
222 word[(sizeof word_len) - 1] = 0;
229 /* disqualify noise words */
230 for (i=0; i<(sizeof(noise_words)/sizeof(char *)); ++i) {
231 if (!strcasecmp(word, noise_words[i])) {
237 /* are we ok with the length? */
238 if ( (word_len >= WB_MIN)
239 && (word_len <= WB_MAX) ) {
240 for (i=0; i<word_len; ++i) {
241 word[i] = tolower(word[i]);
244 CalcCRC16Bytes(word_len, word);
247 if (wb_num_tokens > wb_num_alloc) {
249 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
251 wb_tokens[wb_num_tokens - 1] = word_crc;
256 /* sort and purge dups */
257 if (wb_num_tokens > 1) {
258 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
259 for (i=0; i<(wb_num_tokens-1); ++i) {
260 if (wb_tokens[i] == wb_tokens[i+1]) {
261 memmove(&wb_tokens[i], &wb_tokens[i+1],
262 ((wb_num_tokens - i - 1)*sizeof(int)));
269 *num_tokens = wb_num_tokens;