~ubuntu-branches/ubuntu/quantal/ruby-ferret/quantal

« back to all changes in this revision

Viewing changes to ext/q_term.c

  • Committer: Package Import Robot
  • Author(s): Cédric Boutillier
  • Date: 2012-06-14 23:04:48 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20120614230448-wd5se4ia1yz7dvms
Tags: 0.11.8.4+debian-1
* New upstream version from a new source
  + the new code fixes format security issues (Closes: #672069)
  + change homepage to https://github.com/jkraemer/ferret/
* Build for all Ruby versions (Closes: #655636)
  + change depends accordingly
  + do not set shebang of bin/ferret to ruby1.8
* Repack source to remove convenience copy of bzlib
  + build-dep on libbz2-dev
  + dversionmangle in debian/watch
  + add debian/README.source explaining how to clean the source
* debian/patches:
  + disable_load_path_manipulation.patch: do not override $LOAD_PATH
  + disable_test_causing_segfault.patch: temporarily disable a test known to
    cause segfaults
  + fix_compatibility_with_minitest.patch: fix a failing test with Ruby1.9
  + use_system_bzlib.patch: adapt the source to use system libbz2
  + fix_typos_in_source_code.patch: correct some spelling errors in the
    source code
  + block_variables_have_local_scopes.patch: fix syntax in
    bin/ferret-browser
* Override dh_auto_clean to remove test/temp when cleaning
* Bump Standards-Version to 3.9.3 (no changes needed)
* Set priority of transitional packages to extra
* Add myself to Uploaders:
* Update copyright to DEP-5 copyright-format/1.0
* Add TUTORIAL and debian/README.source to documents
* Override lintian warnings about duplicate descriptions of transitional
  packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "symbol.h"
1
2
#include <string.h>
2
3
#include "search.h"
 
4
#include "internal.h"
3
5
 
4
6
#define TQ(query) ((TermQuery *)(query))
5
7
#define TSc(scorer) ((TermScorer *)(scorer))
103
105
    }
104
106
    return expl_new(sim_tf(self->similarity, (float)tf),
105
107
                    "tf(term_freq(%s:%s)=%d)",
106
 
                    TQ(query)->field, TQ(query)->term, tf);
 
108
                    S(TQ(query)->field), TQ(query)->term, tf);
107
109
}
108
110
 
109
111
static void tsc_destroy(Scorer *self)
144
146
{
145
147
    TermQuery *tq = TQ(self->query);
146
148
    TermDocEnum *tde = ir_term_docs_for(ir, tq->field, tq->term);
147
 
    if (!tde) {
148
 
        return NULL;
149
 
    }
 
149
    /* ir_term_docs_for should always return a TermDocEnum */
 
150
    assert(NULL != tde);
150
151
 
151
152
    return tsc_new(self, tde, ir_get_norms(ir, tq->field));
152
153
}
161
162
    float field_norm;
162
163
    Explanation *field_norm_expl;
163
164
 
164
 
    char *query_str = self->query->to_s(self->query, "");
 
165
    char *query_str = self->query->to_s(self->query, NULL);
165
166
    TermQuery *tq = TQ(self->query);
166
167
    char *term = tq->term;
167
 
    char *field = tq->field;
168
168
 
169
169
    Explanation *expl = expl_new(0.0, "weight(%s in %d), product of:",
170
170
                                 query_str, doc_num);
172
172
    /* We need two of these as it's included in both the query explanation
173
173
     * and the field explanation */
174
174
    Explanation *idf_expl1 = expl_new(self->idf, "idf(doc_freq=%d)",
175
 
                                      ir_doc_freq(ir, field, term));
 
175
                                      ir_doc_freq(ir, tq->field, term));
176
176
    Explanation *idf_expl2 = expl_new(self->idf, "idf(doc_freq=%d)",
177
 
                                      ir_doc_freq(ir, field, term));
 
177
                                      ir_doc_freq(ir, tq->field, term));
