pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / de / GermanLightStemmer.java
1 package org.apache.lucene.analysis.de;
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 /**
56  * Light Stemmer for German.
57  * <p>
58  * This stemmer implements the "UniNE" algorithm in:
59  * <i>Light Stemming Approaches for the French, Portuguese, German and Hungarian Languages</i>
60  * Jacques Savoy
61  */
62 public class GermanLightStemmer {
63   
64   public int stem(char s[], int len) {   
65     for (int i = 0; i < len; i++)
66       switch(s[i]) {
67         case 'ä':
68         case 'à':
69         case 'á':
70         case 'â': s[i] = 'a'; break;
71         case 'ö':
72         case 'ò':
73         case 'ó':
74         case 'ô': s[i] = 'o'; break;
75         case 'ï':
76         case 'ì':
77         case 'í':
78         case 'î': s[i] = 'i'; break;
79         case 'ü': 
80         case 'ù': 
81         case 'ú':
82         case 'û': s[i] = 'u'; break;
83       }
84     
85     len = step1(s, len);
86     return step2(s, len);
87   }
88   
89   private boolean stEnding(char ch) {
90     switch(ch) {
91       case 'b':
92       case 'd':
93       case 'f':
94       case 'g':
95       case 'h':
96       case 'k':
97       case 'l':
98       case 'm':
99       case 'n':
100       case 't': return true;
101       default: return false;
102     }
103   }
104   
105   private int step1(char s[], int len) {
106     if (len > 5 && s[len-3] == 'e' && s[len-2] == 'r' && s[len-1] == 'n')
107       return len - 3;
108     
109     if (len > 4 && s[len-2] == 'e')
110       switch(s[len-1]) {
111         case 'm':
112         case 'n':
113         case 'r':
114         case 's': return len - 2;
115       }
116     
117     if (len > 3 && s[len-1] == 'e')
118       return len - 1;
119     
120     if (len > 3 && s[len-1] == 's' && stEnding(s[len-2]))
121       return len - 1;
122     
123     return len;
124   }
125   
126   private int step2(char s[], int len) {
127     if (len > 5 && s[len-3] == 'e' && s[len-2] == 's' && s[len-1] == 't')
128       return len - 3;
129     
130     if (len > 4 && s[len-2] == 'e' && (s[len-1] == 'r' || s[len-1] == 'n'))
131       return len - 2;
132     
133     if (len > 4 && s[len-2] == 's' && s[len-1] == 't' && stEnding(s[len-3]))
134       return len - 2;
135     
136     return len;
137   }
138 }