pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / analyzing / TestAnalyzingQueryParser.java
1 package org.apache.lucene.queryParser.analyzing;
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
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.analysis.ASCIIFoldingFilter;
24 import org.apache.lucene.analysis.LowerCaseFilter;
25 import org.apache.lucene.analysis.TokenStream;
26 import org.apache.lucene.analysis.standard.StandardFilter;
27 import org.apache.lucene.analysis.standard.StandardTokenizer;
28 import org.apache.lucene.queryParser.ParseException;
29 import org.apache.lucene.util.LuceneTestCase;
30 import org.apache.lucene.util.Version;
31
32 /**
33  * @version $Revision$, $Date$
34  */
35 public class TestAnalyzingQueryParser extends LuceneTestCase {
36
37   private Analyzer a;
38
39   private String[] wildcardInput;
40   private String[] wildcardExpected;
41   private String[] prefixInput;
42   private String[] prefixExpected;
43   private String[] rangeInput;
44   private String[] rangeExpected;
45   private String[] fuzzyInput;
46   private String[] fuzzyExpected;
47
48   @Override
49   public void setUp() throws Exception {
50     super.setUp();
51     wildcardInput = new String[] { "übersetzung über*ung",
52         "Mötley Cr\u00fce Mötl?* Crü?", "Renée Zellweger Ren?? Zellw?ger" };
53     wildcardExpected = new String[] { "ubersetzung uber*ung", "motley crue motl?* cru?",
54         "renee zellweger ren?? zellw?ger" };
55
56     prefixInput = new String[] { "übersetzung übersetz*",
57         "Mötley Crüe Mötl* crü*", "René? Zellw*" };
58     prefixExpected = new String[] { "ubersetzung ubersetz*", "motley crue motl* cru*",
59         "rene? zellw*" };
60
61     rangeInput = new String[] { "[aa TO bb]", "{Anaïs TO Zoé}" };
62     rangeExpected = new String[] { "[aa TO bb]", "{anais TO zoe}" };
63
64     fuzzyInput = new String[] { "Übersetzung Übersetzung~0.9",
65         "Mötley Crüe Mötley~0.75 Crüe~0.5",
66         "Renée Zellweger Renée~0.9 Zellweger~" };
67     fuzzyExpected = new String[] { "ubersetzung ubersetzung~0.9",
68         "motley crue motley~0.75 crue~0.5", "renee zellweger renee~0.9 zellweger~0.5" };
69
70     a = new ASCIIAnalyzer();
71   }
72
73   public void testWildCardQuery() throws ParseException {
74     for (int i = 0; i < wildcardInput.length; i++) {
75       assertEquals("Testing wildcards with analyzer " + a.getClass() + ", input string: "
76           + wildcardInput[i], wildcardExpected[i], parseWithAnalyzingQueryParser(wildcardInput[i], a));
77     }
78   }
79
80   public void testPrefixQuery() throws ParseException {
81     for (int i = 0; i < prefixInput.length; i++) {
82       assertEquals("Testing prefixes with analyzer " + a.getClass() + ", input string: "
83           + prefixInput[i], prefixExpected[i], parseWithAnalyzingQueryParser(prefixInput[i], a));
84     }
85   }
86
87   public void testRangeQuery() throws ParseException {
88     for (int i = 0; i < rangeInput.length; i++) {
89       assertEquals("Testing ranges with analyzer " + a.getClass() + ", input string: "
90           + rangeInput[i], rangeExpected[i], parseWithAnalyzingQueryParser(rangeInput[i], a));
91     }
92   }
93
94   public void testFuzzyQuery() throws ParseException {
95     for (int i = 0; i < fuzzyInput.length; i++) {
96       assertEquals("Testing fuzzys with analyzer " + a.getClass() + ", input string: "
97           + fuzzyInput[i], fuzzyExpected[i], parseWithAnalyzingQueryParser(fuzzyInput[i], a));
98     }
99   }
100
101   private String parseWithAnalyzingQueryParser(String s, Analyzer a) throws ParseException {
102     AnalyzingQueryParser qp = new AnalyzingQueryParser(TEST_VERSION_CURRENT, "field", a);
103     org.apache.lucene.search.Query q = qp.parse(s);
104     return q.toString("field");
105   }
106
107 }
108
109 final class ASCIIAnalyzer extends org.apache.lucene.analysis.Analyzer {
110   public ASCIIAnalyzer() {
111   }
112
113   @Override
114   public TokenStream tokenStream(String fieldName, Reader reader) {
115     TokenStream result = new StandardTokenizer(LuceneTestCase.TEST_VERSION_CURRENT, reader);
116     result = new StandardFilter(Version.LUCENE_31, result);
117     result = new ASCIIFoldingFilter(result);
118     result = new LowerCaseFilter(LuceneTestCase.TEST_VERSION_CURRENT, result);
119     return result;
120   }
121 }