add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / stempel / src / java / org / apache / lucene / analysis / stempel / StempelStemmer.java
1 /**
2  * Copyright 2004 The Apache Software Foundation
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16
17 package org.apache.lucene.analysis.stempel;
18
19 import java.io.BufferedInputStream;
20 import java.io.DataInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Locale;
24
25 import org.egothor.stemmer.Diff;
26 import org.egothor.stemmer.Trie;
27
28 /**
29  * <p>
30  * Stemmer class is a convenient facade for other stemmer-related classes. The
31  * core stemming algorithm and its implementation is taken verbatim from the
32  * Egothor project ( <a href="http://www.egothor.org">www.egothor.org </a>).
33  * </p>
34  * <p>
35  * Even though the stemmer tables supplied in the distribution package are built
36  * for Polish language, there is nothing language-specific here.
37  * </p>
38  */
39 public class StempelStemmer {
40   private Trie stemmer = null;
41   private StringBuilder buffer = new StringBuilder();
42
43   /**
44    * Create a Stemmer using selected stemmer table
45    * 
46    * @param stemmerTable stemmer table.
47    */
48   public StempelStemmer(InputStream stemmerTable) throws IOException {
49     this(load(stemmerTable));
50   }
51
52   /**
53    * Create a Stemmer using pre-loaded stemmer table
54    * 
55    * @param stemmer pre-loaded stemmer table
56    */
57   public StempelStemmer(Trie stemmer) {
58     this.stemmer = stemmer;
59   }
60   
61   /**
62    * Load a stemmer table from an inputstream.
63    */
64   public static Trie load(InputStream stemmerTable) throws IOException {
65     DataInputStream in = null;
66     try {
67       in = new DataInputStream(new BufferedInputStream(stemmerTable));
68       String method = in.readUTF().toUpperCase(Locale.ENGLISH);
69       if (method.indexOf('M') < 0) {
70         return new org.egothor.stemmer.Trie(in);
71       } else {
72         return new org.egothor.stemmer.MultiTrie2(in);
73       }
74     } finally {
75       in.close();
76     }
77   }
78
79   /**
80    * Stem a word. 
81    * 
82    * @param word input word to be stemmed.
83    * @return stemmed word, or null if the stem could not be generated.
84    */
85   public StringBuilder stem(CharSequence word) {
86     CharSequence cmd = stemmer.getLastOnPath(word);
87     
88     if (cmd == null)
89         return null;
90     
91     buffer.setLength(0);
92     buffer.append(word);
93
94     Diff.apply(buffer, cmd);
95     
96     if (buffer.length() > 0)
97       return buffer;
98     else
99       return null;
100   }
101 }