add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / icu / src / test / org / apache / lucene / analysis / icu / segmentation / TestICUTokenizer.java
1 package org.apache.lucene.analysis.icu.segmentation;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
26
27 import java.io.IOException;
28 import java.io.Reader;
29 import java.io.StringReader;
30 import java.util.Arrays;
31
32 public class TestICUTokenizer extends BaseTokenStreamTestCase {
33   
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" });
43   }
44   
45   public void testHugeTerm2() throws IOException {
46     StringBuilder sb = new StringBuilder();
47     for (int i = 0; i < 40960; i++) {
48       sb.append('a');
49     }
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);
55     String expected[] = { 
56         expectedToken, expectedToken, expectedToken, 
57         expectedToken, expectedToken, expectedToken,
58         expectedToken, expectedToken, expectedToken,
59         expectedToken
60     };
61     assertTokenStreamContents(tokenizer, expected);
62   }
63   
64   private Analyzer a = new ReusableAnalyzerBase() {
65     @Override
66     protected TokenStreamComponents createComponents(String fieldName,
67         Reader reader) {
68       Tokenizer tokenizer = new ICUTokenizer(reader);
69       TokenFilter filter = new ICUNormalizer2Filter(tokenizer);
70       return new TokenStreamComponents(tokenizer, filter);
71     }
72   };
73
74   public void testArmenian() throws Exception {
75     assertAnalyzesTo(a, "Վիքիպեդիայի 13 միլիոն հոդվածները (4,600` հայերեն վիքիպեդիայում) գրվել են կամավորների կողմից ու համարյա բոլոր հոդվածները կարող է խմբագրել ցանկաց մարդ ով կարող է բացել Վիքիպեդիայի կայքը։",
76         new String[] { "վիքիպեդիայի", "13", "միլիոն", "հոդվածները", "4,600", "հայերեն", "վիքիպեդիայում", "գրվել", "են", "կամավորների", "կողմից", 
77         "ու", "համարյա", "բոլոր", "հոդվածները", "կարող", "է", "խմբագրել", "ցանկաց", "մարդ", "ով", "կարող", "է", "բացել", "վիքիպեդիայի", "կայքը" } );
78   }
79   
80   public void testAmharic() throws Exception {
81     assertAnalyzesTo(a, "ዊኪፔድያ የባለ ብዙ ቋንቋ የተሟላ ትክክለኛና ነጻ መዝገበ ዕውቀት (ኢንሳይክሎፒዲያ) ነው። ማንኛውም",
82         new String[] { "ዊኪፔድያ", "የባለ", "ብዙ", "ቋንቋ", "የተሟላ", "ትክክለኛና", "ነጻ", "መዝገበ", "ዕውቀት", "ኢንሳይክሎፒዲያ", "ነው", "ማንኛውም" } );
83   }
84   
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" } ); 
89   }
90   
91   public void testAramaic() throws Exception {
92     assertAnalyzesTo(a, "ܘܝܩܝܦܕܝܐ (ܐܢܓܠܝܐ: Wikipedia) ܗܘ ܐܝܢܣܩܠܘܦܕܝܐ ܚܐܪܬܐ ܕܐܢܛܪܢܛ ܒܠܫܢ̈ܐ ܣܓܝܐ̈ܐ܂ ܫܡܗ ܐܬܐ ܡܢ ܡ̈ܠܬܐ ܕ\"ܘܝܩܝ\" ܘ\"ܐܝܢܣܩܠܘܦܕܝܐ\"܀",
93         new String[] { "ܘܝܩܝܦܕܝܐ", "ܐܢܓܠܝܐ", "wikipedia", "ܗܘ", "ܐܝܢܣܩܠܘܦܕܝܐ", "ܚܐܪܬܐ", "ܕܐܢܛܪܢܛ", "ܒܠܫܢ̈ܐ", "ܣܓܝܐ̈ܐ", "ܫܡܗ",
94         "ܐܬܐ", "ܡܢ", "ܡ̈ܠܬܐ", "ܕ", "ܘܝܩܝ", "ܘ", "ܐܝܢܣܩܠܘܦܕܝܐ"});
95   }
96   
97   public void testBengali() throws Exception {
98     assertAnalyzesTo(a, "এই বিশ্বকোষ পরিচালনা করে উইকিমিডিয়া ফাউন্ডেশন (একটি অলাভজনক সংস্থা)। উইকিপিডিয়ার শুরু ১৫ জানুয়ারি, ২০০১ সালে। এখন পর্যন্ত ২০০টিরও বেশী ভাষায় উইকিপিডিয়া রয়েছে।",
99         new String[] { "এই", "বিশ্বকোষ", "পরিচালনা", "করে", "উইকিমিডিয়া", "ফাউন্ডেশন", "একটি", "অলাভজনক", "সংস্থা", "উইকিপিডিয়ার",
100         "শুরু", "১৫", "জানুয়ারি", "২০০১", "সালে", "এখন", "পর্যন্ত", "২০০টিরও", "বেশী", "ভাষায়", "উইকিপিডিয়া", "রয়েছে" });
101   }
102   
103   public void testFarsi() throws Exception {
104     assertAnalyzesTo(a, "ویکی پدیای انگلیسی در تاریخ ۲۵ دی ۱۳۷۹ به صورت مکملی برای دانشنامهٔ تخصصی نوپدیا نوشته شد.",
105         new String[] { "ویکی", "پدیای", "انگلیسی", "در", "تاریخ", "۲۵", "دی", "۱۳۷۹", "به", "صورت", "مکملی",
106         "برای", "دانشنامهٔ", "تخصصی", "نوپدیا", "نوشته", "شد" });
107   }
108   
109   public void testGreek() throws Exception {
110     assertAnalyzesTo(a, "Γράφεται σε συνεργασία από εθελοντές με το λογισμικό wiki, κάτι που σημαίνει ότι άρθρα μπορεί να προστεθούν ή να αλλάξουν από τον καθένα.",
111         new String[] { "γράφεται", "σε", "συνεργασία", "από", "εθελοντέσ", "με", "το", "λογισμικό", "wiki", "κάτι", "που",
112         "σημαίνει", "ότι", "άρθρα", "μπορεί", "να", "προστεθούν", "ή", "να", "αλλάξουν", "από", "τον", "καθένα" });
113   }
114   
115   public void testLao() throws Exception {
116     assertAnalyzesTo(a, "ກວ່າດອກ", new String[] { "ກວ່າ", "ດອກ" });
117   }
118   
119   public void testThai() throws Exception {
120     assertAnalyzesTo(a, "การที่ได้ต้องแสดงว่างานดี. แล้วเธอจะไปไหน? ๑๒๓๔",
121         new String[] { "การ", "ที่", "ได้", "ต้อง", "แสดง", "ว่า", "งาน", "ดี", "แล้ว", "เธอ", "จะ", "ไป", "ไหน", "๑๒๓๔"});
122   }
123   
124   public void testTibetan() throws Exception {
125     assertAnalyzesTo(a, "སྣོན་མཛོད་དང་ལས་འདིས་བོད་ཡིག་མི་ཉམས་གོང་འཕེལ་དུ་གཏོང་བར་ཧ་ཅང་དགེ་མཚན་མཆིས་སོ། །",
126         new String[] { "སྣོན", "མཛོད", "དང", "ལས", "འདིས", "བོད", "ཡིག", "མི", "ཉམས", "གོང", "འཕེལ", "དུ", "གཏོང", "བར", "ཧ", "ཅང", "དགེ", "མཚན", "མཆིས", "སོ" });
127   }
128   
129   /*
130    * For chinese, tokenize as char (these can later form bigrams or whatever)
131    */
132   public void testChinese() throws Exception {
133     assertAnalyzesTo(a, "我是中国人。 1234 Tests ",
134         new String[] { "我", "是", "中", "国", "人", "1234", "tests"});
135   }
136   
137   public void testEmpty() throws Exception {
138     assertAnalyzesTo(a, "", new String[] {});
139     assertAnalyzesTo(a, ".", new String[] {});
140     assertAnalyzesTo(a, " ", new String[] {});
141   }
142   
143   /* test various jira issues this analyzer is related to */
144   
145   public void testLUCENE1545() throws Exception {
146     /*
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".
150      */
151     assertAnalyzesTo(a, "moͤchte", new String[] { "moͤchte" }); 
152   }
153   
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"});
159   }
160
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"});
166   }
167
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"});
176   }
177
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"});
185   }
186
187   public void testTextWithNumbersSA() throws Exception {
188     // numbers
189     assertAnalyzesTo(a, "David has 5000 bones", new String[]{"david", "has", "5000", "bones"});
190   }
191
192   public void testVariousTextSA() throws Exception {
193     // various
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"});
198   }
199
200   public void testKoreanSA() throws Exception {
201     // Korean words
202     assertAnalyzesTo(a, "안녕하세요 한글입니다", new String[]{"안녕하세요", "한글입니다"});
203   }
204   
205   public void testReusableTokenStream() throws Exception {
206     assertAnalyzesToReuse(a, "སྣོན་མཛོད་དང་ལས་འདིས་བོད་ཡིག་མི་ཉམས་གོང་འཕེལ་དུ་གཏོང་བར་ཧ་ཅང་དགེ་མཚན་མཆིས་སོ། །",
207         new String[] { "སྣོན", "མཛོད", "དང", "ལས", "འདིས", "བོད", "ཡིག", "མི", "ཉམས", "གོང", 
208                       "འཕེལ", "དུ", "གཏོང", "བར", "ཧ", "ཅང", "དགེ", "མཚན", "མཆིས", "སོ" });
209   }
210   
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});
216   }
217   
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>" });
222   }
223   
224   public void testKorean() throws Exception {
225     BaseTokenStreamTestCase.assertAnalyzesTo(a, "훈민정음",
226         new String[] { "훈민정음" },
227         new String[] { "<HANGUL>" });
228   }
229   
230   public void testJapanese() throws Exception {
231     BaseTokenStreamTestCase.assertAnalyzesTo(a, "仮名遣い カタカナ",
232         new String[] { "仮", "名", "遣", "い", "カタカナ" },
233         new String[] { "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<HIRAGANA>", "<KATAKANA>" });
234   }
235   
236   /** blast some random strings through the analyzer */
237   public void testRandomStrings() throws Exception {
238     checkRandomData(random, a, 10000*RANDOM_MULTIPLIER);
239   }
240 }