| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: tokenizers_ngram |
| 5 |
|
| 6 |
.. _token-ngram: |
| 7 |
|
| 8 |
``TokenNgram`` |
| 9 |
============== |
| 10 |
|
| 11 |
Summary |
| 12 |
------- |
| 13 |
|
| 14 |
``TokenNgram`` can change define its behavior dynamically via its options. |
| 15 |
For example, we can use it as unigram, bigram, trigram on with changing ``n`` option value as below. |
| 16 |
|
| 17 |
Uni-gram: |
| 18 |
|
| 19 |
.. groonga-command |
| 20 |
.. include:: ../../example/reference/tokenizers/token-ngram-uni-gram.log |
| 21 |
.. tokenize --tokenizer 'TokenNgram("n", 1)' --string "Hello World" |
| 22 |
|
| 23 |
Bi-gram: |
| 24 |
|
| 25 |
.. groonga-command |
| 26 |
.. include:: ../../example/reference/tokenizers/token-ngram-bi-gram.log |
| 27 |
.. tokenize --tokenizer 'TokenNgram("n", 2)' --string "Hello World" |
| 28 |
|
| 29 |
Tri-gram: |
| 30 |
|
| 31 |
.. groonga-command |
| 32 |
.. include:: ../../example/reference/tokenizers/token-ngram-tri-gram.log |
| 33 |
.. tokenize --tokenizer 'TokenNgram("n", 3)' --string "Hello World" |
| 34 |
|
| 35 |
This tokenizer also has options other than the above. |
| 36 |
|
| 37 |
Syntax |
| 38 |
------ |
| 39 |
|
| 40 |
``TokenNgram`` has optional parameter. |
| 41 |
|
| 42 |
No options:: |
| 43 |
|
| 44 |
TokenNgram |
| 45 |
|
| 46 |
If we don't use options, ``TokenNgram`` is the same behavior for ``TokenBigram``. |
| 47 |
|
| 48 |
Specify option:: |
| 49 |
|
| 50 |
TokenNgram("n", true) |
| 51 |
|
| 52 |
TokenNgram("loose_symbol", true) |
| 53 |
|
| 54 |
TokenNgram("loose_blank", true) |
| 55 |
|
| 56 |
TokenNgram("remove_blank", true) |
| 57 |
|
| 58 |
TokenNgram("report_source_location", true) |
| 59 |
|
| 60 |
TokenNgram("unify_alphabet", true) |
| 61 |
|
| 62 |
TokenNgram("unify_symbol", true) |
| 63 |
|
| 64 |
TokenNgram("unify_digit", true) |
| 65 |
|
| 66 |
Specify multiple options:: |
| 67 |
|
| 68 |
TokenNgram("loose_symbol", true, "loose_blank", true) |
| 69 |
|
| 70 |
We can also specify multiple options at the same time except the above examples. |
| 71 |
|
| 72 |
Usage |
| 73 |
----- |
| 74 |
|
| 75 |
Simple usage |
| 76 |
------------ |
| 77 |
|
| 78 |
Here is an example of ``TokenNgram``. |
| 79 |
|
| 80 |
If we use ``TokenNgram`` in the nothing of option. |
| 81 |
``TokenNgram`` behavior is the same as ``TokenBigram`` as below: |
| 82 |
|
| 83 |
* If no normalizer is used. |
| 84 |
|
| 85 |
.. groonga-command |
| 86 |
.. include:: ../../example/reference/tokenizers/token-ngram-no-option.log |
| 87 |
.. tokenize --tokenizer TokenNgram --string "Hello World" |
| 88 |
|
| 89 |
* If normalizer is used. |
| 90 |
|
| 91 |
.. groonga-command |
| 92 |
.. include:: ../../example/reference/tokenizers/token-ngram-no-option-with-normalizer.log |
| 93 |
.. tokenize --tokenizer TokenNgram --string "Hello World" --normalizer NormalizerAuto |
| 94 |
|
| 95 |
Advanced usage |
| 96 |
-------------- |
| 97 |
|
| 98 |
We can specify multiple options for ``TokenNgram``. |
| 99 |
|
| 100 |
For example, we can deal with variantsas as phone number by using ``loose_symbol`` and ``loose_blank`` as below. |
| 101 |
|
| 102 |
We can search ``0123(45)6789``, ``0123-45-6789`` and, ``0123 45 6789`` by using ``0123456789`` as below example. |
| 103 |
|
| 104 |
.. groonga-command |
| 105 |
.. include:: ../../example/reference/tokenizers/token-ngram-loose-symbol-and-loose-blank.log |
| 106 |
.. tokenize --tokenizer 'TokenNgram("loose_symbol", true, "loose_blank", true)' --string "0123(45)6789" --normalizer NormalizerAuto |
| 107 |
.. tokenize --tokenizer 'TokenNgram("loose_symbol", true, "loose_blank", true)' --string "0123-45-6789" --normalizer NormalizerAuto |
| 108 |
.. tokenize --tokenizer 'TokenNgram("loose_symbol", true, "loose_blank", true)' --string "0123 45 6789" --normalizer NormalizerAuto |
| 109 |
|
| 110 |
Parameters |
| 111 |
---------- |
| 112 |
|
| 113 |
Optional parameter |
| 114 |
^^^^^^^^^^^^^^^^^^ |
| 115 |
|
| 116 |
There are eight optional parameters. |
| 117 |
|
| 118 |
.. _token-ngram-n: |
| 119 |
|
| 120 |
``n`` |
| 121 |
""""" |
| 122 |
|
| 123 |
This option shows ``N`` of Ngram. For example, ``n`` is ``3`` for trigram. |
| 124 |
|
| 125 |
.. _token-ngram-ignore-blank: |
| 126 |
|
| 127 |
``ignore_blank`` |
| 128 |
"""""""""""""""" |
| 129 |
|
| 130 |
It ignores white-spaces in continuous symbols and non-ASCII characters. |
| 131 |
For example, the keyword ``日 本 語 ! ! !`` is tokenized as ``日本語!!!``. |
| 132 |
|
| 133 |
.. _token-ngram-loose-symbol: |
| 134 |
|
| 135 |
``loose_symbol`` |
| 136 |
"""""""""""""""" |
| 137 |
|
| 138 |
Tokenize keywords including symbols, to be searched by both queries with/without symbols. |
| 139 |
For example, a keyword ``090-1111-2222`` will be found by any of ``09011112222``, ``090``, ``1111``, ``2222`` and ``090-1111-2222``. |
| 140 |
|
| 141 |
.. _token-ngram-loose-blank: |
| 142 |
|
| 143 |
``loose_blank`` |
| 144 |
""""""""""""""" |
| 145 |
|
| 146 |
Tokenize keywords including blanks, to be searched by both queries with/without blanks. |
| 147 |
For example, a keyword ``090 1111 2222`` will be found by any of ``09011112222``, ``090``, ``1111``, ``2222`` and ``090 1111 2222``. |
| 148 |
|
| 149 |
.. _token-ngram-remove-blank: |
| 150 |
|
| 151 |
``remove_blank`` |
| 152 |
"""""""""""""""" |
| 153 |
|
| 154 |
Tokenize keywords including blanks, to be searched by queries without blanks. |
| 155 |
For example, a keyword ``090 1111 2222`` will be found by any of ``09011112222``, ``090``, ``1111`` or ``2222``. |
| 156 |
|
| 157 |
Note that the keyword won’t be found by a query including blanks like ``090 1111 2222``. |
| 158 |
|
| 159 |
.. _token-ngram-report-source-location: |
| 160 |
|
| 161 |
``report_source_location`` |
| 162 |
"""""""""""""""""""""""""" |
| 163 |
|
| 164 |
This option tells us a location of token of highlight target when we highlight token by ``highlight_html``. |
| 165 |
|
| 166 |
We only use this option when we want to highlight token by ``highlight_html``. |
| 167 |
|
| 168 |
When Groonga tokenize texts that target highlight, always used ``NormalizerAuto`` to normalizer until now. |
| 169 |
Therefore, if we use ``NormalizerNFKC100`` to normalizer, sometimes it |
| 170 |
can't find the position of the highlight as below. |
| 171 |
|
| 172 |
.. groonga-command |
| 173 |
.. include:: ../../example/reference/tokenizers/token-ngram-no-report-source-location.log |
| 174 |
.. table_create Entries TABLE_NO_KEY |
| 175 |
.. column_create Entries body COLUMN_SCALAR ShortText |
| 176 |
.. table_create Terms TABLE_PAT_KEY ShortText --normalizer 'NormalizerNFKC100' |
| 177 |
.. column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body |
| 178 |
.. load --table Entries |
| 179 |
.. [ |
| 180 |
.. {"body": "ア㌕Az"} |
| 181 |
.. ] |
| 182 |
.. select Entries \ |
| 183 |
.. --match_columns body \ |
| 184 |
.. --query 'グラム' \ |
| 185 |
.. --output_columns 'highlight_html(body, Terms)' |
| 186 |
|
| 187 |
Because we use different normalizer to normalize token. |
| 188 |
|
| 189 |
This option is used to reduce the shift of the position of the highlight as below. |
| 190 |
|
| 191 |
.. groonga-command |
| 192 |
.. include:: ../../example/reference/tokenizers/token-ngram-report-source-location.log |
| 193 |
.. table_remove Terms |
| 194 |
.. table_create Terms TABLE_PAT_KEY ShortText \ |
| 195 |
.. --default_tokenizer 'TokenNgram("report_source_location", true)' \ |
| 196 |
.. --normalizer 'NormalizerNFKC100' |
| 197 |
.. column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body |
| 198 |
.. select Entries \ |
| 199 |
.. --match_columns body \ |
| 200 |
.. --query 'グラム' \ |
| 201 |
.. --output_columns 'highlight_html(body, Terms)' |
| 202 |
|
| 203 |
.. _token-ngram-unify-alphabet: |
| 204 |
|
| 205 |
``unify_alphabet`` |
| 206 |
"""""""""""""""""" |
| 207 |
|
| 208 |
If we set false, ``TokenNgram`` uses bigram tokenize method for ASCII character. |
| 209 |
|
| 210 |
Default value of this option is true. |
| 211 |
|
| 212 |
.. groonga-command |
| 213 |
.. include:: ../../example/reference/tokenizers/token-ngram-unify-alphabet.log |
| 214 |
.. tokenize --tokenizer 'TokenNgram("unify_alphabet", false)' --string "Hello World" --normalizer NormalizerAuto |
| 215 |
|
| 216 |
.. _token-ngram-unify-symbol: |
| 217 |
|
| 218 |
``unify_symbol`` |
| 219 |
"""""""""""""""" |
| 220 |
|
| 221 |
If we set false, ``TokenNgram`` uses bigram tokenize method for symbols. |
| 222 |
``TokenNgram("unify_symbol", false)`` is same behavior of ``TokenBigramSplitSymbol``. |
| 223 |
|
| 224 |
Default value of this option is true. |
| 225 |
|
| 226 |
.. groonga-command |
| 227 |
.. include:: ../../example/reference/tokenizers/token-ngram-unify-symbol.log |
| 228 |
.. tokenize --tokenizer 'TokenNgram("unify_symbol", false)' --string "___---" --normalizer NormalizerAuto |
| 229 |
|
| 230 |
.. _token-ngram-unify-digit: |
| 231 |
|
| 232 |
``unify_digit`` |
| 233 |
""""""""""""""" |
| 234 |
|
| 235 |
If we set false, ``TokenNgram`` uses bigram tokenize method for digits. |
| 236 |
|
| 237 |
Default value of this option is true. |
| 238 |
|
| 239 |
.. groonga-command |
| 240 |
.. include:: ../../example/reference/tokenizers/token-ngram-unify-digit.log |
| 241 |
.. tokenize --tokenizer 'TokenNgram("unify_digit", false)' --string "012345 6789" --normalizer NormalizerAuto |
| 242 |
|
| 243 |
See also |
| 244 |
---------- |
| 245 |
|
| 246 |
* :doc:`../commands/tokenize` |