178
178
 
179
179
    /* explain query weight */
180
180
    Explanation *query_expl = expl_new(0.0, "query_weight(%s), product of:",
197
197
 
198
198
    /* explain field weight */
199
199
    field_expl = expl_new(0.0, "field_weight(%s:%s in %d), product of:",
200
 
                          field, term, doc_num);
 
200
                          S(tq->field), term, doc_num);
201
201
 
202
202
    scorer = self->scorer(self, ir);
203
203
    tf_expl = scorer->explain(scorer, doc_num);
205
205
    expl_add_detail(field_expl, tf_expl);
206
206
    expl_add_detail(field_expl, idf_expl2);
207
207
 
208
 
    field_norms = ir_get_norms(ir, field);
 
208
    field_norms = ir_get_norms(ir, tq->field);
209
209
    field_norm = (field_norms 
210
210
                  ? sim_decode_norm(self->similarity, field_norms[doc_num]) 
211
211
                  : (float)0.0);
212
212
    field_norm_expl = expl_new(field_norm, "field_norm(field=%s, doc=%d)",
213
 
                               field, doc_num);
 
213
                               S(tq->field), doc_num);
214
214
 
215
215
    expl_add_detail(field_expl, field_norm_expl);
216
216
 
259
259
static void tq_destroy(Query *self)
260
260
{
261
261
    free(TQ(self)->term);
262
 
    free(TQ(self)->field);
263
262
    q_destroy_i(self);
264
263
}
265
264
 
266
 
static char *tq_to_s(Query *self, const char *field)
 
265
static char *tq_to_s(Query *self, Symbol default_field)
267
266
{
268
 
    size_t flen = strlen(TQ(self)->field);
 
267
    const char *field = S(TQ(self)->field);
 
268
    size_t flen = strlen(field);
269
269
    size_t tlen = strlen(TQ(self)->term);
270
270
    char *buffer = ALLOC_N(char, 34 + flen + tlen);
271
271
    char *b = buffer;
272
 
    if (strcmp(field, TQ(self)->field) != 0) {
273
 
        memcpy(b, TQ(self)->field, sizeof(char) * flen);
 
272
    if (default_field != TQ(self)->field) {
 
273
        memcpy(b, field, sizeof(char) * flen);
274
274
        b[flen] = ':';
275
275
        b += flen + 1;
276
276
    }
291
291
 
292
292
static unsigned long tq_hash(Query *self)
293
293
{
294
 
    return str_hash(TQ(self)->term) ^ str_hash(TQ(self)->field);
 
294
    return str_hash(TQ(self)->term) ^ sym_hash(TQ(self)->field);
295
295
}
296
296
 
297
297
static int tq_eq(Query *self, Query *o)
298
298
{
299
299
    return (strcmp(TQ(self)->term, TQ(o)->term) == 0) 
300
 
        && (strcmp(TQ(self)->field, TQ(o)->field) == 0);
 
300
        && (TQ(self)->field == TQ(o)->field);
301
301
}
302
302
 
303
303
static MatchVector *tq_get_matchv_i(Query *self, MatchVector *mv,
304
304
                                    TermVector *tv)
305
305
{
306
 
    if (strcmp(tv->field, TQ(self)->field) == 0) {
 
306
    if (tv->field == TQ(self)->field) {
307
307
        int i;
308
308
        TVTerm *tv_term = tv_get_tv_term(tv, TQ(self)->term);
309
309
        if (tv_term) {
316
316
    return mv;
317
317
}
318
318
 
319
 
Query *tq_new(const char *field, const char *term)
 
319
Query *tq_new(Symbol field, const char *term)
320
320
{
321
321
    Query *self             = q_new(TermQuery);
322
322
 
323
 
    TQ(self)->field         = estrdup(field);
 
323
    TQ(self)->field         = field;
324
324
    TQ(self)->term          = estrdup(term);
325
325
 
326
326
    self->type              = TERM_QUERY;