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"
40 #include "serv_extensions.h"
45 #include "ft_wordbreaker.h"
51 int intcmp(const void *rec1, const void *rec2) {
54 i1 = *(const int *)rec1;
55 i2 = *(const int *)rec2;
57 if (i1 > i2) return(1);
58 if (i1 < i2) return(-1);
63 void wordbreaker(char *text, int *num_tokens, int **tokens) {
65 int wb_num_tokens = 0;
67 int *wb_tokens = NULL;
78 if (text == NULL) { /* no NULL text please */
84 if (text[0] == 0) { /* no empty text either */
101 if ( (!isalnum(ch)) && (word_start) ) {
105 /* extract the word */
106 word_len = word_end - word_start + 1;
107 safestrncpy(word, word_start, sizeof word);
111 /* are we ok with the length? */
112 if ( (word_len >= WB_MIN)
113 && (word_len <= WB_MAX) ) {
114 for (i=0; i<word_len; ++i) {
115 word[i] = tolower(word[i]);
118 CalcCRC16Bytes(word_len, word);
121 if (wb_num_tokens > wb_num_alloc) {
123 wb_tokens = realloc(wb_tokens, (sizeof(int) * wb_num_alloc));
125 wb_tokens[wb_num_tokens - 1] = word_crc;
130 /* sort and purge dups */
131 if (wb_num_tokens > 1) {
132 qsort(wb_tokens, wb_num_tokens, sizeof(int), intcmp);
133 for (i=0; i<(wb_num_tokens-1); ++i) {
134 if (wb_tokens[i] == wb_tokens[i+1]) {
135 memmove(&wb_tokens[i], &wb_tokens[i+1],
136 ((wb_num_tokens - i - 1)*sizeof(int)));
142 *num_tokens = wb_num_tokens;