Skip to content

Apache Lucene open-source search software

Add type to compound word (#16680)

Renato Haeberlie082cde759cf
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index cad59846c8d..b767db4e58c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -406,6 +406,10 @@ API Changes
New Features
---------------------
+* GITHUB#16680: Add a "subword" type to the subword tokens generated by
+ the compound word token filters, so downstream filters can distinguish
+ them from original terms. (Renato Haeberli)
+
* GITHUB#16418: Incremental doc-values updates. A set-only doc-values update (NUMERIC or BINARY) is written as a sparse
"delta" overlay holding just the updated documents and layered over the base column at read time, instead of
rewriting the whole column, turning per-update write amplification from O(column) into O(updated docs). Enabled by
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
index 97237664ae6..e2f9f5a5112 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
@@ -25,9 +25,13 @@ import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
+import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
/** Base class for decomposition token filters. */
public abstract class CompoundWordTokenFilterBase extends TokenFilter {
+ /** Type assigned to subword tokens generated by {@link #decompose()}. */
+ public static final String TOKEN_TYPE_SUBWORD = "subword";
+
/** The default for minimal word length that gets decomposed */
public static final int DEFAULT_MIN_WORD_SIZE = 5;
@@ -48,6 +52,7 @@ public abstract class CompoundWordTokenFilterBase extends TokenFilter {
protected final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
private final PositionIncrementAttribute posIncAtt =
addAttribute(PositionIncrementAttribute.class);
+ private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
private State current;
@@ -103,9 +108,12 @@ public abstract class CompoundWordTokenFilterBase extends TokenFilter {
assert current != null;
CompoundToken token = tokens.poll();
restoreState(current); // keep all other attributes untouched
+
termAtt.setEmpty().append(token.txt);
offsetAtt.setOffset(token.startOffset, token.endOffset);
posIncAtt.setPositionIncrement(0);
+ typeAtt.setType(TOKEN_TYPE_SUBWORD);
+
return true;
}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
index d25d77d350c..bf8b5cfee4b 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
@@ -261,6 +261,30 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
new int[] {1, 0, 0, 0, 0, 0});
}
+ public void testTypeOnExtractedWord() throws Exception {
+ CharArraySet dict = makeDictionary("Schlüssel", "Kasten");
+
+ DictionaryCompoundWordTokenFilter tf =
+ new DictionaryCompoundWordTokenFilter(
+ whitespaceMockTokenizer("Schlüsselkasten"),
+ dict,
+ CompoundWordTokenFilterBase.DEFAULT_MIN_WORD_SIZE,
+ CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
+ CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
+ true);
+
+ String[] output = {"Schlüsselkasten", "Schlüssel", "kasten"};
+ assertTokenStreamContents(
+ tf,
+ output,
+ new int[] {0, 0, 0},
+ new int[] {15, 15, 15},
+ new String[] {"word", "subword", "subword"},
+ new int[] {1, 0, 0},
+ null,
+ null);
+ }
+
public void testTokenEndingWithWordComponentOfMinimumLength() throws Exception {
CharArraySet dict = makeDictionary("ab", "cd", "ef");