pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / fi / FinnishLightStemmer.java
1 package org.apache.lucene.analysis.fi;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /* 
21  * This algorithm is updated based on code located at:
22  * http://members.unine.ch/jacques.savoy/clef/
23  * 
24  * Full copyright for that code follows:
25  */
26
27 /*
28  * Copyright (c) 2005, Jacques Savoy
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without 
32  * modification, are permitted provided that the following conditions are met:
33  *
34  * Redistributions of source code must retain the above copyright notice, this 
35  * list of conditions and the following disclaimer. Redistributions in binary 
36  * form must reproduce the above copyright notice, this list of conditions and
37  * the following disclaimer in the documentation and/or other materials 
38  * provided with the distribution. Neither the name of the author nor the names 
39  * of its contributors may be used to endorse or promote products derived from 
40  * this software without specific prior written permission.
41  * 
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
43  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
45  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
46  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
47  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
48  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
49  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
50  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
51  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52  * POSSIBILITY OF SUCH DAMAGE.
53  */
54
55 import static org.apache.lucene.analysis.util.StemmerUtil.*;
56
57 /**
58  * Light Stemmer for Finnish.
59  * <p>
60  * This stemmer implements the algorithm described in:
61  * <i>Report on CLEF-2003 Monolingual Tracks</i>
62  * Jacques Savoy
63  */
64 public class FinnishLightStemmer {
65   
66   public int stem(char s[], int len) {
67     if (len < 4)
68       return len;
69     
70     for (int i = 0; i < len; i++)
71       switch(s[i]) {
72         case 'ä':
73         case 'å': s[i] = 'a'; break;
74         case 'ö': s[i] = 'o'; break;
75       }
76     
77     len = step1(s, len);
78     len = step2(s, len);
79     len = step3(s, len);
80     len = norm1(s, len);
81     len = norm2(s, len);
82     return len;
83   }
84   
85   private int step1(char s[], int len) {
86     if (len > 8) {
87       if (endsWith(s, len, "kin"))
88         return step1(s, len-3);
89       if (endsWith(s, len, "ko"))
90         return step1(s, len-2);
91     }
92     
93     if (len > 11) {
94       if (endsWith(s, len, "dellinen"))
95         return len-8;
96       if (endsWith(s, len, "dellisuus"))
97         return len-9;
98     }
99     return len;
100   }
101   
102   private int step2(char s[], int len) {
103     if (len > 5) {
104       if (endsWith(s, len, "lla")
105           || endsWith(s, len, "tse")
106           || endsWith(s, len, "sti"))
107         return len-3;
108       
109       if (endsWith(s, len, "ni"))
110         return len-2;
111       
112       if (endsWith(s, len, "aa"))
113         return len-1; // aa -> a
114     }
115     
116     return len;
117   }
118   
119   private int step3(char s[], int len) {
120     if (len > 8) {
121       if (endsWith(s, len, "nnen")) {
122         s[len-4] = 's';
123         return len-3;
124       }
125       
126       if (endsWith(s, len, "ntena")) {
127         s[len-5] = 's';
128         return len-4;
129       }
130       
131       if (endsWith(s, len, "tten"))
132         return len-4;
133       
134       if (endsWith(s, len, "eiden"))
135         return len-5;
136     }
137     
138     if (len > 6) {
139       if (endsWith(s, len, "neen")
140           || endsWith(s, len, "niin")
141           || endsWith(s, len, "seen")
142           || endsWith(s, len, "teen")
143           || endsWith(s, len, "inen"))
144           return len-4;
145       
146       if (s[len-3] == 'h' && isVowel(s[len-2]) && s[len-1] == 'n')
147         return len-3;
148       
149       if (endsWith(s, len, "den")) {
150         s[len-3] = 's';
151         return len-2;
152       }
153       
154       if (endsWith(s, len, "ksen")) {
155         s[len-4] = 's';
156         return len-3;
157       }
158       
159       if (endsWith(s, len, "ssa")
160           || endsWith(s, len, "sta")
161           || endsWith(s, len, "lla")
162           || endsWith(s, len, "lta")
163           || endsWith(s, len, "tta")
164           || endsWith(s, len, "ksi")
165           || endsWith(s, len, "lle"))
166         return len-3; 
167     }
168     
169     if (len > 5) {
170       if (endsWith(s, len, "na")
171           || endsWith(s, len, "ne"))
172         return len-2;
173       
174       if (endsWith(s, len, "nei"))
175         return len-3;
176     }
177     
178     if (len > 4) {
179       if (endsWith(s, len, "ja")
180           || endsWith(s, len, "ta"))
181         return len-2;
182       
183       if (s[len-1] == 'a')
184         return len-1;
185       
186       if (s[len-1] == 'n' && isVowel(s[len-2]))
187         return len-2;
188       
189       if (s[len-1] == 'n')
190         return len-1;
191     }
192     
193     return len;
194   }
195   
196   private int norm1(char s[], int len) {
197     if (len > 5 && endsWith(s, len, "hde")) {
198         s[len-3] = 'k';
199         s[len-2] = 's';
200         s[len-1] = 'i';
201     }
202     
203     if (len > 4) {
204       if (endsWith(s, len, "ei") || endsWith(s, len, "at"))
205         return len-2;
206     }
207     
208     if (len > 3)
209       switch(s[len-1]) {
210         case 't':
211         case 's':
212         case 'j':
213         case 'e':
214         case 'a':
215         case 'i': return len-1;
216       }
217     
218     return len;
219   }
220   
221   private int norm2(char s[], int len) {
222     if (len > 8) {
223       if (s[len-1] == 'e' 
224           || s[len-1] == 'o' 
225           || s[len-1] == 'u')
226         len--;
227     }
228     
229     if (len > 4) {
230       if (s[len-1] == 'i')
231         len--;
232       
233       if (len > 4) {
234         char ch = s[0];
235         for (int i = 1; i < len; i++) {
236           if (s[i] == ch &&
237               (ch == 'k' || ch == 'p' || ch == 't'))
238             len = delete(s, i--, len);
239           else
240             ch = s[i];
241         }
242       }
243     }
244     
245     return len;
246   }
247   
248   private boolean isVowel(char ch) {
249     switch(ch) {
250       case 'a':
251       case 'e':
252       case 'i':
253       case 'o':
254       case 'u':
255       case 'y': return true;
256       default: return false;
257     }
258   }
259 }