summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/scorers.c
blob: 44a3e9b4b70d3979a594d2037532a48b022258c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
*/

#include "grn_db.h"

#include <groonga/scorer.h>

#include <math.h>

static double
scorer_tf_idf(grn_ctx *ctx, grn_scorer_matched_record *record)
{
  double min_score = 1.0;
  double tf;
  double n_all_documents;
  double n_candidates;
  double n_tokens;
  double n_estimated_match_documents;

  tf = grn_scorer_matched_record_get_n_occurrences(ctx, record) +
    grn_scorer_matched_record_get_total_term_weights(ctx, record);
  n_all_documents = grn_scorer_matched_record_get_n_documents(ctx, record);
  n_candidates = grn_scorer_matched_record_get_n_candidates(ctx, record);
  n_tokens = grn_scorer_matched_record_get_n_tokens(ctx, record);
  n_estimated_match_documents = n_candidates / n_tokens;

  if (n_estimated_match_documents >= n_all_documents) {
    return min_score;
  } else {
    double idf;
    double tf_idf;

    idf = log(n_all_documents / n_estimated_match_documents);
    tf_idf = tf * idf;
    return fmax(tf_idf, min_score);
  }
}

static double
scorer_tf_at_most(grn_ctx *ctx, grn_scorer_matched_record *record)
{
  double tf;
  double max;
  grn_obj *max_raw;

  tf = grn_scorer_matched_record_get_n_occurrences(ctx, record) +
    grn_scorer_matched_record_get_total_term_weights(ctx, record);
  max_raw = grn_scorer_matched_record_get_arg(ctx, record, 0);

  if (!max_raw) {
    return tf;
  }

  if (max_raw->header.type != GRN_BULK) {
    return tf;
  }

  if (max_raw->header.domain == GRN_DB_FLOAT) {
    max = GRN_FLOAT_VALUE(max_raw);
  } else {
    grn_obj casted_max_raw;
    GRN_FLOAT_INIT(&casted_max_raw, 0);
    if (grn_obj_cast(ctx, max_raw, &casted_max_raw, GRN_FALSE) != GRN_SUCCESS) {
      GRN_OBJ_FIN(ctx, &casted_max_raw);
      return tf;
    } else {
      max = GRN_FLOAT_VALUE(&casted_max_raw);
    }
    GRN_OBJ_FIN(ctx, &casted_max_raw);
  }

  return fmin(tf, max);
}

grn_rc
grn_db_init_builtin_scorers(grn_ctx *ctx)
{
  grn_scorer_register(ctx, "scorer_tf_idf", -1, scorer_tf_idf);
  grn_scorer_register(ctx, "scorer_tf_at_most", -1, scorer_tf_at_most);
  return GRN_SUCCESS;
}