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
SnowballFilter.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 Lucene.Net.Analysis.Tokenattributes;
20 using Token = Lucene.Net.Analysis.Token;
21 using TokenFilter = Lucene.Net.Analysis.TokenFilter;
22 using TokenStream = Lucene.Net.Analysis.TokenStream;
23 using SnowballProgram = SF.Snowball.SnowballProgram;
24 using SF.Snowball.Ext;
25 
26 namespace Lucene.Net.Analysis.Snowball
27 {
28 
35 
36  public sealed class SnowballFilter : TokenFilter
37  {
38  private static readonly System.Object[] EMPTY_ARGS = new System.Object[0];
39 
40  private SnowballProgram stemmer;
41  private ITermAttribute termAtt;
42  //private System.Reflection.MethodInfo stemMethod;
43 
45  : base(input)
46  {
47  this.stemmer = stemmer;
48  termAtt = AddAttribute<ITermAttribute>();
49  }
50 
58  public SnowballFilter(TokenStream input, System.String name) : base(input)
59  {
60  try
61  {
62  System.Type stemClass = System.Type.GetType("SF.Snowball.Ext." + name + "Stemmer");
63  stemmer = (SnowballProgram) System.Activator.CreateInstance(stemClass);
64  }
65  catch (System.Exception e)
66  {
67  throw new System.SystemException(e.ToString());
68  }
69  termAtt = AddAttribute<ITermAttribute>();
70  }
71 
73  public sealed override bool IncrementToken()
74  {
75  if (input.IncrementToken())
76  {
77  String originalTerm = termAtt.Term;
78  stemmer.SetCurrent(originalTerm);
79  stemmer.Stem();
80  String finalTerm = stemmer.GetCurrent();
81  // Don't bother updating, if it is unchanged.
82  if (!originalTerm.Equals(finalTerm))
83  termAtt.SetTermBuffer(finalTerm);
84  return true;
85  }
86  else
87  {
88  return false;
89  }
90  }
91  }
92 }