Debian Codegraph @

doc/source/reference/tokenizers/token_bigram.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: tokenizers
5
6 .. _token-bigram:
7
8 ``TokenBigram``
9 ===============
10
11 Summary
12 -------
13
14 ``TokenBigram`` is a bigram based tokenizer. It's recommended to use
15 this tokenizer for most cases.
16
17 Bigram tokenize method tokenizes a text to two adjacent characters
18 tokens. For example, ``Hello`` is tokenized to the following tokens:
19
20 * ``He``
21 * ``el``
22 * ``ll``
23 * ``lo``
24
25 Bigram tokenize method is good for recall because you can find all
26 texts by query consists of two or more characters.
27
28 In general, you can't find all texts by query consists of one
29 character because one character token doesn't exist. But you can find
30 all texts by query consists of one character in Groonga. Because
31 Groonga find tokens that start with query by predictive search. For
32 example, Groonga can find ``ll`` and ``lo`` tokens by ``l`` query.
33
34 Bigram tokenize method isn't good for precision because you can find
35 texts that includes query in word. For example, you can find ``world``
36 by ``or``. This is more sensitive for ASCII only languages rather than
37 non-ASCII languages. ``TokenBigram`` has solution for this problem
38 described in the below.
39
40 Syntax
41 ------
42
43 ``TokenBigram`` hasn't parameter::
44
45 TokenBigram
46
47 Usage
48 -----
49
50 ``TokenBigram`` behavior is different when it's worked with any
51 :doc:`/reference/normalizers`.
52
53 If no normalizer is used, ``TokenBigram`` uses pure bigram (all tokens
54 except the last token have two characters) tokenize method:
55
56 .. groonga-command
57 .. include:: ../../example/reference/tokenizers/token-bigram-no-normalizer.log
58 .. tokenize TokenBigram "Hello World"
59
60 If normalizer is used, ``TokenBigram`` uses white-space-separate like
61 tokenize method for ASCII characters. ``TokenBigram`` uses bigram
62 tokenize method for non-ASCII characters.
63
64 You may be confused with this combined behavior. But it's reasonable
65 for most use cases such as English text (only ASCII characters) and
66 Japanese text (ASCII and non-ASCII characters are mixed).
67
68 Most languages consists of only ASCII characters use white-space for
69 word separator. White-space-separate tokenize method is suitable for
70 the case.
71
72 Languages consists of non-ASCII characters don't use white-space for
73 word separator. Bigram tokenize method is suitable for the case.
74
75 Mixed tokenize method is suitable for mixed language case.
76
77 If you want to use bigram tokenize method for ASCII character, see
78 ``TokenBigramSplitXXX`` type tokenizers such as
79 :ref:`token-bigram-split-symbol-alpha`.
80
81 Let's confirm ``TokenBigram`` behavior by example.
82
83 ``TokenBigram`` uses one or more white-spaces as token delimiter for
84 ASCII characters:
85
86 .. groonga-command
87 .. include:: ../../example/reference/tokenizers/token-bigram-ascii-and-white-space-with-normalizer.log
88 .. tokenize TokenBigram "Hello World" NormalizerAuto
89
90 ``TokenBigram`` uses character type change as token delimiter for
91 ASCII characters. Character type is one of them:
92
93 * Alphabet
94 * Digit
95 * Symbol (such as ``(``, ``)`` and ``!``)
96 * Hiragana
97 * Katakana
98 * Kanji
99 * Others
100
101 The following example shows two token delimiters:
102
103 * at between ``100`` (digits) and ``cents`` (alphabets)
104 * at between ``cents`` (alphabets) and ``!!!`` (symbols)
105
106 .. groonga-command
107 .. include:: ../../example/reference/tokenizers/token-bigram-ascii-and-character-type-change-with-normalizer.log
108 .. tokenize TokenBigram "100cents!!!" NormalizerAuto
109
110 Here is an example that ``TokenBigram`` uses bigram tokenize method
111 for non-ASCII characters.
112
113 .. groonga-command
114 .. include:: ../../example/reference/tokenizers/token-bigram-non-ascii-with-normalizer.log
115 .. tokenize TokenBigram "日本語の勉強" NormalizerAuto