/* * 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. */ using System; using System.Collections.Generic; using Lucene.Net.Analysis.Tokenattributes; using Lucene.Net.Util; using QueryParser = Lucene.Net.QueryParsers.QueryParser; using Version = Lucene.Net.Util.Version; namespace Lucene.Net.Analysis { /// Removes stop words from a token stream. public sealed class StopFilter:TokenFilter { private readonly CharArraySet stopWords; private bool enablePositionIncrements = false; private readonly ITermAttribute termAtt; private readonly IPositionIncrementAttribute posIncrAtt; /// Construct a token stream filtering the given input. /// If stopWords is an instance of (true if /// makeStopSet() was used to construct the set) it will be directly used /// and ignoreCase will be ignored since CharArraySet /// directly controls case sensitivity. ///

/// If stopWords is not an instance of , /// a new CharArraySet will be constructed and ignoreCase will be /// used to specify the case sensitivity of that set. ///

/// true if token positions should record the removed stop words /// Input TokenStream /// A Set of strings or strings or char[] or any other ToString()-able set representing the stopwords /// if true, all words are lower cased first public StopFilter(bool enablePositionIncrements, TokenStream input, ISet stopWords, bool ignoreCase) : base(input) { if (stopWords is CharArraySet) { this.stopWords = (CharArraySet) stopWords; } else { this.stopWords = new CharArraySet(stopWords.Count, ignoreCase); this.stopWords.AddAll(stopWords); } this.enablePositionIncrements = enablePositionIncrements; termAtt = AddAttribute(); posIncrAtt = AddAttribute(); } /// Constructs a filter which removes words from the input /// TokenStream that are named in the Set. /// /// true if token positions should record the removed stop words /// Input stream /// A Set of strings or char[] or any other ToString()-able set representing the stopwords /// public StopFilter(bool enablePositionIncrements, TokenStream @in, ISet stopWords) : this(enablePositionIncrements, @in, stopWords, false) { } /// Builds a Set from an array of stop words, /// appropriate for passing into the StopFilter constructor. /// This permits this stopWords construction to be cached once when /// an Analyzer is constructed. /// /// /// passing false to ignoreCase public static ISet MakeStopSet(params string[] stopWords) { return MakeStopSet(stopWords, false); } /// Builds a Set from an array of stop words, /// appropriate for passing into the StopFilter constructor. /// This permits this stopWords construction to be cached once when /// an Analyzer is constructed. /// /// A list of strings or char[] or any other ToString()-able list representing the stop words /// passing false to ignoreCase public static ISet MakeStopSet(IList stopWords) { return MakeStopSet(stopWords, false); } /// /// An array of stopwords /// If true, all words are lower cased first. /// a Set containing the words public static ISet MakeStopSet(string[] stopWords, bool ignoreCase) { var stopSet = new CharArraySet(stopWords.Length, ignoreCase); stopSet.AddAll(stopWords); return stopSet; } /// /// A List of Strings or char[] or any other toString()-able list representing the stopwords /// if true, all words are lower cased first /// A Set ()containing the words public static ISet MakeStopSet(IList stopWords, bool ignoreCase) { var stopSet = new CharArraySet(stopWords.Count, ignoreCase); foreach(var word in stopWords) stopSet.Add(word.ToString()); return stopSet; } /// Returns the next input Token whose term() is not a stop word. public override bool IncrementToken() { // return the first non-stop word found int skippedPositions = 0; while (input.IncrementToken()) { if (!stopWords.Contains(termAtt.TermBuffer(), 0, termAtt.TermLength())) { if (enablePositionIncrements) { posIncrAtt.PositionIncrement = posIncrAtt.PositionIncrement + skippedPositions; } return true; } skippedPositions += posIncrAtt.PositionIncrement; } // reached EOS -- return false return false; } /// Returns version-dependent default for enablePositionIncrements. Analyzers /// that embed StopFilter use this method when creating the StopFilter. Prior /// to 2.9, this returns false. On 2.9 or later, it returns true. /// public static bool GetEnablePositionIncrementsVersionDefault(Version matchVersion) { return matchVersion.OnOrAfter(Version.LUCENE_29); } /// If true, this StopFilter will preserve /// positions of the incoming tokens (ie, accumulate and /// set position increments of the removed stop tokens). /// Generally, true is best as it does not /// lose information (positions of the original tokens) /// during indexing. /// ///

When set, when a token is stopped /// (omitted), the position increment of the following /// token is incremented. /// ///

NOTE: be sure to also /// set if /// you use QueryParser to create queries. ///

public bool EnablePositionIncrements { get { return enablePositionIncrements; } set { enablePositionIncrements = value; } } } }