Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
StandardAnalyzer.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using Lucene.Net.Analysis;
22 using Lucene.Net.Util;
23 using Version = Lucene.Net.Util.Version;
24 
25 namespace Lucene.Net.Analysis.Standard
26 {
27 
42  public class StandardAnalyzer : Analyzer
43  {
44  private ISet<string> stopSet;
45 
49  private bool replaceInvalidAcronym, enableStopPositionIncrements;
50 
54  public static readonly ISet<string> STOP_WORDS_SET;
55  private Version matchVersion;
56 
60  public StandardAnalyzer(Version matchVersion)
61  : this(matchVersion, STOP_WORDS_SET)
62  { }
63 
70  public StandardAnalyzer(Version matchVersion, ISet<string> stopWords)
71  {
72  stopSet = stopWords;
73  SetOverridesTokenStreamMethod<StandardAnalyzer>();
74  enableStopPositionIncrements = StopFilter.GetEnablePositionIncrementsVersionDefault(matchVersion);
75  replaceInvalidAcronym = matchVersion.OnOrAfter(Version.LUCENE_24);
76  this.matchVersion = matchVersion;
77  }
78 
87  public StandardAnalyzer(Version matchVersion, System.IO.FileInfo stopwords)
88  : this (matchVersion, WordlistLoader.GetWordSet(stopwords))
89  {
90  }
91 
100  public StandardAnalyzer(Version matchVersion, System.IO.TextReader stopwords)
101  : this(matchVersion, WordlistLoader.GetWordSet(stopwords))
102  { }
103 
107  public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
108  {
109  StandardTokenizer tokenStream = new StandardTokenizer(matchVersion, reader);
110  tokenStream.MaxTokenLength = maxTokenLength;
111  TokenStream result = new StandardFilter(tokenStream);
112  result = new LowerCaseFilter(result);
113  result = new StopFilter(enableStopPositionIncrements, result, stopSet);
114  return result;
115  }
116 
117  private sealed class SavedStreams
118  {
119  internal StandardTokenizer tokenStream;
120  internal TokenStream filteredTokenStream;
121  }
122 
124  public const int DEFAULT_MAX_TOKEN_LENGTH = 255;
125 
126  private int maxTokenLength = DEFAULT_MAX_TOKEN_LENGTH;
127 
133  public virtual int MaxTokenLength
134  {
135  get { return maxTokenLength; }
136  set { maxTokenLength = value; }
137  }
138 
139  public override TokenStream ReusableTokenStream(System.String fieldName, System.IO.TextReader reader)
140  {
141  if (overridesTokenStreamMethod)
142  {
143  // LUCENE-1678: force fallback to tokenStream() if we
144  // have been subclassed and that subclass overrides
145  // tokenStream but not reusableTokenStream
146  return TokenStream(fieldName, reader);
147  }
148  SavedStreams streams = (SavedStreams) PreviousTokenStream;
149  if (streams == null)
150  {
151  streams = new SavedStreams();
152  PreviousTokenStream = streams;
153  streams.tokenStream = new StandardTokenizer(matchVersion, reader);
154  streams.filteredTokenStream = new StandardFilter(streams.tokenStream);
155  streams.filteredTokenStream = new LowerCaseFilter(streams.filteredTokenStream);
156  streams.filteredTokenStream = new StopFilter(enableStopPositionIncrements,
157  streams.filteredTokenStream, stopSet);
158  }
159  else
160  {
161  streams.tokenStream.Reset(reader);
162  }
163  streams.tokenStream.MaxTokenLength = maxTokenLength;
164 
165  streams.tokenStream.SetReplaceInvalidAcronym(replaceInvalidAcronym);
166 
167  return streams.filteredTokenStream;
168  }
169  static StandardAnalyzer()
170  {
171  STOP_WORDS_SET = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
172  }
173  }
174 }