Apache Lucene open-source search software
[LUCENE-3744] - applied patch for whiteList usage in TypeTokenFilter
Tommaso Teofili6d3bb736f36d
diff --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.javaindex 16d786f0475..f564af2212b 100644--- 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.java@@ -31,17 +31,24 @@ public final class TypeTokenFilter extends FilteringTokenFilter {private final Set<String> stopTypes;private final TypeAttribute typeAttribute = addAttribute(TypeAttribute.class);+ private final boolean useWhiteList;- public TypeTokenFilter(boolean enablePositionIncrements, TokenStream input, Set<String> stopTypes) {+ public TypeTokenFilter(boolean enablePositionIncrements, TokenStream input, Set<String> stopTypes, boolean useWhiteList) {super(enablePositionIncrements, input);this.stopTypes = stopTypes;+ this.useWhiteList = useWhiteList;+ }++ public TypeTokenFilter(boolean enablePositionIncrements, TokenStream input, Set<String> stopTypes) {+ this(enablePositionIncrements, input, stopTypes, false);}/**- * Returns the next input Token whose typeAttribute.type() is not a stop type.+ * By default accept the token if its type is not a stop type.+ * When the useWhiteList parameter is set to true then accept the token if its type is contained in the stopTypes*/@Overrideprotected boolean accept() throws IOException {- return !stopTypes.contains(typeAttribute.type());+ return useWhiteList == 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.javaindex 36b547c4c65..6479fb9f372 100644--- 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.java@@ -27,6 +27,7 @@ import org.apache.lucene.util.English;import java.io.IOException;import java.io.StringReader;+import java.util.Collections;import java.util.Set;@@ -83,6 +84,13 @@ public class TestTypeTokenFilter extends BaseTokenStreamTestCase {stpf.close();}+ public void testTypeFilterWhitelist() throws IOException {+ StringReader reader = new StringReader("121 is palindrome, while 123 is not");+ Set<String> stopTypes = Collections.singleton("<NUM>");+ TokenStream stream = new TypeTokenFilter(true, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopTypes, true);+ assertTokenStreamContents(stream, new String[]{"121", "123"});+ }+// print debug info depending on VERBOSEprivate static void log(String s) {if (VERBOSE) {