jk-ferret 0.11.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +90 -0
  4. data/RELEASE_CHANGES +137 -0
  5. data/RELEASE_NOTES +60 -0
  6. data/Rakefile +443 -0
  7. data/TODO +109 -0
  8. data/TUTORIAL +231 -0
  9. data/bin/ferret-browser +79 -0
  10. data/ext/BZLIB_blocksort.c +1094 -0
  11. data/ext/BZLIB_bzlib.c +1578 -0
  12. data/ext/BZLIB_compress.c +672 -0
  13. data/ext/BZLIB_crctable.c +104 -0
  14. data/ext/BZLIB_decompress.c +626 -0
  15. data/ext/BZLIB_huffman.c +205 -0
  16. data/ext/BZLIB_randtable.c +84 -0
  17. data/ext/STEMMER_api.c +66 -0
  18. data/ext/STEMMER_libstemmer.c +93 -0
  19. data/ext/STEMMER_stem_ISO_8859_1_danish.c +337 -0
  20. data/ext/STEMMER_stem_ISO_8859_1_dutch.c +624 -0
  21. data/ext/STEMMER_stem_ISO_8859_1_english.c +1117 -0
  22. data/ext/STEMMER_stem_ISO_8859_1_finnish.c +762 -0
  23. data/ext/STEMMER_stem_ISO_8859_1_french.c +1246 -0
  24. data/ext/STEMMER_stem_ISO_8859_1_german.c +503 -0
  25. data/ext/STEMMER_stem_ISO_8859_1_hungarian.c +1230 -0
  26. data/ext/STEMMER_stem_ISO_8859_1_italian.c +1065 -0
  27. data/ext/STEMMER_stem_ISO_8859_1_norwegian.c +297 -0
  28. data/ext/STEMMER_stem_ISO_8859_1_porter.c +749 -0
  29. data/ext/STEMMER_stem_ISO_8859_1_portuguese.c +1017 -0
  30. data/ext/STEMMER_stem_ISO_8859_1_spanish.c +1093 -0
  31. data/ext/STEMMER_stem_ISO_8859_1_swedish.c +307 -0
  32. data/ext/STEMMER_stem_ISO_8859_2_romanian.c +998 -0
  33. data/ext/STEMMER_stem_KOI8_R_russian.c +700 -0
  34. data/ext/STEMMER_stem_UTF_8_danish.c +339 -0
  35. data/ext/STEMMER_stem_UTF_8_dutch.c +634 -0
  36. data/ext/STEMMER_stem_UTF_8_english.c +1125 -0
  37. data/ext/STEMMER_stem_UTF_8_finnish.c +768 -0
  38. data/ext/STEMMER_stem_UTF_8_french.c +1256 -0
  39. data/ext/STEMMER_stem_UTF_8_german.c +509 -0
  40. data/ext/STEMMER_stem_UTF_8_hungarian.c +1234 -0
  41. data/ext/STEMMER_stem_UTF_8_italian.c +1073 -0
  42. data/ext/STEMMER_stem_UTF_8_norwegian.c +299 -0
  43. data/ext/STEMMER_stem_UTF_8_porter.c +755 -0
  44. data/ext/STEMMER_stem_UTF_8_portuguese.c +1023 -0
  45. data/ext/STEMMER_stem_UTF_8_romanian.c +1004 -0
  46. data/ext/STEMMER_stem_UTF_8_russian.c +694 -0
  47. data/ext/STEMMER_stem_UTF_8_spanish.c +1097 -0
  48. data/ext/STEMMER_stem_UTF_8_swedish.c +309 -0
  49. data/ext/STEMMER_stem_UTF_8_turkish.c +2205 -0
  50. data/ext/STEMMER_utilities.c +478 -0
  51. data/ext/analysis.c +1710 -0
  52. data/ext/analysis.h +266 -0
  53. data/ext/api.h +26 -0
  54. data/ext/array.c +125 -0
  55. data/ext/array.h +62 -0
  56. data/ext/bitvector.c +96 -0
  57. data/ext/bitvector.h +594 -0
  58. data/ext/bzlib.h +282 -0
  59. data/ext/bzlib_private.h +503 -0
  60. data/ext/compound_io.c +384 -0
  61. data/ext/config.h +52 -0
  62. data/ext/document.c +159 -0
  63. data/ext/document.h +63 -0
  64. data/ext/except.c +102 -0
  65. data/ext/except.h +176 -0
  66. data/ext/extconf.rb +15 -0
  67. data/ext/ferret.c +416 -0
  68. data/ext/ferret.h +94 -0
  69. data/ext/field_index.c +262 -0
  70. data/ext/field_index.h +52 -0
  71. data/ext/filter.c +157 -0
  72. data/ext/fs_store.c +493 -0
  73. data/ext/global.c +458 -0
  74. data/ext/global.h +302 -0
  75. data/ext/hash.c +524 -0
  76. data/ext/hash.h +515 -0
  77. data/ext/hashset.c +192 -0
  78. data/ext/hashset.h +215 -0
  79. data/ext/header.h +58 -0
  80. data/ext/helper.c +63 -0
  81. data/ext/helper.h +21 -0
  82. data/ext/index.c +6804 -0
  83. data/ext/index.h +935 -0
  84. data/ext/internal.h +1019 -0
  85. data/ext/lang.c +10 -0
  86. data/ext/lang.h +68 -0
  87. data/ext/libstemmer.h +79 -0
  88. data/ext/mempool.c +88 -0
  89. data/ext/mempool.h +43 -0
  90. data/ext/modules.h +190 -0
  91. data/ext/multimapper.c +351 -0
  92. data/ext/multimapper.h +60 -0
  93. data/ext/posh.c +1006 -0
  94. data/ext/posh.h +973 -0
  95. data/ext/priorityqueue.c +149 -0
  96. data/ext/priorityqueue.h +155 -0
  97. data/ext/q_boolean.c +1621 -0
  98. data/ext/q_const_score.c +162 -0
  99. data/ext/q_filtered_query.c +212 -0
  100. data/ext/q_fuzzy.c +280 -0
  101. data/ext/q_match_all.c +149 -0
  102. data/ext/q_multi_term.c +673 -0
  103. data/ext/q_parser.c +3103 -0
  104. data/ext/q_phrase.c +1206 -0
  105. data/ext/q_prefix.c +98 -0
  106. data/ext/q_range.c +682 -0
  107. data/ext/q_span.c +2390 -0
  108. data/ext/q_term.c +337 -0
  109. data/ext/q_wildcard.c +167 -0
  110. data/ext/r_analysis.c +2626 -0
  111. data/ext/r_index.c +3468 -0
  112. data/ext/r_qparser.c +635 -0
  113. data/ext/r_search.c +4490 -0
  114. data/ext/r_store.c +513 -0
  115. data/ext/r_utils.c +1131 -0
  116. data/ext/ram_store.c +476 -0
  117. data/ext/scanner.c +895 -0
  118. data/ext/scanner.h +36 -0
  119. data/ext/scanner_mb.c +6701 -0
  120. data/ext/scanner_utf8.c +4415 -0
  121. data/ext/search.c +1864 -0
  122. data/ext/search.h +953 -0
  123. data/ext/similarity.c +151 -0
  124. data/ext/similarity.h +89 -0
  125. data/ext/sort.c +786 -0
  126. data/ext/stem_ISO_8859_1_danish.h +16 -0
  127. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  128. data/ext/stem_ISO_8859_1_english.h +16 -0
  129. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  130. data/ext/stem_ISO_8859_1_french.h +16 -0
  131. data/ext/stem_ISO_8859_1_german.h +16 -0
  132. data/ext/stem_ISO_8859_1_hungarian.h +16 -0
  133. data/ext/stem_ISO_8859_1_italian.h +16 -0
  134. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  135. data/ext/stem_ISO_8859_1_porter.h +16 -0
  136. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  137. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  138. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  139. data/ext/stem_ISO_8859_2_romanian.h +16 -0
  140. data/ext/stem_KOI8_R_russian.h +16 -0
  141. data/ext/stem_UTF_8_danish.h +16 -0
  142. data/ext/stem_UTF_8_dutch.h +16 -0
  143. data/ext/stem_UTF_8_english.h +16 -0
  144. data/ext/stem_UTF_8_finnish.h +16 -0
  145. data/ext/stem_UTF_8_french.h +16 -0
  146. data/ext/stem_UTF_8_german.h +16 -0
  147. data/ext/stem_UTF_8_hungarian.h +16 -0
  148. data/ext/stem_UTF_8_italian.h +16 -0
  149. data/ext/stem_UTF_8_norwegian.h +16 -0
  150. data/ext/stem_UTF_8_porter.h +16 -0
  151. data/ext/stem_UTF_8_portuguese.h +16 -0
  152. data/ext/stem_UTF_8_romanian.h +16 -0
  153. data/ext/stem_UTF_8_russian.h +16 -0
  154. data/ext/stem_UTF_8_spanish.h +16 -0
  155. data/ext/stem_UTF_8_swedish.h +16 -0
  156. data/ext/stem_UTF_8_turkish.h +16 -0
  157. data/ext/stopwords.c +410 -0
  158. data/ext/store.c +698 -0
  159. data/ext/store.h +799 -0
  160. data/ext/symbol.c +10 -0
  161. data/ext/symbol.h +23 -0
  162. data/ext/term_vectors.c +73 -0
  163. data/ext/threading.h +31 -0
  164. data/ext/win32.h +62 -0
  165. data/lib/ferret.rb +30 -0
  166. data/lib/ferret/browser.rb +246 -0
  167. data/lib/ferret/browser/s/global.js +192 -0
  168. data/lib/ferret/browser/s/style.css +148 -0
  169. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  170. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  171. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  172. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  173. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  174. data/lib/ferret/browser/views/layout.rhtml +22 -0
  175. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  176. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  177. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  178. data/lib/ferret/browser/webrick.rb +14 -0
  179. data/lib/ferret/document.rb +130 -0
  180. data/lib/ferret/field_infos.rb +44 -0
  181. data/lib/ferret/field_symbol.rb +87 -0
  182. data/lib/ferret/index.rb +973 -0
  183. data/lib/ferret/number_tools.rb +157 -0
  184. data/lib/ferret/version.rb +3 -0
  185. data/setup.rb +1555 -0
  186. data/test/long_running/largefile/tc_largefile.rb +46 -0
  187. data/test/test_all.rb +5 -0
  188. data/test/test_helper.rb +29 -0
  189. data/test/test_installed.rb +1 -0
  190. data/test/threading/number_to_spoken.rb +132 -0
  191. data/test/threading/thread_safety_index_test.rb +88 -0
  192. data/test/threading/thread_safety_read_write_test.rb +73 -0
  193. data/test/threading/thread_safety_test.rb +133 -0
  194. data/test/unit/analysis/tc_analyzer.rb +550 -0
  195. data/test/unit/analysis/tc_token_stream.rb +653 -0
  196. data/test/unit/index/tc_index.rb +867 -0
  197. data/test/unit/index/tc_index_reader.rb +699 -0
  198. data/test/unit/index/tc_index_writer.rb +447 -0
  199. data/test/unit/index/th_doc.rb +332 -0
  200. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  201. data/test/unit/search/tc_filter.rb +156 -0
  202. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  203. data/test/unit/search/tc_index_searcher.rb +67 -0
  204. data/test/unit/search/tc_multi_searcher.rb +128 -0
  205. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  206. data/test/unit/search/tc_search_and_sort.rb +179 -0
  207. data/test/unit/search/tc_sort.rb +49 -0
  208. data/test/unit/search/tc_sort_field.rb +27 -0
  209. data/test/unit/search/tc_spans.rb +190 -0
  210. data/test/unit/search/tm_searcher.rb +436 -0
  211. data/test/unit/store/tc_fs_store.rb +115 -0
  212. data/test/unit/store/tc_ram_store.rb +35 -0
  213. data/test/unit/store/tm_store.rb +34 -0
  214. data/test/unit/store/tm_store_lock.rb +68 -0
  215. data/test/unit/tc_document.rb +81 -0
  216. data/test/unit/tc_field_symbol.rb +26 -0
  217. data/test/unit/ts_analysis.rb +2 -0
  218. data/test/unit/ts_index.rb +2 -0
  219. data/test/unit/ts_largefile.rb +4 -0
  220. data/test/unit/ts_query_parser.rb +2 -0
  221. data/test/unit/ts_search.rb +2 -0
  222. data/test/unit/ts_store.rb +2 -0
  223. data/test/unit/ts_utils.rb +2 -0
  224. data/test/unit/utils/tc_bit_vector.rb +295 -0
  225. data/test/unit/utils/tc_number_tools.rb +117 -0
  226. data/test/unit/utils/tc_priority_queue.rb +106 -0
  227. data/test/utils/content_generator.rb +226 -0
  228. metadata +319 -0
