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 EdgeNGramTokenizer} for correctness.
28 public class EdgeNGramTokenizerTest 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 EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 0, 0);
41 } catch (IllegalArgumentException e) {
44 assertTrue(gotException);
47 public void testInvalidInput2() throws Exception {
48 boolean gotException = false;
50 new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 2, 1);
51 } catch (IllegalArgumentException e) {
54 assertTrue(gotException);
57 public void testInvalidInput3() throws Exception {
58 boolean gotException = false;
60 new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, -1, 2);
61 } catch (IllegalArgumentException e) {
64 assertTrue(gotException);
67 public void testFrontUnigram() throws Exception {
68 EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 1, 1);
69 assertTokenStreamContents(tokenizer, new String[]{"a"}, new int[]{0}, new int[]{1}, 5 /* abcde */);
72 public void testBackUnigram() throws Exception {
73 EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.BACK, 1, 1);
74 assertTokenStreamContents(tokenizer, new String[]{"e"}, new int[]{4}, new int[]{5}, 5 /* abcde */);
77 public void testOversizedNgrams() throws Exception {
78 EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 6, 6);
79 assertTokenStreamContents(tokenizer, new String[0], new int[0], new int[0], 5 /* abcde */);
82 public void testFrontRangeOfNgrams() throws Exception {
83 EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 1, 3);
84 assertTokenStreamContents(tokenizer, new String[]{"a","ab","abc"}, new int[]{0,0,0}, new int[]{1,2,3}, 5 /* abcde */);
87 public void testBackRangeOfNgrams() throws Exception {
88 EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.BACK, 1, 3);
89 assertTokenStreamContents(tokenizer, new String[]{"e","de","cde"}, new int[]{4,3,2}, new int[]{5,5,5}, 5 /* abcde */);
92 public void testReset() throws Exception {
93 EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 1, 3);
94 assertTokenStreamContents(tokenizer, new String[]{"a","ab","abc"}, new int[]{0,0,0}, new int[]{1,2,3}, 5 /* abcde */);
95 tokenizer.reset(new StringReader("abcde"));
96 assertTokenStreamContents(tokenizer, new String[]{"a","ab","abc"}, new int[]{0,0,0}, new int[]{1,2,3}, 5 /* abcde */);