Debian Codegraph @

lib/proc/proc_fuzzy_search.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2016 Brazil
3 Copyright (C) 2019-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 #include "../grn_ii.h"
22 #include "../grn_proc.h"
23 #include "../grn_rset.h"
24 #include "../grn_posting.h"
25
26 #include <groonga/plugin.h>
27
28 #include <string.h>
29
30 #define DIST(ox,oy) (dists[((lx + 1) * (oy)) + (ox)])
31
32 static uint32_t
33 calc_edit_distance(grn_ctx *ctx, char *sx, char *ex, char *sy, char *ey, int flags)
34 {
35 uint32_t d = 0;
36 int cx;
37 int cy;
38 uint32_t lx, ly, *dists;
39 char *px, *py;
40 for (px = sx, lx = 0;
41 px < ex && (cx = grn_charlen(ctx, px, ex));
42 px += cx, lx++) {
43 }
44 for (py = sy, ly = 0;
45 py < ey && (cy = grn_charlen(ctx, py, ey));
46 py += cy, ly++) {
47 }
48 if ((dists = GRN_PLUGIN_MALLOC(ctx, (lx + 1) * (ly + 1) * sizeof(uint32_t)))) {
49 uint32_t x, y;
50 for (x = 0; x <= lx; x++) { DIST(x, 0) = x; }
51 for (y = 0; y <= ly; y++) { DIST(0, y) = y; }
52 for (x = 1, px = sx; x <= lx; x++, px += cx) {
53 cx = grn_charlen(ctx, px, ex);
54 for (y = 1, py = sy; y <= ly; y++, py += cy) {
55 cy = grn_charlen(ctx, py, ey);
56 if (cx == cy && !memcmp(px, py, (size_t)cx)) {
57 DIST(x, y) = DIST(x - 1, y - 1);
58 } else {
59 uint32_t a = DIST(x - 1, y) + 1;
60 uint32_t b = DIST(x, y - 1) + 1;
61 uint32_t c = DIST(x - 1, y - 1) + 1;
62 DIST(x, y) = ((a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c));
63 if (flags & GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION &&
64 x > 1 && y > 1 && cx == cy &&
65 memcmp(px, py - cy, (size_t)cx) == 0 &&
66 memcmp(px - cx, py, (size_t)cx) == 0) {
67 uint32_t t = DIST(x - 2, y - 2) + 1;
68 DIST(x, y) = ((DIST(x, y) < t) ? DIST(x, y) : t);
69 }
70 }
71 }
72 }
73 d = DIST(lx, ly);
74 GRN_PLUGIN_FREE(ctx, dists);
75 }
76 return d;
77 }
78
79 static grn_obj *
80 func_edit_distance(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
81 {
82 #define N_REQUIRED_ARGS 2
83 #define MAX_ARGS 3
84 uint32_t d = 0;
85 int flags = 0;
86 grn_obj *obj;
87 if (nargs >= N_REQUIRED_ARGS && nargs <= MAX_ARGS) {
88 if (nargs == MAX_ARGS && GRN_BOOL_VALUE(args[2])) {
89 flags |= GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION;
90 }
91 d = calc_edit_distance(ctx,
92 GRN_TEXT_VALUE(args[0]),
93 GRN_BULK_CURR(args[0]),
94 GRN_TEXT_VALUE(args[1]),
95 GRN_BULK_CURR(args[1]),
96 flags);
97 }
98 if ((obj = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_UINT32, 0))) {
99 GRN_UINT32_SET(ctx, obj, d);
100 }
101 return obj;
102 #undef N_REQUIRED_ARGS
103 #undef MAX_ARGS
104 }
105
106 void
107 grn_proc_init_edit_distance(grn_ctx *ctx)
108 {
109 grn_proc_create(ctx, "edit_distance", -1, GRN_PROC_FUNCTION,
110 func_edit_distance, NULL, NULL, 0, NULL);
111 }
112
113 #define SCORE_HEAP_SIZE 256
114
115 typedef struct {
116 grn_id id;
117 uint32_t score;
118 } score_heap_node;
119
120 typedef struct {
121 uint32_t n_entries;
122 uint32_t limit;
123 score_heap_node *nodes;
124 } score_heap;
125
126 static inline score_heap *
127 score_heap_open(grn_ctx *ctx, uint32_t max)
128 {
129 score_heap *h = GRN_PLUGIN_MALLOC(ctx, sizeof(score_heap));
130 if (!h) { return NULL; }
131 h->nodes = GRN_PLUGIN_MALLOC(ctx, sizeof(score_heap_node) * max);
132 if (!h->nodes) {
133 GRN_PLUGIN_FREE(ctx, h);
134 return NULL;
135 }
136 h->n_entries = 0;
137 h->limit = max;
138 return h;
139 }
140
141 static inline bool
142 score_heap_push(grn_ctx *ctx, score_heap *h, grn_id id, uint32_t score)
143 {
144 uint32_t n, n2;
145 score_heap_node node = {id, score};
146 score_heap_node node2;
147 if (h->n_entries >= h->limit) {
148 uint32_t max = h->limit * 2;
149 score_heap_node *nodes;
150 nodes = GRN_PLUGIN_REALLOC(ctx, h->nodes, sizeof(score_heap) * max);
151 if (!nodes) {
152 return false;
153 }
154 h->limit = max;
155 h->nodes = nodes;
156 }
157 h->nodes[h->n_entries] = node;
158 n = h->n_entries++;
159 while (n) {
160 n2 = (n - 1) >> 1;
161 if (h->nodes[n2].score <= h->nodes[n].score) { break; }
162 node2 = h->nodes[n];
163 h->nodes[n] = h->nodes[n2];
164 h->nodes[n2] = node2;
165 n = n2;
166 }
167 return true;
168 }
169
170 static inline void
171 score_heap_close(grn_ctx *ctx, score_heap *h)
172 {
173 GRN_PLUGIN_FREE(ctx, h->nodes);
174 GRN_PLUGIN_FREE(ctx, h);
175 }
176
177 static grn_rc
178 sequential_fuzzy_search(grn_ctx *ctx, grn_obj *table, grn_obj *column, grn_obj *query,
179 uint32_t max_distance, uint32_t prefix_match_size,
180 uint32_t max_expansion, int flags, grn_obj *res, grn_operator op)
181 {
182 grn_table_cursor *tc;
183 char *sx = GRN_TEXT_VALUE(query);
184 char *ex = GRN_BULK_CURR(query);
185
186 if (op == GRN_OP_AND) {
187 tc = grn_table_cursor_open(ctx, res, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_BY_ID);
188 } else {
189 tc = grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_BY_ID);
190 }
191 if (tc) {
192 grn_id id;
193 grn_obj value;
194 score_heap *heap;
195 uint32_t i, n;
196 GRN_TEXT_INIT(&value, 0);
197
198 heap = score_heap_open(ctx, SCORE_HEAP_SIZE);
199 if (!heap) {
200 grn_table_cursor_close(ctx, tc);
201 grn_obj_unlink(ctx, &value);
202 return GRN_NO_MEMORY_AVAILABLE;
203 }
204
205 while ((id = grn_table_cursor_next(ctx, tc))) {
206 unsigned int distance = 0;
207 grn_obj *domain;
208 grn_id record_id;
209
210 if (op == GRN_OP_AND) {
211 grn_id *key;
212 grn_table_cursor_get_key(ctx, tc, (void **)&key);
213 record_id = *key;
214 } else {
215 record_id = id;
216 }
217 GRN_BULK_REWIND(&value);
218 grn_obj_get_value(ctx, column, record_id, &value);
219 domain = grn_ctx_at(ctx, ((&value))->header.domain);
220 if ((&(value))->header.type == GRN_VECTOR) {
221 n = grn_vector_size(ctx, &value);
222 for (i = 0; i < n; i++) {
223 unsigned int length;
224 const char *vector_value = NULL;
225 length = grn_vector_get_element(ctx, &value, i, &vector_value, NULL, NULL);
226
227 if (!prefix_match_size ||
228 (prefix_match_size > 0 && length >= prefix_match_size &&
229 !memcmp(sx, vector_value, prefix_match_size))) {
230 distance = calc_edit_distance(ctx, sx, ex,
231 (char *)vector_value,
232 (char *)vector_value + length, flags);
233 if (distance <= max_distance) {
234 score_heap_push(ctx, heap, record_id, distance);
235 break;
236 }
237 }
238 }
239 } else if ((&(value))->header.type == GRN_UVECTOR &&
240 grn_obj_is_table(ctx, domain)) {
241 n = grn_vector_size(ctx, &value);
242 for (i = 0; i < n; i++) {
243 grn_id rid;
244 char key_name[GRN_TABLE_MAX_KEY_SIZE];
245 int key_length;
246 rid = grn_uvector_get_element(ctx, &value, i, NULL);
247 key_length = grn_table_get_key(ctx, domain, rid, key_name, GRN_TABLE_MAX_KEY_SIZE);
248
249 if (!prefix_match_size ||
250 (prefix_match_size > 0 &&
251 (uint32_t)key_length >= prefix_match_size &&
252 !memcmp(sx, key_name, prefix_match_size))) {
253 distance = calc_edit_distance(ctx, sx, ex,
254 key_name, key_name + key_length, flags);
255 if (distance <= max_distance) {
256 score_heap_push(ctx, heap, record_id, distance);
257 break;
258 }
259 }
260 }
261 } else {
262 if (grn_obj_is_reference_column(ctx, column)) {
263 grn_id rid;
264 char key_name[GRN_TABLE_MAX_KEY_SIZE];
265 int key_length;
266 rid = GRN_RECORD_VALUE(&value);
267 key_length = grn_table_get_key(ctx, domain, rid, key_name, GRN_TABLE_MAX_KEY_SIZE);
268 if (!prefix_match_size ||
269 (prefix_match_size > 0 &&
270 (uint32_t)key_length >= prefix_match_size &&
271 !memcmp(sx, key_name, prefix_match_size))) {
272 distance = calc_edit_distance(ctx, sx, ex,
273 key_name, key_name + key_length, flags);
274 if (distance <= max_distance) {
275 score_heap_push(ctx, heap, record_id, distance);
276 }
277 }
278 } else {
279 if (!prefix_match_size ||
280 (prefix_match_size > 0 && GRN_TEXT_LEN(&value) >= prefix_match_size &&
281 !memcmp(sx, GRN_TEXT_VALUE(&value), prefix_match_size))) {
282 distance = calc_edit_distance(ctx, sx, ex,
283 GRN_TEXT_VALUE(&value),
284 GRN_BULK_CURR(&value), flags);
285 if (distance <= max_distance) {
286 score_heap_push(ctx, heap, record_id, distance);
287 }
288 }
289 }
290 }
291 grn_obj_unlink(ctx, domain);
292 }
293 grn_table_cursor_close(ctx, tc);
294 grn_obj_unlink(ctx, &value);
295
296 for (i = 0; i < heap->n_entries; i++) {
297 if (max_expansion > 0 && (size_t)i >= max_expansion) {
298 break;
299 }
300 {
301 grn_posting_internal posting = {0};
302 posting.rid = heap->nodes[i].id;
303 posting.sid = 1;
304 posting.pos = 0;
305 posting.weight_float = (float)(max_distance - heap->nodes[i].score + 1);
306 grn_ii_posting_add_float(ctx,
307 (grn_posting *)(&posting),
308 (grn_hash *)res,
309 op);
310 }
311 }
312 grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
313 score_heap_close(ctx, heap);
314 }
315
316 return GRN_SUCCESS;
317 }
318
319 typedef struct {
320 grn_obj *target;
321 grn_obj *query;
322 uint32_t max_distance;
323 uint32_t prefix_length;
324 uint32_t prefix_match_size;
325 uint32_t max_expansions;
326 int flags;
327 } fuzzy_search_data;
328
329 static grn_rc
330 selector_fuzzy_search_execute(grn_ctx *ctx,
331 grn_obj *index,
332 grn_operator op,
333 grn_obj *res,
334 grn_operator logical_op,
335 void *user_data)
336 {
337 grn_rc rc = GRN_SUCCESS;
338 fuzzy_search_data *data = user_data;
339 grn_obj *target = index;
340 bool use_sequential_search = false;
341
342 if (grn_obj_is_accessor(ctx, target)) {
343 if (grn_obj_is_key_accessor(ctx, target) &&
344 ((grn_accessor *)target)->obj->header.type == GRN_TABLE_PAT_KEY) {
345 target = ((grn_accessor *)target)->obj;
346 use_sequential_search = false;
347 } else {
348 use_sequential_search = true;
349 }
350 } else if (target) {
351 use_sequential_search = true;
352 if (target->header.type == GRN_TABLE_PAT_KEY) {
353 use_sequential_search = false;
354 } else {
355 grn_obj *lexicon;
356 lexicon = grn_ctx_at(ctx, target->header.domain);
357 if (lexicon && lexicon->header.type == GRN_TABLE_PAT_KEY) {
358 use_sequential_search = false;
359 }
360 }
361 } else {
362 use_sequential_search = true;
363 }
364
365 if (data->prefix_length) {
366 const char *s = GRN_TEXT_VALUE(data->query);
367 const char *e = GRN_BULK_CURR(data->query);
368 const char *p;
369 int cl = 0;
370 unsigned int length = 0;
371 for (p = s; p < e && (cl = grn_charlen(ctx, p, e)); p += cl) {
372 length++;
373 if (length > data->prefix_length) {
374 break;
375 }
376 }
377 data->prefix_match_size = (uint32_t)(p - s);
378 }
379
380 if (use_sequential_search) {
381 grn_obj *table;
382 table = grn_ctx_at(ctx, res->header.domain);
383 rc = sequential_fuzzy_search(ctx,
384 table,
385 data->target,
386 data->query,
387 data->max_distance,
388 data->prefix_match_size,
389 data->max_expansions,
390 data->flags,
391 res,
392 logical_op);
393 goto exit;
394 }
395
396 if (!target) {
397 grn_obj inspected;
398 GRN_TEXT_INIT(&inspected, 0);
399 grn_inspect(ctx, &inspected, target);
400 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
401 "fuzzy_search(): "
402 "column must be COLUMN_INDEX or TABLE_PAT_KEY: <%.*s>",
403 (int)GRN_TEXT_LEN(&inspected),
404 GRN_TEXT_VALUE(&inspected));
405 rc = ctx->rc;
406 GRN_OBJ_FIN(ctx, &inspected);
407 } else {
408 grn_search_optarg options = {0};
409 options.mode = GRN_OP_FUZZY;
410 options.fuzzy.prefix_match_size = data->prefix_match_size;
411 options.fuzzy.max_distance = data->max_distance;
412 options.fuzzy.max_expansion = data->max_expansions;
413 options.fuzzy.flags = data->flags;
414 grn_obj_search(ctx, target, data->query, res, logical_op, &options);
415 }
416
417 exit :
418 return rc;
419 }
420
421 static grn_rc
422 selector_fuzzy_search(grn_ctx *ctx, grn_obj *table, grn_obj *index,
423 int nargs, grn_obj **args,
424 grn_obj *res, grn_operator op)
425 {
426 const char *tag = "[fuzzy-search]";
427 grn_rc rc = GRN_SUCCESS;
428
429 if ((nargs - 1) < 2) {
430 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
431 "%s wrong number of arguments (%d for 2..)",
432 tag,
433 nargs - 1);
434 rc = ctx->rc;
435 goto exit;
436 }
437
438 fuzzy_search_data data;
439 data.target = args[1];
440 data.query = args[2];
441 data.max_distance = 1;
442 data.prefix_length = 0;
443 data.prefix_match_size = 0;
444 data.max_expansions = 0;
445 data.flags = 0;
446
447 if (nargs == 4) {
448 grn_obj *options = args[3];
449
450 switch (options->header.type) {
451 case GRN_BULK :
452 data.max_distance = GRN_UINT32_VALUE(options);
453 break;
454 case GRN_TABLE_HASH_KEY :
455 {
456 bool with_transposition = false;
457 rc = grn_proc_options_parse(ctx,
458 options,
459 tag,
460 "max_distance",
461 GRN_PROC_OPTION_VALUE_UINT32,
462 &(data.max_distance),
463 "prefix_length",
464 GRN_PROC_OPTION_VALUE_UINT32,
465 &(data.prefix_length),
466 /* Deprecated since 13.0.8.
467 * Keep this for backward compatibility. */
468 "max_expansion",
469 GRN_PROC_OPTION_VALUE_UINT32,
470 &(data.max_expansions),
471 "max_expansions",
472 GRN_PROC_OPTION_VALUE_UINT32,
473 &(data.max_expansions),
474 "with_transposition",
475 GRN_PROC_OPTION_VALUE_BOOL,
476 &with_transposition,
477 NULL);
478 if (rc != GRN_SUCCESS) {
479 goto exit;
480 }
481 if (with_transposition) {
482 data.flags |= GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION;
483 }
484 }
485 break;
486 default :
487 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
488 "%s "
489 "3rd argument must be integer or object literal: <%.*s>",
490 tag,
491 (int)GRN_TEXT_LEN(options),
492 GRN_TEXT_VALUE(options));
493 goto exit;
494 }
495 }
496
497 if (grn_obj_is_accessor(ctx, data.target)) {
498 rc = grn_accessor_execute(ctx,
499 data.target,
500 selector_fuzzy_search_execute,
501 &data,
502 GRN_OP_FUZZY,
503 res,
504 op);
505 } else {
506 grn_index_datum index_datum;
507 unsigned int n_index_datum;
508 if (grn_obj_is_index_column(ctx, data.target)) {
509 index_datum.index = data.target;
510 n_index_datum = 1;
511 } else {
512 n_index_datum = grn_column_find_index_data(ctx,
513 data.target,
514 GRN_OP_FUZZY,
515 &index_datum,
516 1);
517 }
518 if (n_index_datum == 0) {
519 index_datum.index = NULL;
520 }
521 rc = selector_fuzzy_search_execute(ctx,
522 index_datum.index,
523 GRN_OP_FUZZY,
524 res,
525 op,
526 &data);
527 }
528
529 exit :
530 return rc;
531 }
532
533 void
534 grn_proc_init_fuzzy_search(grn_ctx *ctx)
535 {
536 grn_obj *selector_proc;
537
538 selector_proc = grn_proc_create(ctx, "fuzzy_search", -1,
539 GRN_PROC_FUNCTION,
540 NULL, NULL, NULL, 0, NULL);
541 grn_proc_set_selector(ctx, selector_proc, selector_fuzzy_search);
542 grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP);
543 }