pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / ngram / NGramTokenFilterTest.java
1 package org.apache.lucene.analysis.ngram;
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 org.apache.lucene.analysis.MockTokenizer;
21 import org.apache.lucene.analysis.TokenStream;
22 import org.apache.lucene.analysis.WhitespaceTokenizer;
23 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
24
25 import java.io.StringReader;
26
27 /**
28  * Tests {@link NGramTokenFilter} for correctness.
29  */
30 public class NGramTokenFilterTest extends BaseTokenStreamTestCase {
31     private TokenStream input;
32     
33     @Override
34     public void setUp() throws Exception {
35         super.setUp();
36         input = new MockTokenizer(new StringReader("abcde"), MockTokenizer.WHITESPACE, false);
37     }
38
39     public void testInvalidInput() throws Exception {
40         boolean gotException = false;
41         try {        
42             new NGramTokenFilter(input, 2, 1);
43         } catch (IllegalArgumentException e) {
44             gotException = true;
45         }
46         assertTrue(gotException);
47     }
48
49     public void testInvalidInput2() throws Exception {
50         boolean gotException = false;
51         try {        
52             new NGramTokenFilter(input, 0, 1);
53         } catch (IllegalArgumentException e) {
54             gotException = true;
55         }
56         assertTrue(gotException);
57     }
58
59     public void testUnigrams() throws Exception {
60       NGramTokenFilter filter = new NGramTokenFilter(input, 1, 1);
61       assertTokenStreamContents(filter, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5});
62     }
63
64     public void testBigrams() throws Exception {
65       NGramTokenFilter filter = new NGramTokenFilter(input, 2, 2);
66       assertTokenStreamContents(filter, new String[]{"ab","bc","cd","de"}, new int[]{0,1,2,3}, new int[]{2,3,4,5});
67     }
68
69     public void testNgrams() throws Exception {
70       NGramTokenFilter filter = new NGramTokenFilter(input, 1, 3);
71       assertTokenStreamContents(filter,
72         new String[]{"a","b","c","d","e", "ab","bc","cd","de", "abc","bcd","cde"}, 
73         new int[]{0,1,2,3,4, 0,1,2,3, 0,1,2},
74         new int[]{1,2,3,4,5, 2,3,4,5, 3,4,5}
75       );
76     }
77
78     public void testOversizedNgrams() throws Exception {
79       NGramTokenFilter filter = new NGramTokenFilter(input, 6, 7);
80       assertTokenStreamContents(filter, new String[0], new int[0], new int[0]);
81     }
82     
83     public void testSmallTokenInStream() throws Exception {
84       input = new MockTokenizer(new StringReader("abc de fgh"), MockTokenizer.WHITESPACE, false);
85       NGramTokenFilter filter = new NGramTokenFilter(input, 3, 3);
86       assertTokenStreamContents(filter, new String[]{"abc","fgh"}, new int[]{0,7}, new int[]{3,10});
87     }
88     
89     public void testReset() throws Exception {
90       WhitespaceTokenizer tokenizer = new WhitespaceTokenizer(TEST_VERSION_CURRENT, new StringReader("abcde"));
91       NGramTokenFilter filter = new NGramTokenFilter(tokenizer, 1, 1);
92       assertTokenStreamContents(filter, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5});
93       tokenizer.reset(new StringReader("abcde"));
94       assertTokenStreamContents(filter, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5});
95     }
96 }