/* * 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.Collections.Generic; using Version = Lucene.Net.Util.Version; namespace Lucene.Net.Analysis { /// Filters with and /// . /// /// ///

/// You must specify the required compatibility when creating /// StopAnalyzer: /// /// As of 2.9, position increments are preserved /// ///

public sealed class StopAnalyzer:Analyzer { private readonly ISet stopWords; private readonly bool enablePositionIncrements; /// An unmodifiable set containing some common English words that are not usually useful /// for searching. /// public static ISet ENGLISH_STOP_WORDS_SET; /// Builds an analyzer which removes words in ENGLISH_STOP_WORDS. public StopAnalyzer(Version matchVersion) { stopWords = ENGLISH_STOP_WORDS_SET; enablePositionIncrements = StopFilter.GetEnablePositionIncrementsVersionDefault(matchVersion); } /// Builds an analyzer with the stop words from the given set. public StopAnalyzer(Version matchVersion, ISet stopWords) { this.stopWords = stopWords; enablePositionIncrements = StopFilter.GetEnablePositionIncrementsVersionDefault(matchVersion); } /// Builds an analyzer with the stop words from the given file. /// /// /// /// /// See above /// /// File to load stop words from /// public StopAnalyzer(Version matchVersion, System.IO.FileInfo stopwordsFile) { stopWords = WordlistLoader.GetWordSet(stopwordsFile); enablePositionIncrements = StopFilter.GetEnablePositionIncrementsVersionDefault(matchVersion); } /// Builds an analyzer with the stop words from the given reader. /// /// /// See above /// /// Reader to load stop words from /// public StopAnalyzer(Version matchVersion, System.IO.TextReader stopwords) { stopWords = WordlistLoader.GetWordSet(stopwords); enablePositionIncrements = StopFilter.GetEnablePositionIncrementsVersionDefault(matchVersion); } /// Filters LowerCaseTokenizer with StopFilter. public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader) { return new StopFilter(enablePositionIncrements, new LowerCaseTokenizer(reader), stopWords); } /// Filters LowerCaseTokenizer with StopFilter. private class SavedStreams { public SavedStreams(StopAnalyzer enclosingInstance) { InitBlock(enclosingInstance); } private void InitBlock(StopAnalyzer enclosingInstance) { this.enclosingInstance = enclosingInstance; } private StopAnalyzer enclosingInstance; public StopAnalyzer Enclosing_Instance { get { return enclosingInstance; } } internal Tokenizer source; internal TokenStream result; } public override TokenStream ReusableTokenStream(System.String fieldName, System.IO.TextReader reader) { var streams = (SavedStreams) PreviousTokenStream; if (streams == null) { streams = new SavedStreams(this) {source = new LowerCaseTokenizer(reader)}; streams.result = new StopFilter(enablePositionIncrements, streams.source, stopWords); PreviousTokenStream = streams; } else streams.source.Reset(reader); return streams.result; } static StopAnalyzer() { { var stopWords = new System.String[]{"a", "an", "and", "are", "as", "at", "be", "but", "by", "for", "if", "in", "into", "is", "it", "no", "not", "of", "on", "or", "such", "that", "the", "their", "then", "there", "these", "they", "this", "to", "was", "will", "with"}; var stopSet = new CharArraySet(stopWords.Length, false); stopSet.AddAll(stopWords); ENGLISH_STOP_WORDS_SET = CharArraySet.UnmodifiableSet(stopSet); } } } }