1 package org.apache.lucene.analysis.ngram;
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 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;
25 import java.io.StringReader;
28 * Tests {@link NGramTokenFilter} for correctness.
30 public class NGramTokenFilterTest extends BaseTokenStreamTestCase {
31 private TokenStream input;
34 public void setUp() throws Exception {
36 input = new MockTokenizer(new StringReader("abcde"), MockTokenizer.WHITESPACE, false);
39 public void testInvalidInput() throws Exception {
40 boolean gotException = false;
42 new NGramTokenFilter(input, 2, 1);
43 } catch (IllegalArgumentException e) {
46 assertTrue(gotException);
49 public void testInvalidInput2() throws Exception {
50 boolean gotException = false;
52 new NGramTokenFilter(input, 0, 1);
53 } catch (IllegalArgumentException e) {
56 assertTrue(gotException);
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});
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});
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}
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]);
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});
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});