pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / lv / LatvianStemmer.java
1 package org.apache.lucene.analysis.lv;
2
3 import static org.apache.lucene.analysis.util.StemmerUtil.*;
4
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /**
23  * Light stemmer for Latvian.
24  * <p>
25  * This is a light version of the algorithm in Karlis Kreslin's PhD thesis
26  * <i>A stemming algorithm for Latvian</i> with the following modifications:
27  * <ul>
28  *   <li>Only explicitly stems noun and adjective morphology
29  *   <li>Stricter length/vowel checks for the resulting stems (verb etc suffix stripping is removed)
30  *   <li>Removes only the primary inflectional suffixes: case and number for nouns ; 
31  *       case, number, gender, and definitiveness for adjectives.
32  *   <li>Palatalization is only handled when a declension II,V,VI noun suffix is removed.
33  * </ul>
34  */
35 public class LatvianStemmer {
36   /**
37    * Stem a latvian word. returns the new adjusted length.
38    */
39   public int stem(char s[], int len) {
40     int numVowels = numVowels(s, len);
41     
42     for (int i = 0; i < affixes.length; i++) {
43       Affix affix = affixes[i];
44       if (numVowels > affix.vc && len >= affix.affix.length + 3 && endsWith(s, len, affix.affix)) {
45         len -= affix.affix.length;
46         return affix.palatalizes ? unpalatalize(s, len) : len;
47       }
48     }
49     
50     return len;
51   }
52   
53   static final Affix affixes[] = {
54     new Affix("ajiem", 3, false), new Affix("ajai",  3, false), 
55     new Affix("ajam",  2, false), new Affix("ajām",  2, false),
56     new Affix("ajos",  2, false), new Affix("ajās",  2, false),
57     new Affix("iem",   2, true),  new Affix("ajā",   2, false),
58     new Affix("ais",   2, false), new Affix("ai",    2, false),
59     new Affix("ei",    2, false), new Affix("ām",    1, false),
60     new Affix("am",    1, false), new Affix("ēm",    1, false),
61     new Affix("īm",    1, false), new Affix("im",    1, false),
62     new Affix("um",    1, false), new Affix("us",    1, true),
63     new Affix("as",    1, false), new Affix("ās",    1, false),
64     new Affix("es",    1, false), new Affix("os",    1, true),
65     new Affix("ij",    1, false), new Affix("īs",    1, false),
66     new Affix("ēs",    1, false), new Affix("is",    1, false),
67     new Affix("ie",    1, false), new Affix("u",     1, true),
68     new Affix("a",     1, true),  new Affix("i",     1, true),
69     new Affix("e",     1, false), new Affix("ā",     1, false),
70     new Affix("ē",     1, false), new Affix("ī",     1, false),
71     new Affix("ū",     1, false), new Affix("o",     1, false),
72     new Affix("s",     0, false), new Affix("š",     0, false),
73   };
74
75   static class Affix {
76     char affix[];         // suffix
77     int vc;               // vowel count of the suffix
78     boolean palatalizes;  // true if we should fire palatalization rules.
79     
80     Affix(String affix, int vc, boolean palatalizes) {
81       this.affix = affix.toCharArray();
82       this.vc = vc;
83       this.palatalizes = palatalizes;
84     }
85   }
86
87   /**
88    * Most cases are handled except for the ambiguous ones:
89    * <ul>
90    *  <li> s -> š
91    *  <li> t -> š
92    *  <li> d -> ž
93    *  <li> z -> ž
94    * </ul>
95    */
96   private int unpalatalize(char s[], int len) {
97     // we check the character removed: if its -u then 
98     // its 2,5, or 6 gen pl., and these two can only apply then.
99     if (s[len] == 'u') {
100       // kš -> kst
101       if (endsWith(s, len, "kš")) {
102         len++;
103         s[len-2] = 's';
104         s[len-1] = 't';
105         return len;
106       }
107       // ņņ -> nn
108       if (endsWith(s, len, "ņņ")) {
109         s[len-2] = 'n';
110         s[len-1] = 'n';
111         return len;
112       }
113     }
114     
115     // otherwise all other rules
116     if (endsWith(s, len, "pj") || endsWith(s, len, "bj") 
117         || endsWith(s, len, "mj") || endsWith(s, len, "vj")) {
118       // labial consonant
119       return len-1;
120     } else if (endsWith(s, len, "šņ")) {
121       s[len-2] = 's';
122       s[len-1] = 'n';
123       return len;
124     } else if (endsWith(s, len, "žņ")) {
125       s[len-2] = 'z';
126       s[len-1] = 'n';
127       return len;
128     } else if (endsWith(s, len, "šļ")) {
129       s[len-2] = 's';
130       s[len-1] = 'l';
131       return len;
132     } else if (endsWith(s, len, "žļ")) {
133       s[len-2] = 'z';
134       s[len-1] = 'l';
135       return len;
136     } else if (endsWith(s, len, "ļņ")) {
137       s[len-2] = 'l';
138       s[len-1] = 'n';
139       return len;
140     } else if (endsWith(s, len, "ļļ")) {
141       s[len-2] = 'l';
142       s[len-1] = 'l';
143       return len;
144     } else if (s[len-1] == 'č') {
145       s[len-1] = 'c';
146       return len;
147     } else if (s[len-1] == 'ļ') {
148       s[len-1] = 'l';
149       return len;
150     } else if (s[len-1] == 'ņ') {
151       s[len-1] = 'n';
152       return len;
153     }
154     
155     return len;
156   }
157   
158   /**
159    * Count the vowels in the string, we always require at least
160    * one in the remaining stem to accept it.
161    */
162   private int numVowels(char s[], int len) {
163     int n = 0;
164     for (int i = 0; i < len; i++) {
165       switch(s[i]) {
166         case 'a': case 'e': case 'i':  
167         case 'o': case 'u': case 'ā':  
168         case 'ī': case 'ē': case 'ū':
169           n++;
170       }
171     }
172     return n;
173   }
174 }