001: package org.apache.lucene.queryParser.analyzing;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.Reader;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.lucene.analysis.Analyzer;
025: import org.apache.lucene.analysis.ISOLatin1AccentFilter;
026: import org.apache.lucene.analysis.LowerCaseFilter;
027: import org.apache.lucene.analysis.TokenStream;
028: import org.apache.lucene.analysis.standard.StandardFilter;
029: import org.apache.lucene.analysis.standard.StandardTokenizer;
030: import org.apache.lucene.queryParser.ParseException;
031:
032: /**
033: * @author Ronnie Kolehmainen (ronnie.kolehmainen at ub.uu.se)
034: * @version $Revision$, $Date$
035: */
036: public class TestAnalyzingQueryParser extends TestCase {
037:
038: private Analyzer a;
039:
040: private String[] wildcardInput;
041: private String[] wildcardExpected;
042: private String[] prefixInput;
043: private String[] prefixExpected;
044: private String[] rangeInput;
045: private String[] rangeExpected;
046: private String[] fuzzyInput;
047: private String[] fuzzyExpected;
048:
049: public void setUp() {
050: wildcardInput = new String[] { "übersetzung über*ung",
051: "Mötley Cr\u00fce Mötl?* Crü?",
052: "Renée Zellweger Ren?? Zellw?ger" };
053: wildcardExpected = new String[] { "ubersetzung uber*ung",
054: "motley crue motl?* cru?",
055: "renee zellweger ren?? zellw?ger" };
056:
057: prefixInput = new String[] { "übersetzung übersetz*",
058: "Mötley Crüe Mötl* crü*", "René? Zellw*" };
059: prefixExpected = new String[] { "ubersetzung ubersetz*",
060: "motley crue motl* cru*", "rene? zellw*" };
061:
062: rangeInput = new String[] { "[aa TO bb]", "{Anaïs TO Zoé}" };
063: rangeExpected = new String[] { "[aa TO bb]", "{anais TO zoe}" };
064:
065: fuzzyInput = new String[] { "Übersetzung Übersetzung~0.9",
066: "Mötley Crüe Mötley~0.75 Crüe~0.5",
067: "Renée Zellweger Renée~0.9 Zellweger~" };
068: fuzzyExpected = new String[] { "ubersetzung ubersetzung~0.9",
069: "motley crue motley~0.75 crue~0.5",
070: "renee zellweger renee~0.9 zellweger~0.5" };
071:
072: a = new ASCIIAnalyzer();
073: }
074:
075: public void testWildCardQuery() throws ParseException {
076: for (int i = 0; i < wildcardInput.length; i++) {
077: assertEquals("Testing wildcards with analyzer "
078: + a.getClass() + ", input string: "
079: + wildcardInput[i], wildcardExpected[i],
080: parseWithAnalyzingQueryParser(wildcardInput[i], a));
081: }
082: }
083:
084: public void testPrefixQuery() throws ParseException {
085: for (int i = 0; i < prefixInput.length; i++) {
086: assertEquals("Testing prefixes with analyzer "
087: + a.getClass() + ", input string: "
088: + prefixInput[i], prefixExpected[i],
089: parseWithAnalyzingQueryParser(prefixInput[i], a));
090: }
091: }
092:
093: public void testRangeQuery() throws ParseException {
094: for (int i = 0; i < rangeInput.length; i++) {
095: assertEquals("Testing ranges with analyzer " + a.getClass()
096: + ", input string: " + rangeInput[i],
097: rangeExpected[i], parseWithAnalyzingQueryParser(
098: rangeInput[i], a));
099: }
100: }
101:
102: public void testFuzzyQuery() throws ParseException {
103: for (int i = 0; i < fuzzyInput.length; i++) {
104: assertEquals("Testing fuzzys with analyzer " + a.getClass()
105: + ", input string: " + fuzzyInput[i],
106: fuzzyExpected[i], parseWithAnalyzingQueryParser(
107: fuzzyInput[i], a));
108: }
109: }
110:
111: private String parseWithAnalyzingQueryParser(String s, Analyzer a)
112: throws ParseException {
113: AnalyzingQueryParser qp = new AnalyzingQueryParser("field", a);
114: org.apache.lucene.search.Query q = qp.parse(s);
115: return q.toString("field");
116: }
117:
118: }
119:
120: class ASCIIAnalyzer extends org.apache.lucene.analysis.Analyzer {
121: public ASCIIAnalyzer() {
122: }
123:
124: public TokenStream tokenStream(String fieldName, Reader reader) {
125: TokenStream result = new StandardTokenizer(reader);
126: result = new StandardFilter(result);
127: result = new ISOLatin1AccentFilter(result);
128: result = new LowerCaseFilter(result);
129: return result;
130: }
131: }
|