[Groonga-commit] groonga/groonga at be849e2 [master] Add highlight_html() function

Back to archive index

naoa null+****@clear*****
Sun Aug 10 06:56:57 JST 2014


naoa	2014-08-10 06:56:57 +0900 (Sun, 10 Aug 2014)

  New Revision: be849e2ec03c14f0a7ae74ccdf388738c2f58524
  https://github.com/groonga/groonga/commit/be849e2ec03c14f0a7ae74ccdf388738c2f58524

  Merged c514768: Merge pull request #185 from naoa/add-highlight_html-function

  Message:
    Add highlight_html() function
    
    It tags output text after it html_escape and normalize text. It can use easily without option.
    
        highlight_html(column)
    
    TODO: Document it.

  Added files:
    test/command/suite/select/function/highlight_html/empty_column.expected
    test/command/suite/select/function/highlight_html/empty_column.test
    test/command/suite/select/function/highlight_html/empty_query.expected
    test/command/suite/select/function/highlight_html/empty_query.test
    test/command/suite/select/function/highlight_html/no_hit.expected
    test/command/suite/select/function/highlight_html/no_hit.test
    test/command/suite/select/function/highlight_html/no_query.expected
    test/command/suite/select/function/highlight_html/no_query.test
    test/command/suite/select/function/highlight_html/one_hit.expected
    test/command/suite/select/function/highlight_html/one_hit.test
    test/command/suite/select/function/highlight_html/one_keyword.expected
    test/command/suite/select/function/highlight_html/one_keyword.test
    test/command/suite/select/function/highlight_html/twice_hits.expected
    test/command/suite/select/function/highlight_html/twice_hits.test
    test/command/suite/select/function/highlight_html/two_keywords.expected
    test/command/suite/select/function/highlight_html/two_keywords.test
  Modified files:
    lib/proc.c

  Modified: lib/proc.c (+86 -0)
===================================================================
--- lib/proc.c    2014-08-10 06:45:26 +0900 (5eaacaf)
+++ lib/proc.c    2014-08-10 06:56:57 +0900 (bc13a42)
@@ -4785,6 +4785,89 @@ grn_pat_tag_keys(grn_ctx *ctx, grn_obj *keywords,
 }
 
 static grn_obj *
