add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / query / QueryAutoStopWordAnalyzerTest.java
1 package org.apache.lucene.analysis.query;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.io.IOException;
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.TokenStream;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.index.IndexReader;
31 import org.apache.lucene.index.IndexWriter;
32 import org.apache.lucene.index.IndexWriterConfig;
33 import org.apache.lucene.index.Term;
34 import org.apache.lucene.queryParser.ParseException;
35 import org.apache.lucene.queryParser.QueryParser;
36 import org.apache.lucene.search.IndexSearcher;
37 import org.apache.lucene.search.Query;
38 import org.apache.lucene.store.RAMDirectory;
39
40 public class QueryAutoStopWordAnalyzerTest extends BaseTokenStreamTestCase {
41   String variedFieldValues[] = {"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "boring", "dog"};
42   String repetitiveFieldValues[] = {"boring", "boring", "vaguelyboring"};
43   RAMDirectory dir;
44   Analyzer appAnalyzer;
45   IndexReader reader;
46   QueryAutoStopWordAnalyzer protectedAnalyzer;
47
48   @Override
49   public void setUp() throws Exception {
50     super.setUp();
51     dir = new RAMDirectory();
52     appAnalyzer = new MockAnalyzer(random, MockTokenizer.WHITESPACE, false);
53     IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, appAnalyzer));
54     int numDocs = 200;
55     for (int i = 0; i < numDocs; i++) {
56       Document doc = new Document();
57       String variedFieldValue = variedFieldValues[i % variedFieldValues.length];
58       String repetitiveFieldValue = repetitiveFieldValues[i % repetitiveFieldValues.length];
59       doc.add(new Field("variedField", variedFieldValue, Field.Store.YES, Field.Index.ANALYZED));
60       doc.add(new Field("repetitiveField", repetitiveFieldValue, Field.Store.YES, Field.Index.ANALYZED));
61       writer.addDocument(doc);
62     }
63     writer.close();
64     reader = IndexReader.open(dir, true);
65     protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer);
66   }
67
68   @Override
69   public void tearDown() throws Exception {
70     reader.close();
71     super.tearDown();
72   }
73
74   //Helper method to query
75   private int search(Analyzer a, String queryString) throws IOException, ParseException {
76     QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "repetitiveField", a);
77     Query q = qp.parse(queryString);
78     IndexSearcher searcher = newSearcher(reader);
79     int hits = searcher.search(q, null, 1000).totalHits;
80     searcher.close();
81     return hits;
82   }
83
84   public void testUninitializedAnalyzer() throws Exception {
85     //Note: no calls to "addStopWord"
86     String query = "variedField:quick repetitiveField:boring";
87     int numHits1 = search(protectedAnalyzer, query);
88     int numHits2 = search(appAnalyzer, query);
89     assertEquals("No filtering test", numHits1, numHits2);
90   }
91
92   /*
93     * Test method for 'org.apache.lucene.analysis.QueryAutoStopWordAnalyzer.addStopWords(IndexReader)'
94     */
95   public void testDefaultAddStopWordsIndexReader() throws Exception {
96     protectedAnalyzer.addStopWords(reader);
97     int numHits = search(protectedAnalyzer, "repetitiveField:boring");
98     assertEquals("Default filter should remove all docs", 0, numHits);
99   }
100
101
102   /*
103     * Test method for 'org.apache.lucene.analysis.QueryAutoStopWordAnalyzer.addStopWords(IndexReader, int)'
104     */
105   public void testAddStopWordsIndexReaderInt() throws Exception {
106     protectedAnalyzer.addStopWords(reader, 1f / 2f);
107     int numHits = search(protectedAnalyzer, "repetitiveField:boring");
108     assertEquals("A filter on terms in > one half of docs remove boring docs", 0, numHits);
109
110     numHits = search(protectedAnalyzer, "repetitiveField:vaguelyboring");
111     assertTrue("A filter on terms in > half of docs should not remove vaguelyBoring docs", numHits > 1);
112
113     protectedAnalyzer.addStopWords(reader, 1f / 4f);
114     numHits = search(protectedAnalyzer, "repetitiveField:vaguelyboring");
115     assertEquals("A filter on terms in > quarter of docs should remove vaguelyBoring docs", 0, numHits);
116   }
117
118
119   public void testAddStopWordsIndexReaderStringFloat() throws Exception {
120     protectedAnalyzer.addStopWords(reader, "variedField", 1f / 2f);
121     int numHits = search(protectedAnalyzer, "repetitiveField:boring");
122     assertTrue("A filter on one Field should not affect queris on another", numHits > 0);
123
124     protectedAnalyzer.addStopWords(reader, "repetitiveField", 1f / 2f);
125     numHits = search(protectedAnalyzer, "repetitiveField:boring");
126     assertEquals("A filter on the right Field should affect queries on it", numHits, 0);
127   }
128
129   public void testAddStopWordsIndexReaderStringInt() throws Exception {
130     int numStopWords = protectedAnalyzer.addStopWords(reader, "repetitiveField", 10);
131     assertTrue("Should have identified stop words", numStopWords > 0);
132
133     Term[] t = protectedAnalyzer.getStopWords();
134     assertEquals("num terms should = num stopwords returned", t.length, numStopWords);
135
136     int numNewStopWords = protectedAnalyzer.addStopWords(reader, "variedField", 10);
137     assertTrue("Should have identified more stop words", numNewStopWords > 0);
138     t = protectedAnalyzer.getStopWords();
139     assertEquals("num terms should = num stopwords returned", t.length, numStopWords + numNewStopWords);
140   }
141
142   public void testNoFieldNamePollution() throws Exception {
143     protectedAnalyzer.addStopWords(reader, "repetitiveField", 10);
144     int numHits = search(protectedAnalyzer, "repetitiveField:boring");
145     assertEquals("Check filter set up OK", 0, numHits);
146
147     numHits = search(protectedAnalyzer, "variedField:boring");
148     assertTrue("Filter should not prevent stopwords in one field being used in another ", numHits > 0);
149
150   }
151   
152   /*
153    * analyzer that does not support reuse
154    * it is LetterTokenizer on odd invocations, WhitespaceTokenizer on even.
155    */
156   private class NonreusableAnalyzer extends Analyzer {
157     int invocationCount = 0;
158     @Override
159     public TokenStream tokenStream(String fieldName, Reader reader) {
160       if (++invocationCount % 2 == 0)
161         return new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
162       else
163         return new MockTokenizer(reader, MockTokenizer.SIMPLE, false);
164     }
165   }
166   
167   public void testWrappingNonReusableAnalyzer() throws Exception {
168     QueryAutoStopWordAnalyzer a = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, new NonreusableAnalyzer());
169     a.addStopWords(reader, 10);
170     int numHits = search(a, "repetitiveField:boring");
171     assertTrue(numHits == 0);
172     numHits = search(a, "repetitiveField:vaguelyboring");
173     assertTrue(numHits == 0);
174   }
175   
176   public void testTokenStream() throws Exception {
177     QueryAutoStopWordAnalyzer a = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false));
178     a.addStopWords(reader, 10);
179     TokenStream ts = a.tokenStream("repetitiveField", new StringReader("this boring"));
180     assertTokenStreamContents(ts, new String[] { "this" });
181   }
182 }