Apache Solr open-source search software
LUCENE-2198: Support protected words in stemming TokenFilters using a new KeywordAttribute
Uwe Schindlere9a979f1eb80
diff --git a/CHANGES.txt b/CHANGES.txtindex 4a6483c89ec..4954f45004d 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -125,6 +125,9 @@ New featuresstopwords, and implement many analyzers in contrib with it.(Simon Willnauer via Robert Muir)+* LUCENE-2198: Support protected words in stemming TokenFilters using a+ new KeywordAttribute. (Simon Willnauer via Uwe Schindler)+Optimizations* LUCENE-2086: When resolving deleted terms, do so in term sort orderdiff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicAnalyzer.javaindex 7b17e6fad1b..eae5bafa2e7 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicAnalyzer.java@@ -26,6 +26,8 @@ import java.util.Set;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @link+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.StopwordAnalyzerBase;import org.apache.lucene.analysis.TokenStream;@@ -93,6 +95,8 @@ public final class ArabicAnalyzer extends StopwordAnalyzerBase {}}}++ private final Set<?> stemExclusionSet;/*** Builds an analyzer with the default stop words: {@link #DEFAULT_STOPWORD_FILE}.@@ -110,7 +114,25 @@ public final class ArabicAnalyzer extends StopwordAnalyzerBase {* a stopword set*/public ArabicAnalyzer(Version matchVersion, Set<?> stopwords){+ this(matchVersion, stopwords, CharArraySet.EMPTY_SET);+ }++ /**+ * Builds an analyzer with the given stop word. If a none-empty stem exclusion set is+ * provided this analyzer will add a {@link KeywordMarkerTokenFilter} before+ * {@link ArabicStemFilter}.+ *+ * @param matchVersion+ * lucene compatibility version+ * @param stopwords+ * a stopword set+ * @param stemExclusionSet+ * a set of terms not to be stemmed+ */+ public ArabicAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){super(matchVersion, stopwords);+ this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(+ matchVersion, stemExclusionSet));}/**@@ -145,7 +167,8 @@ public final class ArabicAnalyzer extends StopwordAnalyzerBase {* Creates {@link TokenStreamComponents} used to tokenize all the text in the provided {@link Reader}.** @return {@link TokenStreamComponents} built from an {@link ArabicLetterTokenizer} filtered with- * {@link LowerCaseFilter}, {@link StopFilter}, {@link ArabicNormalizationFilter}+ * {@link LowerCaseFilter}, {@link StopFilter}, {@link ArabicNormalizationFilter},+ * {@link KeywordMarkerTokenFilter} if a stem exclusion set is provided* and {@link ArabicStemFilter}.*/@Override@@ -155,7 +178,11 @@ public final class ArabicAnalyzer extends StopwordAnalyzerBase {TokenStream result = new LowerCaseFilter(matchVersion, source);// the order here is important: the stopword list is not normalized!result = new StopFilter( matchVersion, result, stopwords);+ // TODO maybe we should make ArabicNormalization filter also KeywordAttribute aware?!result = new ArabicNormalizationFilter(result);+ if(!stemExclusionSet.isEmpty()) {+ result = new KeywordMarkerTokenFilter(result, stemExclusionSet);+ }return new TokenStreamComponents(source, new ArabicStemFilter(result));}}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicStemFilter.javaindex 566d8821543..298726257a2 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ar/ArabicStemFilter.java@@ -19,31 +19,41 @@ package org.apache.lucene.analysis.ar;import java.io.IOException;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/*** A {@link TokenFilter} that applies {@link ArabicStemmer} to stem Arabic words..- *- */+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>+ * @see KeywordMarkerTokenFilter */public final class ArabicStemFilter extends TokenFilter {private final ArabicStemmer stemmer;private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public ArabicStemFilter(TokenStream input) {super(input);stemmer = new ArabicStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}@Overridepublic boolean incrementToken() throws IOException {if (input.incrementToken()) {- int newlen = stemmer.stem(termAtt.termBuffer(), termAtt.termLength());- termAtt.setTermLength(newlen);+ if(!keywordAttr.isKeyword()) {+ final int newlen = stemmer.stem(termAtt.termBuffer(), termAtt.termLength());+ termAtt.setTermLength(newlen);+ }return true;} else {return false;diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianAnalyzer.javaindex 2c2aba65b7c..9df60a51435 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianAnalyzer.java@@ -25,6 +25,8 @@ import java.util.Set;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @link+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.StopwordAnalyzerBase;import org.apache.lucene.analysis.TokenStream;@@ -88,6 +90,8 @@ public final class BulgarianAnalyzer extends StopwordAnalyzerBase {}}}++ private final Set<?> stemExclusionSet;/*** Builds an analyzer with the default stop words:@@ -101,16 +105,27 @@ public final class BulgarianAnalyzer extends StopwordAnalyzerBase {* Builds an analyzer with the given stop words.*/public BulgarianAnalyzer(Version matchVersion, Set<?> stopwords) {- super(matchVersion, stopwords);+ this(matchVersion, stopwords, CharArraySet.EMPTY_SET);}+ /**+ * Builds an analyzer with the given stop words and a stem exclusion set.+ * If a stem exclusion set is provided this analyzer will add a {@link KeywordMarkerTokenFilter}+ * before {@link BulgarianStemFilter}.+ */+ public BulgarianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet) {+ super(matchVersion, stopwords);+ this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(+ matchVersion, stemExclusionSet)); }+/*** Creates a {@link TokenStreamComponents} which tokenizes all the text in the provided* {@link Reader}.** @return A {@link TokenStreamComponents} built from an {@link StandardTokenizer}* filtered with {@link StandardFilter}, {@link LowerCaseFilter},- * {@link StopFilter}, and {@link BulgarianStemFilter}.+ * {@link StopFilter}, {@link KeywordMarkerTokenFilter} if a stem+ * exclusion set is provided and {@link BulgarianStemFilter}.*/@Overridepublic TokenStreamComponents createComponents(String fieldName, Reader reader) {@@ -118,6 +133,8 @@ public final class BulgarianAnalyzer extends StopwordAnalyzerBase {TokenStream result = new StandardFilter(source);result = new LowerCaseFilter(matchVersion, result);result = new StopFilter(matchVersion, result, stopwords);+ if(!stemExclusionSet.isEmpty())+ result = new KeywordMarkerTokenFilter(result, stemExclusionSet);result = new BulgarianStemFilter(result);return new TokenStreamComponents(source, result);}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianStemFilter.javaindex b2bfc408c2f..abf19aff9c9 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/bg/BulgarianStemFilter.java@@ -19,29 +19,40 @@ package org.apache.lucene.analysis.bg;import java.io.IOException;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter; // for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/*** A {@link TokenFilter} that applies {@link BulgarianStemmer} to stem Bulgarian* words.+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>*/public final class BulgarianStemFilter extends TokenFilter {private final BulgarianStemmer stemmer;private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public BulgarianStemFilter(final TokenStream input) {super(input);stemmer = new BulgarianStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}@Overridepublic boolean incrementToken() throws IOException {if (input.incrementToken()) {- final int newlen = stemmer.stem(termAtt.termBuffer(), termAtt.termLength());- termAtt.setTermLength(newlen);+ if(!keywordAttr.isKeyword()) {+ final int newlen = stemmer.stem(termAtt.termBuffer(), termAtt.termLength());+ termAtt.setTermLength(newlen);+ }return true;} else {return false;diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianAnalyzer.javaindex 351c6b9c9bb..fc064f33bb1 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianAnalyzer.java@@ -30,6 +30,7 @@ import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @link+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.StopwordAnalyzerBase;import org.apache.lucene.analysis.TokenStream;@@ -204,8 +205,9 @@ public final class BrazilianAnalyzer extends StopwordAnalyzerBase {TokenStream result = new LowerCaseFilter(matchVersion, source);result = new StandardFilter(result);result = new StopFilter(matchVersion, result, stopwords);- return new TokenStreamComponents(source, new BrazilianStemFilter(result,- excltable));+ if(excltable != null && !excltable.isEmpty())+ result = new KeywordMarkerTokenFilter(result, excltable);+ return new TokenStreamComponents(source, new BrazilianStemFilter(result));}}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianStemFilter.javaindex 9d5faf826fa..d691cd4a1db 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/br/BrazilianStemFilter.java@@ -20,13 +20,21 @@ package org.apache.lucene.analysis.br;import java.io.IOException;import java.util.Set;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter; // for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/*** A {@link TokenFilter} that applies {@link BrazilianStemmer}.- *+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>+ * @see KeywordMarkerTokenFilter+ **/public final class BrazilianStemFilter extends TokenFilter {@@ -34,16 +42,31 @@ public final class BrazilianStemFilter extends TokenFilter {* {@link BrazilianStemmer} in use by this filter.*/private BrazilianStemmer stemmer = null;- private Set exclusions = null;- private TermAttribute termAtt;-+ private Set<?> exclusions = null;+ private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;++ /**+ * Creates a new BrazilianStemFilter+ *+ * @param in the source {@link TokenStream}+ */public BrazilianStemFilter(TokenStream in) {super(in);stemmer = new BrazilianStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}-- public BrazilianStemFilter(TokenStream in, Set exclusiontable) {++ /**+ * Creates a new BrazilianStemFilter+ *+ * @param in the source {@link TokenStream}+ * @param exclusiontable a set of terms that should be prevented from being stemmed.+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.+ */+ @Deprecated+ public BrazilianStemFilter(TokenStream in, Set<?> exclusiontable) {this(in);this.exclusions = exclusiontable;}@@ -51,10 +74,10 @@ public final class BrazilianStemFilter extends TokenFilter {@Overridepublic boolean incrementToken() throws IOException {if (input.incrementToken()) {- String term = termAtt.term();+ final String term = termAtt.term();// Check the exclusion table.- if (exclusions == null || !exclusions.contains(term)) {- String s = stemmer.stem(term);+ if (!keywordAttr.isKeyword() && (exclusions == null || !exclusions.contains(term))) {+ final String s = stemmer.stem(term);// If not stemmed, don't waste the time adjusting the token.if ((s != null) && !s.equals(term))termAtt.setTermBuffer(s);diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechAnalyzer.javaindex ffce33463e3..c757f32ab7c 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechAnalyzer.java@@ -21,6 +21,7 @@ import org.apache.lucene.analysis.ReusableAnalyzerBase;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @linkimport org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.TokenStream;@@ -105,6 +106,7 @@ public final class CzechAnalyzer extends ReusableAnalyzerBase {// TODO once loadStopWords is gone those member should be removed too in favor of StopwordAnalyzerBaseprivate Set<?> stoptable;private final Version matchVersion;+ private final Set<?> stemExclusionTable;/*** Builds an analyzer with the default stop words ({@link #CZECH_STOP_WORDS}).@@ -124,8 +126,22 @@ public final class CzechAnalyzer extends ReusableAnalyzerBase {* @param stopwords a stopword set*/public CzechAnalyzer(Version matchVersion, Set<?> stopwords) {+ this(matchVersion, stopwords, CharArraySet.EMPTY_SET);+ }++ /**+ * Builds an analyzer with the given stop words and a set of work to be+ * excluded from the {@link CzechStemFilter}.+ *+ * @param matchVersion Lucene version to match See+ * {@link <a href="#version">above</a>}+ * @param stopwords a stopword set+ * @param a stemming exclusion set+ */+ public CzechAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionTable) {this.matchVersion = matchVersion;this.stoptable = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stopwords));+ this.stemExclusionTable = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionTable));}@@ -207,7 +223,9 @@ public final class CzechAnalyzer extends ReusableAnalyzerBase {* @return {@link TokenStreamComponents} built from a {@link StandardTokenizer}* filtered with {@link StandardFilter}, {@link LowerCaseFilter},* {@link StopFilter}, and {@link CzechStemFilter} (only if version is- * >= LUCENE_31)+ * >= LUCENE_31). If a version is >= LUCENE_31 and a stem exclusion set+ * is provided via {@link #CzechAnalyzer(Version, Set, Set)} a+ * {@link KeywordMarkerTokenFilter} is added before {@link CzechStemFilter}.*/@Overrideprotected TokenStreamComponents createComponents(String fieldName,@@ -216,8 +234,11 @@ public final class CzechAnalyzer extends ReusableAnalyzerBase {TokenStream result = new StandardFilter(source);result = new LowerCaseFilter(matchVersion, result);result = new StopFilter( matchVersion, result, stoptable);- if (matchVersion.onOrAfter(Version.LUCENE_31))+ if (matchVersion.onOrAfter(Version.LUCENE_31)) {+ if(!this.stemExclusionTable.isEmpty())+ result = new KeywordMarkerTokenFilter(result, stemExclusionTable);result = new CzechStemFilter(result);+ }return new TokenStreamComponents(source, result);}}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechStemFilter.javaindex d01bc009ccd..4a809eca91b 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/cz/CzechStemFilter.java@@ -2,8 +2,10 @@ package org.apache.lucene.analysis.cz;import java.io.IOException;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;// for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/**@@ -25,25 +27,34 @@ import org.apache.lucene.analysis.tokenattributes.TermAttribute;/*** A {@link TokenFilter} that applies {@link CzechStemmer} to stem Czech words.- *+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>* <p><b>NOTE</b>: Input is expected to be in lowercase,* but with diacritical marks</p>+ * @see KeywordMarkerTokenFilter*/public final class CzechStemFilter extends TokenFilter {private final CzechStemmer stemmer;private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public CzechStemFilter(TokenStream input) {super(input);stemmer = new CzechStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}@Overridepublic boolean incrementToken() throws IOException {if (input.incrementToken()) {- int newlen = stemmer.stem(termAtt.termBuffer(), termAtt.termLength());- termAtt.setTermLength(newlen);+ if(!keywordAttr.isKeyword()) {+ final int newlen = stemmer.stem(termAtt.termBuffer(), termAtt.termLength());+ termAtt.setTermLength(newlen);+ }return true;} else {return false;diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.javaindex 51b03121d23..013556109cc 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java@@ -30,6 +30,7 @@ import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @link+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.StopwordAnalyzerBase;import org.apache.lucene.analysis.TokenStream;@@ -208,6 +209,7 @@ public final class GermanAnalyzer extends StopwordAnalyzerBase {TokenStream result = new StandardFilter(source);result = new LowerCaseFilter(matchVersion, result);result = new StopFilter( matchVersion, result, stopwords);- return new TokenStreamComponents(source, new GermanStemFilter(result, exclusionSet));+ result = new KeywordMarkerTokenFilter(result, exclusionSet);+ return new TokenStreamComponents(source, new GermanStemFilter(result));}}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanStemFilter.javaindex fdcaeb5b7fb..6a777f91974 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/de/GermanStemFilter.java@@ -20,8 +20,10 @@ package org.apache.lucene.analysis.de;import java.io.IOException;import java.util.Set;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;// for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/**@@ -31,6 +33,12 @@ import org.apache.lucene.analysis.tokenattributes.TermAttribute;* not be stemmed at all. The stemmer used can be changed at runtime after the* filter object is created (as long as it is a {@link GermanStemmer}).* </p>+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>+ * @see KeywordMarkerTokenFilter*/public final class GermanStemFilter extends TokenFilter{@@ -38,21 +46,29 @@ public final class GermanStemFilter extends TokenFilter* The actual token in the input stream.*/private GermanStemmer stemmer = null;- private Set exclusionSet = null;+ private Set<?> exclusionSet = null;- private TermAttribute termAtt;+ private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;+ /**+ * Creates a {@link GermanStemFilter} instance+ * @param in the source {@link TokenStream}+ */public GermanStemFilter( TokenStream in ){super(in);stemmer = new GermanStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}/*** Builds a GermanStemFilter that uses an exclusion table.+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.*/- public GermanStemFilter( TokenStream in, Set exclusionSet )+ @Deprecated+ public GermanStemFilter( TokenStream in, Set<?> exclusionSet ){this( in );this.exclusionSet = exclusionSet;@@ -66,7 +82,7 @@ public final class GermanStemFilter extends TokenFilterif (input.incrementToken()) {String term = termAtt.term();// Check the exclusion table.- if (exclusionSet == null || !exclusionSet.contains(term)) {+ if (!keywordAttr.isKeyword() && (exclusionSet == null || !exclusionSet.contains(term))) {String s = stemmer.stem(term);// If not stemmed, don't waste the time adjusting the token.if ((s != null) && !s.equals(term))@@ -91,8 +107,10 @@ public final class GermanStemFilter extends TokenFilter/*** Set an alternative exclusion list for this filter.+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.*/- public void setExclusionSet( Set exclusionSet )+ @Deprecated+ public void setExclusionSet( Set<?> exclusionSet ){this.exclusionSet = exclusionSet;}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchAnalyzer.javaindex 52d5f8a478b..78247d241dc 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchAnalyzer.java@@ -21,6 +21,7 @@ import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @link+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.StopwordAnalyzerBase;import org.apache.lucene.analysis.TokenStream;@@ -215,7 +216,9 @@ public final class FrenchAnalyzer extends StopwordAnalyzerBase {final Tokenizer source = new StandardTokenizer(matchVersion, reader);TokenStream result = new StandardFilter(source);result = new StopFilter(matchVersion, result, stopwords);- result = new FrenchStemFilter(result, excltable);+ if(!excltable.isEmpty())+ result = new KeywordMarkerTokenFilter(result, excltable);+ result = new FrenchStemFilter(result);// Convert to lowercase after stemming!return new TokenStreamComponents(source, new LowerCaseFilter(matchVersion, result));}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchStemFilter.javaindex 74322e7fb5c..74720cf8c26 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/fr/FrenchStemFilter.java@@ -17,8 +17,10 @@ package org.apache.lucene.analysis.fr;* limitations under the License.*/+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;// for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;import java.io.IOException;@@ -29,10 +31,15 @@ import java.util.Set;/*** A {@link TokenFilter} that stems french words.* <p>- * It supports a table of words that should- * not be stemmed at all. The used stemmer can be changed at runtime after the+ * The used stemmer can be changed at runtime after the* filter object is created (as long as it is a {@link FrenchStemmer}).* </p>+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>+ * @see KeywordMarkerTokenFilter*/public final class FrenchStemFilter extends TokenFilter {@@ -40,18 +47,26 @@ public final class FrenchStemFilter extends TokenFilter {* The actual token in the input stream.*/private FrenchStemmer stemmer = null;- private Set exclusions = null;+ private Set<?> exclusions = null;- private TermAttribute termAtt;+ private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public FrenchStemFilter( TokenStream in ) {super(in);stemmer = new FrenchStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}-- public FrenchStemFilter( TokenStream in, Set exclusiontable ) {+ /**+ *+ * @param in the {@link TokenStream} to filter+ * @param exclusiontable a set of terms not to be stemmed+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.+ */+ @Deprecated // TODO remove in 3.2+ public FrenchStemFilter( TokenStream in, Set<?> exclusiontable ) {this( in );exclusions = exclusiontable;}@@ -65,7 +80,7 @@ public final class FrenchStemFilter extends TokenFilter {String term = termAtt.term();// Check the exclusion table- if ( exclusions == null || !exclusions.contains( term ) ) {+ if ( !keywordAttr.isKeyword() && (exclusions == null || !exclusions.contains( term )) ) {String s = stemmer.stem( term );// If not stemmed, don't waste the time adjusting the token.if ((s != null) && !s.equals( term ) )@@ -86,8 +101,10 @@ public final class FrenchStemFilter extends TokenFilter {}/*** Set an alternative exclusion list for this filter.+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.*/- public void setExclusionTable( Map exclusiontable ) {+ @Deprecated // TODO remove in 3.2+ public void setExclusionTable( Map<?,?> exclusiontable ) {exclusions = new HashSet(exclusiontable.keySet());}}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchAnalyzer.javaindex 1e66060def2..e30e02987a0 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchAnalyzer.java@@ -19,6 +19,8 @@ package org.apache.lucene.analysis.nl;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;+import org.apache.lucene.analysis.ReusableAnalyzerBase;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.Tokenizer;@@ -52,7 +54,7 @@ import java.util.Map;* <p><b>NOTE</b>: This class uses the same {@link Version}* dependent settings as {@link StandardAnalyzer}.</p>*/-public final class DutchAnalyzer extends Analyzer {+public final class DutchAnalyzer extends ReusableAnalyzerBase {/*** List of typical Dutch stopwords.* @deprecated use {@link #getDefaultStopSet()} instead@@ -215,28 +217,7 @@ public final class DutchAnalyzer extends Analyzer {}}- /**- * Creates a {@link TokenStream} which tokenizes all the text in the- * provided {@link Reader}.- *- * @return A {@link TokenStream} built from a {@link StandardTokenizer}- * filtered with {@link StandardFilter}, {@link StopFilter},- * and {@link DutchStemFilter}- */- @Override- public TokenStream tokenStream(String fieldName, Reader reader) {- TokenStream result = new StandardTokenizer(matchVersion, reader);- result = new StandardFilter(result);- result = new StopFilter(matchVersion, result, stoptable);- result = new DutchStemFilter(result, excltable, stemdict);- return result;- }-- private class SavedStreams {- Tokenizer source;- TokenStream result;- };-+/*** Returns a (possibly reused) {@link TokenStream} which tokenizes all the* text in the provided {@link Reader}.@@ -246,19 +227,14 @@ public final class DutchAnalyzer extends Analyzer {* and {@link DutchStemFilter}*/@Override- public TokenStream reusableTokenStream(String fieldName, Reader reader)- throws IOException {- SavedStreams streams = (SavedStreams) getPreviousTokenStream();- if (streams == null) {- streams = new SavedStreams();- streams.source = new StandardTokenizer(matchVersion, reader);- streams.result = new StandardFilter(streams.source);- streams.result = new StopFilter(matchVersion, streams.result, stoptable);- streams.result = new DutchStemFilter(streams.result, excltable, stemdict);- setPreviousTokenStream(streams);- } else {- streams.source.reset(reader);- }- return streams.result;+ protected TokenStreamComponents createComponents(String fieldName,+ Reader aReader) {+ final Tokenizer source = new StandardTokenizer(matchVersion, aReader);+ TokenStream result = new StandardFilter(source);+ result = new StopFilter(matchVersion, result, stoptable);+ if (!excltable.isEmpty())+ result = new KeywordMarkerTokenFilter(result, excltable);+ result = new DutchStemFilter(result, stemdict);+ return new TokenStreamComponents(source, result);}}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchStemFilter.javaindex 0ef67fc0d85..70b6bd0c4a4 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/nl/DutchStemFilter.java@@ -23,8 +23,10 @@ import java.util.HashSet;import java.util.Map;import java.util.Set;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;// for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/**@@ -34,34 +36,54 @@ import org.apache.lucene.analysis.tokenattributes.TermAttribute;* not be stemmed at all. The stemmer used can be changed at runtime after the* filter object is created (as long as it is a {@link DutchStemmer}).* </p>+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>+ * @see KeywordMarkerTokenFilter*/public final class DutchStemFilter extends TokenFilter {/*** The actual token in the input stream.*/private DutchStemmer stemmer = null;- private Set exclusions = null;+ private Set<?> exclusions = null;- private TermAttribute termAtt;+ private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public DutchStemFilter(TokenStream _in) {super(_in);stemmer = new DutchStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}/*** Builds a DutchStemFilter that uses an exclusion table.+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.*/- public DutchStemFilter(TokenStream _in, Set exclusiontable) {+ @Deprecated+ public DutchStemFilter(TokenStream _in, Set<?> exclusiontable) {this(_in);exclusions = exclusiontable;}++ /**+ * @param stemdictionary Dictionary of word stem pairs, that overrule the algorithm+ */+ public DutchStemFilter(TokenStream _in, Map<?,?> stemdictionary) {+ this(_in);+ stemmer.setStemDictionary(stemdictionary);+ }/*** @param stemdictionary Dictionary of word stem pairs, that overrule the algorithm+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.*/- public DutchStemFilter(TokenStream _in, Set exclusiontable, Map stemdictionary) {+ @Deprecated+ public DutchStemFilter(TokenStream _in, Set<?> exclusiontable, Map<?,?> stemdictionary) {this(_in, exclusiontable);stemmer.setStemDictionary(stemdictionary);}@@ -72,11 +94,11 @@ public final class DutchStemFilter extends TokenFilter {@Overridepublic boolean incrementToken() throws IOException {if (input.incrementToken()) {- String term = termAtt.term();+ final String term = termAtt.term();// Check the exclusion table.- if (exclusions == null || !exclusions.contains(term)) {- String s = stemmer.stem(term);+ if (!keywordAttr.isKeyword() && (exclusions == null || !exclusions.contains(term))) {+ final String s = stemmer.stem(term);// If not stemmed, don't waste the time adjusting the token.if ((s != null) && !s.equals(term))termAtt.setTermBuffer(s);@@ -98,8 +120,10 @@ public final class DutchStemFilter extends TokenFilter {/*** Set an alternative exclusion list for this filter.+ * @deprecated use {@link KeywordAttribute} with {@link KeywordMarkerTokenFilter} instead.*/- public void setExclusionTable(HashSet exclusiontable) {+ @Deprecated+ public void setExclusionTable(HashSet<?> exclusiontable) {exclusions = exclusiontable;}@@ -107,7 +131,7 @@ public final class DutchStemFilter extends TokenFilter {* Set dictionary for stemming, this dictionary overrules the algorithm,* so you can correct for a particular unwanted word-stem pair.*/- public void setStemDictionary(HashMap dict) {+ public void setStemDictionary(HashMap<?,?> dict) {if (stemmer != null)stemmer.setStemDictionary(dict);}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianAnalyzer.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianAnalyzer.javaindex 5c8d8d0a933..7006da84d8d 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianAnalyzer.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianAnalyzer.java@@ -26,6 +26,7 @@ import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.analysis.LowerCaseFilter;import org.apache.lucene.analysis.ReusableAnalyzerBase.TokenStreamComponents; // javadoc @link+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;import org.apache.lucene.analysis.StopFilter;import org.apache.lucene.analysis.StopwordAnalyzerBase;import org.apache.lucene.analysis.TokenStream;@@ -63,6 +64,17 @@ public final class RussianAnalyzer extends StopwordAnalyzerBase.unmodifiableSet(new CharArraySet(Version.LUCENE_CURRENT,Arrays.asList(RUSSIAN_STOP_WORDS), false));}++ private final Set<?> stemExclusionSet;++ /**+ * Returns an unmodifiable instance of the default stop-words set.+ *+ * @return an unmodifiable instance of the default stop-words set.+ */+ public static Set<?> getDefaultStopSet() {+ return DefaultSetHolder.DEFAULT_STOP_SET;+ }public RussianAnalyzer(Version matchVersion) {this(matchVersion, DefaultSetHolder.DEFAULT_STOP_SET);@@ -86,9 +98,24 @@ public final class RussianAnalyzer extends StopwordAnalyzerBase* a stopword set*/public RussianAnalyzer(Version matchVersion, Set<?> stopwords){+ this(matchVersion, stopwords, CharArraySet.EMPTY_SET);+ }++ /**+ * Builds an analyzer with the given stop words+ *+ * @param matchVersion+ * lucene compatibility version+ * @param stopwords+ * a stopword set+ * @param stemExclusionSet a set of words not to be stemmed+ */+ public RussianAnalyzer(Version matchVersion, Set<?> stopwords, Set<?> stemExclusionSet){super(matchVersion, stopwords);+ this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(matchVersion, stemExclusionSet));}+/*** Builds an analyzer with the given stop words.* TODO: create a Set version of this ctor@@ -115,6 +142,8 @@ public final class RussianAnalyzer extends StopwordAnalyzerBasefinal Tokenizer source = new RussianLetterTokenizer(reader);TokenStream result = new LowerCaseFilter(matchVersion, source);result = new StopFilter(matchVersion, result, stopwords);+ if(!stemExclusionSet.isEmpty())+ result = new KeywordMarkerTokenFilter(result, stemExclusionSet);return new TokenStreamComponents(source, new RussianStemFilter(result));}diff --git a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianStemFilter.java b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianStemFilter.javaindex b9564932d8b..f5817c52a29 100644--- a/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianStemFilter.java+++ b/contrib/analyzers/common/src/java/org/apache/lucene/analysis/ru/RussianStemFilter.java@@ -17,9 +17,11 @@ package org.apache.lucene.analysis.ru;* limitations under the License.*/+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;// for javadocimport org.apache.lucene.analysis.LowerCaseFilter; // for javadocimport org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;import org.apache.lucene.analysis.ru.RussianStemmer;//javadoc @link@@ -32,6 +34,12 @@ import java.io.IOException;* The input should be filtered by {@link LowerCaseFilter} before passing it to RussianStemFilter ,* because RussianStemFilter only works with lowercase characters.* </p>+ * <p>+ * To prevent terms from being stemmed use an instance of+ * {@link KeywordMarkerTokenFilter} or a custom {@link TokenFilter} that sets+ * the {@link KeywordAttribute} before this {@link TokenStream}.+ * </p>+ * @see KeywordMarkerTokenFilter*/public final class RussianStemFilter extends TokenFilter{@@ -40,13 +48,15 @@ public final class RussianStemFilter extends TokenFilter*/private RussianStemmer stemmer = null;- private TermAttribute termAtt;+ private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public RussianStemFilter(TokenStream in){super(in);stemmer = new RussianStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}/*** Returns the next token in the stream, or null at EOS@@ -55,10 +65,12 @@ public final class RussianStemFilter extends TokenFilterpublic final boolean incrementToken() throws IOException{if (input.incrementToken()) {- String term = termAtt.term();- String s = stemmer.stem(term);- if (s != null && !s.equals(term))- termAtt.setTermBuffer(s);+ if(!keywordAttr.isKeyword()) {+ final String term = termAtt.term();+ final String s = stemmer.stem(term);+ if (s != null && !s.equals(term))+ termAtt.setTermBuffer(s);+ }return true;} else {return false;diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicAnalyzer.javaindex 73d7d767512..92884b61176 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicAnalyzer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicAnalyzer.java@@ -17,11 +17,15 @@ package org.apache.lucene.analysis.ar;* limitations under the License.*/+import java.io.IOException;import java.util.Collections;import java.util.HashSet;import java.util.Set;+import javax.print.DocFlavor.CHAR_ARRAY;+import org.apache.lucene.analysis.BaseTokenStreamTestCase;+import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.util.Version;/**@@ -84,4 +88,17 @@ public class TestArabicAnalyzer extends BaseTokenStreamTestCase {assertAnalyzesTo(a, "The quick brown fox.", new String[] { "quick","brown", "fox" });}++ public void testWithStemExclusionSet() throws IOException {+ Set<String> set = new HashSet<String>();+ set.add("ساهدهات");+ ArabicAnalyzer a = new ArabicAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, set);+ assertAnalyzesTo(a, "كبيرة the quick ساهدهات", new String[] { "كبير","the", "quick", "ساهدهات" });+ assertAnalyzesToReuse(a, "كبيرة the quick ساهدهات", new String[] { "كبير","the", "quick", "ساهدهات" });+++ a = new ArabicAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, CharArraySet.EMPTY_SET);+ assertAnalyzesTo(a, "كبيرة the quick ساهدهات", new String[] { "كبير","the", "quick", "ساهد" });+ assertAnalyzesToReuse(a, "كبيرة the quick ساهدهات", new String[] { "كبير","the", "quick", "ساهد" });+ }}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicStemFilter.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicStemFilter.javaindex 47f744447fd..e5c2d2b293d 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicStemFilter.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ar/TestArabicStemFilter.java@@ -21,7 +21,9 @@ import java.io.IOException;import java.io.StringReader;import org.apache.lucene.analysis.BaseTokenStreamTestCase;-import org.apache.lucene.analysis.tokenattributes.TermAttribute;+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;+import org.apache.lucene.util.Version;/*** Test the Arabic Normalization Filter@@ -112,11 +114,19 @@ public class TestArabicStemFilter extends BaseTokenStreamTestCase {public void testNonArabic() throws IOException {check("English", "English");}++ public void testWithKeywordAttribute() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("ساهدهات");+ ArabicLetterTokenizer tokenStream = new ArabicLetterTokenizer(new StringReader("ساهدهات"));++ ArabicStemFilter filter = new ArabicStemFilter(new KeywordMarkerTokenFilter(tokenStream, set));+ assertTokenStreamContents(filter, new String[]{"ساهدهات"});+ }private void check(final String input, final String expected) throws IOException {ArabicLetterTokenizer tokenStream = new ArabicLetterTokenizer(new StringReader(input));ArabicStemFilter filter = new ArabicStemFilter(tokenStream);assertTokenStreamContents(filter, new String[]{expected});}-}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianAnalyzer.javaindex 2203d355637..f4665b9d993 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianAnalyzer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianAnalyzer.java@@ -22,6 +22,7 @@ import java.util.Collections;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.BaseTokenStreamTestCase;+import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.util.Version;/**@@ -67,4 +68,11 @@ public class TestBulgarianAnalyzer extends BaseTokenStreamTestCase {assertAnalyzesTo(a, "градове", new String[] {"град"});}++ public void testWithStemExclusionSet() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_31, 1, true);+ set.add("строеве");+ Analyzer a = new BulgarianAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, set);+ assertAnalyzesTo(a, "строевете строеве", new String[] { "строй", "строеве" });+ }}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianStemmer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianStemmer.javaindex e01bb4932aa..d238425b149 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianStemmer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/bg/TestBulgarianStemmer.java@@ -18,8 +18,12 @@ package org.apache.lucene.analysis.bg;*/import java.io.IOException;+import java.io.StringReader;import org.apache.lucene.analysis.BaseTokenStreamTestCase;+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;+import org.apache.lucene.analysis.WhitespaceTokenizer;import org.apache.lucene.util.Version;/**@@ -207,4 +211,15 @@ public class TestBulgarianStemmer extends BaseTokenStreamTestCase {assertAnalyzesTo(a, "строя", new String[] {"стр"});assertAnalyzesTo(a, "строят", new String[] {"стр"});}++ public void testWithKeywordAttribute() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_31, 1, true);+ set.add("строеве");+ WhitespaceTokenizer tokenStream = new WhitespaceTokenizer(+ new StringReader("строевете строеве"));++ BulgarianStemFilter filter = new BulgarianStemFilter(+ new KeywordMarkerTokenFilter(tokenStream, set));+ assertTokenStreamContents(filter, new String[] { "строй", "строеве" });+ }}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/br/TestBrazilianStemmer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/br/TestBrazilianStemmer.javaindex 51cc740aa53..bfa60f6de4e 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/br/TestBrazilianStemmer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/br/TestBrazilianStemmer.java@@ -17,12 +17,14 @@ package org.apache.lucene.analysis.br;* limitations under the License.*/-import java.util.Arrays;-import java.util.Collections;-import java.util.HashSet;+import java.io.IOException;+import java.io.StringReader;import org.apache.lucene.analysis.BaseTokenStreamTestCase;import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;+import org.apache.lucene.analysis.LowerCaseTokenizer;import org.apache.lucene.util.Version;/**@@ -139,6 +141,34 @@ public class TestBrazilianStemmer extends BaseTokenStreamTestCase {checkReuse(a, "quintessência", "quintessência"); // excluded words will be completely unchanged.}+ public void testStemExclusionTableBWCompat() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("Brasília");+ BrazilianStemFilter filter = new BrazilianStemFilter(+ new LowerCaseTokenizer(new StringReader("Brasília Brasilia")), set);+ assertTokenStreamContents(filter, new String[] { "brasília", "brasil" });+ }++ public void testWithKeywordAttribute() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("Brasília");+ BrazilianStemFilter filter = new BrazilianStemFilter(+ new KeywordMarkerTokenFilter(new LowerCaseTokenizer(new StringReader(+ "Brasília Brasilia")), set));+ assertTokenStreamContents(filter, new String[] { "brasília", "brasil" });+ }++ public void testWithKeywordAttributeAndExclusionTable() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("Brasília");+ CharArraySet set1 = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set1.add("Brasilia");+ BrazilianStemFilter filter = new BrazilianStemFilter(+ new KeywordMarkerTokenFilter(new LowerCaseTokenizer(new StringReader(+ "Brasília Brasilia")), set), set1);+ assertTokenStreamContents(filter, new String[] { "brasília", "brasilia" });+ }+/** Test that changes to the exclusion table are applied immediately* when using reusable token streams.diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.javaindex c0107cdb93a..5c5bcd19e7b 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java@@ -24,6 +24,7 @@ import java.io.InputStream;import org.apache.lucene.analysis.BaseTokenStreamTestCase;import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.util.Version;/**@@ -109,5 +110,11 @@ public class TestCzechAnalyzer extends BaseTokenStreamTestCase {assertAnalyzesToReuse(cz, "Česká Republika", new String[] { "česká" });}-++ public void testWithStemExclusionSet() throws IOException{+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("hole");+ CzechAnalyzer cz = new CzechAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, set);+ assertAnalyzesTo(cz, "hole desek", new String[] {"hole", "desk"});+ }}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechStemmer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechStemmer.javaindex d6a27611431..c89309e875c 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechStemmer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechStemmer.java@@ -18,8 +18,12 @@ package org.apache.lucene.analysis.cz;*/import java.io.IOException;+import java.io.StringReader;import org.apache.lucene.analysis.BaseTokenStreamTestCase;+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;+import org.apache.lucene.analysis.WhitespaceTokenizer;import org.apache.lucene.util.Version;/**@@ -270,4 +274,13 @@ public class TestCzechStemmer extends BaseTokenStreamTestCase {assertAnalyzesTo(cz, "e", new String[] { "e" });assertAnalyzesTo(cz, "zi", new String[] { "zi" });}++ public void testWithKeywordAttribute() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("hole");+ CzechStemFilter filter = new CzechStemFilter(new KeywordMarkerTokenFilter(+ new WhitespaceTokenizer(new StringReader("hole desek")), set));+ assertTokenStreamContents(filter, new String[] { "hole", "desk" });+ }+}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.javaindex 6c22bf797f7..6fe2ea39b20 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java@@ -20,10 +20,15 @@ package org.apache.lucene.analysis.de;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;+import java.io.IOException;import java.io.InputStreamReader;+import java.io.StringReader;import org.apache.lucene.analysis.BaseTokenStreamTestCase;import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.CharArraySet;+import org.apache.lucene.analysis.KeywordMarkerTokenFilter;+import org.apache.lucene.analysis.LowerCaseTokenizer;import org.apache.lucene.util.Version;/**@@ -64,6 +69,37 @@ public class TestGermanStemFilter extends BaseTokenStreamTestCase {checkReuse(a, "Tischen", "tisch");}+ public void testExclusionTableBWCompat() throws IOException {+ GermanStemFilter filter = new GermanStemFilter(new LowerCaseTokenizer(+ new StringReader("Fischen Trinken")));+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("fischen");+ filter.setExclusionSet(set);+ assertTokenStreamContents(filter, new String[] { "fischen", "trink" });+ }++ public void testWithKeywordAttribute() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("fischen");+ GermanStemFilter filter = new GermanStemFilter(+ new KeywordMarkerTokenFilter(new LowerCaseTokenizer(new StringReader(+ "Fischen Trinken")), set));+ assertTokenStreamContents(filter, new String[] { "fischen", "trink" });+ }++ public void testWithKeywordAttributeAndExclusionTable() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("fischen");+ CharArraySet set1 = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set1.add("trinken");+ set1.add("fischen");+ GermanStemFilter filter = new GermanStemFilter(+ new KeywordMarkerTokenFilter(new LowerCaseTokenizer(new StringReader(+ "Fischen Trinken")), set));+ filter.setExclusionSet(set1);+ assertTokenStreamContents(filter, new String[] { "fischen", "trinken" });+ }+/** Test that changes to the exclusion table are applied immediately* when using reusable token streams.@@ -75,6 +111,7 @@ public class TestGermanStemFilter extends BaseTokenStreamTestCase {checkReuse(a, "tischen", "tischen");}+private void check(final String input, final String expected) throws Exception {checkOneTerm(new GermanAnalyzer(Version.LUCENE_CURRENT), input, expected);}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/fr/TestFrenchAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/fr/TestFrenchAnalyzer.javaindex caf7ac0bffc..f6eefc0a917 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/fr/TestFrenchAnalyzer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/fr/TestFrenchAnalyzer.java@@ -17,11 +17,8 @@ package org.apache.lucene.analysis.fr;* limitations under the License.*/-import java.io.StringReader;-import org.apache.lucene.analysis.BaseTokenStreamTestCase;-import org.apache.lucene.analysis.Analyzer;-import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.util.Version;/**@@ -147,4 +144,17 @@ public class TestFrenchAnalyzer extends BaseTokenStreamTestCase {fa.setStemExclusionTable(new String[] { "habitable" });assertAnalyzesToReuse(fa, "habitable", new String[] { "habitable" });}++ public void testExclusionTableViaCtor() throws Exception {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("habitable");+ FrenchAnalyzer fa = new FrenchAnalyzer(Version.LUCENE_CURRENT,+ CharArraySet.EMPTY_SET, set);+ assertAnalyzesToReuse(fa, "habitable chiste", new String[] { "habitable",+ "chist" });++ fa = new FrenchAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, set);+ assertAnalyzesTo(fa, "habitable chiste", new String[] { "habitable",+ "chist" });+ }}diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.javaindex d6b54e2b1e2..053b4495ff1 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java@@ -18,9 +18,11 @@ package org.apache.lucene.analysis.nl;*/import java.io.File;+import java.io.IOException;import org.apache.lucene.analysis.BaseTokenStreamTestCase;import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.util.Version;/**@@ -133,6 +135,19 @@ public class TestDutchStemmer extends BaseTokenStreamTestCase {checkOneTermReuse(a, "lichamelijk", "licham");a.setStemExclusionTable(new String[] { "lichamelijk" });checkOneTermReuse(a, "lichamelijk", "lichamelijk");+++ }++ public void testExclusionTableViaCtor() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_30, 1, true);+ set.add("lichamelijk");+ DutchAnalyzer a = new DutchAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, set);+ assertAnalyzesToReuse(a, "lichamelijk lichamelijke", new String[] { "lichamelijk", "licham" });++ a = new DutchAnalyzer(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET, set);+ assertAnalyzesTo(a, "lichamelijk lichamelijke", new String[] { "lichamelijk", "licham" });+}/*diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.javaindex 52c8646a591..a3a12d86f04 100644--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java@@ -26,6 +26,7 @@ import java.io.StringReader;import org.apache.lucene.analysis.BaseTokenStreamTestCase;import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.tokenattributes.TermAttribute;import org.apache.lucene.util.Version;@@ -116,4 +117,14 @@ public class TestRussianAnalyzer extends BaseTokenStreamTestCaseassertAnalyzesToReuse(a, "Но знание это хранилось в тайне",new String[] { "знан", "хран", "тайн" });}+++ public void testWithStemExclusionSet() throws Exception {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("представление");+ Analyzer a = new RussianAnalyzer(Version.LUCENE_CURRENT, RussianAnalyzer.getDefaultStopSet() , set);+ assertAnalyzesToReuse(a, "Вместе с тем о силе электромагнитной энергии имели представление еще",+ new String[] { "вмест", "сил", "электромагнитн", "энерг", "имел", "представление" });++ }}diff --git a/src/java/org/apache/lucene/analysis/KeywordMarkerTokenFilter.java b/src/java/org/apache/lucene/analysis/KeywordMarkerTokenFilter.javanew file mode 100644index 00000000000..f7e76daa5ac--- /dev/null+++ b/src/java/org/apache/lucene/analysis/KeywordMarkerTokenFilter.java@@ -0,0 +1,82 @@+package org.apache.lucene.analysis;++/**+ * 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 java.io.IOException;+import java.util.Set;++import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;+import org.apache.lucene.analysis.tokenattributes.TermAttribute;+import org.apache.lucene.util.Version;++/**+ * Marks terms as keywords via the {@link KeywordAttribute}. Each token+ * contained in the provided is marked as a keyword by setting+ * {@link KeywordAttribute#setKeyword(boolean)} to <code>true</code>.+ *+ * @see KeywordAttribute+ */+public final class KeywordMarkerTokenFilter extends TokenFilter {++ private final KeywordAttribute keywordAttr;+ private final TermAttribute termAtt;+ private final CharArraySet keywordSet;++ /**+ * Create a new KeywordMarkerTokenFilter, that marks the current token as a+ * keyword if the tokens term buffer is contained in the given set via the+ * {@link KeywordAttribute}.+ *+ * @param in+ * TokenStream to filter+ * @param keywordSet+ * the keywords set to lookup the current termbuffer+ */+ public KeywordMarkerTokenFilter(final TokenStream in,+ final CharArraySet keywordSet) {+ super(in);+ termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);+ this.keywordSet = keywordSet;+ }++ /**+ * Create a new KeywordMarkerTokenFilter, that marks the current token as a+ * keyword if the tokens term buffer is contained in the given set via the+ * {@link KeywordAttribute}.+ *+ * @param in+ * TokenStream to filter+ * @param keywordSet+ * the keywords set to lookup the current termbuffer+ */+ public KeywordMarkerTokenFilter(final TokenStream in, final Set<?> keywordSet) {+ this(in, keywordSet instanceof CharArraySet ? (CharArraySet) keywordSet+ : CharArraySet.copy(Version.LUCENE_31, keywordSet));+ }++ @Override+ public final boolean incrementToken() throws IOException {+ if (input.incrementToken()) {+ keywordAttr.setKeyword(keywordSet.contains(termAtt.termBuffer(), 0,+ termAtt.termLength()));+ return true;+ } else+ return false;+ }+}diff --git a/src/java/org/apache/lucene/analysis/PorterStemFilter.java b/src/java/org/apache/lucene/analysis/PorterStemFilter.javaindex 3982bb97376..645ab9c577f 100644--- a/src/java/org/apache/lucene/analysis/PorterStemFilter.java+++ b/src/java/org/apache/lucene/analysis/PorterStemFilter.java@@ -19,6 +19,7 @@ package org.apache.lucene.analysis;import java.io.IOException;+import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;import org.apache.lucene.analysis.tokenattributes.TermAttribute;/** Transforms the token stream as per the Porter stemming algorithm.@@ -38,15 +39,23 @@ import org.apache.lucene.analysis.tokenattributes.TermAttribute;}}</PRE>+ <p>+ Note: This filter is aware of the {@link KeywordAttribute}. To prevent+ certain terms from being passed to the stemmer+ {@link KeywordAttribute#isKeyword()} should be set to <code>true</code>+ in a previous {@link TokenStream}.+ </p>*/public final class PorterStemFilter extends TokenFilter {- private PorterStemmer stemmer;- private TermAttribute termAtt;+ private final PorterStemmer stemmer;+ private final TermAttribute termAtt;+ private final KeywordAttribute keywordAttr;public PorterStemFilter(TokenStream in) {super(in);stemmer = new PorterStemmer();termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);}@Override@@ -54,7 +63,7 @@ public final class PorterStemFilter extends TokenFilter {if (!input.incrementToken())return false;- if (stemmer.stem(termAtt.termBuffer(), 0, termAtt.termLength()))+ if ((!keywordAttr.isKeyword()) && stemmer.stem(termAtt.termBuffer(), 0, termAtt.termLength()))termAtt.setTermBuffer(stemmer.getResultBuffer(), 0, stemmer.getResultLength());return true;}diff --git a/src/java/org/apache/lucene/analysis/tokenattributes/KeywordAttribute.java b/src/java/org/apache/lucene/analysis/tokenattributes/KeywordAttribute.javanew file mode 100644index 00000000000..f7ba4e8114a--- /dev/null+++ b/src/java/org/apache/lucene/analysis/tokenattributes/KeywordAttribute.java@@ -0,0 +1,49 @@+package org.apache.lucene.analysis.tokenattributes;++/**+ * 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.util.Attribute;++/**+ * This attribute can be used to mark a token as a keyword. Keyword aware+ * {@link TokenStream}s can decide to modify a token based on the return value+ * of {@link #isKeyword()} if the token is modified. Stemming filters for+ * instance can use this attribute to conditionally skip a term if+ * {@link #isKeyword()} returns <code>true</code>.+ */+public interface KeywordAttribute extends Attribute {++ /**+ * Returns <code>true</code> iff the current token is a keyword, otherwise+ * <code>false</code>/+ *+ * @return <code>true</code> iff the current token is a keyword, otherwise+ * <code>false</code>/+ */+ public boolean isKeyword();++ /**+ * Marks the current token as keyword iff set to <code>true</code>.+ *+ * @param isKeyword+ * <code>true</code> iff the current token is a keyword, otherwise+ * <code>false</code>.+ */+ public void setKeyword(boolean isKeyword);+}diff --git a/src/java/org/apache/lucene/analysis/tokenattributes/KeywordAttributeImpl.java b/src/java/org/apache/lucene/analysis/tokenattributes/KeywordAttributeImpl.javanew file mode 100644index 00000000000..92581e57178--- /dev/null+++ b/src/java/org/apache/lucene/analysis/tokenattributes/KeywordAttributeImpl.java@@ -0,0 +1,82 @@+package org.apache.lucene.analysis.tokenattributes;++/**+ * 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.util.AttributeImpl;++/**+ *This attribute can be used to mark a token as a keyword. Keyword aware+ * {@link TokenStream}s can decide to modify a token based on the return value+ * of {@link #isKeyword()} if the token is modified. Stemming filters for+ * instance can use this attribute to conditionally skip a term if+ * {@link #isKeyword()} returns <code>true</code>.+ */+public final class KeywordAttributeImpl extends AttributeImpl implements+ KeywordAttribute {+ private boolean keyword;++ @Override+ public void clear() {+ keyword = false;+ }++ @Override+ public void copyTo(AttributeImpl target) {+ KeywordAttribute attr = (KeywordAttribute) target;+ attr.setKeyword(keyword);+ }++ @Override+ public int hashCode() {+ return keyword ? 31 : 37;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj)+ return true;+ if (getClass() != obj.getClass())+ return false;+ final KeywordAttributeImpl other = (KeywordAttributeImpl) obj;+ return keyword == other.keyword;+ }++ /**+ * Returns <code>true</code> iff the current token is a keyword, otherwise+ * <code>false</code>/+ *+ * @return <code>true</code> iff the current token is a keyword, otherwise+ * <code>false</code>/+ */+ public boolean isKeyword() {+ return keyword;+ }++ /**+ * Marks the current token as keyword iff set to <code>true</code>.+ *+ * @param isKeyword+ * <code>true</code> iff the current token is a keyword, otherwise+ * <code>false</code>.+ */+ public void setKeyword(boolean isKeyword) {+ keyword = isKeyword;+ }++}diff --git a/src/test/org/apache/lucene/analysis/TestKeywordMarkerTokenFilter.java b/src/test/org/apache/lucene/analysis/TestKeywordMarkerTokenFilter.javanew file mode 100644index 00000000000..69fbd672c31--- /dev/null+++ b/src/test/org/apache/lucene/analysis/TestKeywordMarkerTokenFilter.java@@ -0,0 +1,77 @@+package org.apache.lucene.analysis;++import java.io.IOException;+import java.io.StringReader;+import java.util.HashSet;+import java.util.Set;++import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;+import org.apache.lucene.analysis.tokenattributes.TermAttribute;+import org.apache.lucene.util.Version;+import org.junit.Test;++/**+ * 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.+ */++/**+ * Testcase for {@link KeywordMarkerTokenFilter}+ */+public class TestKeywordMarkerTokenFilter extends BaseTokenStreamTestCase {++ @Test+ public void testIncrementToken() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_31, 5, true);+ set.add("lucenefox");+ String[] output = new String[] { "the", "quick", "brown", "LuceneFox",+ "jumps" };+ assertTokenStreamContents(new LowerCaseFilterMock(+ new KeywordMarkerTokenFilter(new WhitespaceTokenizer(new StringReader(+ "The quIck browN LuceneFox Jumps")), set)), output);+ Set<String> jdkSet = new HashSet<String>();+ jdkSet.add("LuceneFox");+ assertTokenStreamContents(new LowerCaseFilterMock(+ new KeywordMarkerTokenFilter(new WhitespaceTokenizer(new StringReader(+ "The quIck browN LuceneFox Jumps")), jdkSet)), output);+ Set<?> set2 = set;+ assertTokenStreamContents(new LowerCaseFilterMock(+ new KeywordMarkerTokenFilter(new WhitespaceTokenizer(new StringReader(+ "The quIck browN LuceneFox Jumps")), set2)), output);+ }++ public static class LowerCaseFilterMock extends TokenFilter {++ private TermAttribute termAtt;+ private KeywordAttribute keywordAttr;++ public LowerCaseFilterMock(TokenStream in) {+ super(in);+ termAtt = addAttribute(TermAttribute.class);+ keywordAttr = addAttribute(KeywordAttribute.class);+ }++ @Override+ public boolean incrementToken() throws IOException {+ if (input.incrementToken()) {+ if (!keywordAttr.isKeyword())+ termAtt.setTermBuffer(termAtt.term().toLowerCase());+ return true;+ }+ return false;+ }++ }+}diff --git a/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java b/src/test/org/apache/lucene/analysis/TestPorterStemFilter.javaindex 471ac08d2a2..e35a773f8c1 100644--- a/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java+++ b/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java@@ -25,6 +25,8 @@ import java.io.InputStreamReader;import java.io.StringReader;import java.util.zip.ZipFile;+import org.apache.lucene.util.Version;+/*** Test the PorterStemFilter with Martin Porter's test data.*/@@ -56,4 +58,12 @@ public class TestPorterStemFilter extends BaseTokenStreamTestCase {outputReader.close();zipFile.close();}++ public void testWithKeywordAttribute() throws IOException {+ CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);+ set.add("yourselves");+ Tokenizer tokenizer = new WhitespaceTokenizer(new StringReader("yourselves yours"));+ TokenStream filter = new PorterStemFilter(new KeywordMarkerTokenFilter(tokenizer, set));+ assertTokenStreamContents(filter, new String[] {"yourselves", "your"});+ }}diff --git a/src/test/org/apache/lucene/analysis/tokenattributes/TestSimpleAttributeImpls.java b/src/test/org/apache/lucene/analysis/tokenattributes/TestSimpleAttributeImpls.javaindex 5d173537d2b..4c9e7a03b72 100644--- a/src/test/org/apache/lucene/analysis/tokenattributes/TestSimpleAttributeImpls.java+++ b/src/test/org/apache/lucene/analysis/tokenattributes/TestSimpleAttributeImpls.java@@ -20,6 +20,7 @@ package org.apache.lucene.analysis.tokenattributes;import org.apache.lucene.index.Payload;import org.apache.lucene.util.AttributeImpl;import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.AttributeSource.AttributeFactory;public class TestSimpleAttributeImpls extends LuceneTestCase {@@ -118,6 +119,25 @@ public class TestSimpleAttributeImpls extends LuceneTestCase {assertEquals(0, att.endOffset());}+ public void testKeywordAttribute() {+ AttributeImpl attrImpl = AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY.createAttributeInstance(KeywordAttribute.class);+ assertSame(KeywordAttributeImpl.class, attrImpl.getClass());+ KeywordAttributeImpl att = (KeywordAttributeImpl) attrImpl;+ assertFalse(att.isKeyword());+ att.setKeyword(true);+ assertTrue(att.isKeyword());++ KeywordAttributeImpl assertCloneIsEqual = (KeywordAttributeImpl) assertCloneIsEqual(att);+ assertTrue(assertCloneIsEqual.isKeyword());+ assertCloneIsEqual.clear();+ assertFalse(assertCloneIsEqual.isKeyword());+ assertTrue(att.isKeyword());++ att.copyTo(assertCloneIsEqual);+ assertTrue(assertCloneIsEqual.isKeyword());+ assertTrue(att.isKeyword());+ }+public static final AttributeImpl assertCloneIsEqual(AttributeImpl att) {AttributeImpl clone = (AttributeImpl) att.clone();assertEquals("Clone must be equal", att, clone);