pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / hunspell / HunspellStemmerTest.java
1 package org.apache.lucene.analysis.hunspell;
2
3 /*
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.util.Version;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.text.ParseException;
27 import java.util.List;
28
29 import static junit.framework.Assert.assertEquals;
30
31 public class HunspellStemmerTest {
32
33   private static HunspellStemmer stemmer;
34
35   @BeforeClass
36   public static void beforeClass() throws IOException, ParseException {
37     createStemmer(true);
38   }
39
40   @Test
41   public void testStem_simpleSuffix() {
42     List<HunspellStemmer.Stem> stems = stemmer.stem("lucene");
43
44     assertEquals(2, stems.size());
45     assertEquals("lucene", stems.get(0).getStemString());
46     assertEquals("lucen", stems.get(1).getStemString());
47
48     stems = stemmer.stem("mahoute");
49     assertEquals(1, stems.size());
50     assertEquals("mahout", stems.get(0).getStemString());
51   }
52
53   @Test
54   public void testStem_simplePrefix() {
55     List<HunspellStemmer.Stem> stems = stemmer.stem("solr");
56
57     assertEquals(1, stems.size());
58     assertEquals("olr", stems.get(0).getStemString());
59   }
60
61   @Test
62   public void testStem_recursiveSuffix() {
63     List<HunspellStemmer.Stem> stems = stemmer.stem("abcd");
64
65     assertEquals(1, stems.size());
66     assertEquals("ab", stems.get(0).getStemString());
67   }
68
69   @Test
70   public void testStem_ignoreCase() throws IOException, ParseException {
71     List<HunspellStemmer.Stem> stems;
72     createStemmer(true);
73
74     stems = stemmer.stem("apache");
75     assertEquals(1, stems.size());
76     assertEquals("apach", stems.get(0).getStemString());
77
78     stems = stemmer.stem("APACHE");
79     assertEquals(1, stems.size());
80     assertEquals("apach", stems.get(0).getStemString());
81
82     stems = stemmer.stem("Apache");
83     assertEquals(1, stems.size());
84     assertEquals("apach", stems.get(0).getStemString());
85     
86     stems = stemmer.stem("foos");
87     assertEquals(1, stems.size());
88     assertEquals("foo", stems.get(0).getStemString());
89     
90     stems = stemmer.stem("food");
91     assertEquals(1, stems.size());
92     assertEquals("foo", stems.get(0).getStemString());
93     
94     stems = stemmer.stem("Foos");
95     assertEquals(1, stems.size());
96     assertEquals("foo", stems.get(0).getStemString());
97     
98     stems = stemmer.stem("Food");
99     assertEquals(1, stems.size());
100     assertEquals("foo", stems.get(0).getStemString());
101   }
102
103   @Test
104   public void testStem_caseSensitive() throws IOException, ParseException {
105     createStemmer(false);
106     List<HunspellStemmer.Stem> stems = stemmer.stem("apache");
107     assertEquals(0, stems.size());
108
109     stems = stemmer.stem("Apache");
110     assertEquals(1, stems.size());
111     assertEquals("Apach", stems.get(0).getStemString());
112   }
113
114   
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");
118
119     HunspellDictionary dictionary = new HunspellDictionary(affixStream, dictStream, Version.LUCENE_34, ignoreCase);
120     stemmer = new HunspellStemmer(dictionary);
121
122     affixStream.close();
123     dictStream.close();
124   }
125
126 }