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