@@ -0,0 +1,162 @@
1
+ #include "search.h"
2
+ #include <string.h>
3
+ #include "internal.h"
4
+
5
+ /***************************************************************************
6
+ *
7
+ * ConstantScoreScorer
8
+ *
9
+ ***************************************************************************/
10
+
11
+ #define CScQ(query) ((ConstantScoreQuery *)(query))
12
+ #define CScSc(scorer) ((ConstantScoreScorer *)(scorer))
13
+
14
+ typedef struct ConstantScoreScorer
15
+ {
16
+ Scorer super;
17
+ BitVector *bv;
18
+ float score;
19
+ } ConstantScoreScorer;
20
+
21
+ static float cssc_score(Scorer *self)
22
+ {
23
+ return CScSc(self)->score;
24
+ }
25
+
26
+ static bool cssc_next(Scorer *self)
27
+ {
28
+ return ((self->doc = bv_scan_next(CScSc(self)->bv)) >= 0);
29
+ }
30
+
31
+ static bool cssc_skip_to(Scorer *self, int doc_num)
32
+ {
33
+ return ((self->doc = bv_scan_next_from(CScSc(self)->bv, doc_num)) >= 0);
34
+ }
35
+
36
+ static Explanation *cssc_explain(Scorer *self, int doc_num)
37
+ {
38
+ (void)self; (void)doc_num;
39
+ return expl_new(1.0, "ConstantScoreScorer");
40
+ }
41
+
42
+ static Scorer *cssc_new(Weight *weight, IndexReader *ir)
43
+ {
44
+ Scorer *self = scorer_new(ConstantScoreScorer, weight->similarity);
45
+ Filter *filter = CScQ(weight->query)->filter;
46
+
47
+ CScSc(self)->score = weight->value;
48
+ CScSc(self)->bv = filt_get_bv(filter, ir);
49
+
50
+ self->score = &cssc_score;
51
+ self->next = &cssc_next;
52
+ self->skip_to = &cssc_skip_to;
53
+ self->explain = &cssc_explain;
54
+ self->destroy = &scorer_destroy_i;
55
+ return self;
56
+ }
57
+
58
+ /***************************************************************************
59
+ *
60
+ * ConstantScoreWeight
61
+ *
62
+ ***************************************************************************/
63
+
64
+ static char *csw_to_s(Weight *self)
65
+ {
66
+ return strfmt("ConstantScoreWeight(%f)", self->value);
67
+ }
68
+
69
+ static Explanation *csw_explain(Weight *self, IndexReader *ir, int doc_num)
70
+ {
71
+ Filter *filter = CScQ(self->query)->filter;
72
+ Explanation *expl;
73
+ char *filter_str = filter->to_s(filter);
74
+ BitVector *bv = filt_get_bv(filter, ir);
75
+
76
+ if (bv_get(bv, doc_num)) {
77
+ expl = expl_new(self->value,
78
+ "ConstantScoreQuery(%s), product of:", filter_str);
79
+ expl_add_detail(expl, expl_new(self->query->boost, "boost"));
80
+ expl_add_detail(expl, expl_new(self->qnorm, "query_norm"));
81
+ }
82
+ else {
83
+ expl = expl_new(self->value,
84
+ "ConstantScoreQuery(%s), does not match id %d",
85
+ filter_str, doc_num);
86
+ }
87
+ free(filter_str);
88
+ return expl;
89
+ }
90
+
91
+ static Weight *csw_new(Query *query, Searcher *searcher)
92
+ {
93
+ Weight *self = w_new(Weight, query);
94
+
95
+ self->scorer = &cssc_new;
96
+ self->explain = &csw_explain;
97
+ self->to_s = &csw_to_s;
98
+
99
+ self->similarity = query->get_similarity(query, searcher);
100
+ self->idf = 1.0;
101
+
102
+ return self;
103
+ }
104
+
105
+ /***************************************************************************
106
+ *
107
+ * ConstantScoreQuery
108
+ *
109
+ ***************************************************************************/
110
+
111
+ static char *csq_to_s(Query *self, Symbol default_field)
112
+ {
113
+ Filter *filter = CScQ(self)->filter;
114
+ char *filter_str = filter->to_s(filter);
115
+ char *buffer;
116
+ (void)default_field;
117
+ if (self->boost == 1.0) {
118
+ buffer = strfmt("ConstantScore(%s)", filter_str);
119
+ }
120
+ else {
121
+ buffer = strfmt("ConstantScore(%s)^%f", filter_str, self->boost);
122
+ }
123
+ free(filter_str);
124
+ return buffer;;
125
+ }
126
+
127
+ static void csq_destroy(Query *self)
128
+ {
129
+ filt_deref(CScQ(self)->filter);
130
+ q_destroy_i(self);
131
+ }
132
+
133
+ static unsigned long csq_hash(Query *self)
134
+ {
135
+ return filt_hash(CScQ(self)->filter);
136
+ }
137
+
138
+ static int csq_eq(Query *self, Query *o)
139
+ {
140
+ return filt_eq(CScQ(self)->filter, CScQ(o)->filter);
141
+ }
142
+
143
+ Query *csq_new_nr(Filter *filter)
144
+ {
145
+ Query *self = q_new(ConstantScoreQuery);
146
+ CScQ(self)->filter = filter;
147
+
148
+ self->type = CONSTANT_QUERY;
149
+ self->to_s = &csq_to_s;
150
+ self->hash = &csq_hash;
151
+ self->eq = &csq_eq;
152
+ self->destroy_i = &csq_destroy;
153
+ self->create_weight_i = &csw_new;
154
+
155
+ return self;
156
+ }
157
+
158
+ Query *csq_new(Filter *filter)
159
+ {
160
+ REF(filter);
161
+ return csq_new_nr(filter);
162
+ }
@@ -0,0 +1,212 @@
1
+ #include "search.h"
2
+ #include <string.h>
3
+ #include "internal.h"
4
+
5
+ /***************************************************************************
6
+ *
7
+ * FilteredQueryScorer
8
+ *
9
+ ***************************************************************************/
10
+
11
+ #define FQSc(scorer) ((FilteredQueryScorer *)(scorer))
12
+ #define FQQ(query) ((FilteredQuery *)(query))
13
+
14
+ typedef struct FilteredQueryScorer
15
+ {
16
+ Scorer super;
17
+ Scorer *sub_scorer;
18
+ BitVector *bv;
19
+ } FilteredQueryScorer;
20
+
21
+ static float fqsc_score(Scorer *self)
22
+ {
23
+ Scorer *sub_sc = FQSc(self)->sub_scorer;
24
+ return sub_sc->score(sub_sc);
25
+ }
26
+
27
+ static bool fqsc_next(Scorer *self)
28
+ {
29
+ Scorer *sub_sc = FQSc(self)->sub_scorer;
30
+ BitVector *bv = FQSc(self)->bv;
31
+ while (sub_sc->next(sub_sc)) {
32
+ self->doc = sub_sc->doc;
33
+ if (bv_get(bv, self->doc)) return true;
34
+ }
35
+ return false;
36
+ }
37
+
38
+ static bool fqsc_skip_to(Scorer *self, int doc_num)
39
+ {
40
+ Scorer *sub_sc = FQSc(self)->sub_scorer;
41
+ BitVector *bv = FQSc(self)->bv;
42
+ if (sub_sc->skip_to(sub_sc, doc_num)) {
43
+ do {
44
+ self->doc = sub_sc->doc;
45
+ if (bv_get(bv, self->doc)) {
46
+ return true;
47
+ }
48
+ } while (sub_sc->next(sub_sc));
49
+ }
50
+ return false;
51
+ }
52
+
53
+ static Explanation *fqsc_explain(Scorer *self, int doc_num)
54
+ {
55
+ Scorer *sub_sc = FQSc(self)->sub_scorer;
56
+ return sub_sc->explain(sub_sc, doc_num);
57
+ }
58
+
59
+ static void fqsc_destroy(Scorer *self)
60
+ {
61
+ FilteredQueryScorer *fqsc = FQSc(self);
62
+ fqsc->sub_scorer->destroy(fqsc->sub_scorer);
63
+ scorer_destroy_i(self);
64
+ }
65
+
66
+ static Scorer *fqsc_new(Scorer *scorer, BitVector *bv, Similarity *sim)
67
+ {
68
+ Scorer *self = scorer_new(FilteredQueryScorer, sim);
69
+
70
+ FQSc(self)->sub_scorer = scorer;
71
+ FQSc(self)->bv = bv;
72
+
73
+ self->score = &fqsc_score;
74
+ self->next = &fqsc_next;
75
+ self->skip_to = &fqsc_skip_to;
76
+ self->explain = &fqsc_explain;
77
+ self->destroy = &fqsc_destroy;
78
+
79
+ return self;
80
+ }
81
+
82
+ /***************************************************************************
83
+ *
84
+ * Weight
85
+ *
86
+ ***************************************************************************/
87
+
88
+ #define FQW(weight) ((FilteredQueryWeight *)(weight))
89
+ typedef struct FilteredQueryWeight
90
+ {
91
+ Weight super;
92
+ Weight *sub_weight;
93
+ } FilteredQueryWeight;
94
+
95
+ static char *fqw_to_s(Weight *self)
96
+ {
97
+ return strfmt("FilteredQueryWeight(%f)", self->value);
98
+ }
99
+
100
+ static float fqw_sum_of_squared_weights(Weight *self)
101
+ {
102
+ Weight *sub_weight = FQW(self)->sub_weight;
103
+ return sub_weight->sum_of_squared_weights(sub_weight);
104
+ }
105
+
106
+ static void fqw_normalize(Weight *self, float normalization_factor)
107
+ {
108
+ Weight *sub_weight = FQW(self)->sub_weight;
109
+ sub_weight->normalize(sub_weight, normalization_factor);
110
+ }
111
+
112
+ static float fqw_get_value(Weight *self)
113
+ {
114
+ Weight *sub_weight = FQW(self)->sub_weight;
115
+ return sub_weight->get_value(sub_weight);
116
+ }
117
+
118
+ static Explanation *fqw_explain(Weight *self, IndexReader *ir, int doc_num)
119
+ {
120
+ Weight *sub_weight = FQW(self)->sub_weight;
121
+ return sub_weight->explain(sub_weight, ir, doc_num);
122
+ }
123
+
124
+ static Scorer *fqw_scorer(Weight *self, IndexReader *ir)
125
+ {
126
+ Weight *sub_weight = FQW(self)->sub_weight;
127
+ Scorer *scorer = sub_weight->scorer(sub_weight, ir);
128
+ Filter *filter = FQQ(self->query)->filter;
129
+
130
+ return fqsc_new(scorer, filt_get_bv(filter, ir), self->similarity);
131
+ }
132
+
133
+ static void fqw_destroy(Weight *self)
134
+ {
135
+ Weight *sub_weight = FQW(self)->sub_weight;
136
+ sub_weight->destroy(sub_weight);
137
+ w_destroy(self);
138
+ }
139
+
140
+ static Weight *fqw_new(Query *query, Weight *sub_weight, Similarity *sim)
141
+ {
142
+ Weight *self = w_new(FilteredQueryWeight, query);
143
+
144
+ FQW(self)->sub_weight = sub_weight;
145
+
146
+ self->get_value = &fqw_get_value;
147
+ self->normalize = &fqw_normalize;
148
+ self->scorer = &fqw_scorer;
149
+ self->explain = &fqw_explain;
150
+ self->to_s = &fqw_to_s;
151
+ self->destroy = &fqw_destroy;
152
+ self->sum_of_squared_weights = &fqw_sum_of_squared_weights;
153
+
154
+ self->similarity = sim;
155
+ self->idf = 1.0;
156
+ self->value = sub_weight->value;
157
+
158
+ return self;
159
+ }
160
+
161
+ /***************************************************************************
162
+ *
163
+ * FilteredQuery
164
+ *
165
+ ***************************************************************************/
166
+
167
+ static char *fq_to_s(Query *self, Symbol default_field)
168
+ {
169
+ FilteredQuery *fq = FQQ(self);
170
+ char *filter_str = fq->filter->to_s(fq->filter);
171
+ char *query_str = fq->query->to_s(fq->query, default_field);
172
+ char *buffer;
173
+ if (self->boost == 1.0) {
174
+ buffer = strfmt("FilteredQuery(query:%s, filter:%s)",
175
+ query_str, filter_str);
176
+ } else {
177
+ buffer = strfmt("FilteredQuery(query:%s, filter:%s)^%f",
178
+ query_str, filter_str, self->boost);
179
+ }
180
+ free(filter_str);
181
+ free(query_str);
182
+ return buffer;;
183
+ }
184
+
185
+ static void fq_destroy(Query *self)
186
+ {
187
+ filt_deref(FQQ(self)->filter);
188
+ q_deref(FQQ(self)->query);
189
+ q_destroy_i(self);
190
+ }
191
+
192
+ static Weight *fq_new_weight(Query *self, Searcher *searcher)
193
+ {
194
+ Query *sub_query = FQQ(self)->query;
195
+ return fqw_new(self, q_weight(sub_query, searcher),
196
+ searcher->similarity);
197
+ }
198
+
199
+ Query *fq_new(Query *query, Filter *filter)
200
+ {
201
+ Query *self = q_new(FilteredQuery);
202
+
203
+ FQQ(self)->query = query;
204
+ FQQ(self)->filter = filter;
205
+
206
+ self->type = FILTERED_QUERY;
207
+ self->to_s = &fq_to_s;
208
+ self->destroy_i = &fq_destroy;
209
+ self->create_weight_i = &fq_new_weight;
210
+
211
+ return self;
212
+ }
data/ext/q_fuzzy.c ADDED
@@ -0,0 +1,280 @@
1
+ #include <string.h>
2
+ #include "search.h"
3
+ #include "helper.h"
4
+ #include "internal.h"
5
+
6
+ /****************************************************************************
7
+ *
8
+ * FuzzyStuff
9
+ *
10
+ * The main method here is the fuzq_score_mn method which scores a term
11
+ * against another term. The other methods all act in support.
12
+ *
13
+ * To learn more about the fuzzy scoring algorithm see;
14
+ *
15
+ * http://en.wikipedia.org/wiki/Levenshtein_distance
16
+ *
17
+ ****************************************************************************/
18
+
19
+ /**
20
+ * Calculate the maximum nomber of allowed edits (or maximum edit distance)
21
+ * for a word to be a match.
22
+ *
23
+ * Note that fuzq->text_len and m are both the lengths text *after* the prefix
24
+ * so `MIN(fuzq->text_len, m) + fuzq->pre_len)` actually gets the byte length
25
+ * of the shorter string out of the query string and the index term being
26
+ * compared.
27
+ */
28
+ static INLINE int fuzq_calculate_max_distance(FuzzyQuery *fuzq, int m)
29
+ {
30
+ return (int)((1.0 - fuzq->min_sim) * (MIN(fuzq->text_len, m) + fuzq->pre_len));
31
+ }
32
+
33
+ /**
34
+ * The max-distance formula gets used a lot - it needs to be calculated for
35
+ * every possible match in the index - so we cache the results for all
36
+ * lengths up to the TYPICAL_LONGEST_WORD limit. For words longer than this we
37
+ * calculate the value live.
38
+ */
39
+ static void fuzq_initialize_max_distances(FuzzyQuery *fuzq)
40
+ {
41
+ int i;
42
+ for (i = 0; i < TYPICAL_LONGEST_WORD; i++) {
43
+ fuzq->max_distances[i] = fuzq_calculate_max_distance(fuzq, i);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Return the cached max-distance value if the word is within the
49
+ * TYPICAL_LONGEST_WORD limit.
50
+ */
51
+ static INLINE int fuzq_get_max_distance(FuzzyQuery *fuzq, int m)
52
+ {
53
+ if (m < TYPICAL_LONGEST_WORD)
54
+ return fuzq->max_distances[m];
55
+ return fuzq_calculate_max_distance(fuzq, m);
56
+ }
57
+
58
+ /**
59
+ * Calculate the similarity score for the +target+ against the query.
60
+ *
61
+ * @params fuzq The Fuzzy Query
62
+ * @params target *the term to compare against minus the prefix
63
+ * @params m the string length of +target+
64
+ * @params n the string length of the query string minus length of the prefix
65
+ */
66
+ static INLINE float fuzq_score_mn(FuzzyQuery *fuzq,
67
+ const char *target,
68
+ const int m, const int n)
69
+ {
70
+ int i, j, prune;
71
+ int *d_curr, *d_prev;
72
+ const char *text = fuzq->text;
73
+ const int max_distance = fuzq_get_max_distance(fuzq, m);
74
+
75
+ /* Just adding the characters of m to n or vice-versa results in
76
+ * too many edits for example "pre" length is 3 and "prefixes"
77
+ * length is 8. We can see that given this optimal circumstance,
78
+ * the edit distance cannot be less than 5 which is 8-3 or more
79
+ * precisesly Math.abs(3-8). If our maximum edit distance is 4,
80
+ * then we can discard this word without looking at it. */
81
+ if (max_distance < ABS(m-n)) {
82
+ return 0.0f;
83
+ }
84
+
85
+ d_curr = fuzq->da;
86
+ d_prev = d_curr + n + 1;
87
+
88
+ /* init array */
89
+ for (j = 0; j <= n; j++) {
90
+ d_curr[j] = j;
91
+ }
92
+
93
+ /* start computing edit distance */
94
+ for (i = 0; i < m;) {
95
+ char s_i = target[i];
96
+ /* swap d_current into d_prev */
97
+ int *d_tmp = d_prev;
98
+ d_prev = d_curr;
99
+ d_curr = d_tmp;
100
+ prune = (d_curr[0] = ++i) > max_distance;
101
+
102
+ for (j = 0; j < n; j++) {
103
+ d_curr[j + 1] = (s_i == text[j])
104
+ ? min3(d_prev[j + 1] + 1, d_curr[j] + 1, d_prev[j])
105
+ : min3(d_prev[j + 1], d_curr[j], d_prev[j]) + 1;
106
+ if (prune && d_curr[j + 1] <= max_distance) {
107
+ prune = false;
108
+ }
109
+ }
110
+ if (prune) {
111
+ return 0.0f;
112
+ }
113
+ }
114
+
115
+ /* this will return less than 0.0 when the edit distance is greater
116
+ * than the number of characters in the shorter word. but this was
117
+ * the formula that was previously used in FuzzyTermEnum, so it has
118
+ * not been changed (even though min_sim must be greater than 0.0) */
119
+ return 1.0f - ((float)d_curr[n] / (float) (fuzq->pre_len + min2(n, m)));
120
+ }
121
+
122
+ /**
123
+ * The following algorithm is taken from Bob Carpenter's FuzzyTermEnum
124
+ * implentation here;
125
+ *
126
+ * http://mail-archives.apache.org/mod_mbox/lucene-java-dev/200606.mbox/%3c448F0E8C.3050901@alias-i.com%3e
127
+ */
128
+ float fuzq_score(FuzzyQuery *fuzq, const char *target)
129
+ {
130
+ const int m = (int)strlen(target);
131
+ const int n = fuzq->text_len;
132
+
133
+ /* we don't have anything to compare. That means if we just add
134
+ * the letters for m we get the new word */
135
+ if (m == 0 || n == 0) {
136
+ if (fuzq->pre_len == 0)
137
+ return 0.0f;
138
+ return 1.0f - ((float) (m+n) / fuzq->pre_len);
139
+ }
140
+
141
+ return fuzq_score_mn(fuzq, target, m, n);
142
+ }
143
+
144
+ /****************************************************************************
145
+ *
146
+ * FuzzyQuery
147
+ *
148
+ ****************************************************************************/
149
+
150
+ #define FzQ(query) ((FuzzyQuery *)(query))
151
+
152
+ static char *fuzq_to_s(Query *self, Symbol curr_field)
153
+ {
154
+ char *buffer, *bptr;
155
+ char *term = FzQ(self)->term;
156
+ Symbol field = FzQ(self)->field;
157
+ bptr = buffer = ALLOC_N(char, strlen(term) + sym_len(field) + 70);
158
+
159
+ if (curr_field != field) {
160
+ bptr += sprintf(bptr, "%s:", S(field));
161
+ }
162
+
163
+ bptr += sprintf(bptr, "%s~", term);
164
+ if (FzQ(self)->min_sim != 0.5) {
165
+ dbl_to_s(bptr, FzQ(self)->min_sim);
166
+ bptr += strlen(bptr);
167
+ }
168
+
169
+ if (self->boost != 1.0) {
170
+ *bptr = '^';
171
+ dbl_to_s(++bptr, self->boost);
172
+ }
173
+
174
+ return buffer;
175
+ }
176
+
177
+ static Query *fuzq_rewrite(Query *self, IndexReader *ir)
178
+ {
179
+ Query *q;
180
+ FuzzyQuery *fuzq = FzQ(self);
181
+
182
+ int pre_len = fuzq->pre_len;
183
+ char *prefix = NULL;
184
+ const char *term = fuzq->term;
185
+ const int field_num = fis_get_field_num(ir->fis, fuzq->field);
186
+ TermEnum *te;
187
+
188
+ if (field_num < 0) {
189
+ return bq_new(true);
190
+ }
191
+ if (fuzq->pre_len >= (int)strlen(term)) {
192
+ return tq_new(fuzq->field, term);
193
+ }
194
+
195
+ q = multi_tq_new_conf(fuzq->field, MTQMaxTerms(self), fuzq->min_sim);
196
+ if (pre_len > 0) {
197
+ prefix = ALLOC_N(char, pre_len + 1);
198
+ strncpy(prefix, term, pre_len);
199
+ prefix[pre_len] = '\0';
200
+ te = ir->terms_from(ir, field_num, prefix);
201
+ }
202
+ else {
203
+ te = ir->terms(ir, field_num);
204
+ }
205
+
206
+ assert(NULL != te);
207
+
208
+ fuzq->scale_factor = (float)(1.0 / (1.0 - fuzq->min_sim));
209
+ fuzq->text = term + pre_len;
210
+ fuzq->text_len = (int)strlen(fuzq->text);
211
+ fuzq->da = REALLOC_N(fuzq->da, int, fuzq->text_len * 2 + 2);
212
+ fuzq_initialize_max_distances(fuzq);
213
+
214
+ do {
215
+ const char *curr_term = te->curr_term;
216
+ const char *curr_suffix = curr_term + pre_len;
217
+ float score = 0.0;
218
+
219
+ if (prefix && strncmp(curr_term, prefix, pre_len) != 0)
220
+ break;
221
+
222
+ score = fuzq_score(fuzq, curr_suffix);
223
+ multi_tq_add_term_boost(q, curr_term, score);
224
+ } while (te->next(te) != NULL);
225
+
226
+ te->close(te);
227
+ if (prefix) free(prefix);
228
+ return q;
229
+ }
230
+
231
+ static void fuzq_destroy(Query *self)
232
+ {
233
+ free(FzQ(self)->term);
234
+ free(FzQ(self)->da);
235
+ q_destroy_i(self);
236
+ }
237
+
238
+ static unsigned long fuzq_hash(Query *self)
239
+ {
240
+ return str_hash(FzQ(self)->term) ^ sym_hash(FzQ(self)->field)
241
+ ^ float2int(FzQ(self)->min_sim) ^ FzQ(self)->pre_len;
242
+ }
243
+
244
+ static int fuzq_eq(Query *self, Query *o)
245
+ {
246
+ FuzzyQuery *fq1 = FzQ(self);
247
+ FuzzyQuery *fq2 = FzQ(o);
248
+
249
+ return (strcmp(fq1->term, fq2->term) == 0)
250
+ && (fq1->field == fq2->field)
251
+ && (fq1->pre_len == fq2->pre_len)
252
+ && (fq1->min_sim == fq2->min_sim);
253
+ }
254
+
255
+ Query *fuzq_new_conf(Symbol field, const char *term,
256
+ float min_sim, int pre_len, int max_terms)
257
+ {
258
+ Query *self = q_new(FuzzyQuery);
259
+
260
+ FzQ(self)->field = field;
261
+ FzQ(self)->term = estrdup(term);
262
+ FzQ(self)->pre_len = pre_len ? pre_len : DEF_PRE_LEN;
263
+ FzQ(self)->min_sim = min_sim ? min_sim : DEF_MIN_SIM;
264
+ MTQMaxTerms(self) = max_terms ? max_terms : DEF_MAX_TERMS;
265
+
266
+ self->type = FUZZY_QUERY;
267
+ self->to_s = &fuzq_to_s;
268
+ self->hash = &fuzq_hash;
269
+ self->eq = &fuzq_eq;
270
+ self->rewrite = &fuzq_rewrite;
271
+ self->destroy_i = &fuzq_destroy;
272
+ self->create_weight_i = &q_create_weight_unsup;
273
+
274
+ return self;
275
+ }
276
+
277
+ Query *fuzq_new(Symbol field, const char *term)
278
+ {
279
+ return fuzq_new_conf(field, term, 0.0f, 0, 0);
280
+ }