| 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"cxx . . $ `<file>/lib/grn_db.h`/ |
| 21 | |
| 22 | #include <groonga/scorer.h>cxx . . $ `<file>/include/groonga/scorer.h`/ |
| 23 | |
| 24 | #include <math.h> |
| 25 | |
| 26 | static double |
| 27 | scorer_tf_idfcxx . . $ scorer_tf_idf(b391fd9c67060962).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_scorer_matched_recordcxx . . $ grn_scorer_matched_record# *record) |
| 28 | { |
| 29 | double min_score = 1.0; |
| 30 | double tf; |
| 31 | uint64_tcxx . . $ uint64_t# n_all_documents; |
| 32 | uint64_tcxx . . $ uint64_t# n_candidates; |
| 33 | uint32_tcxx . . $ uint32_t# n_tokens; |
| 34 | double n_estimated_match_documents; |
| 35 | |
| 36 | tf = grn_scorer_matched_record_get_n_occurrencescxx . . $ grn_scorer_matched_record_get_n_occurrences(b7b640a8991c71b0).(ctx, record) + |
| 37 | grn_scorer_matched_record_get_total_term_weightscxx . . $ grn_scorer_matched_record_get_total_term_weights(b7b640a8991c71b0).(ctx, record); |
| 38 | n_all_documents = grn_scorer_matched_record_get_n_documentscxx . . $ grn_scorer_matched_record_get_n_documents(876307874c9a6fa3).(ctx, record); |
| 39 | n_candidates = grn_scorer_matched_record_get_n_candidatescxx . . $ grn_scorer_matched_record_get_n_candidates(876307874c9a6fa3).(ctx, record); |
| 40 | n_tokens = grn_scorer_matched_record_get_n_tokenscxx . . $ grn_scorer_matched_record_get_n_tokens(b7b640a8991c71b0).(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_mostcxx . . $ scorer_tf_at_most(b391fd9c67060962).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_scorer_matched_recordcxx . . $ grn_scorer_matched_record# *record) |
| 57 | { |
| 58 | double tf; |
| 59 | double max; |
| 60 | grn_objcxx . . $ grn_obj# *max_raw; |
| 61 | |
| 62 | tf = grn_scorer_matched_record_get_n_occurrencescxx . . $ grn_scorer_matched_record_get_n_occurrences(b7b640a8991c71b0).(ctx, record) + |
| 63 | grn_scorer_matched_record_get_total_term_weightscxx . . $ grn_scorer_matched_record_get_total_term_weights(b7b640a8991c71b0).(ctx, record); |
| 64 | max_raw = grn_scorer_matched_record_get_argcxx . . $ grn_scorer_matched_record_get_arg(5a582bc68c143fb5).(ctx, record, 0); |
| 65 | |
| 66 | if (!max_raw) { |
| 67 | return tf; |
| 68 | } |
| 69 | |
| 70 | if (max_raw->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type. != GRN_BULKcxx . . $ `include/groonga/groonga.h:868:9`!) { |
| 71 | return tf; |
| 72 | } |
| 73 | |
| 74 | if (max_raw->headercxx . . $ _grn_obj#header..domaincxx . . $ _grn_obj_header#domain. == GRN_DB_FLOATcxx . . $ GRN_DB_FLOAT.) { |
| 75 | max = GRN_FLOAT_VALUEcxx . . $ `include/groonga/groonga.h:3108:9`!(max_raw); |
| 76 | } else { |
| 77 | grn_objcxx . . $ grn_obj# casted_max_raw; |
| 78 | GRN_FLOAT_INITcxx . . $ `include/groonga/groonga.h:2864:9`!(&casted_max_raw, 0); |
| 79 | if (grn_obj_castcxx . . $ grn_obj_cast(a6fd93da1614eccd).(ctx, max_raw, &casted_max_raw, falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!) != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 80 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &casted_max_raw); |
| 81 | return tf; |
| 82 | } else { |
| 83 | max = GRN_FLOAT_VALUEcxx . . $ `include/groonga/groonga.h:3108:9`!(&casted_max_raw); |
| 84 | } |
| 85 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &casted_max_raw); |
| 86 | } |
| 87 | |
| 88 | return fmin(tf, max); |
| 89 | } |
| 90 | |
| 91 | grn_rccxx . . $ grn_rc# |
| 92 | grn_db_init_builtin_scorerscxx . . $ grn_db_init_builtin_scorers(2b4fa601bf4f1e5b).(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 93 | { |
| 94 | grn_scorer_registercxx . . $ grn_scorer_register(e334657bb5aaacf).(ctx, "scorer_tf_idf", -1, scorer_tf_idfcxx . . $ scorer_tf_idf(b391fd9c67060962).); |
| 95 | grn_scorer_registercxx . . $ grn_scorer_register(e334657bb5aaacf).(ctx, "scorer_tf_at_most", -1, scorer_tf_at_mostcxx . . $ scorer_tf_at_most(b391fd9c67060962).); |
| 96 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 97 | } |