1 package org.apache.lucene.analysis.standard;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
23 * This class implements Word Break rules from the Unicode Text Segmentation
24 * algorithm, as specified in
25 * <a href="http://unicode.org/reports/tr29/">Unicode Standard Annex #29</a>
27 * Tokens produced are of the following types:
29 * <li><ALPHANUM>: A sequence of alphabetic and numeric characters</li>
30 * <li><NUM>: A number</li>
31 * <li><SOUTHEAST_ASIAN>: A sequence of characters from South and Southeast
32 * Asian languages, including Thai, Lao, Myanmar, and Khmer</li>
33 * <li><IDEOGRAPHIC>: A single CJKV ideographic character</li>
34 * <li><HIRAGANA>: A single hiragana character</li>
43 %class StandardTokenizerImpl
44 %implements StandardTokenizerInterface
45 %function getNextToken
48 %include src/java/org/apache/lucene/analysis/standard/SUPPLEMENTARY.jflex-macro
49 ALetter = ([\p{WB:ALetter}] | {ALetterSupp})
50 Format = ([\p{WB:Format}] | {FormatSupp})
51 Numeric = ([\p{WB:Numeric}] | {NumericSupp})
52 Extend = ([\p{WB:Extend}] | {ExtendSupp})
53 Katakana = ([\p{WB:Katakana}] | {KatakanaSupp})
54 MidLetter = ([\p{WB:MidLetter}] | {MidLetterSupp})
55 MidNum = ([\p{WB:MidNum}] | {MidNumSupp})
56 MidNumLet = ([\p{WB:MidNumLet}] | {MidNumLetSupp})
57 ExtendNumLet = ([\p{WB:ExtendNumLet}] | {ExtendNumLetSupp})
58 ComplexContext = ([\p{LB:Complex_Context}] | {ComplexContextSupp})
59 Han = ([\p{Script:Han}] | {HanSupp})
60 Hiragana = ([\p{Script:Hiragana}] | {HiraganaSupp})
62 // Script=Hangul & Aletter
63 HangulEx = (!(!\p{Script:Hangul}|!\p{WB:ALetter})) ({Format} | {Extend})*
64 // UAX#29 WB4. X (Extend | Format)* --> X
66 ALetterEx = {ALetter} ({Format} | {Extend})*
67 // TODO: Convert hard-coded full-width numeric range to property intersection (something like [\p{Full-Width}&&\p{Numeric}]) once JFlex supports it
68 NumericEx = ({Numeric} | [\uFF10-\uFF19]) ({Format} | {Extend})*
69 KatakanaEx = {Katakana} ({Format} | {Extend})*
70 MidLetterEx = ({MidLetter} | {MidNumLet}) ({Format} | {Extend})*
71 MidNumericEx = ({MidNum} | {MidNumLet}) ({Format} | {Extend})*
72 ExtendNumLetEx = {ExtendNumLet} ({Format} | {Extend})*
74 HanEx = {Han} ({Format} | {Extend})*
75 HiraganaEx = {Hiragana} ({Format} | {Extend})*
78 /** Alphanumeric sequences */
79 public static final int WORD_TYPE = StandardTokenizer.ALPHANUM;
82 public static final int NUMERIC_TYPE = StandardTokenizer.NUM;
85 * Chars in class \p{Line_Break = Complex_Context} are from South East Asian
86 * scripts (Thai, Lao, Myanmar, Khmer, etc.). Sequences of these are kept
87 * together as as a single token rather than broken up, because the logic
88 * required to break them at word boundaries is too complex for UAX#29.
90 * See Unicode Line Breaking Algorithm: http://www.unicode.org/reports/tr14/#SA
92 public static final int SOUTH_EAST_ASIAN_TYPE = StandardTokenizer.SOUTHEAST_ASIAN;
94 public static final int IDEOGRAPHIC_TYPE = StandardTokenizer.IDEOGRAPHIC;
96 public static final int HIRAGANA_TYPE = StandardTokenizer.HIRAGANA;
98 public static final int KATAKANA_TYPE = StandardTokenizer.KATAKANA;
100 public static final int HANGUL_TYPE = StandardTokenizer.HANGUL;
102 public final int yychar()
108 * Fills CharTermAttribute with the current token text.
110 public final void getText(CharTermAttribute t) {
111 t.copyBuffer(zzBuffer, zzStartRead, zzMarkedPos-zzStartRead);
120 <<EOF>> { return StandardTokenizerInterface.YYEOF; }
122 // UAX#29 WB8. Numeric × Numeric
123 // WB11. Numeric (MidNum | MidNumLet) × Numeric
124 // WB12. Numeric × (MidNum | MidNumLet) Numeric
125 // WB13a. (ALetter | Numeric | Katakana | ExtendNumLet) × ExtendNumLet
126 // WB13b. ExtendNumLet × (ALetter | Numeric | Katakana)
128 {ExtendNumLetEx}* {NumericEx} ({ExtendNumLetEx}+ {NumericEx}
129 | {MidNumericEx} {NumericEx}
132 { return NUMERIC_TYPE; }
134 // subset of the below for typing purposes only!
136 { return HANGUL_TYPE; }
139 { return KATAKANA_TYPE; }
141 // UAX#29 WB5. ALetter × ALetter
142 // WB6. ALetter × (MidLetter | MidNumLet) ALetter
143 // WB7. ALetter (MidLetter | MidNumLet) × ALetter
144 // WB9. ALetter × Numeric
145 // WB10. Numeric × ALetter
146 // WB13. Katakana × Katakana
147 // WB13a. (ALetter | Numeric | Katakana | ExtendNumLet) × ExtendNumLet
148 // WB13b. ExtendNumLet × (ALetter | Numeric | Katakana)
150 {ExtendNumLetEx}* ( {KatakanaEx} ({ExtendNumLetEx}* {KatakanaEx})*
151 | ( {NumericEx} ({ExtendNumLetEx}+ {NumericEx} | {MidNumericEx} {NumericEx} | {NumericEx})*
152 | {ALetterEx} ({ExtendNumLetEx}+ {ALetterEx} | {MidLetterEx} {ALetterEx} | {ALetterEx})* )+ )
153 ({ExtendNumLetEx}+ ( {KatakanaEx} ({ExtendNumLetEx}* {KatakanaEx})*
154 | ( {NumericEx} ({ExtendNumLetEx}+ {NumericEx} | {MidNumericEx} {NumericEx} | {NumericEx})*
155 | {ALetterEx} ({ExtendNumLetEx}+ {ALetterEx} | {MidLetterEx} {ALetterEx} | {ALetterEx})* )+ ) )*
157 { return WORD_TYPE; }
162 // [C]haracters with the Line_Break property values of Contingent_Break (CB),
163 // Complex_Context (SA/South East Asian), and XX (Unknown) are assigned word
164 // boundary property values based on criteria outside of the scope of this
165 // annex. That means that satisfactory treatment of languages like Chinese
166 // or Thai requires special handling.
168 // In Unicode 6.0, only one character has the \p{Line_Break = Contingent_Break}
169 // property: U+FFFC (  ) OBJECT REPLACEMENT CHARACTER.
171 // In the ICU implementation of UAX#29, \p{Line_Break = Complex_Context}
172 // character sequences (from South East Asian scripts like Thai, Myanmar, Khmer,
173 // Lao, etc.) are kept together. This grammar does the same below.
175 // See also the Unicode Line Breaking Algorithm:
177 // http://www.unicode.org/reports/tr14/#SA
179 {ComplexContext}+ { return SOUTH_EAST_ASIAN_TYPE; }
181 // UAX#29 WB14. Any ÷ Any
183 {HanEx} { return IDEOGRAPHIC_TYPE; }
184 {HiraganaEx} { return HIRAGANA_TYPE; }
187 // UAX#29 WB3. CR × LF
188 // WB3a. (Newline | CR | LF) ÷
189 // WB3b. ÷ (Newline | CR | LF)
192 [^] { /* Not numeric, word, ideographic, hiragana, or SE Asian -- ignore it. */ }