Apache Lucene open-source search software
LUCENE-3671: Add TypeTokenFilter that filters tokens based on their TypeAttribute
Uwe Schindleraf9b4d816f6a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 61416ef1f2f..c05942fdeb5 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -790,6 +790,9 @@ New Featuresinput mapping to it) for FSTs that have strictly monotonic longoutputs (such as an ord). (Mike McCandless)+* LUCENE-3121: Add TypeTokenFilter that filters tokens based on+ their TypeAttribute. (Tommaso Teofili via Uwe Schindler)+Bug fixes* LUCENE-3595: Fixed FieldCacheRangeFilter and FieldCacheTermsFilterdiff --git a/modules/analysis/common/src/java/org/apache/lucene/analysis/core/TypeTokenFilter.java b/modules/analysis/common/src/java/org/apache/lucene/analysis/core/TypeTokenFilter.javanew file mode 100644index 00000000000..16d786f0475--- /dev/null+++ b/modules/analysis/common/src/java/org/apache/lucene/analysis/core/TypeTokenFilter.java@@ -0,0 +1,47 @@+package org.apache.lucene.analysis.core;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.TypeAttribute;+import org.apache.lucene.analysis.util.FilteringTokenFilter;++import java.io.IOException;+import java.util.Set;++/**+ * Removes tokens whose types appear in a set of blocked types from a token stream.+ */+public final class TypeTokenFilter extends FilteringTokenFilter {++ private final Set<String> stopTypes;+ private final TypeAttribute typeAttribute = addAttribute(TypeAttribute.class);++ public TypeTokenFilter(boolean enablePositionIncrements, TokenStream input, Set<String> stopTypes) {+ super(enablePositionIncrements, input);+ this.stopTypes = stopTypes;+ }++ /**+ * Returns the next input Token whose typeAttribute.type() is not a stop type.+ */+ @Override+ protected boolean accept() throws IOException {+ return !stopTypes.contains(typeAttribute.type());+ }+}diff --git a/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilter.java b/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilter.javanew file mode 100644index 00000000000..36b547c4c65--- /dev/null+++ b/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestTypeTokenFilter.java@@ -0,0 +1,92 @@+package org.apache.lucene.analysis.core;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.lucene.analysis.BaseTokenStreamTestCase;+import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.standard.StandardTokenizer;+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;+import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;+import org.apache.lucene.analysis.tokenattributes.TypeAttribute;+import org.apache.lucene.util.English;++import java.io.IOException;+import java.io.StringReader;+import java.util.Set;+++public class TestTypeTokenFilter extends BaseTokenStreamTestCase {++ public void testTypeFilter() throws IOException {+ StringReader reader = new StringReader("121 is palindrome, while 123 is not");+ Set<String> stopTypes = asSet("<NUM>");+ TokenStream stream = new TypeTokenFilter(true, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopTypes);+ assertTokenStreamContents(stream, new String[]{"is", "palindrome", "while", "is", "not"});+ }++ /**+ * Test Position increments applied by TypeTokenFilter with and without enabling this option.+ */+ public void testStopPositons() throws IOException {+ StringBuilder sb = new StringBuilder();+ for (int i = 10; i < 20; i++) {+ if (i % 3 != 0) {+ sb.append(i).append(" ");+ } else {+ String w = English.intToEnglish(i).trim();+ sb.append(w).append(" ");+ }+ }+ log(sb.toString());+ String stopTypes[] = new String[]{"<NUM>"};+ Set<String> stopSet = asSet(stopTypes);++ // with increments+ StringReader reader = new StringReader(sb.toString());+ TypeTokenFilter typeTokenFilter = new TypeTokenFilter(true, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet);+ testPositons(typeTokenFilter);++ // without increments+ reader = new StringReader(sb.toString());+ typeTokenFilter = new TypeTokenFilter(false, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet);+ testPositons(typeTokenFilter);++ }++ private void testPositons(TypeTokenFilter stpf) throws IOException {+ TypeAttribute typeAtt = stpf.getAttribute(TypeAttribute.class);+ CharTermAttribute termAttribute = stpf.getAttribute(CharTermAttribute.class);+ PositionIncrementAttribute posIncrAtt = stpf.getAttribute(PositionIncrementAttribute.class);+ stpf.reset();+ boolean enablePositionIncrements = stpf.getEnablePositionIncrements();+ while (stpf.incrementToken()) {+ log("Token: " + termAttribute.toString() + ": " + typeAtt.type() + " - " + posIncrAtt.getPositionIncrement());+ assertEquals("if position increment is enabled the positionIncrementAttribute value should be 3, otherwise 1",+ posIncrAtt.getPositionIncrement(), enablePositionIncrements ? 3 : 1);+ }+ stpf.end();+ stpf.close();+ }++ // print debug info depending on VERBOSE+ private static void log(String s) {+ if (VERBOSE) {+ System.out.println(s);+ }+ }+}