add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / synonym / TestWordnetSynonymParser.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis.synonym;
19
20 import java.io.Reader;
21 import java.io.StringReader;
22
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.analysis.MockTokenizer;
27 import org.apache.lucene.analysis.Tokenizer;
28 import org.apache.lucene.analysis.ReusableAnalyzerBase;
29
30 public class TestWordnetSynonymParser extends BaseTokenStreamTestCase {
31   Analyzer analyzer;
32
33   String synonymsFile = 
34     "s(100000001,1,'woods',n,1,0).\n" +
35     "s(100000001,2,'wood',n,1,0).\n" +
36     "s(100000001,3,'forest',n,1,0).\n" +
37     "s(100000002,1,'wolfish',n,1,0).\n" +
38     "s(100000002,2,'ravenous',n,1,0).\n" +
39     "s(100000003,1,'king',n,1,1).\n" +
40     "s(100000003,2,'baron',n,1,1).\n" +
41     "s(100000004,1,'king''s evil',n,1,1).\n" +
42     "s(100000004,2,'king''s meany',n,1,1).\n";
43   
44   public void testSynonyms() throws Exception {
45     WordnetSynonymParser parser = new WordnetSynonymParser(true, true, new MockAnalyzer(random));
46     parser.add(new StringReader(synonymsFile));
47     final SynonymMap map = parser.build();
48     
49     Analyzer analyzer = new ReusableAnalyzerBase() {
50       @Override
51       protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
52         Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
53         return new TokenStreamComponents(tokenizer, new SynonymFilter(tokenizer, map, false));
54       }
55     };
56     
57     /* all expansions */
58     assertAnalyzesTo(analyzer, "Lost in the woods",
59         new String[] { "Lost", "in", "the", "woods", "wood", "forest" },
60         new int[] { 0, 5, 8, 12, 12, 12 },
61         new int[] { 4, 7, 11, 17, 17, 17 },
62         new int[] { 1, 1, 1, 1, 0, 0 });
63     
64     /* single quote */
65     assertAnalyzesTo(analyzer, "king",
66         new String[] { "king", "baron" });
67     
68     /* multi words */
69     assertAnalyzesTo(analyzer, "king's evil",
70         new String[] { "king's", "king's", "evil", "meany" });
71   }
72 }