1 package org.apache.lucene.analysis.position;
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 java.io.IOException;
22 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.shingle.ShingleFilter;
25 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
27 public class PositionFilterTest extends BaseTokenStreamTestCase {
29 public class TestTokenStream extends TokenStream {
31 protected int index = 0;
32 protected String[] testToken;
33 protected final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
35 public TestTokenStream(String[] testToken) {
37 this.testToken = testToken;
41 public final boolean incrementToken() throws IOException {
43 if (index < testToken.length) {
44 termAtt.setEmpty().append(testToken[index++]);
56 public static final String[] TEST_TOKEN = new String[]{
64 public static final int[] TEST_TOKEN_POSITION_INCREMENTS = new int[]{
67 public static final int[] TEST_TOKEN_NON_ZERO_POSITION_INCREMENTS = new int[]{
71 public static final String[] SIX_GRAM_NO_POSITIONS_TOKENS = new String[]{
75 "please divide this sentence",
76 "please divide this sentence into",
77 "please divide this sentence into shingles",
80 "divide this sentence",
81 "divide this sentence into",
82 "divide this sentence into shingles",
86 "this sentence into shingles",
89 "sentence into shingles",
94 public static final int[] SIX_GRAM_NO_POSITIONS_INCREMENTS = new int[]{
95 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
97 public static final String[] SIX_GRAM_NO_POSITIONS_TYPES = new String[]{
98 "word", "shingle", "shingle", "shingle", "shingle", "shingle",
99 "word", "shingle", "shingle", "shingle", "shingle",
100 "word", "shingle", "shingle", "shingle",
101 "word", "shingle", "shingle",
106 public void testFilter() throws Exception {
108 assertTokenStreamContents(new PositionFilter(new TestTokenStream(TEST_TOKEN)),
110 TEST_TOKEN_POSITION_INCREMENTS);
113 public void testNonZeroPositionIncrement() throws Exception {
115 assertTokenStreamContents(new PositionFilter(new TestTokenStream(TEST_TOKEN), 5),
117 TEST_TOKEN_NON_ZERO_POSITION_INCREMENTS);
120 public void testReset() throws Exception {
122 PositionFilter filter = new PositionFilter(new TestTokenStream(TEST_TOKEN));
123 assertTokenStreamContents(filter, TEST_TOKEN, TEST_TOKEN_POSITION_INCREMENTS);
125 // Make sure that the reset filter provides correct position increments
126 assertTokenStreamContents(filter, TEST_TOKEN, TEST_TOKEN_POSITION_INCREMENTS);
129 /** Tests ShingleFilter up to six shingles against six terms.
130 * Tests PositionFilter setting all but the first positionIncrement to zero.
131 * @throws java.io.IOException @see Token#next(Token)
133 public void test6GramFilterNoPositions() throws Exception {
135 ShingleFilter filter = new ShingleFilter(new TestTokenStream(TEST_TOKEN), 6);
136 assertTokenStreamContents(new PositionFilter(filter),
137 SIX_GRAM_NO_POSITIONS_TOKENS,
138 SIX_GRAM_NO_POSITIONS_INCREMENTS);