[Groonga-commit] groonga/groonga [master] Added word correction for suggest function.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 8月 23日 (月) 16:56:29 JST


Tasuku SUENAGA a.k.a. gunyarakun	2010-08-23 07:56:29 +0000 (Mon, 23 Aug 2010)

  New Revision: cf8e1f98bd49c781b03807d7c2248a7ca6f94d97

  Log:
    Added word correction for suggest function.

  Modified files:
    lib/ii.c
    modules/suggest/suggest.c

  Modified: lib/ii.c (+3 -0)
===================================================================
--- lib/ii.c    2010-08-23 07:46:17 +0000 (4f58d55)
+++ lib/ii.c    2010-08-23 07:56:29 +0000 (20462f4)
@@ -5569,6 +5569,7 @@ grn_ii_similar_search(grn_ctx *ctx, grn_ii *ii,
     }
   }
   grn_token_close(ctx, token);
+  grn_p(ctx, h);
   {
     grn_hash_cursor *c = grn_hash_cursor_open(ctx, h, NULL, 0, NULL, 0, 0, -1, 0);
     if (!c) {
@@ -5606,6 +5607,8 @@ grn_ii_similar_search(grn_ctx *ctx, grn_ii *ii,
       return GRN_NO_MEMORY_AVAILABLE;
     }
     grn_hash_sort(ctx, h, limit, sorted, &arg);
+    grn_p(ctx, h);
+    grn_p(ctx, sorted);
     /* todo support subrec
     rep = (s->record_unit == grn_rec_position || s->subrec_unit == grn_rec_position);
     */

  Modified: modules/suggest/suggest.c (+68 -60)
===================================================================
--- modules/suggest/suggest.c    2010-08-23 07:46:17 +0000 (4b90720)
+++ modules/suggest/suggest.c    2010-08-23 07:56:29 +0000 (8b713c8)
@@ -34,75 +34,83 @@ func_suggest(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
       grn_obj *res;
       if ((res = grn_table_create(ctx, NULL, 0, NULL,
                                    GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, table, NULL))) {
-        /* RK search */
-        grn_obj *index;
-        /* FIXME: support index selection */
-        if (grn_column_index(ctx, col, GRN_OP_PREFIX,
-                             &index, 1, NULL)) {
-          grn_table_cursor *cur;
-          if ((cur = grn_table_cursor_open(ctx, grn_ctx_at(ctx, index->header.domain),
-                                           GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)),
-                                           NULL, 0,
-                                           0, -1,
-                                           GRN_CURSOR_PREFIX | GRN_CURSOR_RK))) {
-            grn_id id;
-            while ((id = grn_table_cursor_next(ctx, cur))) {
-              grn_ii_cursor *icur;
-              if ((icur = grn_ii_cursor_open(ctx, (grn_ii *)index, id,
-                                             GRN_ID_NIL, GRN_ID_MAX, 1, 0))) {
-                grn_ii_posting *p;
-                while ((p = grn_ii_cursor_next(ctx, icur))) {
-                  grn_hash_add(ctx, (grn_hash *)res, &p->rid, sizeof(grn_id), NULL, NULL);
+        grn_obj *sorted;
+        if ((sorted = grn_table_create(ctx, NULL, 0, NULL,
+                                       GRN_OBJ_TABLE_NO_KEY, NULL, res))) {
+#if 1
+          /* RK search */
+          grn_obj *index;
+          /* FIXME: support index selection */
+          if (grn_column_index(ctx, col, GRN_OP_PREFIX,
+                               &index, 1, NULL)) {
+            grn_table_cursor *cur;
+            if ((cur = grn_table_cursor_open(ctx, grn_ctx_at(ctx, index->header.domain),
+                                             GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)),
+                                             NULL, 0,
+                                             0, -1,
+                                             GRN_CURSOR_PREFIX | GRN_CURSOR_RK))) {
+              grn_id id;
+              while ((id = grn_table_cursor_next(ctx, cur))) {
+                grn_ii_cursor *icur;
+                if ((icur = grn_ii_cursor_open(ctx, (grn_ii *)index, id,
+                                               GRN_ID_NIL, GRN_ID_MAX, 1, 0))) {
+                  grn_ii_posting *p;
+                  while ((p = grn_ii_cursor_next(ctx, icur))) {
+                    grn_hash_add(ctx, (grn_hash *)res, &p->rid, sizeof(grn_id), NULL, NULL);
+                  }
+                  grn_ii_cursor_close(ctx, icur);
+                } else {
+                  ERR(GRN_UNKNOWN_ERROR, "cannot open cursor for index.");
+                  break;
                 }
-                grn_ii_cursor_close(ctx, icur);
-              } else {
-                ERR(GRN_UNKNOWN_ERROR, "cannot open cursor for index.");
-                break;
               }
+              grn_table_cursor_close(ctx, cur);
+            } else {
+              ERR(GRN_UNKNOWN_ERROR, "cannot open cursor for pk.");
             }
-            /* sort */
-            {
-              grn_obj *sorted;
-              if ((sorted = grn_table_create(ctx, NULL, 0, NULL,
-                                             GRN_OBJ_TABLE_NO_KEY, NULL, res))) {
-                uint32_t nkeys;
-                grn_table_sort_key *keys;
-                if ((keys = grn_table_sort_key_from_str(ctx, CONST_STR_LEN("-score"), res, &nkeys))) {
-                  grn_table_cursor *scur;
-                  /* TODO: support offset limit */
-                  grn_table_sort(ctx, res, 0, grn_table_size(ctx, res), sorted, keys, nkeys);
-                  GRN_OUTPUT_ARRAY_OPEN("RESULT", -1);
-                  if ((scur = grn_table_cursor_open(ctx, sorted, NULL, 0, NULL, 0, 0, -1, 0))) {
-                    grn_id sid;
-                    while ((sid = grn_table_cursor_next(ctx, scur))) {
-                      grn_id stid;
-                      unsigned int key_len;
-                      char key[GRN_TABLE_MAX_KEY_SIZE];
-                      grn_table_get_key(ctx, sorted, sid, &stid, sizeof(grn_id));
-                      grn_table_get_key(ctx, res, stid, &stid, sizeof(grn_id));
-                      key_len = grn_table_get_key(ctx, table, stid, key, GRN_TABLE_MAX_KEY_SIZE);
-                      GRN_OUTPUT_STR(key, key_len);
-                    }
-                    grn_table_cursor_close(ctx, scur);
-                  } else {
-                    ERR(GRN_UNKNOWN_ERROR, "cannot open sorted cursor.");
-                  }
-                  GRN_OUTPUT_ARRAY_CLOSE();
-                  grn_table_sort_key_close(ctx, keys, nkeys);
-                } else {
-                  ERR(GRN_UNKNOWN_ERROR, "cannot sort.");
+          } else {
+            ERR(GRN_UNKNOWN_ERROR, "cannot find index for prefix search.");
+          }
+#else
+          grn_select_optarg optarg;
+          memset(&optarg, 0, sizeof(grn_select_optarg));
+          optarg.mode = GRN_OP_SIMILAR;
+
+          grn_ii_select(ctx, (grn_ii *)grn_ctx_get(ctx, CONST_STR_LEN("SuggestBigram.suggest_key")),
+                        GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)), (grn_hash *)res, GRN_OP_OR, &optarg);
+#endif
+          /* sort */
+          {
+            uint32_t nkeys;
+            grn_table_sort_key *keys;
+            if ((keys = grn_table_sort_key_from_str(ctx, CONST_STR_LEN("-score"), res, &nkeys))) {
+              grn_table_cursor *scur;
+              /* TODO: support offset limit */
+              grn_table_sort(ctx, res, 0, grn_table_size(ctx, res), sorted, keys, nkeys);
+              GRN_OUTPUT_ARRAY_OPEN("RESULT", -1);
+              if ((scur = grn_table_cursor_open(ctx, sorted, NULL, 0, NULL, 0, 0, -1, 0))) {
+                grn_id id;
+                while ((id = grn_table_cursor_next(ctx, scur))) {
+                  unsigned int key_len;
+                  char key[GRN_TABLE_MAX_KEY_SIZE];
+                  grn_table_get_key(ctx, sorted, id, &id, sizeof(grn_id));
+                  grn_table_get_key(ctx, res, id, &id, sizeof(grn_id));
+                  key_len = grn_table_get_key(ctx, table, id, key, GRN_TABLE_MAX_KEY_SIZE);
+                  GRN_OUTPUT_STR(key, key_len);
                 }
-                grn_obj_close(ctx, sorted);
+                grn_table_cursor_close(ctx, scur);
               } else {
-                ERR(GRN_UNKNOWN_ERROR, "cannot create temporary sort table.");
+                ERR(GRN_UNKNOWN_ERROR, "cannot open sorted cursor.");
               }
+              GRN_OUTPUT_ARRAY_CLOSE();
+              grn_table_sort_key_close(ctx, keys, nkeys);
+            } else {
+              ERR(GRN_UNKNOWN_ERROR, "cannot sort.");
             }
-            grn_table_cursor_close(ctx, cur);
-          } else {
-            ERR(GRN_UNKNOWN_ERROR, "cannot open cursor for pk.");
           }
+          grn_obj_close(ctx, sorted);
         } else {
-          ERR(GRN_UNKNOWN_ERROR, "cannot find index for prefix search.");
+          ERR(GRN_UNKNOWN_ERROR, "cannot create temporary sort table.");
         }
         grn_obj_close(ctx, res);
       } else {




Groonga-commit メーリングリストの案内
Back to archive index