pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / snowball / TestSnowball.java
1 package org.apache.lucene.analysis.snowball;
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.analysis.BaseTokenStreamTestCase;
21 import org.apache.lucene.analysis.Analyzer;
22 import org.apache.lucene.index.Payload;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.standard.StandardAnalyzer;
25 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26 import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
27 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
28 import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
29 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
30 import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
31 import org.apache.lucene.util.Version;
32
33 public class TestSnowball extends BaseTokenStreamTestCase {
34
35   public void testEnglish() throws Exception {
36     Analyzer a = new SnowballAnalyzer(TEST_VERSION_CURRENT, "English");
37     assertAnalyzesTo(a, "he abhorred accents",
38         new String[]{"he", "abhor", "accent"});
39   }
40   
41   public void testStopwords() throws Exception {
42     Analyzer a = new SnowballAnalyzer(TEST_VERSION_CURRENT, "English",
43         StandardAnalyzer.STOP_WORDS_SET);
44     assertAnalyzesTo(a, "the quick brown fox jumped",
45         new String[]{"quick", "brown", "fox", "jump"});
46   }
47
48   /**
49    * Test english lowercasing. Test both cases (pre-3.1 and post-3.1) to ensure
50    * we lowercase I correct for non-Turkish languages in either case.
51    */
52   public void testEnglishLowerCase() throws Exception {
53     Analyzer a = new SnowballAnalyzer(TEST_VERSION_CURRENT, "English");
54     assertAnalyzesTo(a, "cryogenic", new String[] { "cryogen" });
55     assertAnalyzesTo(a, "CRYOGENIC", new String[] { "cryogen" });
56     
57     Analyzer b = new SnowballAnalyzer(Version.LUCENE_30, "English");
58     assertAnalyzesTo(b, "cryogenic", new String[] { "cryogen" });
59     assertAnalyzesTo(b, "CRYOGENIC", new String[] { "cryogen" });
60   }
61   
62   /**
63    * Test turkish lowercasing
64    */
65   public void testTurkish() throws Exception {
66     Analyzer a = new SnowballAnalyzer(TEST_VERSION_CURRENT, "Turkish");
67
68     assertAnalyzesTo(a, "ağacı", new String[] { "ağaç" });
69     assertAnalyzesTo(a, "AĞACI", new String[] { "ağaç" });
70   }
71   
72   /**
73    * Test turkish lowercasing (old buggy behavior)
74    * @deprecated Remove this when support for 3.0 indexes is no longer required
75    */
76   @Deprecated
77   public void testTurkishBWComp() throws Exception {
78     Analyzer a = new SnowballAnalyzer(Version.LUCENE_30, "Turkish");
79     // AĞACI in turkish lowercases to ağacı, but with lowercase filter ağaci.
80     // this fails due to wrong casing, because the stemmer
81     // will only remove -ı, not -i
82     assertAnalyzesTo(a, "ağacı", new String[] { "ağaç" });
83     assertAnalyzesTo(a, "AĞACI", new String[] { "ağaci" });
84   }
85
86   
87   public void testReusableTokenStream() throws Exception {
88     Analyzer a = new SnowballAnalyzer(TEST_VERSION_CURRENT, "English");
89     assertAnalyzesToReuse(a, "he abhorred accents",
90         new String[]{"he", "abhor", "accent"});
91     assertAnalyzesToReuse(a, "she abhorred him",
92         new String[]{"she", "abhor", "him"});
93   }
94   
95   public void testFilterTokens() throws Exception {
96     SnowballFilter filter = new SnowballFilter(new TestTokenStream(), "English");
97     CharTermAttribute termAtt = filter.getAttribute(CharTermAttribute.class);
98     OffsetAttribute offsetAtt = filter.getAttribute(OffsetAttribute.class);
99     TypeAttribute typeAtt = filter.getAttribute(TypeAttribute.class);
100     PayloadAttribute payloadAtt = filter.getAttribute(PayloadAttribute.class);
101     PositionIncrementAttribute posIncAtt = filter.getAttribute(PositionIncrementAttribute.class);
102     FlagsAttribute flagsAtt = filter.getAttribute(FlagsAttribute.class);
103     
104     filter.incrementToken();
105
106     assertEquals("accent", termAtt.toString());
107     assertEquals(2, offsetAtt.startOffset());
108     assertEquals(7, offsetAtt.endOffset());
109     assertEquals("wrd", typeAtt.type());
110     assertEquals(3, posIncAtt.getPositionIncrement());
111     assertEquals(77, flagsAtt.getFlags());
112     assertEquals(new Payload(new byte[]{0,1,2,3}), payloadAtt.getPayload());
113   }
114   
115   private final class TestTokenStream extends TokenStream {
116     private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
117     private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
118     private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
119     private final PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
120     private final PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
121     private final FlagsAttribute flagsAtt = addAttribute(FlagsAttribute.class);
122     
123     TestTokenStream() {
124       super();
125     }
126     
127     @Override
128     public boolean incrementToken() {
129       clearAttributes();
130       termAtt.setEmpty().append("accents");
131       offsetAtt.setOffset(2, 7);
132       typeAtt.setType("wrd");
133       posIncAtt.setPositionIncrement(3);
134       payloadAtt.setPayload(new Payload(new byte[]{0,1,2,3}));
135       flagsAtt.setFlags(77);
136       return true;
137     }
138   }
139 }