+func_highlight_html(grn_ctx *ctx, int nargs, grn_obj **args,
+                    grn_user_data *user_data)
+{
+  grn_obj *highlighted = NULL;
+
+#define N_REQUIRED_ARGS 1
+  if (nargs == N_REQUIRED_ARGS) {
+    grn_obj *string = args[0];
+    grn_obj *expression = NULL;
+    grn_obj *condition_ptr = NULL;
+    grn_obj *condition = NULL;
+    grn_bool html_escape_flag = GRN_TRUE;
+    unsigned int n_keyword_sets = 1;
+    const char *open_tags[1];
+    unsigned int open_tag_lengths[1];
+    const char *close_tags[1];
+    unsigned int close_tag_lengths[1];
+    grn_obj *keywords;
+    grn_obj got_keywords;
+
+    open_tags[0] = "<span class=\"keyword\">";
+    open_tag_lengths[0] = strlen("<span class=\"keyword\">");
+    close_tags[0]  = "</span>";
+    close_tag_lengths[0] = strlen("</span>");
+
+    keywords = grn_table_create(ctx, NULL, 0, NULL,
+                                GRN_OBJ_TABLE_PAT_KEY,
+                                grn_ctx_at(ctx, GRN_DB_SHORT_TEXT),
+                                NULL);
+    {
+      grn_obj * normalizer;
+      normalizer = grn_ctx_get(ctx, "NormalizerAuto", -1);
+      grn_obj_set_info(ctx, keywords, GRN_INFO_NORMALIZER, normalizer);
+      grn_obj_unlink(ctx, normalizer);
+    }
+
+    grn_proc_get_info(ctx, user_data, NULL, NULL, &expression);
+    condition_ptr = grn_expr_get_var(ctx, expression,
+                                     GRN_SELECT_INTERNAL_VAR_CONDITION,
+                                     strlen(GRN_SELECT_INTERNAL_VAR_CONDITION));
+    if (condition_ptr) {
+      condition = GRN_PTR_VALUE(condition_ptr);
+    }
+
+    if (condition) {
+      GRN_PTR_INIT(&got_keywords, GRN_OBJ_VECTOR, GRN_ID_NIL);
+      grn_expr_get_keywords(ctx, condition, &got_keywords);
+
+      for (;;) {
+        grn_obj *keyword;
+        GRN_PTR_POP(&got_keywords, keyword);
+        if (!keyword) { break; }
+        grn_table_add(ctx, keywords,
+                      GRN_TEXT_VALUE(keyword),
+                      GRN_TEXT_LEN(keyword),
+                      NULL);
+      }
+      grn_obj_unlink(ctx, &got_keywords);
+    }
+
+    highlighted = GRN_PROC_ALLOC(GRN_DB_TEXT, 0);
+    grn_pat_tag_keys(ctx, keywords,
+                     GRN_TEXT_VALUE(string), GRN_TEXT_LEN(string),
+                     open_tags,
+                     open_tag_lengths,
+                     close_tags,
+                     close_tag_lengths,
+                     n_keyword_sets,
+                     highlighted,
+                     html_escape_flag);
+
+    grn_obj_unlink(ctx, keywords);
+  }
+#undef N_REQUIRED_ARGS
+
+  if (!highlighted) {
+    highlighted = GRN_PROC_ALLOC(GRN_DB_VOID, 0);
+  }
+
+  return highlighted;
+}
+
+static grn_obj *
 func_highlight_full(grn_ctx *ctx, int nargs, grn_obj **args,
                     grn_user_data *user_data)
 {
@@ -5122,6 +5205,9 @@ grn_db_init_builtin_query(grn_ctx *ctx)
     grn_proc_set_selector(ctx, selector_proc, selector_between);
   }
 
