1 package org.apache.lucene.analysis.en;
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 static org.apache.lucene.analysis.VocabularyAssert.assertVocabulary;
22 import java.io.Reader;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
26 import org.apache.lucene.analysis.MockTokenizer;
27 import org.apache.lucene.analysis.Tokenizer;
28 import org.apache.lucene.analysis.ReusableAnalyzerBase;
31 * Tests for {@link KStemmer}
33 public class TestKStemmer extends BaseTokenStreamTestCase {
34 Analyzer a = new ReusableAnalyzerBase() {
36 protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
37 Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, true);
38 return new TokenStreamComponents(tokenizer, new KStemFilter(tokenizer));
42 /** blast some random strings through the analyzer */
43 public void testRandomStrings() throws Exception {
44 checkRandomData(random, a, 10000*RANDOM_MULTIPLIER);
48 * test the kstemmer optimizations against a bunch of words
49 * that were stemmed with the original java kstemmer (generated from
50 * testCreateMap, commented out below).
52 public void testVocabulary() throws Exception {
53 assertVocabulary(a, getDataFile("kstemTestData.zip"), "kstem_examples.txt");
56 /****** requires original java kstem source code to create map
57 public void testCreateMap() throws Exception {
58 String input = getBigDoc();
59 Reader r = new StringReader(input);
60 TokenFilter tf = new LowerCaseFilter(new LetterTokenizer(r));
61 // tf = new KStemFilter(tf);
63 KStemmer kstem = new KStemmer();
64 Map<String,String> map = new TreeMap<String,String>();
68 String s = t.termText();
69 if (map.containsKey(s)) continue;
70 map.put(s, kstem.stem(s));
73 Writer out = new BufferedWriter(new FileWriter("kstem_examples.txt"));
74 for (String key : map.keySet()) {
77 out.write(map.get(key));