1 package org.apache.lucene.analysis;
3 import org.apache.lucene.analysis.standard.StandardAnalyzer;
4 import org.apache.lucene.analysis.standard.StandardTokenizer;
5 import org.apache.lucene.util.Version;
7 import java.io.IOException;
9 import java.io.StringReader;
10 import java.util.Arrays;
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
20 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 public class TestStandardAnalyzer extends BaseTokenStreamTestCase {
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" });
42 private Analyzer a = new ReusableAnalyzerBase() {
44 protected TokenStreamComponents createComponents
45 (String fieldName, Reader reader) {
47 Tokenizer tokenizer = new StandardTokenizer(TEST_VERSION_CURRENT, reader);
48 return new TokenStreamComponents(tokenizer);
52 public void testArmenian() throws Exception {
53 BaseTokenStreamTestCase.assertAnalyzesTo(a, "Վիքիպեդիայի 13 միլիոն հոդվածները (4,600` հայերեն վիքիպեդիայում) գրվել են կամավորների կողմից ու համարյա բոլոր հոդվածները կարող է խմբագրել ցանկաց մարդ ով կարող է բացել Վիքիպեդիայի կայքը։",
54 new String[] { "Վիքիպեդիայի", "13", "միլիոն", "հոդվածները", "4,600", "հայերեն", "վիքիպեդիայում", "գրվել", "են", "կամավորների", "կողմից",
55 "ու", "համարյա", "բոլոր", "հոդվածները", "կարող", "է", "խմբագրել", "ցանկաց", "մարդ", "ով", "կարող", "է", "բացել", "Վիքիպեդիայի", "կայքը" } );
58 public void testAmharic() throws Exception {
59 BaseTokenStreamTestCase.assertAnalyzesTo(a, "ዊኪፔድያ የባለ ብዙ ቋንቋ የተሟላ ትክክለኛና ነጻ መዝገበ ዕውቀት (ኢንሳይክሎፒዲያ) ነው። ማንኛውም",
60 new String[] { "ዊኪፔድያ", "የባለ", "ብዙ", "ቋንቋ", "የተሟላ", "ትክክለኛና", "ነጻ", "መዝገበ", "ዕውቀት", "ኢንሳይክሎፒዲያ", "ነው", "ማንኛውም" } );
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" } );
69 public void testAramaic() throws Exception {
70 BaseTokenStreamTestCase.assertAnalyzesTo(a, "ܘܝܩܝܦܕܝܐ (ܐܢܓܠܝܐ: Wikipedia) ܗܘ ܐܝܢܣܩܠܘܦܕܝܐ ܚܐܪܬܐ ܕܐܢܛܪܢܛ ܒܠܫܢ̈ܐ ܣܓܝܐ̈ܐ܂ ܫܡܗ ܐܬܐ ܡܢ ܡ̈ܠܬܐ ܕ\"ܘܝܩܝ\" ܘ\"ܐܝܢܣܩܠܘܦܕܝܐ\"܀",
71 new String[] { "ܘܝܩܝܦܕܝܐ", "ܐܢܓܠܝܐ", "Wikipedia", "ܗܘ", "ܐܝܢܣܩܠܘܦܕܝܐ", "ܚܐܪܬܐ", "ܕܐܢܛܪܢܛ", "ܒܠܫܢ̈ܐ", "ܣܓܝܐ̈ܐ", "ܫܡܗ",
72 "ܐܬܐ", "ܡܢ", "ܡ̈ܠܬܐ", "ܕ", "ܘܝܩܝ", "ܘ", "ܐܝܢܣܩܠܘܦܕܝܐ"});
75 public void testBengali() throws Exception {
76 BaseTokenStreamTestCase.assertAnalyzesTo(a, "এই বিশ্বকোষ পরিচালনা করে উইকিমিডিয়া ফাউন্ডেশন (একটি অলাভজনক সংস্থা)। উইকিপিডিয়ার শুরু ১৫ জানুয়ারি, ২০০১ সালে। এখন পর্যন্ত ২০০টিরও বেশী ভাষায় উইকিপিডিয়া রয়েছে।",
77 new String[] { "এই", "বিশ্বকোষ", "পরিচালনা", "করে", "উইকিমিডিয়া", "ফাউন্ডেশন", "একটি", "অলাভজনক", "সংস্থা", "উইকিপিডিয়ার",
78 "শুরু", "১৫", "জানুয়ারি", "২০০১", "সালে", "এখন", "পর্যন্ত", "২০০টিরও", "বেশী", "ভাষায়", "উইকিপিডিয়া", "রয়েছে" });
81 public void testFarsi() throws Exception {
82 BaseTokenStreamTestCase.assertAnalyzesTo(a, "ویکی پدیای انگلیسی در تاریخ ۲۵ دی ۱۳۷۹ به صورت مکملی برای دانشنامهٔ تخصصی نوپدیا نوشته شد.",
83 new String[] { "ویکی", "پدیای", "انگلیسی", "در", "تاریخ", "۲۵", "دی", "۱۳۷۹", "به", "صورت", "مکملی",
84 "برای", "دانشنامهٔ", "تخصصی", "نوپدیا", "نوشته", "شد" });
87 public void testGreek() throws Exception {
88 BaseTokenStreamTestCase.assertAnalyzesTo(a, "Γράφεται σε συνεργασία από εθελοντές με το λογισμικό wiki, κάτι που σημαίνει ότι άρθρα μπορεί να προστεθούν ή να αλλάξουν από τον καθένα.",
89 new String[] { "Γράφεται", "σε", "συνεργασία", "από", "εθελοντές", "με", "το", "λογισμικό", "wiki", "κάτι", "που",
90 "σημαίνει", "ότι", "άρθρα", "μπορεί", "να", "προστεθούν", "ή", "να", "αλλάξουν", "από", "τον", "καθένα" });
93 public void testThai() throws Exception {
94 BaseTokenStreamTestCase.assertAnalyzesTo(a, "การที่ได้ต้องแสดงว่างานดี. แล้วเธอจะไปไหน? ๑๒๓๔",
95 new String[] { "การที่ได้ต้องแสดงว่างานดี", "แล้วเธอจะไปไหน", "๑๒๓๔" });
98 public void testLao() throws Exception {
99 BaseTokenStreamTestCase.assertAnalyzesTo(a, "ສາທາລະນະລັດ ປະຊາທິປະໄຕ ປະຊາຊົນລາວ",
100 new String[] { "ສາທາລະນະລັດ", "ປະຊາທິປະໄຕ", "ປະຊາຊົນລາວ" });
103 public void testTibetan() throws Exception {
104 BaseTokenStreamTestCase.assertAnalyzesTo(a, "སྣོན་མཛོད་དང་ལས་འདིས་བོད་ཡིག་མི་ཉམས་གོང་འཕེལ་དུ་གཏོང་བར་ཧ་ཅང་དགེ་མཚན་མཆིས་སོ། །",
105 new String[] { "སྣོན", "མཛོད", "དང", "ལས", "འདིས", "བོད", "ཡིག",
106 "མི", "ཉམས", "གོང", "འཕེལ", "དུ", "གཏོང", "བར",
107 "ཧ", "ཅང", "དགེ", "མཚན", "མཆིས", "སོ" });
111 * For chinese, tokenize as char (these can later form bigrams or whatever)
113 public void testChinese() throws Exception {
114 BaseTokenStreamTestCase.assertAnalyzesTo(a, "我是中国人。 1234 Tests ",
115 new String[] { "我", "是", "中", "国", "人", "1234", "Tests"});
118 public void testEmpty() throws Exception {
119 BaseTokenStreamTestCase.assertAnalyzesTo(a, "", new String[] {});
120 BaseTokenStreamTestCase.assertAnalyzesTo(a, ".", new String[] {});
121 BaseTokenStreamTestCase.assertAnalyzesTo(a, " ", new String[] {});
124 /* test various jira issues this analyzer is related to */
126 public void testLUCENE1545() throws Exception {
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".
132 BaseTokenStreamTestCase.assertAnalyzesTo(a, "moͤchte", new String[] { "moͤchte" });
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"});
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"});
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"});
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"});
167 public void testTextWithNumbersSA() throws Exception {
169 BaseTokenStreamTestCase.assertAnalyzesTo(a, "David has 5000 bones", new String[]{"David", "has", "5000", "bones"});
172 public void testVariousTextSA() throws Exception {
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"});
180 public void testKoreanSA() throws Exception {
182 BaseTokenStreamTestCase.assertAnalyzesTo(a, "안녕하세요 한글입니다", new String[]{"안녕하세요", "한글입니다"});
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});
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>" });
198 public void testUnicodeWordBreaks() throws Exception {
199 WordBreakTestUnicode_6_0_0 wordBreakTest = new WordBreakTestUnicode_6_0_0();
200 wordBreakTest.test(a);
203 public void testSupplementary() throws Exception {
204 BaseTokenStreamTestCase.assertAnalyzesTo(a, "𩬅艱鍟䇹愯瀛",
205 new String[] {"𩬅", "艱", "鍟", "䇹", "愯", "瀛"},
206 new String[] { "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>" });
209 public void testKorean() throws Exception {
210 BaseTokenStreamTestCase.assertAnalyzesTo(a, "훈민정음",
211 new String[] { "훈민정음" },
212 new String[] { "<HANGUL>" });
215 public void testJapanese() throws Exception {
216 BaseTokenStreamTestCase.assertAnalyzesTo(a, "仮名遣い カタカナ",
217 new String[] { "仮", "名", "遣", "い", "カタカナ" },
218 new String[] { "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<IDEOGRAPHIC>", "<HIRAGANA>", "<KATAKANA>" });
221 public void testCombiningMarks() throws Exception {
222 checkOneTerm(a, "ざ", "ざ"); // hiragana
223 checkOneTerm(a, "ザ", "ザ"); // katakana
224 checkOneTerm(a, "壹゙", "壹゙"); // ideographic
225 checkOneTerm(a, "아゙", "아゙"); // hangul
228 /** @deprecated remove this and sophisticated backwards layer in 5.0 */
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
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);