+  grn_proc_create(ctx, "highlight_html", -1, GRN_PROC_FUNCTION,
+                  func_highlight_html, NULL, NULL, 0, NULL);
+
   grn_proc_create(ctx, "highlight_full", -1, GRN_PROC_FUNCTION,
                   func_highlight_full, NULL, NULL, 0, NULL);
 }

  Added: test/command/suite/select/function/highlight_html/empty_column.expected (+15 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/empty_column.expected    2014-08-10 06:56:57 +0900 (3e75756)
@@ -0,0 +1,15 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": ""}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query 'groonga'   --output_columns 'highlight_html(body)' --command_version 2
+[[0,0.0,0.0],[[[0],[["highlight_html","null"]]]]]

  Added: test/command/suite/select/function/highlight_html/empty_column.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/empty_column.test    2014-08-10 06:56:57 +0900 (da7d660)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": ""}
+]
+
+select Entries --output_columns \
+  --match_columns body --query 'groonga' \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/empty_query.expected (+37 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/empty_query.expected    2014-08-10 06:56:57 +0900 (6734f65)
@@ -0,0 +1,37 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query ''   --output_columns 'highlight_html(body)' --command_version 2
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "highlight_html",
+          "null"
+        ]
+      ],
+      [
+        "Mroonga is a MySQL storage engine based on Groonga. &lt;b&gt;Rroonga&lt;/b&gt; is a Ruby binding of Groonga."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/highlight_html/empty_query.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/empty_query.test    2014-08-10 06:56:57 +0900 (ab5e888)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries --output_columns \
+  --match_columns body --query '' \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/no_hit.expected (+15 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/no_hit.expected    2014-08-10 06:56:57 +0900 (eec979f)
@@ -0,0 +1,15 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query 'droonga'   --output_columns 'highlight_html(body)' --command_version 2
+[[0,0.0,0.0],[[[0],[["highlight_html","null"]]]]]

  Added: test/command/suite/select/function/highlight_html/no_hit.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/no_hit.test    2014-08-10 06:56:57 +0900 (41700fb)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries --output_columns \
+  --match_columns body --query 'droonga' \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/no_query.expected (+37 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/no_query.expected    2014-08-10 06:56:57 +0900 (bbdeffa)
@@ -0,0 +1,37 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries   --output_columns 'highlight_html(body)' --command_version 2
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "highlight_html",
+          "null"
+        ]
+      ],
+      [
+        "Mroonga is a MySQL storage engine based on Groonga. &lt;b&gt;Rroonga&lt;/b&gt; is a Ruby binding of Groonga."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/highlight_html/no_query.test (+13 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/no_query.test    2014-08-10 06:56:57 +0900 (f45ebb0)
@@ -0,0 +1,13 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/one_hit.expected (+37 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/one_hit.expected    2014-08-10 06:56:57 +0900 (969a35d)
@@ -0,0 +1,37 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query 'rroonga'   --output_columns 'highlight_html(body)' --command_version 2
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "highlight_html",
+          "null"
+        ]
+      ],
+      [
+        "Mroonga is a MySQL storage engine based on Groonga. &lt;b&gt;<span class=\"keyword\">Rroonga</span>&lt;/b&gt; is a Ruby binding of Groonga."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/highlight_html/one_hit.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/one_hit.test    2014-08-10 06:56:57 +0900 (7c04975)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries --output_columns \
+  --match_columns body --query 'rroonga' \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/one_keyword.expected (+37 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/one_keyword.expected    2014-08-10 06:56:57 +0900 (4501b81)
@@ -0,0 +1,37 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query 'groonga'   --output_columns 'highlight_html(body)' --command_version 2
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "highlight_html",
+          "null"
+        ]
+      ],
+      [
+        "Mroonga is a MySQL storage engine based on <span class=\"keyword\">Groonga</span>. &lt;b&gt;Rroonga&lt;/b&gt; is a Ruby binding of <span class=\"keyword\">Groonga</span>."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/highlight_html/one_keyword.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/one_keyword.test    2014-08-10 06:56:57 +0900 (8248cf2)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries --output_columns \
+  --match_columns body --query 'groonga' \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/twice_hits.expected (+37 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/twice_hits.expected    2014-08-10 06:56:57 +0900 (e70ed39)
@@ -0,0 +1,37 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query 'Groonga'   --output_columns 'highlight_html(body)' --command_version 2
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "highlight_html",
+          "null"
+        ]
+      ],
+      [
+        "Mroonga is a MySQL storage engine based on <span class=\"keyword\">Groonga</span>. &lt;b&gt;Rroonga&lt;/b&gt; is a Ruby binding of <span class=\"keyword\">Groonga</span>."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/highlight_html/twice_hits.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/twice_hits.test    2014-08-10 06:56:57 +0900 (08b8637)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries --output_columns \
+  --match_columns body --query 'Groonga' \
+  --output_columns 'highlight_html(body)' --command_version 2

  Added: test/command/suite/select/function/highlight_html/two_keywords.expected (+37 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/two_keywords.expected    2014-08-10 06:56:57 +0900 (27655e0)
@@ -0,0 +1,37 @@
+table_create Entries TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Entries body COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+[[0,0.0,0.0],true]
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+[[0,0.0,0.0],true]
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+[[0,0.0,0.0],1]
+select Entries --output_columns   --match_columns body --query 'groonga mysql'   --output_columns 'highlight_html(body)' --command_version 2
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "highlight_html",
+          "null"
+        ]
+      ],
+      [
+        "Mroonga is a <span class=\"keyword\">MySQL</span> storage engine based on <span class=\"keyword\">Groonga</span>. &lt;b&gt;Rroonga&lt;/b&gt; is a Ruby binding of <span class=\"keyword\">Groonga</span>."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/highlight_html/two_keywords.test (+14 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/highlight_html/two_keywords.test    2014-08-10 06:56:57 +0900 (462f7bd)
@@ -0,0 +1,14 @@
+table_create Entries TABLE_NO_KEY
+column_create Entries body COLUMN_SCALAR ShortText
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
+
+load --table Entries
+[
+{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
+]
+
+select Entries --output_columns \
+  --match_columns body --query 'groonga mysql' \
+  --output_columns 'highlight_html(body)' --command_version 2
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index