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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*
Copyright(C) 2015-2018 Brazil
Copyright(C) 2020-2022 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
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-1301 USA
*/
#include <string.h>
#include "grn.h"
#include "grn_db.h"
#include "grn_expr.h"
#include "grn_scorer.h"
#include <groonga/scorer.h>
grn_obj *
grn_scorer_matched_record_get_table(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->table;
}
grn_obj *
grn_scorer_matched_record_get_lexicon(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->lexicon;
}
grn_id
grn_scorer_matched_record_get_id(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->id;
}
grn_obj *
grn_scorer_matched_record_get_terms(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return &(record->terms);
}
grn_obj *
grn_scorer_matched_record_get_term_weights(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return &(record->term_weights);
}
uint32_t
grn_scorer_matched_record_get_total_term_weights(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->total_term_weights;
}
uint64_t
grn_scorer_matched_record_get_n_documents(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->n_documents;
}
uint32_t
grn_scorer_matched_record_get_n_occurrences(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->n_occurrences;
}
uint64_t
grn_scorer_matched_record_get_n_candidates(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->n_candidates;
}
uint32_t
grn_scorer_matched_record_get_n_tokens(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->n_tokens;
}
int
grn_scorer_matched_record_get_weight(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
return record->weight;
}
grn_obj *
grn_scorer_matched_record_get_arg(grn_ctx *ctx,
grn_scorer_matched_record *record,
unsigned int i)
{
grn_expr *expr;
grn_expr_code *codes_original;
uint32_t codes_curr_original;
grn_obj *arg;
if (!record->args_expr) {
return NULL;
}
expr = (grn_expr *)(record->args_expr);
/* TODO: support getting column value */
codes_original = expr->codes;
codes_curr_original = expr->codes_curr;
expr->codes += record->args_expr_offset;
expr->codes_curr = 1; /* TODO: support 1 or more codes */
arg = grn_expr_exec(ctx, (grn_obj *)expr, 0);
expr->codes_curr = codes_curr_original;
expr->codes = codes_original;
return arg;
}
unsigned int
grn_scorer_matched_record_get_n_args(grn_ctx *ctx,
grn_scorer_matched_record *record)
{
grn_expr *expr;
grn_expr_code *codes;
unsigned int n_args = 0;
if (!record->args_expr) {
return 0;
}
expr = (grn_expr *)(record->args_expr);
codes = expr->codes + record->args_expr_offset;
if (codes[0].op == GRN_OP_CALL) {
return 0;
}
n_args++;
for (; codes[0].op != GRN_OP_CALL; codes++) {
if (codes[0].op == GRN_OP_COMMA) {
n_args++;
}
}
return n_args;
}
grn_rc
grn_scorer_register(grn_ctx *ctx,
const char *scorer_name_ptr,
int scorer_name_length,
grn_scorer_score_func *score)
{
if (scorer_name_length == -1) {
scorer_name_length = (int)strlen(scorer_name_ptr);
}
{
grn_obj *scorer_object = grn_proc_create(ctx,
scorer_name_ptr,
scorer_name_length,
GRN_PROC_SCORER,
NULL, NULL, NULL, 0, NULL);
if (scorer_object == NULL) {
GRN_PLUGIN_ERROR(ctx, GRN_SCORER_ERROR,
"[scorer][%.*s] failed to grn_proc_create()",
scorer_name_length, scorer_name_ptr);
return ctx->rc;
}
{
grn_proc *scorer = (grn_proc *)scorer_object;
scorer->callbacks.scorer.score = score;
}
}
return GRN_SUCCESS;
}
|