| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: tokenizers_delimit |
| 5 |
|
| 6 |
.. _token-delimit: |
| 7 |
|
| 8 |
``TokenDelimit`` |
| 9 |
================ |
| 10 |
|
| 11 |
Summary |
| 12 |
------- |
| 13 |
|
| 14 |
``TokenDelimit`` extracts token by splitting one or more space |
| 15 |
characters (``U+0020``). For example, ``Hello World`` is tokenized to |
| 16 |
``Hello`` and ``World``. |
| 17 |
|
| 18 |
``TokenDelimit`` is suitable for tag text. You can extract ``groonga`` |
| 19 |
and ``full-text-search`` and ``http`` as tags from ``groonga |
| 20 |
full-text-search http``. |
| 21 |
|
| 22 |
Syntax |
| 23 |
------ |
| 24 |
|
| 25 |
``TokenDelimit`` has optional parameter. |
| 26 |
|
| 27 |
No options(Extracts token by splitting one or more space characters (``U+0020``)):: |
| 28 |
|
| 29 |
TokenDelimit |
| 30 |
|
| 31 |
Specify delimiter:: |
| 32 |
|
| 33 |
TokenDelimit("delimiter", "delimiter1", "delimiter", "delimiter2", ...) |
| 34 |
|
| 35 |
Specify delimiter with regular expression:: |
| 36 |
|
| 37 |
TokenDelimit("pattern", pattern) |
| 38 |
|
| 39 |
The ``delimiter`` option and a ``pattern`` option are not use at the same time. |
| 40 |
|
| 41 |
Usage |
| 42 |
----- |
| 43 |
|
| 44 |
Simple usage |
| 45 |
------------ |
| 46 |
|
| 47 |
Here is an example of ``TokenDelimit``: |
| 48 |
|
| 49 |
.. groonga-command |
| 50 |
.. include:: ../../example/reference/tokenizers/token-delimit.log |
| 51 |
.. tokenize TokenDelimit "Groonga full-text-search HTTP" NormalizerAuto |
| 52 |
|
| 53 |
``TokenDelimit`` can also specify options. |
| 54 |
``TokenDelimit`` has ``delimiter`` option and ``pattern`` option. |
| 55 |
|
| 56 |
``delimiter`` option can split token with a specified character. |
| 57 |
|
| 58 |
For example, ``Hello,World`` is tokenized to ``Hello`` and ``World`` |
| 59 |
with ``delimiter`` option as below. |
| 60 |
|
| 61 |
.. groonga-command |
| 62 |
.. include:: ../../example/reference/tokenizers/token-delimit-delimiter-option.log |
| 63 |
.. tokenize 'TokenDelimit("delimiter", ",")' "Hello,World" |
| 64 |
|
| 65 |
``pattern`` option can split token with a regular expression. |
| 66 |
You can except needless space by ``pattern`` option. |
| 67 |
|
| 68 |
For example, ``This is a pen. This is an apple`` is tokenized to ``This is a pen`` and |
| 69 |
``This is an apple`` with ``pattern`` option as below. |
| 70 |
|
| 71 |
Normally, when ``This is a pen. This is an apple.`` is splitted by ``.``, |
| 72 |
needless spaces are included at the beginning of "This is an apple.". |
| 73 |
|
| 74 |
You can except the needless spaces by a ``pattern`` option as below example. |
| 75 |
|
| 76 |
.. groonga-command |
| 77 |
.. include:: ../../example/reference/tokenizers/token-delimit-pattern-option.log |
| 78 |
.. tokenize 'TokenDelimit("pattern", "\\\\.\\\\s*")' "This is a pen. This is an apple." |
| 79 |
|
| 80 |
Advanced usage |
| 81 |
-------------- |
| 82 |
|
| 83 |
``delimiter`` option can also specify multiple delimiters. |
| 84 |
|
| 85 |
For example, ``Hello, World`` is tokenized to ``Hello`` and ``World``. |
| 86 |
``","`` and ``" "`` are delimiters in below example. |
| 87 |
|
| 88 |
.. groonga-command |
| 89 |
.. include:: ../../example/reference/tokenizers/token-delimit-delimiter-option-multiple-delimiters.log |
| 90 |
.. tokenize 'TokenDelimit("delimiter", ",", "delimiter", " ")' "Hello, World" |
| 91 |
|
| 92 |
You can extract token in complex conditions by ``pattern`` option. |
| 93 |
|
| 94 |
For example, ``これはペンですか!?リンゴですか?「リンゴです。」`` is tokenize to ``これはペンですか`` and ``リンゴですか``, ``「リンゴです。」`` with ``delimiter`` option as below. |
| 95 |
|
| 96 |
.. groonga-command |
| 97 |
.. include:: ../../example/reference/tokenizers/token-delimit-pattern-option-with-complex-pattern.log |
| 98 |
.. tokenize 'TokenDelimit("pattern", "([。!?]+(?![)」])|[\\r\\n]+)\\s*")' "これはペンですか!?リンゴですか?「リンゴです。」" |
| 99 |
|
| 100 |
``\\s*`` of the end of above regular expression match 0 or more spaces after a delimiter. |
| 101 |
|
| 102 |
``[。!?]+`` matches 1 or more ``。`` or ``!``, ``?``. |
| 103 |
For example, ``[。!?]+`` matches ``!?`` of ``これはペンですか!?``. |
| 104 |
|
| 105 |
``(?![)」])`` is negative lookahead. |
| 106 |
``(?![)」])`` matches if a character is not matched ``)`` or ``」``. |
| 107 |
negative lookahead interprets in combination regular expression of just before. |
| 108 |
|
| 109 |
Therefore it interprets ``[。!?]+(?![)」])``. |
| 110 |
|
| 111 |
``[。!?]+(?![)」])`` matches if there are not ``)`` or ``」`` after ``。`` or ``!``, ``?``. |
| 112 |
|
| 113 |
In other words, ``[。!?]+(?![)」])`` matches ``。`` of ``これはペンですか。``. But ``[。!?]+(?![)」])`` doesn't match ``。`` of ``「リンゴです。」``. |
| 114 |
Because there is ``」`` after ``。``. |
| 115 |
|
| 116 |
``[\\r\\n]+`` match 1 or more newline character. |
| 117 |
|
| 118 |
In conclusion, ``([。!?]+(?![)」])|[\\r\\n]+)\\s*`` uses ``。`` and ``!`` and ``?``, newline character as delimiter. However, ``。`` and ``!``, ``?`` are not delimiters if there is ``)`` or ``」`` after ``。`` or ``!``, ``?``. |
| 119 |
|
| 120 |
Parameters |
| 121 |
---------- |
| 122 |
|
| 123 |
Optional parameter |
| 124 |
^^^^^^^^^^^^^^^^^^ |
| 125 |
|
| 126 |
There are two optional parameters ``delimiter`` and ``pattern``. |
| 127 |
|
| 128 |
``delimiter`` |
| 129 |
""""""""""""" |
| 130 |
|
| 131 |
Split token with a specified one or more characters. |
| 132 |
|
| 133 |
You can use one or more characters for a delimiter. |
| 134 |
|
| 135 |
``pattern`` |
| 136 |
""""""""""" |
| 137 |
|
| 138 |
Split token with a regular expression. |
| 139 |
|
| 140 |
See also |
| 141 |
---------- |
| 142 |
|
| 143 |
* :doc:`../commands/tokenize` |