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.
21
import java.io.StringReader;
23
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
26
* Tests {@link NGramTokenizer} for correctness.
28
public class NGramTokenizerTest extends BaseTokenStreamTestCase {
29
private StringReader input;
32
public void setUp() throws Exception {
34
input = new StringReader("abcde");
37
public void testInvalidInput() throws Exception {
38
boolean gotException = false;
40
new NGramTokenizer(input, 2, 1);
41
} catch (IllegalArgumentException e) {
44
assertTrue(gotException);
47
public void testInvalidInput2() throws Exception {
48
boolean gotException = false;
50
new NGramTokenizer(input, 0, 1);
51
} catch (IllegalArgumentException e) {
54
assertTrue(gotException);
57
public void testUnigrams() throws Exception {
58
NGramTokenizer tokenizer = new NGramTokenizer(input, 1, 1);
59
assertTokenStreamContents(tokenizer, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5}, 5 /* abcde */);
62
public void testBigrams() throws Exception {
63
NGramTokenizer tokenizer = new NGramTokenizer(input, 2, 2);
64
assertTokenStreamContents(tokenizer, new String[]{"ab","bc","cd","de"}, new int[]{0,1,2,3}, new int[]{2,3,4,5}, 5 /* abcde */);
67
public void testNgrams() throws Exception {
68
NGramTokenizer tokenizer = new NGramTokenizer(input, 1, 3);
69
assertTokenStreamContents(tokenizer,
70
new String[]{"a","b","c","d","e", "ab","bc","cd","de", "abc","bcd","cde"},
71
new int[]{0,1,2,3,4, 0,1,2,3, 0,1,2},
72
new int[]{1,2,3,4,5, 2,3,4,5, 3,4,5},
77
public void testOversizedNgrams() throws Exception {
78
NGramTokenizer tokenizer = new NGramTokenizer(input, 6, 7);
79
assertTokenStreamContents(tokenizer, new String[0], new int[0], new int[0], 5 /* abcde */);
82
public void testReset() throws Exception {
83
NGramTokenizer tokenizer = new NGramTokenizer(input, 1, 1);
84
assertTokenStreamContents(tokenizer, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5}, 5 /* abcde */);
85
tokenizer.reset(new StringReader("abcde"));
86
assertTokenStreamContents(tokenizer, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5}, 5 /* abcde */);