pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / synonym / TestSolrSynonymParser.java
1 package org.apache.lucene.analysis.synonym;
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 java.io.Reader;
21 import java.io.StringReader;
22 import java.text.ParseException;
23
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.analysis.MockTokenizer;
28 import org.apache.lucene.analysis.Tokenizer;
29 import org.apache.lucene.analysis.en.EnglishAnalyzer;
30 import org.apache.lucene.analysis.ReusableAnalyzerBase;
31 import org.junit.Test;
32
33 /**
34  * Tests parser for the Solr synonyms format
35  * @lucene.experimental
36  */
37 public class TestSolrSynonymParser extends BaseTokenStreamTestCase {
38   
39   /** Tests some simple examples from the solr wiki */
40   public void testSimple() throws Exception {
41     String testFile = 
42     "i-pod, ipod, ipoooood\n" + 
43     "foo => foo bar\n" +
44     "foo => baz\n" +
45     "this test, that testing";
46     
47     SolrSynonymParser parser = new SolrSynonymParser(true, true, new MockAnalyzer(random));
48     parser.add(new StringReader(testFile));
49     final SynonymMap map = parser.build();
50     
51     Analyzer analyzer = new ReusableAnalyzerBase() {
52       @Override
53       protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
54         Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, true);
55         return new TokenStreamComponents(tokenizer, new SynonymFilter(tokenizer, map, true));
56       }
57     };
58     
59     assertAnalyzesTo(analyzer, "ball", 
60         new String[] { "ball" },
61         new int[] { 1 });
62     
63     assertAnalyzesTo(analyzer, "i-pod",
64         new String[] { "i-pod", "ipod", "ipoooood" },
65         new int[] { 1, 0, 0 });
66     
67     assertAnalyzesTo(analyzer, "foo",
68         new String[] { "foo", "baz", "bar" },
69         new int[] { 1, 0, 1 });
70     
71     assertAnalyzesTo(analyzer, "this test",
72         new String[] { "this", "that", "test", "testing" },
73         new int[] { 1, 0, 1, 0 });
74   }
75   
76   /** parse a syn file with bad syntax */
77   @Test(expected=ParseException.class)
78   public void testInvalidDoubleMap() throws Exception {
79     String testFile = "a => b => c"; 
80     SolrSynonymParser parser = new SolrSynonymParser(true, true, new MockAnalyzer(random));
81     parser.add(new StringReader(testFile));
82   }
83   
84   /** parse a syn file with bad syntax */
85   @Test(expected=ParseException.class)
86   public void testInvalidAnalyzesToNothingOutput() throws Exception {
87     String testFile = "a => 1"; 
88     SolrSynonymParser parser = new SolrSynonymParser(true, true, new MockAnalyzer(random, MockTokenizer.SIMPLE, false));
89     parser.add(new StringReader(testFile));
90   }
91   
92   /** parse a syn file with bad syntax */
93   @Test(expected=ParseException.class)
94   public void testInvalidAnalyzesToNothingInput() throws Exception {
95     String testFile = "1 => a"; 
96     SolrSynonymParser parser = new SolrSynonymParser(true, true, new MockAnalyzer(random, MockTokenizer.SIMPLE, false));
97     parser.add(new StringReader(testFile));
98   }
99   
100   /** parse a syn file with bad syntax */
101   @Test(expected=ParseException.class)
102   public void testInvalidPositionsInput() throws Exception {
103     String testFile = "testola => the test";
104     SolrSynonymParser parser = new SolrSynonymParser(true, true, new EnglishAnalyzer(TEST_VERSION_CURRENT));
105     parser.add(new StringReader(testFile));
106   }
107   
108   /** parse a syn file with bad syntax */
109   @Test(expected=ParseException.class)
110   public void testInvalidPositionsOutput() throws Exception {
111     String testFile = "the test => testola";
112     SolrSynonymParser parser = new SolrSynonymParser(true, true, new EnglishAnalyzer(TEST_VERSION_CURRENT));
113     parser.add(new StringReader(testFile));
114   }
115   
116   /** parse a syn file with some escaped syntax chars */
117   public void testEscapedStuff() throws Exception {
118     String testFile = 
119       "a\\=>a => b\\=>b\n" +
120       "a\\,a => b\\,b";
121     SolrSynonymParser parser = new SolrSynonymParser(true, true, new MockAnalyzer(random, MockTokenizer.KEYWORD, false));
122     parser.add(new StringReader(testFile));
123     final SynonymMap map = parser.build();
124     Analyzer analyzer = new ReusableAnalyzerBase() {
125       @Override
126       protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
127         Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.KEYWORD, false);
128         return new TokenStreamComponents(tokenizer, new SynonymFilter(tokenizer, map, false));
129       }
130     };
131     
132     assertAnalyzesTo(analyzer, "ball", 
133         new String[] { "ball" },
134         new int[] { 1 });
135     
136     assertAnalyzesTo(analyzer, "a=>a",
137         new String[] { "b=>b" },
138         new int[] { 1 });
139     
140     assertAnalyzesTo(analyzer, "a,a",
141         new String[] { "b,b" },
142         new int[] { 1 });
143   }
144 }