/*
* 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