1 package org.apache.lucene.analysis.hunspell;
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 org.apache.lucene.util.Version;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.text.ParseException;
27 import java.util.List;
29 import static junit.framework.Assert.assertEquals;
31 public class HunspellStemmerTest {
33 private static HunspellStemmer stemmer;
36 public static void beforeClass() throws IOException, ParseException {
41 public void testStem_simpleSuffix() {
42 List<HunspellStemmer.Stem> stems = stemmer.stem("lucene");
44 assertEquals(2, stems.size());
45 assertEquals("lucene", stems.get(0).getStemString());
46 assertEquals("lucen", stems.get(1).getStemString());
48 stems = stemmer.stem("mahoute");
49 assertEquals(1, stems.size());
50 assertEquals("mahout", stems.get(0).getStemString());
54 public void testStem_simplePrefix() {
55 List<HunspellStemmer.Stem> stems = stemmer.stem("solr");
57 assertEquals(1, stems.size());
58 assertEquals("olr", stems.get(0).getStemString());
62 public void testStem_recursiveSuffix() {
63 List<HunspellStemmer.Stem> stems = stemmer.stem("abcd");
65 assertEquals(1, stems.size());
66 assertEquals("ab", stems.get(0).getStemString());
70 public void testStem_ignoreCase() throws IOException, ParseException {
71 List<HunspellStemmer.Stem> stems;
74 stems = stemmer.stem("apache");
75 assertEquals(1, stems.size());
76 assertEquals("apach", stems.get(0).getStemString());
78 stems = stemmer.stem("APACHE");
79 assertEquals(1, stems.size());
80 assertEquals("apach", stems.get(0).getStemString());
82 stems = stemmer.stem("Apache");
83 assertEquals(1, stems.size());
84 assertEquals("apach", stems.get(0).getStemString());
86 stems = stemmer.stem("foos");
87 assertEquals(1, stems.size());
88 assertEquals("foo", stems.get(0).getStemString());
90 stems = stemmer.stem("food");
91 assertEquals(1, stems.size());
92 assertEquals("foo", stems.get(0).getStemString());
94 stems = stemmer.stem("Foos");
95 assertEquals(1, stems.size());
96 assertEquals("foo", stems.get(0).getStemString());
98 stems = stemmer.stem("Food");
99 assertEquals(1, stems.size());
100 assertEquals("foo", stems.get(0).getStemString());
104 public void testStem_caseSensitive() throws IOException, ParseException {
105 createStemmer(false);
106 List<HunspellStemmer.Stem> stems = stemmer.stem("apache");
107 assertEquals(0, stems.size());
109 stems = stemmer.stem("Apache");
110 assertEquals(1, stems.size());
111 assertEquals("Apach", stems.get(0).getStemString());
115 private static void createStemmer(boolean ignoreCase) throws IOException, ParseException {
116 InputStream affixStream = HunspellStemmerTest.class.getResourceAsStream("test.aff");
117 InputStream dictStream = HunspellStemmerTest.class.getResourceAsStream("test.dic");
119 HunspellDictionary dictionary = new HunspellDictionary(affixStream, dictStream, Version.LUCENE_34, ignoreCase);
120 stemmer = new HunspellStemmer(dictionary);