Debian Codegraph @

lib/scorers.c

raw symbols SCIP
1 /*
2 Copyright(C) 2015 Brazil
3 Copyright(C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_db.h"
21
22 #include <groonga/scorer.h>
23
24 #include <math.h>
25
26 static double
27 scorer_tf_idf(grn_ctx *ctx, grn_scorer_matched_record *record)
28 {
29 double min_score = 1.0;
30 double tf;
31 uint64_t n_all_documents;
32 uint64_t n_candidates;
33 uint32_t n_tokens;
34 double n_estimated_match_documents;
35
36 tf = grn_scorer_matched_record_get_n_occurrences(ctx, record) +
37 grn_scorer_matched_record_get_total_term_weights(ctx, record);
38 n_all_documents = grn_scorer_matched_record_get_n_documents(ctx, record);
39 n_candidates = grn_scorer_matched_record_get_n_candidates(ctx, record);
40 n_tokens = grn_scorer_matched_record_get_n_tokens(ctx, record);
41 n_estimated_match_documents = (double)n_candidates / (double)n_tokens;
42
43 if (n_estimated_match_documents >= (double)n_all_documents) {
44 return min_score;
45 } else {
46 double idf;
47 double tf_idf;
48
49 idf = log((double)n_all_documents / n_estimated_match_documents);
50 tf_idf = tf * idf;
51 return fmax(tf_idf, min_score);
52 }
53 }
54
55 static double
56 scorer_tf_at_most(grn_ctx *ctx, grn_scorer_matched_record *record)
57 {
58 double tf;
59 double max;
60 grn_obj *max_raw;
61
62 tf = grn_scorer_matched_record_get_n_occurrences(ctx, record) +
63 grn_scorer_matched_record_get_total_term_weights(ctx, record);
64 max_raw = grn_scorer_matched_record_get_arg(ctx, record, 0);
65
66 if (!max_raw) {
67 return tf;
68 }
69
70 if (max_raw->header.type != GRN_BULK) {
71 return tf;
72 }
73
74 if (max_raw->header.domain == GRN_DB_FLOAT) {
75 max = GRN_FLOAT_VALUE(max_raw);
76 } else {
77 grn_obj casted_max_raw;
78 GRN_FLOAT_INIT(&casted_max_raw, 0);
79 if (grn_obj_cast(ctx, max_raw, &casted_max_raw, false) != GRN_SUCCESS) {
80 GRN_OBJ_FIN(ctx, &casted_max_raw);
81 return tf;
82 } else {
83 max = GRN_FLOAT_VALUE(&casted_max_raw);
84 }
85 GRN_OBJ_FIN(ctx, &casted_max_raw);
86 }
87
88 return fmin(tf, max);
89 }
90
91 grn_rc
92 grn_db_init_builtin_scorers(grn_ctx *ctx)
93 {
94 grn_scorer_register(ctx, "scorer_tf_idf", -1, scorer_tf_idf);
95 grn_scorer_register(ctx, "scorer_tf_at_most", -1, scorer_tf_at_most);
96 return GRN_SUCCESS;
97 }