1 package org.apache.lucene.analysis.synonym;
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 java.io.Reader;
21 import java.io.StringReader;
22 import java.text.ParseException;
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;
34 * Tests parser for the Solr synonyms format
35 * @lucene.experimental
37 public class TestSolrSynonymParser extends BaseTokenStreamTestCase {
39 /** Tests some simple examples from the solr wiki */
40 public void testSimple() throws Exception {
42 "i-pod, ipod, ipoooood\n" +
45 "this test, that testing";
47 SolrSynonymParser parser = new SolrSynonymParser(true, true, new MockAnalyzer(random));
48 parser.add(new StringReader(testFile));
49 final SynonymMap map = parser.build();
51 Analyzer analyzer = new ReusableAnalyzerBase() {
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));
59 assertAnalyzesTo(analyzer, "ball",
60 new String[] { "ball" },
63 assertAnalyzesTo(analyzer, "i-pod",
64 new String[] { "i-pod", "ipod", "ipoooood" },
65 new int[] { 1, 0, 0 });
67 assertAnalyzesTo(analyzer, "foo",
68 new String[] { "foo", "baz", "bar" },
69 new int[] { 1, 0, 1 });
71 assertAnalyzesTo(analyzer, "this test",
72 new String[] { "this", "that", "test", "testing" },
73 new int[] { 1, 0, 1, 0 });
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));
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));
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));
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));
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));
116 /** parse a syn file with some escaped syntax chars */
117 public void testEscapedStuff() throws Exception {
119 "a\\=>a => b\\=>b\n" +
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() {
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));
132 assertAnalyzesTo(analyzer, "ball",
133 new String[] { "ball" },
136 assertAnalyzesTo(analyzer, "a=>a",
137 new String[] { "b=>b" },
140 assertAnalyzesTo(analyzer, "a,a",
141 new String[] { "b,b" },