1 package org.apache.lucene.analysis.icu.segmentation;
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.Analyzer;
21 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
22 import org.apache.lucene.analysis.ReusableAnalyzerBase;
23 import org.apache.lucene.analysis.TokenFilter;
24 import org.apache.lucene.analysis.Tokenizer;
25 import org.apache.lucene.analysis.icu.ICUNormalizer2Filter;
27 import java.io.IOException;
28 import java.io.Reader;
29 import java.io.StringReader;
30 import java.util.Arrays;
32 public class TestICUTokenizer extends BaseTokenStreamTestCase {
34 public void testHugeDoc() throws IOException {
35 StringBuilder sb = new StringBuilder();
36 char whitespace[] = new char[4094];
37 Arrays.fill(whitespace, ' ');
38 sb.append(whitespace);
39 sb.append("testing 1234");
40 String input = sb.toString();
41 ICUTokenizer tokenizer = new ICUTokenizer(new StringReader(input));
42 assertTokenStreamContents(tokenizer, new String[] { "testing", "1234" });
45 public void testHugeTerm2() throws IOException {
46 StringBuilder sb = new StringBuilder();
47 for (int i = 0; i < 40960; i++) {
50 String input = sb.toString();
51 ICUTokenizer tokenizer = new ICUTokenizer(new StringReader(input));
52 char token[] = new char[4096];
53 Arrays.fill(token, 'a');
54 String expectedToken = new String(token);
56 expectedToken, expectedToken, expectedToken,
57 expectedToken, expectedToken, expectedToken,
58 expectedToken, expectedToken, expectedToken,
61 assertTokenStreamContents(tokenizer, expected);
64 private Analyzer a = new ReusableAnalyzerBase() {
66 protected TokenStreamComponents createComponents(String fieldName,
68 Tokenizer tokenizer = new ICUTokenizer(reader);
69 TokenFilter filter = new ICUNormalizer2Filter(tokenizer);
70 return new TokenStreamComponents(tokenizer, filter);
74 public void testArmenian() throws Exception {
75 assertAnalyzesTo(a, "Վիքիպեդիայի 13 միլիոն հոդվածները (4,600` հայերեն վիքիպեդիայում) գրվել են կամավորների կողմից ու համարյա բոլոր հոդվածները կարող է խմբագրել ցանկաց մարդ ով կարող է բացել Վիքիպեդիայի կայքը։",
76 new String[] { "վիքիպեդիայի", "13", "միլիոն", "հոդվածները", "4,600", "հայերեն", "վիքիպեդիայում", "գրվել", "են", "կամավորների", "կողմից",
77 "ու", "համարյա", "բոլոր", "հոդվածները", "կարող", "է", "խմբագրել", "ցանկաց", "մարդ", "ով", "կարող", "է", "բացել", "վիքիպեդիայի", "կայքը" } );
80 public void testAmharic() throws Exception {
81 assertAnalyzesTo(a, "ዊኪፔድያ የባለ ብዙ ቋንቋ የተሟላ ትክክለኛና ነጻ መዝገበ ዕውቀት (ኢንሳይክሎፒዲያ) ነው። ማንኛውም",
82 new String[] { "ዊኪፔድያ", "የባለ", "ብዙ", "ቋንቋ", "የተሟላ", "ትክክለኛና", "ነጻ", "መዝገበ", "ዕውቀት", "ኢንሳይክሎፒዲያ", "ነው", "ማንኛውም" } );
85 public void testArabic() throws Exception {
86 assertAnalyzesTo(a, "الفيلم الوثائقي الأول عن ويكيبيديا يسمى \"الحقيقة بالأرقام: قصة ويكيبيديا\" (بالإنجليزية: Truth in Numbers: The Wikipedia Story)، سيتم إطلاقه في 2008.",
87 new String[] { "الفيلم", "الوثائقي", "الأول", "عن", "ويكيبيديا", "يسمى", "الحقيقة", "بالأرقام", "قصة", "ويكيبيديا",
88 "بالإنجليزية", "truth", "in", "numbers", "the", "wikipedia", "story", "سيتم", "إطلاقه", "في", "2008" } );
91 public void testAramaic() throws Exception {
92 assertAnalyzesTo(a, "ܘܝܩܝܦܕܝܐ (ܐܢܓܠܝܐ: Wikipedia) ܗܘ ܐܝܢܣܩܠܘܦܕܝܐ ܚܐܪܬܐ ܕܐܢܛܪܢܛ ܒܠܫܢ̈ܐ ܣܓܝܐ̈ܐ܂ ܫܡܗ ܐܬܐ ܡܢ ܡ̈ܠܬܐ ܕ\"ܘܝܩܝ\" ܘ\"ܐܝܢܣܩܠܘܦܕܝܐ\"܀",
93 new String[] { "ܘܝܩܝܦܕܝܐ", "ܐܢܓܠܝܐ", "wikipedia", "ܗܘ", "ܐܝܢܣܩܠܘܦܕܝܐ", "ܚܐܪܬܐ", "ܕܐܢܛܪܢܛ", "ܒܠܫܢ̈ܐ", "ܣܓܝܐ̈ܐ", "ܫܡܗ",
94 "ܐܬܐ", "ܡܢ", "ܡ̈ܠܬܐ", "ܕ", "ܘܝܩܝ", "ܘ", "ܐܝܢܣܩܠܘܦܕܝܐ"});
97 public void testBengali() throws Exception {
98 assertAnalyzesTo(a, "এই বিশ্বকোষ পরিচালনা করে উইকিমিডিয়া ফাউন্ডেশন (একটি অলাভজনক সংস্থা)। উইকিপিডিয়ার শুরু ১৫ জানুয়ারি, ২০০১ সালে। এখন পর্যন্ত ২০০টিরও বেশী ভাষায় উইকিপিডিয়া রয়েছে।",
99 new String[] { "এই", "বিশ্বকোষ", "পরিচালনা", "করে", "উইকিমিডিয়া", "ফাউন্ডেশন", "একটি", "অলাভজনক", "সংস্থা", "উইকিপিডিয়ার",
100 "শুরু", "১৫", "জানুয়ারি", "২০০১", "সালে", "এখন", "পর্যন্ত", "২০০টিরও", "বেশী", "ভাষায়", "উইকিপিডিয়া", "রয়েছে" });
103 public void testFarsi() throws Exception {
104 assertAnalyzesTo(a, "ویکی پدیای انگلیسی در تاریخ ۲۵ دی ۱۳۷۹ به صورت مکملی برای دانشنامهٔ تخصصی نوپدیا نوشته شد.",
105 new String[] { "ویکی", "پدیای", "انگلیسی", "در", "تاریخ", "۲۵", "دی", "۱۳۷۹", "به", "صورت", "مکملی",
106 "برای", "دانشنامهٔ", "تخصصی", "نوپدیا", "نوشته", "شد" });
109 public void testGreek() throws Exception {
110 assertAnalyzesTo(a, "Γράφεται σε συνεργασία από εθελοντές με το λογισμικό wiki, κάτι που σημαίνει ότι άρθρα μπορεί να προστεθούν ή να αλλάξουν από τον καθένα.",
111 new String[] { "γράφεται", "σε", "συνεργασία", "από", "εθελοντέσ", "με", "το", "λογισμικό", "wiki", "κάτι", "που",
112 "σημαίνει", "ότι", "άρθρα", "μπορεί", "να", "προστεθούν", "ή", "να", "αλλάξουν", "από", "τον", "καθένα" });
115 public void testLao() throws Exception {
116 assertAnalyzesTo(a, "ກວ່າດອກ", new String[] { "ກວ່າ", "ດອກ" });
119 public void testThai() throws Exception {
120 assertAnalyzesTo(a, "การที่ได้ต้องแสดงว่างานดี. แล้วเธอจะไปไหน? ๑๒๓๔",
121 new String[] { "การ", "ที่", "ได้", "ต้อง", "แสดง", "ว่า", "งาน", "ดี", "แล้ว", "เธอ", "จะ", "ไป", "ไหน", "๑๒๓๔"});
124 public void testTibetan() throws Exception {
125 assertAnalyzesTo(a, "སྣོན་མཛོད་དང་ལས་འདིས་བོད་ཡིག་མི་ཉམས་གོང་འཕེལ་དུ་གཏོང་བར་ཧ་ཅང་དགེ་མཚན་མཆིས་སོ། །",
126 new String[] { "སྣོན", "མཛོད", "དང", "ལས", "འདིས", "བོད", "ཡིག", "མི", "ཉམས", "གོང", "འཕེལ", "དུ", "གཏོང", "བར", "ཧ", "ཅང", "དགེ", "མཚན", "མཆིས", "སོ" });
130 * For chinese, tokenize as char (these can later form bigrams or whatever)
132 public void testChinese() throws Exception {
133 assertAnalyzesTo(a, "我是中国人。 1234 Tests ",
134 new String[] { "我", "是", "中", "国", "人", "1234", "tests"});
137 public void testEmpty() throws Exception {
138 assertAnalyzesTo(a, "", new String[] {});
139 assertAnalyzesTo(a, ".", new String[] {});
140 assertAnalyzesTo(a, " ", new String[] {});
143 /* test various jira issues this analyzer is related to */
145 public void testLUCENE1545() throws Exception {
147 * Standard analyzer does not correctly tokenize combining character U+0364 COMBINING LATIN SMALL LETTRE E.
148 * The word "moͤchte" is incorrectly tokenized into "mo" "chte", the combining character is lost.
149 * Expected result is only on token "moͤchte".
151 assertAnalyzesTo(a, "moͤchte", new String[] { "moͤchte" });
154 /* Tests from StandardAnalyzer, just to show behavior is similar */
155 public void testAlphanumericSA() throws Exception {
156 // alphanumeric tokens
157 assertAnalyzesTo(a, "B2B", new String[]{"b2b"});
158 assertAnalyzesTo(a, "2B", new String[]{"2b"});
161 public void testDelimitersSA() throws Exception {
162 // other delimiters: "-", "/", ","
163 assertAnalyzesTo(a, "some-dashed-phrase", new String[]{"some", "dashed", "phrase"});
164 assertAnalyzesTo(a, "dogs,chase,cats", new String[]{"dogs", "chase", "cats"});
165 assertAnalyzesTo(a, "ac/dc", new String[]{"ac", "dc"});
168 public void testApostrophesSA() throws Exception {
169 // internal apostrophes: O'Reilly, you're, O'Reilly's
170 assertAnalyzesTo(a, "O'Reilly", new String[]{"o'reilly"});
171 assertAnalyzesTo(a, "you're", new String[]{"you're"});
172 assertAnalyzesTo(a, "she's", new String[]{"she's"});
173 assertAnalyzesTo(a, "Jim's", new String[]{"jim's"});
174 assertAnalyzesTo(a, "don't", new String[]{"don't"});
175 assertAnalyzesTo(a, "O'Reilly's", new String[]{"o'reilly's"});
178 public void testNumericSA() throws Exception {
179 // floating point, serial, model numbers, ip addresses, etc.
180 // every other segment must have at least one digit
181 assertAnalyzesTo(a, "21.35", new String[]{"21.35"});
182 assertAnalyzesTo(a, "R2D2 C3PO", new String[]{"r2d2", "c3po"});
183 assertAnalyzesTo(a, "216.239.63.104", new String[]{"216.239.63.104"});
184 assertAnalyzesTo(a, "216.239.63.104", new String[]{"216.239.63.104"});
187 public void testTextWithNumbersSA() throws Exception {
189 assertAnalyzesTo(a, "David has 5000 bones", new String[]{"david", "has", "5000", "bones"});
192 public void testVariousTextSA() throws Exception {
194 assertAnalyzesTo(a, "C embedded developers wanted", new String[]{"c", "embedded", "developers", "wanted"});
195 assertAnalyzesTo(a, "foo bar FOO BAR", new String[]{"foo", "bar", "foo", "bar"});
196 assertAnalyzesTo(a, "foo bar . FOO <> BAR", new String[]{"foo", "bar", "foo", "bar"});
197 assertAnalyzesTo(a, "\"QUOTED\" word", new String[]{"quoted", "word"});
200 public void testKoreanSA() throws Exception {
202 assertAnalyzesTo(a, "안녕하세요 한글입니다", new String[]{"안녕하세요", "한글입니다"});
205 public void testReusableTokenStream() throws Exception {
206 assertAnalyzesToReuse(a, "སྣོན་མཛོད་དང་ལས་འདིས་བོད་ཡིག་མི་ཉམས་གོང་འཕེལ་དུ་གཏོང་བར་ཧ་ཅང་དགེ་མཚན་མཆིས་སོ། །",
207 new String[] { "སྣོན", "མཛོད", "དང", "ལས", "འདིས", "བོད", "ཡིག", "མི", "ཉམས", "གོང",
208 "འཕེལ", "དུ", "གཏོང", "བར", "ཧ", "ཅང", "དགེ", "མཚན", "མཆིས", "སོ" });
211 public void testOffsets() throws Exception {
212 assertAnalyzesTo(a, "David has 5000 bones",
213 new String[] {"david", "has", "5000", "bones"},
214 new int[] {0, 6, 10, 15},
215 new int[] {5, 9, 14, 20});
218 public void testTypes() throws Exception {
219 assertAnalyzesTo(a, "David has 5000 bones",
220 new String[] {"david", "has", "5000", "bones"},
221 new String[] { "<ALPHANUM>", "<ALPHANUM>", "<NUM>", "<ALPHANUM>" });
224 public void testKorean() throws Exception {
225 BaseTokenStreamTestCase.assertAnalyzesTo(a, "훈민정음",
226 new String[] { "훈민정음" },
227 new String[] { "<HANGUL>" });
230 public void testJapanese() throws Exception {
231 BaseTokenStreamTestCase.assertAnalyzesTo(a, "仮名遣い カタカナ",
232 new String[] { "仮", "名", "遣", "い", "カタカナ" },
233 new String[] { "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<HIRAGANA>", "<KATAKANA>" });
236 /** blast some random strings through the analyzer */
237 public void testRandomStrings() throws Exception {
238 checkRandomData(random, a, 10000*RANDOM_MULTIPLIER);