pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / position / PositionFilterTest.java
1 package org.apache.lucene.analysis.position;
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 java.io.IOException;
21
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;
26
27 public class PositionFilterTest extends BaseTokenStreamTestCase {
28
29   public class TestTokenStream extends TokenStream {
30
31     protected int index = 0;
32     protected String[] testToken;
33     protected final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
34
35     public TestTokenStream(String[] testToken) {
36       super();
37       this.testToken = testToken;
38     }
39
40     @Override
41     public final boolean incrementToken() throws IOException {
42       clearAttributes();
43       if (index < testToken.length) {
44         termAtt.setEmpty().append(testToken[index++]);
45         return true;
46       } else {
47         return false;
48       }
49     }
50     @Override
51     public void reset() {
52       index = 0;
53     }
54   }
55
56   public static final String[] TEST_TOKEN = new String[]{
57     "please",
58     "divide",
59     "this",
60     "sentence",
61     "into",
62     "shingles",
63   };
64   public static final int[] TEST_TOKEN_POSITION_INCREMENTS = new int[]{
65     1, 0, 0, 0, 0, 0
66   };
67   public static final int[] TEST_TOKEN_NON_ZERO_POSITION_INCREMENTS = new int[]{
68     1, 5, 5, 5, 5, 5
69   };
70
71   public static final String[] SIX_GRAM_NO_POSITIONS_TOKENS = new String[]{
72     "please",
73     "please divide",
74     "please divide this",
75     "please divide this sentence",
76     "please divide this sentence into",
77     "please divide this sentence into shingles",
78     "divide",
79     "divide this",
80     "divide this sentence",
81     "divide this sentence into",
82     "divide this sentence into shingles",
83     "this",
84     "this sentence",
85     "this sentence into",
86     "this sentence into shingles",
87     "sentence",
88     "sentence into",
89     "sentence into shingles",
90     "into",
91     "into shingles",
92     "shingles",
93   };
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
96   };
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",
102     "word", "shingle",
103     "word"
104   };
105
106   public void testFilter() throws Exception {
107
108     assertTokenStreamContents(new PositionFilter(new TestTokenStream(TEST_TOKEN)),
109                TEST_TOKEN,
110                TEST_TOKEN_POSITION_INCREMENTS);
111   }
112
113   public void testNonZeroPositionIncrement() throws Exception {
114     
115     assertTokenStreamContents(new PositionFilter(new TestTokenStream(TEST_TOKEN), 5),
116                TEST_TOKEN,
117                TEST_TOKEN_NON_ZERO_POSITION_INCREMENTS);
118   }
119   
120   public void testReset() throws Exception {
121
122     PositionFilter filter = new PositionFilter(new TestTokenStream(TEST_TOKEN));
123     assertTokenStreamContents(filter, TEST_TOKEN, TEST_TOKEN_POSITION_INCREMENTS);
124     filter.reset();
125     // Make sure that the reset filter provides correct position increments
126     assertTokenStreamContents(filter, TEST_TOKEN, TEST_TOKEN_POSITION_INCREMENTS);
127   }
128   
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)
132    */
133   public void test6GramFilterNoPositions() throws Exception {
134
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);
139   }
140
141 }