sdsykes-ferret 0.11.6.19
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +24 -0
- data/MIT-LICENSE +20 -0
- data/README +102 -0
- data/Rakefile +338 -0
- data/TODO +17 -0
- data/TUTORIAL +231 -0
- data/bin/ferret-browser +79 -0
- data/ext/analysis.c +1555 -0
- data/ext/analysis.h +219 -0
- data/ext/api.c +69 -0
- data/ext/api.h +27 -0
- data/ext/array.c +123 -0
- data/ext/array.h +53 -0
- data/ext/bitvector.c +540 -0
- data/ext/bitvector.h +272 -0
- data/ext/compound_io.c +383 -0
- data/ext/config.h +42 -0
- data/ext/document.c +156 -0
- data/ext/document.h +53 -0
- data/ext/except.c +120 -0
- data/ext/except.h +168 -0
- data/ext/extconf.rb +14 -0
- data/ext/ferret.c +402 -0
- data/ext/ferret.h +91 -0
- data/ext/filter.c +156 -0
- data/ext/fs_store.c +483 -0
- data/ext/global.c +418 -0
- data/ext/global.h +117 -0
- data/ext/hash.c +567 -0
- data/ext/hash.h +473 -0
- data/ext/hashset.c +170 -0
- data/ext/hashset.h +187 -0
- data/ext/header.h +58 -0
- data/ext/helper.c +62 -0
- data/ext/helper.h +13 -0
- data/ext/inc/lang.h +48 -0
- data/ext/inc/threading.h +31 -0
- data/ext/index.c +6425 -0
- data/ext/index.h +961 -0
- data/ext/lang.h +66 -0
- data/ext/libstemmer.c +92 -0
- data/ext/libstemmer.h +79 -0
- data/ext/mempool.c +87 -0
- data/ext/mempool.h +35 -0
- data/ext/modules.h +162 -0
- data/ext/multimapper.c +310 -0
- data/ext/multimapper.h +51 -0
- data/ext/posh.c +1006 -0
- data/ext/posh.h +1007 -0
- data/ext/priorityqueue.c +151 -0
- data/ext/priorityqueue.h +143 -0
- data/ext/q_boolean.c +1608 -0
- data/ext/q_const_score.c +161 -0
- data/ext/q_filtered_query.c +209 -0
- data/ext/q_fuzzy.c +268 -0
- data/ext/q_match_all.c +148 -0
- data/ext/q_multi_term.c +677 -0
- data/ext/q_parser.c +2825 -0
- data/ext/q_phrase.c +1126 -0
- data/ext/q_prefix.c +100 -0
- data/ext/q_range.c +350 -0
- data/ext/q_span.c +2402 -0
- data/ext/q_term.c +337 -0
- data/ext/q_wildcard.c +171 -0
- data/ext/r_analysis.c +2575 -0
- data/ext/r_index.c +3472 -0
- data/ext/r_qparser.c +585 -0
- data/ext/r_search.c +4105 -0
- data/ext/r_store.c +513 -0
- data/ext/r_utils.c +963 -0
- data/ext/ram_store.c +471 -0
- data/ext/search.c +1741 -0
- data/ext/search.h +885 -0
- data/ext/similarity.c +150 -0
- data/ext/similarity.h +82 -0
- data/ext/sort.c +983 -0
- data/ext/stem_ISO_8859_1_danish.c +338 -0
- data/ext/stem_ISO_8859_1_danish.h +16 -0
- data/ext/stem_ISO_8859_1_dutch.c +635 -0
- data/ext/stem_ISO_8859_1_dutch.h +16 -0
- data/ext/stem_ISO_8859_1_english.c +1156 -0
- data/ext/stem_ISO_8859_1_english.h +16 -0
- data/ext/stem_ISO_8859_1_finnish.c +792 -0
- data/ext/stem_ISO_8859_1_finnish.h +16 -0
- data/ext/stem_ISO_8859_1_french.c +1276 -0
- data/ext/stem_ISO_8859_1_french.h +16 -0
- data/ext/stem_ISO_8859_1_german.c +512 -0
- data/ext/stem_ISO_8859_1_german.h +16 -0
- data/ext/stem_ISO_8859_1_italian.c +1091 -0
- data/ext/stem_ISO_8859_1_italian.h +16 -0
- data/ext/stem_ISO_8859_1_norwegian.c +296 -0
- data/ext/stem_ISO_8859_1_norwegian.h +16 -0
- data/ext/stem_ISO_8859_1_porter.c +776 -0
- data/ext/stem_ISO_8859_1_porter.h +16 -0
- data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
- data/ext/stem_ISO_8859_1_portuguese.h +16 -0
- data/ext/stem_ISO_8859_1_spanish.c +1119 -0
- data/ext/stem_ISO_8859_1_spanish.h +16 -0
- data/ext/stem_ISO_8859_1_swedish.c +307 -0
- data/ext/stem_ISO_8859_1_swedish.h +16 -0
- data/ext/stem_KOI8_R_russian.c +701 -0
- data/ext/stem_KOI8_R_russian.h +16 -0
- data/ext/stem_UTF_8_danish.c +344 -0
- data/ext/stem_UTF_8_danish.h +16 -0
- data/ext/stem_UTF_8_dutch.c +653 -0
- data/ext/stem_UTF_8_dutch.h +16 -0
- data/ext/stem_UTF_8_english.c +1176 -0
- data/ext/stem_UTF_8_english.h +16 -0
- data/ext/stem_UTF_8_finnish.c +808 -0
- data/ext/stem_UTF_8_finnish.h +16 -0
- data/ext/stem_UTF_8_french.c +1296 -0
- data/ext/stem_UTF_8_french.h +16 -0
- data/ext/stem_UTF_8_german.c +526 -0
- data/ext/stem_UTF_8_german.h +16 -0
- data/ext/stem_UTF_8_italian.c +1113 -0
- data/ext/stem_UTF_8_italian.h +16 -0
- data/ext/stem_UTF_8_norwegian.c +302 -0
- data/ext/stem_UTF_8_norwegian.h +16 -0
- data/ext/stem_UTF_8_porter.c +794 -0
- data/ext/stem_UTF_8_porter.h +16 -0
- data/ext/stem_UTF_8_portuguese.c +1055 -0
- data/ext/stem_UTF_8_portuguese.h +16 -0
- data/ext/stem_UTF_8_russian.c +709 -0
- data/ext/stem_UTF_8_russian.h +16 -0
- data/ext/stem_UTF_8_spanish.c +1137 -0
- data/ext/stem_UTF_8_spanish.h +16 -0
- data/ext/stem_UTF_8_swedish.c +313 -0
- data/ext/stem_UTF_8_swedish.h +16 -0
- data/ext/stopwords.c +401 -0
- data/ext/store.c +692 -0
- data/ext/store.h +777 -0
- data/ext/term_vectors.c +352 -0
- data/ext/threading.h +31 -0
- data/ext/utilities.c +446 -0
- data/ext/win32.h +54 -0
- data/lib/ferret.rb +29 -0
- data/lib/ferret/browser.rb +246 -0
- data/lib/ferret/browser/s/global.js +192 -0
- data/lib/ferret/browser/s/style.css +148 -0
- data/lib/ferret/browser/views/document/list.rhtml +49 -0
- data/lib/ferret/browser/views/document/show.rhtml +27 -0
- data/lib/ferret/browser/views/error/index.rhtml +7 -0
- data/lib/ferret/browser/views/help/index.rhtml +8 -0
- data/lib/ferret/browser/views/home/index.rhtml +29 -0
- data/lib/ferret/browser/views/layout.rhtml +22 -0
- data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
- data/lib/ferret/browser/views/term/index.rhtml +199 -0
- data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
- data/lib/ferret/browser/webrick.rb +14 -0
- data/lib/ferret/document.rb +130 -0
- data/lib/ferret/field_infos.rb +44 -0
- data/lib/ferret/index.rb +786 -0
- data/lib/ferret/number_tools.rb +157 -0
- data/lib/ferret_version.rb +3 -0
- data/setup.rb +1555 -0
- data/test/test_all.rb +5 -0
- data/test/test_helper.rb +24 -0
- data/test/threading/number_to_spoken.rb +132 -0
- data/test/threading/thread_safety_index_test.rb +79 -0
- data/test/threading/thread_safety_read_write_test.rb +76 -0
- data/test/threading/thread_safety_test.rb +133 -0
- data/test/unit/analysis/tc_analyzer.rb +548 -0
- data/test/unit/analysis/tc_token_stream.rb +646 -0
- data/test/unit/index/tc_index.rb +762 -0
- data/test/unit/index/tc_index_reader.rb +699 -0
- data/test/unit/index/tc_index_writer.rb +437 -0
- data/test/unit/index/th_doc.rb +315 -0
- data/test/unit/largefile/tc_largefile.rb +46 -0
- data/test/unit/query_parser/tc_query_parser.rb +238 -0
- data/test/unit/search/tc_filter.rb +135 -0
- data/test/unit/search/tc_fuzzy_query.rb +147 -0
- data/test/unit/search/tc_index_searcher.rb +61 -0
- data/test/unit/search/tc_multi_searcher.rb +128 -0
- data/test/unit/search/tc_multiple_search_requests.rb +58 -0
- data/test/unit/search/tc_search_and_sort.rb +179 -0
- data/test/unit/search/tc_sort.rb +49 -0
- data/test/unit/search/tc_sort_field.rb +27 -0
- data/test/unit/search/tc_spans.rb +190 -0
- data/test/unit/search/tm_searcher.rb +384 -0
- data/test/unit/store/tc_fs_store.rb +77 -0
- data/test/unit/store/tc_ram_store.rb +35 -0
- data/test/unit/store/tm_store.rb +34 -0
- data/test/unit/store/tm_store_lock.rb +68 -0
- data/test/unit/tc_document.rb +81 -0
- data/test/unit/ts_analysis.rb +2 -0
- data/test/unit/ts_index.rb +2 -0
- data/test/unit/ts_largefile.rb +4 -0
- data/test/unit/ts_query_parser.rb +2 -0
- data/test/unit/ts_search.rb +2 -0
- data/test/unit/ts_store.rb +2 -0
- data/test/unit/ts_utils.rb +2 -0
- data/test/unit/utils/tc_bit_vector.rb +295 -0
- data/test/unit/utils/tc_number_tools.rb +117 -0
- data/test/unit/utils/tc_priority_queue.rb +106 -0
- metadata +285 -0
data/ext/q_const_score.c
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
#include "search.h"
|
2
|
+
#include <string.h>
|
3
|
+
|
4
|
+
/***************************************************************************
|
5
|
+
*
|
6
|
+
* ConstantScoreScorer
|
7
|
+
*
|
8
|
+
***************************************************************************/
|
9
|
+
|
10
|
+
#define CScQ(query) ((ConstantScoreQuery *)(query))
|
11
|
+
#define CScSc(scorer) ((ConstantScoreScorer *)(scorer))
|
12
|
+
|
13
|
+
typedef struct ConstantScoreScorer
|
14
|
+
{
|
15
|
+
Scorer super;
|
16
|
+
BitVector *bv;
|
17
|
+
float score;
|
18
|
+
} ConstantScoreScorer;
|
19
|
+
|
20
|
+
static float cssc_score(Scorer *self)
|
21
|
+
{
|
22
|
+
return CScSc(self)->score;
|
23
|
+
}
|
24
|
+
|
25
|
+
static bool cssc_next(Scorer *self)
|
26
|
+
{
|
27
|
+
return ((self->doc = bv_scan_next(CScSc(self)->bv)) >= 0);
|
28
|
+
}
|
29
|
+
|
30
|
+
static bool cssc_skip_to(Scorer *self, int doc_num)
|
31
|
+
{
|
32
|
+
return ((self->doc = bv_scan_next_from(CScSc(self)->bv, doc_num)) >= 0);
|
33
|
+
}
|
34
|
+
|
35
|
+
static Explanation *cssc_explain(Scorer *self, int doc_num)
|
36
|
+
{
|
37
|
+
(void)self; (void)doc_num;
|
38
|
+
return expl_new(1.0, "ConstantScoreScorer");
|
39
|
+
}
|
40
|
+
|
41
|
+
static Scorer *cssc_new(Weight *weight, IndexReader *ir)
|
42
|
+
{
|
43
|
+
Scorer *self = scorer_new(ConstantScoreScorer, weight->similarity);
|
44
|
+
Filter *filter = CScQ(weight->query)->filter;
|
45
|
+
|
46
|
+
CScSc(self)->score = weight->value;
|
47
|
+
CScSc(self)->bv = filt_get_bv(filter, ir);
|
48
|
+
|
49
|
+
self->score = &cssc_score;
|
50
|
+
self->next = &cssc_next;
|
51
|
+
self->skip_to = &cssc_skip_to;
|
52
|
+
self->explain = &cssc_explain;
|
53
|
+
self->destroy = &scorer_destroy_i;
|
54
|
+
return self;
|
55
|
+
}
|
56
|
+
|
57
|
+
/***************************************************************************
|
58
|
+
*
|
59
|
+
* ConstantScoreWeight
|
60
|
+
*
|
61
|
+
***************************************************************************/
|
62
|
+
|
63
|
+
static char *csw_to_s(Weight *self)
|
64
|
+
{
|
65
|
+
return strfmt("ConstantScoreWeight(%f)", self->value);
|
66
|
+
}
|
67
|
+
|
68
|
+
static Explanation *csw_explain(Weight *self, IndexReader *ir, int doc_num)
|
69
|
+
{
|
70
|
+
Filter *filter = CScQ(self->query)->filter;
|
71
|
+
Explanation *expl;
|
72
|
+
char *filter_str = filter->to_s(filter);
|
73
|
+
BitVector *bv = filt_get_bv(filter, ir);
|
74
|
+
|
75
|
+
if (bv_get(bv, doc_num)) {
|
76
|
+
expl = expl_new(self->value,
|
77
|
+
"ConstantScoreQuery(%s), product of:", filter_str);
|
78
|
+
expl_add_detail(expl, expl_new(self->query->boost, "boost"));
|
79
|
+
expl_add_detail(expl, expl_new(self->qnorm, "query_norm"));
|
80
|
+
}
|
81
|
+
else {
|
82
|
+
expl = expl_new(self->value,
|
83
|
+
"ConstantScoreQuery(%s), does not match id %d",
|
84
|
+
filter_str, doc_num);
|
85
|
+
}
|
86
|
+
free(filter_str);
|
87
|
+
return expl;
|
88
|
+
}
|
89
|
+
|
90
|
+
static Weight *csw_new(Query *query, Searcher *searcher)
|
91
|
+
{
|
92
|
+
Weight *self = w_new(Weight, query);
|
93
|
+
|
94
|
+
self->scorer = &cssc_new;
|
95
|
+
self->explain = &csw_explain;
|
96
|
+
self->to_s = &csw_to_s;
|
97
|
+
|
98
|
+
self->similarity = query->get_similarity(query, searcher);
|
99
|
+
self->idf = 1.0;
|
100
|
+
|
101
|
+
return self;
|
102
|
+
}
|
103
|
+
|
104
|
+
/***************************************************************************
|
105
|
+
*
|
106
|
+
* ConstantScoreQuery
|
107
|
+
*
|
108
|
+
***************************************************************************/
|
109
|
+
|
110
|
+
static char *csq_to_s(Query *self, const char *field)
|
111
|
+
{
|
112
|
+
Filter *filter = CScQ(self)->filter;
|
113
|
+
char *filter_str = filter->to_s(filter);
|
114
|
+
char *buffer;
|
115
|
+
(void)field;
|
116
|
+
if (self->boost == 1.0) {
|
117
|
+
buffer = strfmt("ConstantScore(%s)", filter_str);
|
118
|
+
}
|
119
|
+
else {
|
120
|
+
buffer = strfmt("ConstantScore(%s)^%f", filter_str, self->boost);
|
121
|
+
}
|
122
|
+
free(filter_str);
|
123
|
+
return buffer;;
|
124
|
+
}
|
125
|
+
|
126
|
+
static void csq_destroy(Query *self)
|
127
|
+
{
|
128
|
+
filt_deref(CScQ(self)->filter);
|
129
|
+
q_destroy_i(self);
|
130
|
+
}
|
131
|
+
|
132
|
+
static unsigned long csq_hash(Query *self)
|
133
|
+
{
|
134
|
+
return filt_hash(CScQ(self)->filter);
|
135
|
+
}
|
136
|
+
|
137
|
+
static int csq_eq(Query *self, Query *o)
|
138
|
+
{
|
139
|
+
return filt_eq(CScQ(self)->filter, CScQ(o)->filter);
|
140
|
+
}
|
141
|
+
|
142
|
+
Query *csq_new_nr(Filter *filter)
|
143
|
+
{
|
144
|
+
Query *self = q_new(ConstantScoreQuery);
|
145
|
+
CScQ(self)->filter = filter;
|
146
|
+
|
147
|
+
self->type = CONSTANT_QUERY;
|
148
|
+
self->to_s = &csq_to_s;
|
149
|
+
self->hash = &csq_hash;
|
150
|
+
self->eq = &csq_eq;
|
151
|
+
self->destroy_i = &csq_destroy;
|
152
|
+
self->create_weight_i = &csw_new;
|
153
|
+
|
154
|
+
return self;
|
155
|
+
}
|
156
|
+
|
157
|
+
Query *csq_new(Filter *filter)
|
158
|
+
{
|
159
|
+
REF(filter);
|
160
|
+
return csq_new_nr(filter);
|
161
|
+
}
|
@@ -0,0 +1,209 @@
|
|
1
|
+
#include "search.h"
|
2
|
+
#include <string.h>
|
3
|
+
|
4
|
+
/***************************************************************************
|
5
|
+
*
|
6
|
+
* FilteredQueryScorer
|
7
|
+
*
|
8
|
+
***************************************************************************/
|
9
|
+
|
10
|
+
#define FQSc(scorer) ((FilteredQueryScorer *)(scorer))
|
11
|
+
#define FQQ(query) ((FilteredQuery *)(query))
|
12
|
+
|
13
|
+
typedef struct FilteredQueryScorer
|
14
|
+
{
|
15
|
+
Scorer super;
|
16
|
+
Scorer *sub_scorer;
|
17
|
+
BitVector *bv;
|
18
|
+
} FilteredQueryScorer;
|
19
|
+
|
20
|
+
float fqsc_score(Scorer *self)
|
21
|
+
{
|
22
|
+
Scorer *sub_sc = FQSc(self)->sub_scorer;
|
23
|
+
return sub_sc->score(sub_sc);
|
24
|
+
}
|
25
|
+
|
26
|
+
bool fqsc_next(Scorer *self)
|
27
|
+
{
|
28
|
+
Scorer *sub_sc = FQSc(self)->sub_scorer;
|
29
|
+
BitVector *bv = FQSc(self)->bv;
|
30
|
+
while (sub_sc->next(sub_sc)) {
|
31
|
+
self->doc = sub_sc->doc;
|
32
|
+
if (bv_get(bv, self->doc)) return true;
|
33
|
+
}
|
34
|
+
return false;
|
35
|
+
}
|
36
|
+
|
37
|
+
bool fqsc_skip_to(Scorer *self, int doc_num)
|
38
|
+
{
|
39
|
+
Scorer *sub_sc = FQSc(self)->sub_scorer;
|
40
|
+
BitVector *bv = FQSc(self)->bv;
|
41
|
+
if (sub_sc->skip_to(sub_sc, doc_num)) {
|
42
|
+
self->doc = sub_sc->doc;
|
43
|
+
do {
|
44
|
+
if (bv_get(bv, self->doc)) return true;
|
45
|
+
} while (sub_sc->next(sub_sc));
|
46
|
+
}
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
|
50
|
+
Explanation *fqsc_explain(Scorer *self, int doc_num)
|
51
|
+
{
|
52
|
+
Scorer *sub_sc = FQSc(self)->sub_scorer;
|
53
|
+
return sub_sc->explain(sub_sc, doc_num);
|
54
|
+
}
|
55
|
+
|
56
|
+
void fqsc_destroy(Scorer *self)
|
57
|
+
{
|
58
|
+
FilteredQueryScorer *fqsc = FQSc(self);
|
59
|
+
fqsc->sub_scorer->destroy(fqsc->sub_scorer);
|
60
|
+
scorer_destroy_i(self);
|
61
|
+
}
|
62
|
+
|
63
|
+
Scorer *fqsc_new(Scorer *scorer, BitVector *bv, Similarity *sim)
|
64
|
+
{
|
65
|
+
Scorer *self = scorer_new(FilteredQueryScorer, sim);
|
66
|
+
|
67
|
+
FQSc(self)->sub_scorer = scorer;
|
68
|
+
FQSc(self)->bv = bv;
|
69
|
+
|
70
|
+
self->score = &fqsc_score;
|
71
|
+
self->next = &fqsc_next;
|
72
|
+
self->skip_to = &fqsc_skip_to;
|
73
|
+
self->explain = &fqsc_explain;
|
74
|
+
self->destroy = &fqsc_destroy;
|
75
|
+
|
76
|
+
return self;
|
77
|
+
}
|
78
|
+
|
79
|
+
/***************************************************************************
|
80
|
+
*
|
81
|
+
* Weight
|
82
|
+
*
|
83
|
+
***************************************************************************/
|
84
|
+
|
85
|
+
#define FQW(weight) ((FilteredQueryWeight *)(weight))
|
86
|
+
typedef struct FilteredQueryWeight
|
87
|
+
{
|
88
|
+
Weight super;
|
89
|
+
Weight *sub_weight;
|
90
|
+
} FilteredQueryWeight;
|
91
|
+
|
92
|
+
static char *fqw_to_s(Weight *self)
|
93
|
+
{
|
94
|
+
return strfmt("FilteredQueryWeight(%f)", self->value);
|
95
|
+
}
|
96
|
+
|
97
|
+
static float fqw_sum_of_squared_weights(Weight *self)
|
98
|
+
{
|
99
|
+
Weight *sub_weight = FQW(self)->sub_weight;
|
100
|
+
return sub_weight->sum_of_squared_weights(sub_weight);
|
101
|
+
}
|
102
|
+
|
103
|
+
static void fqw_normalize(Weight *self, float normalization_factor)
|
104
|
+
{
|
105
|
+
Weight *sub_weight = FQW(self)->sub_weight;
|
106
|
+
sub_weight->normalize(sub_weight, normalization_factor);
|
107
|
+
}
|
108
|
+
|
109
|
+
static float fqw_get_value(Weight *self)
|
110
|
+
{
|
111
|
+
Weight *sub_weight = FQW(self)->sub_weight;
|
112
|
+
return sub_weight->get_value(sub_weight);
|
113
|
+
}
|
114
|
+
|
115
|
+
static Explanation *fqw_explain(Weight *self, IndexReader *ir, int doc_num)
|
116
|
+
{
|
117
|
+
Weight *sub_weight = FQW(self)->sub_weight;
|
118
|
+
return sub_weight->explain(sub_weight, ir, doc_num);
|
119
|
+
}
|
120
|
+
|
121
|
+
static Scorer *fqw_scorer(Weight *self, IndexReader *ir)
|
122
|
+
{
|
123
|
+
Weight *sub_weight = FQW(self)->sub_weight;
|
124
|
+
Scorer *scorer = sub_weight->scorer(sub_weight, ir);
|
125
|
+
Filter *filter = FQQ(self->query)->filter;
|
126
|
+
|
127
|
+
return fqsc_new(scorer, filt_get_bv(filter, ir), self->similarity);
|
128
|
+
}
|
129
|
+
|
130
|
+
static void fqw_destroy(Weight *self)
|
131
|
+
{
|
132
|
+
Weight *sub_weight = FQW(self)->sub_weight;
|
133
|
+
sub_weight->destroy(sub_weight);
|
134
|
+
w_destroy(self);
|
135
|
+
}
|
136
|
+
|
137
|
+
static Weight *fqw_new(Query *query, Weight *sub_weight, Similarity *sim)
|
138
|
+
{
|
139
|
+
Weight *self = w_new(FilteredQueryWeight, query);
|
140
|
+
|
141
|
+
FQW(self)->sub_weight = sub_weight;
|
142
|
+
|
143
|
+
self->get_value = &fqw_get_value;
|
144
|
+
self->normalize = &fqw_normalize;
|
145
|
+
self->scorer = &fqw_scorer;
|
146
|
+
self->explain = &fqw_explain;
|
147
|
+
self->to_s = &fqw_to_s;
|
148
|
+
self->destroy = &fqw_destroy;
|
149
|
+
self->sum_of_squared_weights = &fqw_sum_of_squared_weights;
|
150
|
+
|
151
|
+
self->similarity = sim;
|
152
|
+
self->idf = 1.0;
|
153
|
+
self->value = sub_weight->value;
|
154
|
+
|
155
|
+
return self;
|
156
|
+
}
|
157
|
+
|
158
|
+
/***************************************************************************
|
159
|
+
*
|
160
|
+
* FilteredQuery
|
161
|
+
*
|
162
|
+
***************************************************************************/
|
163
|
+
|
164
|
+
static char *fq_to_s(Query *self, const char *field)
|
165
|
+
{
|
166
|
+
FilteredQuery *fq = FQQ(self);
|
167
|
+
char *filter_str = fq->filter->to_s(fq->filter);
|
168
|
+
char *query_str = fq->query->to_s(fq->query, field);
|
169
|
+
char *buffer;
|
170
|
+
if (self->boost == 1.0) {
|
171
|
+
buffer = strfmt("FilteredQuery(query:%s, filter:%s)",
|
172
|
+
query_str, filter_str);
|
173
|
+
} else {
|
174
|
+
buffer = strfmt("FilteredQuery(query:%s, filter:%s)^%f",
|
175
|
+
query_str, filter_str, self->boost);
|
176
|
+
}
|
177
|
+
free(filter_str);
|
178
|
+
free(query_str);
|
179
|
+
return buffer;;
|
180
|
+
}
|
181
|
+
|
182
|
+
void fq_destroy(Query *self)
|
183
|
+
{
|
184
|
+
filt_deref(FQQ(self)->filter);
|
185
|
+
q_deref(FQQ(self)->query);
|
186
|
+
q_destroy_i(self);
|
187
|
+
}
|
188
|
+
|
189
|
+
Weight *fq_new_weight(Query *self, Searcher *searcher)
|
190
|
+
{
|
191
|
+
Query *sub_query = FQQ(self)->query;
|
192
|
+
return fqw_new(self, q_weight(sub_query, searcher),
|
193
|
+
searcher->similarity);
|
194
|
+
}
|
195
|
+
|
196
|
+
Query *fq_new(Query *query, Filter *filter)
|
197
|
+
{
|
198
|
+
Query *self = q_new(FilteredQuery);
|
199
|
+
|
200
|
+
FQQ(self)->query = query;
|
201
|
+
FQQ(self)->filter = filter;
|
202
|
+
|
203
|
+
self->type = FILTERED_QUERY;
|
204
|
+
self->to_s = &fq_to_s;
|
205
|
+
self->destroy_i = &fq_destroy;
|
206
|
+
self->create_weight_i = &fq_new_weight;
|
207
|
+
|
208
|
+
return self;
|
209
|
+
}
|
data/ext/q_fuzzy.c
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
#include <string.h>
|
2
|
+
#include "search.h"
|
3
|
+
#include "helper.h"
|
4
|
+
|
5
|
+
/****************************************************************************
|
6
|
+
*
|
7
|
+
* FuzzyStuff
|
8
|
+
*
|
9
|
+
* The main method here is the fuzq_score method which scores a term against
|
10
|
+
* another term. The other methods all act in support.
|
11
|
+
*
|
12
|
+
****************************************************************************/
|
13
|
+
|
14
|
+
static INLINE int fuzq_calculate_max_distance(FuzzyQuery *fuzq, int m)
|
15
|
+
{
|
16
|
+
return (int)((1.0 - fuzq->min_sim) * (MIN(fuzq->text_len, m) + fuzq->pre_len));
|
17
|
+
}
|
18
|
+
|
19
|
+
static void fuzq_initialize_max_distances(FuzzyQuery *fuzq)
|
20
|
+
{
|
21
|
+
int i;
|
22
|
+
for (i = 0; i < TYPICAL_LONGEST_WORD; i++) {
|
23
|
+
fuzq->max_distances[i] = fuzq_calculate_max_distance(fuzq, i);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
static INLINE int fuzq_get_max_distance(FuzzyQuery *fuzq, int m)
|
28
|
+
{
|
29
|
+
return (m < TYPICAL_LONGEST_WORD) ? fuzq->max_distances[m]
|
30
|
+
: fuzq_calculate_max_distance(fuzq, m);
|
31
|
+
}
|
32
|
+
|
33
|
+
/**
|
34
|
+
* The following algorithm is taken from Bob Carpenter's FuzzyTermEnum
|
35
|
+
* implentation here;
|
36
|
+
*
|
37
|
+
* http://mail-archives.apache.org/mod_mbox/lucene-java-dev/200606.mbox/%3c448F0E8C.3050901@alias-i.com%3e
|
38
|
+
*/
|
39
|
+
float fuzq_score(FuzzyQuery *fuzq, const char *target)
|
40
|
+
{
|
41
|
+
const int m = (int)strlen(target);
|
42
|
+
const int n = fuzq->text_len;
|
43
|
+
|
44
|
+
if (n == 0) {
|
45
|
+
/* we don't have anything to compare. That means if we just add
|
46
|
+
* the letters for m we get the new word */
|
47
|
+
return fuzq->pre_len == 0 ? 0.0f : 1.0f - ((float) m / fuzq->pre_len);
|
48
|
+
}
|
49
|
+
else if (m == 0) {
|
50
|
+
return fuzq->pre_len == 0 ? 0.0f : 1.0f - ((float) n / fuzq->pre_len);
|
51
|
+
}
|
52
|
+
else {
|
53
|
+
int i, j, prune;
|
54
|
+
int *d_curr, *d_prev;
|
55
|
+
const char *text = fuzq->text;
|
56
|
+
const int max_distance = fuzq_get_max_distance(fuzq, m);
|
57
|
+
|
58
|
+
/*
|
59
|
+
printf("n%dm%dmd%ddiff%d<%s><%s>\n", n, m, max_distance, m-n,
|
60
|
+
fuzq->text, target);
|
61
|
+
*/
|
62
|
+
if (max_distance < ((m > n) ? (m-n) : (n-m))) { /* abs */
|
63
|
+
/* Just adding the characters of m to n or vice-versa results in
|
64
|
+
* too many edits for example "pre" length is 3 and "prefixes"
|
65
|
+
* length is 8. We can see that given this optimal circumstance,
|
66
|
+
* the edit distance cannot be less than 5 which is 8-3 or more
|
67
|
+
* precisesly Math.abs(3-8). If our maximum edit distance is 4,
|
68
|
+
* then we can discard this word without looking at it. */
|
69
|
+
return 0.0f;
|
70
|
+
}
|
71
|
+
|
72
|
+
d_curr = fuzq->da;
|
73
|
+
d_prev = d_curr + n + 1;
|
74
|
+
|
75
|
+
/* init array */
|
76
|
+
for (j = 0; j <= n; j++) {
|
77
|
+
d_curr[j] = j;
|
78
|
+
}
|
79
|
+
|
80
|
+
/* start computing edit distance */
|
81
|
+
for (i = 0; i < m;) {
|
82
|
+
char s_i = target[i];
|
83
|
+
/* swap d_current into d_prev */
|
84
|
+
int *d_tmp = d_prev;
|
85
|
+
d_prev = d_curr;
|
86
|
+
d_curr = d_tmp;
|
87
|
+
prune = (d_curr[0] = ++i) > max_distance;
|
88
|
+
|
89
|
+
for (j = 0; j < n; j++) {
|
90
|
+
d_curr[j + 1] = (s_i == text[j])
|
91
|
+
? min3(d_prev[j + 1] + 1, d_curr[j] + 1, d_prev[j])
|
92
|
+
: min3(d_prev[j + 1], d_curr[j], d_prev[j]) + 1;
|
93
|
+
if (prune && d_curr[j + 1] <= max_distance) {
|
94
|
+
prune = false;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
if (prune) {
|
98
|
+
return 0.0f;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
/*
|
103
|
+
printf("<%f, d_curr[n] = %d min_len = %d>",
|
104
|
+
1.0f - ((float)d_curr[m] / (float) (fuzq->pre_len + min2(n, m))),
|
105
|
+
d_curr[m], fuzq->pre_len + min2(n, m));
|
106
|
+
*/
|
107
|
+
|
108
|
+
/* this will return less than 0.0 when the edit distance is greater
|
109
|
+
* than the number of characters in the shorter word. but this was
|
110
|
+
* the formula that was previously used in FuzzyTermEnum, so it has
|
111
|
+
* not been changed (even though min_sim must be greater than 0.0) */
|
112
|
+
return 1.0f - ((float)d_curr[n] / (float) (fuzq->pre_len + min2(n, m)));
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
/****************************************************************************
|
117
|
+
*
|
118
|
+
* FuzzyQuery
|
119
|
+
*
|
120
|
+
****************************************************************************/
|
121
|
+
|
122
|
+
#define FzQ(query) ((FuzzyQuery *)(query))
|
123
|
+
|
124
|
+
static char *fuzq_to_s(Query *self, const char *curr_field)
|
125
|
+
{
|
126
|
+
char *buffer, *bptr;
|
127
|
+
char *term = FzQ(self)->term;
|
128
|
+
char *field = FzQ(self)->field;
|
129
|
+
int tlen = (int)strlen(term);
|
130
|
+
int flen = (int)strlen(field);
|
131
|
+
bptr = buffer = ALLOC_N(char, tlen + flen + 70);
|
132
|
+
|
133
|
+
if (strcmp(curr_field, field) != 0) {
|
134
|
+
sprintf(bptr, "%s:", field);
|
135
|
+
bptr += flen + 1;
|
136
|
+
}
|
137
|
+
|
138
|
+
sprintf(bptr, "%s~", term);
|
139
|
+
bptr += tlen + 1;
|
140
|
+
if (FzQ(self)->min_sim != 0.5) {
|
141
|
+
dbl_to_s(bptr, FzQ(self)->min_sim);
|
142
|
+
bptr += strlen(bptr);
|
143
|
+
}
|
144
|
+
|
145
|
+
if (self->boost != 1.0) {
|
146
|
+
*bptr = '^';
|
147
|
+
dbl_to_s(++bptr, self->boost);
|
148
|
+
}
|
149
|
+
|
150
|
+
return buffer;
|
151
|
+
}
|
152
|
+
|
153
|
+
static Query *fuzq_rewrite(Query *self, IndexReader *ir)
|
154
|
+
{
|
155
|
+
Query *q;
|
156
|
+
FuzzyQuery *fuzq = FzQ(self);
|
157
|
+
|
158
|
+
const char *term = fuzq->term;
|
159
|
+
const char *field = fuzq->field;
|
160
|
+
const int field_num = fis_get_field_num(ir->fis, field);
|
161
|
+
|
162
|
+
if (field_num < 0) {
|
163
|
+
q = bq_new(true);
|
164
|
+
}
|
165
|
+
else if (fuzq->pre_len >= (int)strlen(term)) {
|
166
|
+
q = tq_new(field, term);
|
167
|
+
}
|
168
|
+
else {
|
169
|
+
TermEnum *te;
|
170
|
+
char *prefix = NULL;
|
171
|
+
int pre_len = fuzq->pre_len;
|
172
|
+
|
173
|
+
q = multi_tq_new_conf(fuzq->field, MTQMaxTerms(self), fuzq->min_sim);
|
174
|
+
|
175
|
+
if (pre_len > 0) {
|
176
|
+
prefix = ALLOC_N(char, pre_len + 1);
|
177
|
+
strncpy(prefix, term, pre_len);
|
178
|
+
prefix[pre_len] = '\0';
|
179
|
+
te = ir->terms_from(ir, field_num, prefix);
|
180
|
+
}
|
181
|
+
else {
|
182
|
+
te = ir->terms(ir, field_num);
|
183
|
+
}
|
184
|
+
|
185
|
+
fuzq->scale_factor = (float)(1.0 / (1.0 - fuzq->min_sim));
|
186
|
+
fuzq->text = term + pre_len;
|
187
|
+
fuzq->text_len = (int)strlen(fuzq->text);
|
188
|
+
fuzq->da = REALLOC_N(fuzq->da, int, fuzq->text_len * 2 + 2);
|
189
|
+
fuzq_initialize_max_distances(fuzq);
|
190
|
+
|
191
|
+
if (te) {
|
192
|
+
const char *curr_term = te->curr_term;
|
193
|
+
const char *curr_suffix = curr_term + pre_len;
|
194
|
+
float score = 0.0;
|
195
|
+
|
196
|
+
|
197
|
+
do {
|
198
|
+
if ((prefix && strncmp(curr_term, prefix, pre_len) != 0)) {
|
199
|
+
break;
|
200
|
+
}
|
201
|
+
|
202
|
+
score = fuzq_score(fuzq, curr_suffix);
|
203
|
+
/*
|
204
|
+
printf("%s:%s:%f < %f\n", curr_term, term, score, min_score);
|
205
|
+
*/
|
206
|
+
multi_tq_add_term_boost(q, curr_term, score);
|
207
|
+
|
208
|
+
} while (te->next(te) != NULL);
|
209
|
+
|
210
|
+
te->close(te);
|
211
|
+
}
|
212
|
+
free(prefix);
|
213
|
+
}
|
214
|
+
|
215
|
+
return q;
|
216
|
+
}
|
217
|
+
|
218
|
+
static void fuzq_destroy(Query *self)
|
219
|
+
{
|
220
|
+
free(FzQ(self)->term);
|
221
|
+
free(FzQ(self)->field);
|
222
|
+
free(FzQ(self)->da);
|
223
|
+
q_destroy_i(self);
|
224
|
+
}
|
225
|
+
|
226
|
+
static unsigned long fuzq_hash(Query *self)
|
227
|
+
{
|
228
|
+
return str_hash(FzQ(self)->term) ^ str_hash(FzQ(self)->field)
|
229
|
+
^ float2int(FzQ(self)->min_sim) ^ FzQ(self)->pre_len;
|
230
|
+
}
|
231
|
+
|
232
|
+
static int fuzq_eq(Query *self, Query *o)
|
233
|
+
{
|
234
|
+
FuzzyQuery *fq1 = FzQ(self);
|
235
|
+
FuzzyQuery *fq2 = FzQ(o);
|
236
|
+
|
237
|
+
return (strcmp(fq1->term, fq2->term) == 0)
|
238
|
+
&& (strcmp(fq1->field, fq2->field) == 0)
|
239
|
+
&& (fq1->pre_len == fq2->pre_len)
|
240
|
+
&& (fq1->min_sim == fq2->min_sim);
|
241
|
+
}
|
242
|
+
|
243
|
+
Query *fuzq_new_conf(const char *field, const char *term,
|
244
|
+
float min_sim, int pre_len, int max_terms)
|
245
|
+
{
|
246
|
+
Query *self = q_new(FuzzyQuery);
|
247
|
+
|
248
|
+
FzQ(self)->field = estrdup(field);
|
249
|
+
FzQ(self)->term = estrdup(term);
|
250
|
+
FzQ(self)->pre_len = pre_len ? pre_len : DEF_PRE_LEN;
|
251
|
+
FzQ(self)->min_sim = min_sim ? min_sim : DEF_MIN_SIM;
|
252
|
+
MTQMaxTerms(self) = max_terms ? max_terms : DEF_MAX_TERMS;
|
253
|
+
|
254
|
+
self->type = FUZZY_QUERY;
|
255
|
+
self->to_s = &fuzq_to_s;
|
256
|
+
self->hash = &fuzq_hash;
|
257
|
+
self->eq = &fuzq_eq;
|
258
|
+
self->rewrite = &fuzq_rewrite;
|
259
|
+
self->destroy_i = &fuzq_destroy;
|
260
|
+
self->create_weight_i = &q_create_weight_unsup;
|
261
|
+
|
262
|
+
return self;
|
263
|
+
}
|
264
|
+
|
265
|
+
Query *fuzq_new(const char *field, const char *term)
|
266
|
+
{
|
267
|
+
return fuzq_new_conf(field, term, 0.0f, 0, 0);
|
268
|
+
}
|