add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / ru / RussianLightStemmer.java
1 package org.apache.lucene.analysis.ru;
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 Russian.
59  * <p>
60  * This stemmer implements the following algorithm:
61  * <i>Indexing and Searching Strategies for the Russian Language.</i>
62  * Ljiljana Dolamic and Jacques Savoy.
63  */
64 public class RussianLightStemmer {
65
66   public int stem(char s[], int len) {
67     len = removeCase(s, len);
68     return normalize(s, len);
69   }
70   
71   private int normalize(char s[], int len) {
72     if (len > 3)
73       switch(s[len-1]) { 
74         case 'ь':
75         case 'и': return len - 1;
76         case 'н': if (s[len-2] == 'н') return len - 1;
77       }
78     return len;
79   }
80
81   private int removeCase(char s[], int len) {
82     if (len > 6 && 
83         (endsWith(s, len, "иями") ||
84          endsWith(s, len, "оями")))
85       return len - 4;
86     
87     if (len > 5 && 
88         (endsWith(s, len, "иям") ||
89          endsWith(s, len, "иях") ||
90          endsWith(s, len, "оях") ||
91          endsWith(s, len, "ями") ||
92          endsWith(s, len, "оям") ||
93          endsWith(s, len, "оьв") ||
94          endsWith(s, len, "ами") ||
95          endsWith(s, len, "его") ||
96          endsWith(s, len, "ему") ||
97          endsWith(s, len, "ери") ||
98          endsWith(s, len, "ими") ||
99          endsWith(s, len, "ого") ||
100          endsWith(s, len, "ому") ||
101          endsWith(s, len, "ыми") ||
102          endsWith(s, len, "оев")))
103       return len - 3;
104     
105     if (len > 4 &&
106         (endsWith(s, len, "ая") ||
107          endsWith(s, len, "яя") ||
108          endsWith(s, len, "ях") ||
109          endsWith(s, len, "юю") ||
110          endsWith(s, len, "ах") ||
111          endsWith(s, len, "ею") ||
112          endsWith(s, len, "их") ||
113          endsWith(s, len, "ия") ||
114          endsWith(s, len, "ию") ||
115          endsWith(s, len, "ьв") ||
116          endsWith(s, len, "ою") ||
117          endsWith(s, len, "ую") ||
118          endsWith(s, len, "ям") ||
119          endsWith(s, len, "ых") ||
120          endsWith(s, len, "ея") ||
121          endsWith(s, len, "ам") ||
122          endsWith(s, len, "ем") ||
123          endsWith(s, len, "ей") ||
124          endsWith(s, len, "ём") ||
125          endsWith(s, len, "ев") ||
126          endsWith(s, len, "ий") ||
127          endsWith(s, len, "им") ||
128          endsWith(s, len, "ое") ||
129          endsWith(s, len, "ой") ||
130          endsWith(s, len, "ом") ||
131          endsWith(s, len, "ов") ||
132          endsWith(s, len, "ые") ||
133          endsWith(s, len, "ый") ||
134          endsWith(s, len, "ым") ||
135          endsWith(s, len, "ми")))
136       return len - 2;
137     
138     if (len > 3)
139       switch(s[len-1]) {
140         case 'а':
141         case 'е':
142         case 'и':
143         case 'о':
144         case 'у':
145         case 'й':
146         case 'ы':
147         case 'я':
148         case 'ь': return len - 1;
149       }
150     
151     return len;
152   }
153 }