pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / analysis / TestStandardAnalyzer.java
1 package org.apache.lucene.analysis;
2
3 import org.apache.lucene.analysis.standard.StandardAnalyzer;
4 import org.apache.lucene.analysis.standard.StandardTokenizer;
5 import org.apache.lucene.util.Version;
6
7 import java.io.IOException;
8 import java.io.Reader;
9 import java.io.StringReader;
10 import java.util.Arrays;
11
12 /**
13  * Licensed to the Apache Software Foundation (ASF) under one or more
14  * contributor license agreements.  See the NOTICE file distributed with
15  * this work for additional information regarding copyright ownership.
16  * The ASF licenses this file to You under the Apache License, Version 2.0
17  * (the "License"); you may not use this file except in compliance with
18  * the License.  You may obtain a copy of the License at
19  *
20  *     http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28
29 public class TestStandardAnalyzer extends BaseTokenStreamTestCase {
30   
31   public void testHugeDoc() throws IOException {
32     StringBuilder sb = new StringBuilder();
33     char whitespace[] = new char[4094];
34     Arrays.fill(whitespace, ' ');
35     sb.append(whitespace);
36     sb.append("testing 1234");
37     String input = sb.toString();
38     StandardTokenizer tokenizer = new StandardTokenizer(TEST_VERSION_CURRENT, new StringReader(input));
39     BaseTokenStreamTestCase.assertTokenStreamContents(tokenizer, new String[] { "testing", "1234" });
40   }
41
42   private Analyzer a = new ReusableAnalyzerBase() {
43     @Override
44     protected TokenStreamComponents createComponents
45       (String fieldName, Reader reader) {
46
47       Tokenizer tokenizer = new StandardTokenizer(TEST_VERSION_CURRENT, reader);
48       return new TokenStreamComponents(tokenizer);
49     }
50   };
51
52   public void testArmenian() throws Exception {
53     BaseTokenStreamTestCase.assertAnalyzesTo(a, "Վիքիպեդիայի 13 միլիոն հոդվածները (4,600` հայերեն վիքիպեդիայում) գրվել են կամավորների կողմից ու համարյա բոլոր հոդվածները կարող է խմբագրել ցանկաց մարդ ով կարող է բացել Վիքիպեդիայի կայքը։",
54         new String[] { "Վիքիպեդիայի", "13", "միլիոն", "հոդվածները", "4,600", "հայերեն", "վիքիպեդիայում", "գրվել", "են", "կամավորների", "կողմից", 
55         "ու", "համարյա", "բոլոր", "հոդվածները", "կարող", "է", "խմբագրել", "ցանկաց", "մարդ", "ով", "կարող", "է", "բացել", "Վիքիպեդիայի", "կայքը" } );
56   }
57   
58   public void testAmharic() throws Exception {
59     BaseTokenStreamTestCase.assertAnalyzesTo(a, "ዊኪፔድያ የባለ ብዙ ቋንቋ የተሟላ ትክክለኛና ነጻ መዝገበ ዕውቀት (ኢንሳይክሎፒዲያ) ነው። ማንኛውም",
60         new String[] { "ዊኪፔድያ", "የባለ", "ብዙ", "ቋንቋ", "የተሟላ", "ትክክለኛና", "ነጻ", "መዝገበ", "ዕውቀት", "ኢንሳይክሎፒዲያ", "ነው", "ማንኛውም" } );
61   }
62   
63   public void testArabic() throws Exception {
64     BaseTokenStreamTestCase.assertAnalyzesTo(a, "الفيلم الوثائقي الأول عن ويكيبيديا يسمى \"الحقيقة بالأرقام: قصة ويكيبيديا\" (بالإنجليزية: Truth in Numbers: The Wikipedia Story)، سيتم إطلاقه في 2008.",
65         new String[] { "الفيلم", "الوثائقي", "الأول", "عن", "ويكيبيديا", "يسمى", "الحقيقة", "بالأرقام", "قصة", "ويكيبيديا",
66         "بالإنجليزية", "Truth", "in", "Numbers", "The", "Wikipedia", "Story", "سيتم", "إطلاقه", "في", "2008" } ); 
67   }
68   
69   public void testAramaic() throws Exception {
70     BaseTokenStreamTestCase.assertAnalyzesTo(a, "ܘܝܩܝܦܕܝܐ (ܐܢܓܠܝܐ: Wikipedia) ܗܘ ܐܝܢܣܩܠܘܦܕܝܐ ܚܐܪܬܐ ܕܐܢܛܪܢܛ ܒܠܫܢ̈ܐ ܣܓܝܐ̈ܐ܂ ܫܡܗ ܐܬܐ ܡܢ ܡ̈ܠܬܐ ܕ\"ܘܝܩܝ\" ܘ\"ܐܝܢܣܩܠܘܦܕܝܐ\"܀",
71         new String[] { "ܘܝܩܝܦܕܝܐ", "ܐܢܓܠܝܐ", "Wikipedia", "ܗܘ", "ܐܝܢܣܩܠܘܦܕܝܐ", "ܚܐܪܬܐ", "ܕܐܢܛܪܢܛ", "ܒܠܫܢ̈ܐ", "ܣܓܝܐ̈ܐ", "ܫܡܗ",
72         "ܐܬܐ", "ܡܢ", "ܡ̈ܠܬܐ", "ܕ", "ܘܝܩܝ", "ܘ", "ܐܝܢܣܩܠܘܦܕܝܐ"});
73   }
74   
75   public void testBengali() throws Exception {
76     BaseTokenStreamTestCase.assertAnalyzesTo(a, "এই বিশ্বকোষ পরিচালনা করে উইকিমিডিয়া ফাউন্ডেশন (একটি অলাভজনক সংস্থা)। উইকিপিডিয়ার শুরু ১৫ জানুয়ারি, ২০০১ সালে। এখন পর্যন্ত ২০০টিরও বেশী ভাষায় উইকিপিডিয়া রয়েছে।",
77         new String[] { "এই", "বিশ্বকোষ", "পরিচালনা", "করে", "উইকিমিডিয়া", "ফাউন্ডেশন", "একটি", "অলাভজনক", "সংস্থা", "উইকিপিডিয়ার",
78         "শুরু", "১৫", "জানুয়ারি", "২০০১", "সালে", "এখন", "পর্যন্ত", "২০০টিরও", "বেশী", "ভাষায়", "উইকিপিডিয়া", "রয়েছে" });
79   }
80   
81   public void testFarsi() throws Exception {
82     BaseTokenStreamTestCase.assertAnalyzesTo(a, "ویکی پدیای انگلیسی در تاریخ ۲۵ دی ۱۳۷۹ به صورت مکملی برای دانشنامهٔ تخصصی نوپدیا نوشته شد.",
83         new String[] { "ویکی", "پدیای", "انگلیسی", "در", "تاریخ", "۲۵", "دی", "۱۳۷۹", "به", "صورت", "مکملی",
84         "برای", "دانشنامهٔ", "تخصصی", "نوپدیا", "نوشته", "شد" });
85   }
86   
87   public void testGreek() throws Exception {
88     BaseTokenStreamTestCase.assertAnalyzesTo(a, "Γράφεται σε συνεργασία από εθελοντές με το λογισμικό wiki, κάτι που σημαίνει ότι άρθρα μπορεί να προστεθούν ή να αλλάξουν από τον καθένα.",
89         new String[] { "Γράφεται", "σε", "συνεργασία", "από", "εθελοντές", "με", "το", "λογισμικό", "wiki", "κάτι", "που",
90         "σημαίνει", "ότι", "άρθρα", "μπορεί", "να", "προστεθούν", "ή", "να", "αλλάξουν", "από", "τον", "καθένα" });
91   }
92
93   public void testThai() throws Exception {
94     BaseTokenStreamTestCase.assertAnalyzesTo(a, "การที่ได้ต้องแสดงว่างานดี. แล้วเธอจะไปไหน? ๑๒๓๔",
95         new String[] { "การที่ได้ต้องแสดงว่างานดี", "แล้วเธอจะไปไหน", "๑๒๓๔" });
96   }
97   
98   public void testLao() throws Exception {
99     BaseTokenStreamTestCase.assertAnalyzesTo(a, "ສາທາລະນະລັດ ປະຊາທິປະໄຕ ປະຊາຊົນລາວ", 
100         new String[] { "ສາທາລະນະລັດ", "ປະຊາທິປະໄຕ", "ປະຊາຊົນລາວ" });
101   }
102   
103   public void testTibetan() throws Exception {
104     BaseTokenStreamTestCase.assertAnalyzesTo(a, "སྣོན་མཛོད་དང་ལས་འདིས་བོད་ཡིག་མི་ཉམས་གོང་འཕེལ་དུ་གཏོང་བར་ཧ་ཅང་དགེ་མཚན་མཆིས་སོ། །",
105                      new String[] { "སྣོན", "མཛོད", "དང", "ལས", "འདིས", "བོད", "ཡིག", 
106                                     "མི", "ཉམས", "གོང", "འཕེལ", "དུ", "གཏོང", "བར", 
107                                     "ཧ", "ཅང", "དགེ", "མཚན", "མཆིས", "སོ" });
108   }
109   
110   /*
111    * For chinese, tokenize as char (these can later form bigrams or whatever)
112    */
113   public void testChinese() throws Exception {
114     BaseTokenStreamTestCase.assertAnalyzesTo(a, "我是中国人。 1234 Tests ",
115         new String[] { "我", "是", "中", "国", "人", "1234", "Tests"});
116   }
117   
118   public void testEmpty() throws Exception {
119     BaseTokenStreamTestCase.assertAnalyzesTo(a, "", new String[] {});
120     BaseTokenStreamTestCase.assertAnalyzesTo(a, ".", new String[] {});
121     BaseTokenStreamTestCase.assertAnalyzesTo(a, " ", new String[] {});
122   }
123   
124   /* test various jira issues this analyzer is related to */
125   
126   public void testLUCENE1545() throws Exception {
127     /*
128      * Standard analyzer does not correctly tokenize combining character U+0364 COMBINING LATIN SMALL LETTRE E.
129      * The word "moͤchte" is incorrectly tokenized into "mo" "chte", the combining character is lost.
130      * Expected result is only on token "moͤchte".
131      */
132     BaseTokenStreamTestCase.assertAnalyzesTo(a, "moͤchte", new String[] { "moͤchte" }); 
133   }
134   
135   /* Tests from StandardAnalyzer, just to show behavior is similar */
136   public void testAlphanumericSA() throws Exception {
137     // alphanumeric tokens
138     BaseTokenStreamTestCase.assertAnalyzesTo(a, "B2B", new String[]{"B2B"});
139     BaseTokenStreamTestCase.assertAnalyzesTo(a, "2B", new String[]{"2B"});
140   }
141
142   public void testDelimitersSA() throws Exception {
143     // other delimiters: "-", "/", ","
144     BaseTokenStreamTestCase.assertAnalyzesTo(a, "some-dashed-phrase", new String[]{"some", "dashed", "phrase"});
145     BaseTokenStreamTestCase.assertAnalyzesTo(a, "dogs,chase,cats", new String[]{"dogs", "chase", "cats"});
146     BaseTokenStreamTestCase.assertAnalyzesTo(a, "ac/dc", new String[]{"ac", "dc"});
147   }
148
149   public void testApostrophesSA() throws Exception {
150     // internal apostrophes: O'Reilly, you're, O'Reilly's
151     BaseTokenStreamTestCase.assertAnalyzesTo(a, "O'Reilly", new String[]{"O'Reilly"});
152     BaseTokenStreamTestCase.assertAnalyzesTo(a, "you're", new String[]{"you're"});
153     BaseTokenStreamTestCase.assertAnalyzesTo(a, "she's", new String[]{"she's"});
154     BaseTokenStreamTestCase.assertAnalyzesTo(a, "Jim's", new String[]{"Jim's"});
155     BaseTokenStreamTestCase.assertAnalyzesTo(a, "don't", new String[]{"don't"});
156     BaseTokenStreamTestCase.assertAnalyzesTo(a, "O'Reilly's", new String[]{"O'Reilly's"});
157   }
158
159   public void testNumericSA() throws Exception {
160     // floating point, serial, model numbers, ip addresses, etc.
161     BaseTokenStreamTestCase.assertAnalyzesTo(a, "21.35", new String[]{"21.35"});
162     BaseTokenStreamTestCase.assertAnalyzesTo(a, "R2D2 C3PO", new String[]{"R2D2", "C3PO"});
163     BaseTokenStreamTestCase.assertAnalyzesTo(a, "216.239.63.104", new String[]{"216.239.63.104"});
164     BaseTokenStreamTestCase.assertAnalyzesTo(a, "216.239.63.104", new String[]{"216.239.63.104"});
165   }
166
167   public void testTextWithNumbersSA() throws Exception {
168     // numbers
169     BaseTokenStreamTestCase.assertAnalyzesTo(a, "David has 5000 bones", new String[]{"David", "has", "5000", "bones"});
170   }
171
172   public void testVariousTextSA() throws Exception {
173     // various
174     BaseTokenStreamTestCase.assertAnalyzesTo(a, "C embedded developers wanted", new String[]{"C", "embedded", "developers", "wanted"});
175     BaseTokenStreamTestCase.assertAnalyzesTo(a, "foo bar FOO BAR", new String[]{"foo", "bar", "FOO", "BAR"});
176     BaseTokenStreamTestCase.assertAnalyzesTo(a, "foo      bar .  FOO <> BAR", new String[]{"foo", "bar", "FOO", "BAR"});
177     BaseTokenStreamTestCase.assertAnalyzesTo(a, "\"QUOTED\" word", new String[]{"QUOTED", "word"});
178   }
179
180   public void testKoreanSA() throws Exception {
181     // Korean words
182     BaseTokenStreamTestCase.assertAnalyzesTo(a, "안녕하세요 한글입니다", new String[]{"안녕하세요", "한글입니다"});
183   }
184   
185   public void testOffsets() throws Exception {
186     BaseTokenStreamTestCase.assertAnalyzesTo(a, "David has 5000 bones", 
187         new String[] {"David", "has", "5000", "bones"},
188         new int[] {0, 6, 10, 15},
189         new int[] {5, 9, 14, 20});
190   }
191   
192   public void testTypes() throws Exception {
193     BaseTokenStreamTestCase.assertAnalyzesTo(a, "David has 5000 bones", 
194         new String[] {"David", "has", "5000", "bones"},
195         new String[] { "<ALPHANUM>", "<ALPHANUM>", "<NUM>", "<ALPHANUM>" });
196   }
197   
198   public void testUnicodeWordBreaks() throws Exception {
199     WordBreakTestUnicode_6_0_0 wordBreakTest = new WordBreakTestUnicode_6_0_0();
200     wordBreakTest.test(a);
201   }
202   
203   public void testSupplementary() throws Exception {
204     BaseTokenStreamTestCase.assertAnalyzesTo(a, "𩬅艱鍟䇹愯瀛", 
205         new String[] {"𩬅", "艱", "鍟", "䇹", "愯", "瀛"},
206         new String[] { "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>" });
207   }
208   
209   public void testKorean() throws Exception {
210     BaseTokenStreamTestCase.assertAnalyzesTo(a, "훈민정음",
211         new String[] { "훈민정음" },
212         new String[] { "<HANGUL>" });
213   }
214   
215   public void testJapanese() throws Exception {
216     BaseTokenStreamTestCase.assertAnalyzesTo(a, "仮名遣い カタカナ",
217         new String[] { "仮", "名", "遣", "い", "カタカナ" },
218         new String[] { "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<HIRAGANA>", "<KATAKANA>" });
219   }
220   
221   public void testCombiningMarks() throws Exception {
222     checkOneTerm(a, "ざ", "ざ"); // hiragana
223     checkOneTerm(a, "ザ", "ザ"); // katakana
224     checkOneTerm(a, "壹゙", "壹゙"); // ideographic
225     checkOneTerm(a, "아゙",  "아゙"); // hangul
226   }
227   
228   /** @deprecated remove this and sophisticated backwards layer in 5.0 */
229   @Deprecated
230   public void testCombiningMarksBackwards() throws Exception {
231     Analyzer a = new StandardAnalyzer(Version.LUCENE_33);
232     checkOneTerm(a, "ざ", "さ"); // hiragana Bug
233     checkOneTerm(a, "ザ", "ザ"); // katakana Works
234     checkOneTerm(a, "壹゙", "壹"); // ideographic Bug
235     checkOneTerm(a, "아゙",  "아゙"); // hangul Works
236   }
237
238   /** blast some random strings through the analyzer */
239   public void testRandomStrings() throws Exception {
240     checkRandomData(random, new StandardAnalyzer(TEST_VERSION_CURRENT), 10000*RANDOM_MULTIPLIER);
241   }
242 }