1 package org.apache.lucene.analysis.lv;
3 import static org.apache.lucene.analysis.util.StemmerUtil.*;
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * Light stemmer for Latvian.
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:
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.
35 public class LatvianStemmer {
37 * Stem a latvian word. returns the new adjusted length.
39 public int stem(char s[], int len) {
40 int numVowels = numVowels(s, len);
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;
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),
76 char affix[]; // suffix
77 int vc; // vowel count of the suffix
78 boolean palatalizes; // true if we should fire palatalization rules.
80 Affix(String affix, int vc, boolean palatalizes) {
81 this.affix = affix.toCharArray();
83 this.palatalizes = palatalizes;
88 * Most cases are handled except for the ambiguous ones:
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.
101 if (endsWith(s, len, "kš")) {
108 if (endsWith(s, len, "ņņ")) {
115 // otherwise all other rules
116 if (endsWith(s, len, "pj") || endsWith(s, len, "bj")
117 || endsWith(s, len, "mj") || endsWith(s, len, "vj")) {
120 } else if (endsWith(s, len, "šņ")) {
124 } else if (endsWith(s, len, "žņ")) {
128 } else if (endsWith(s, len, "šļ")) {
132 } else if (endsWith(s, len, "žļ")) {
136 } else if (endsWith(s, len, "ļņ")) {
140 } else if (endsWith(s, len, "ļļ")) {
144 } else if (s[len-1] == 'č') {
147 } else if (s[len-1] == 'ļ') {
150 } else if (s[len-1] == 'ņ') {
159 * Count the vowels in the string, we always require at least
160 * one in the remaining stem to accept it.
162 private int numVowels(char s[], int len) {
164 for (int i = 0; i < len; i++) {
166 case 'a': case 'e': case 'i':
167 case 'o': case 'u': case 'ā':
168 case 'ī': case 'ē': case 'ū':