1 package org.apache.lucene.analysis;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 Porter stemmer in Java. The original paper is in
24 Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
27 See also http://www.tartarus.org/~martin/PorterStemmer/index.html
29 Bug 1 (reported by Gonzalo Parra 16/10/99) fixed as marked below.
30 Tthe words 'aed', 'eed', 'oed' leave k at 'a' for step 3, and b[k-1]
31 is then out outside the bounds of b.
35 Bug 2 (reported by Steve Dyrdahl 22/2/00) fixed as marked below.
36 'ion' by itself leaves j = -1 in the test for 'ion' in step 5, and
37 b[j] is then outside the bounds of b.
41 [ This version is derived from Release 3, modified by Brian Goetz to
42 optimize for fewer object creations. ]
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.io.FileInputStream;
51 import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_CHAR;
52 import org.apache.lucene.util.ArrayUtil;
56 * Stemmer, implementing the Porter Stemming Algorithm
58 * The Stemmer class transforms a word into its root form. The input
59 * word can be provided a character at time (by calling add()), or at once
60 * by calling one of the various stem(something) methods.
66 private int i, /* offset into b */
68 private boolean dirty = false;
69 private static final int INITIAL_SIZE = 50;
71 public PorterStemmer() {
72 b = new char[INITIAL_SIZE];
77 * reset() resets the stemmer so it can stem another word. If you invoke
78 * the stemmer by calling add(char) and then stem(), you must call reset()
79 * before starting another word.
81 public void reset() { i = 0; dirty = false; }
84 * Add a character to the word being stemmed. When you are finished
85 * adding characters, you can call stem(void) to process the word.
87 public void add(char ch) {
89 b = ArrayUtil.grow(b, i+1);
95 * After a word has been stemmed, it can be retrieved by toString(),
96 * or a reference to the internal buffer can be retrieved by getResultBuffer
97 * and getResultLength (which is generally more efficient.)
100 public String toString() { return new String(b,0,i); }
103 * Returns the length of the word resulting from the stemming process.
105 public int getResultLength() { return i; }
108 * Returns a reference to a character buffer containing the results of
109 * the stemming process. You also need to consult getResultLength()
110 * to determine the length of the result.
112 public char[] getResultBuffer() { return b; }
114 /* cons(i) is true <=> b[i] is a consonant. */
116 private final boolean cons(int i) {
118 case 'a': case 'e': case 'i': case 'o': case 'u':
121 return (i==k0) ? true : !cons(i-1);
127 /* m() measures the number of consonant sequences between k0 and j. if c is
128 a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
138 private final int m() {
170 /* vowelinstem() is true <=> k0,...j contains a vowel */
172 private final boolean vowelinstem() {
174 for (i = k0; i <= j; i++)
180 /* doublec(j) is true <=> j,(j-1) contain a double consonant. */
182 private final boolean doublec(int j) {
190 /* cvc(i) is true <=> i-2,i-1,i has the form consonant - vowel - consonant
191 and also if the second c is not w,x or y. this is used when trying to
192 restore an e at the end of a short word. e.g.
194 cav(e), lov(e), hop(e), crim(e), but
199 private final boolean cvc(int i) {
200 if (i < k0+2 || !cons(i) || cons(i-1) || !cons(i-2))
204 if (ch == 'w' || ch == 'x' || ch == 'y') return false;
209 private final boolean ends(String s) {
214 for (int i = 0; i < l; i++)
215 if (b[o+i] != s.charAt(i))
221 /* setto(s) sets (j+1),...k to the characters in the string s, readjusting
224 void setto(String s) {
227 for (int i = 0; i < l; i++)
228 b[o+i] = s.charAt(i);
233 /* r(s) is used further down. */
235 void r(String s) { if (m() > 0) setto(s); }
237 /* step1() gets rid of plurals and -ed or -ing. e.g.
259 private final void step1() {
261 if (ends("sses")) k -= 2;
262 else if (ends("ies")) setto("i");
263 else if (b[k-1] != 's') k--;
269 else if ((ends("ed") || ends("ing")) && vowelinstem()) {
271 if (ends("at")) setto("ate");
272 else if (ends("bl")) setto("ble");
273 else if (ends("iz")) setto("ize");
274 else if (doublec(k)) {
276 if (ch == 'l' || ch == 's' || ch == 'z')
279 else if (m() == 1 && cvc(k))
284 /* step2() turns terminal y to i when there is another vowel in the stem. */
286 private final void step2() {
287 if (ends("y") && vowelinstem()) {
293 /* step3() maps double suffices to single ones. so -ization ( = -ize plus
294 -ation) maps to -ize etc. note that the string before the suffix must give
297 private final void step3() {
298 if (k == k0) return; /* For Bug 1 */
301 if (ends("ational")) { r("ate"); break; }
302 if (ends("tional")) { r("tion"); break; }
305 if (ends("enci")) { r("ence"); break; }
306 if (ends("anci")) { r("ance"); break; }
309 if (ends("izer")) { r("ize"); break; }
312 if (ends("bli")) { r("ble"); break; }
313 if (ends("alli")) { r("al"); break; }
314 if (ends("entli")) { r("ent"); break; }
315 if (ends("eli")) { r("e"); break; }
316 if (ends("ousli")) { r("ous"); break; }
319 if (ends("ization")) { r("ize"); break; }
320 if (ends("ation")) { r("ate"); break; }
321 if (ends("ator")) { r("ate"); break; }
324 if (ends("alism")) { r("al"); break; }
325 if (ends("iveness")) { r("ive"); break; }
326 if (ends("fulness")) { r("ful"); break; }
327 if (ends("ousness")) { r("ous"); break; }
330 if (ends("aliti")) { r("al"); break; }
331 if (ends("iviti")) { r("ive"); break; }
332 if (ends("biliti")) { r("ble"); break; }
335 if (ends("logi")) { r("log"); break; }
339 /* step4() deals with -ic-, -full, -ness etc. similar strategy to step3. */
341 private final void step4() {
344 if (ends("icate")) { r("ic"); break; }
345 if (ends("ative")) { r(""); break; }
346 if (ends("alize")) { r("al"); break; }
349 if (ends("iciti")) { r("ic"); break; }
352 if (ends("ical")) { r("ic"); break; }
353 if (ends("ful")) { r(""); break; }
356 if (ends("ness")) { r(""); break; }
361 /* step5() takes off -ant, -ence etc., in context <c>vcvc<v>. */
363 private final void step5() {
364 if (k == k0) return; /* for Bug 1 */
367 if (ends("al")) break;
370 if (ends("ance")) break;
371 if (ends("ence")) break;
374 if (ends("er")) break; return;
376 if (ends("ic")) break; return;
378 if (ends("able")) break;
379 if (ends("ible")) break; return;
381 if (ends("ant")) break;
382 if (ends("ement")) break;
383 if (ends("ment")) break;
384 /* element etc. not stripped before the m */
385 if (ends("ent")) break;
388 if (ends("ion") && j >= 0 && (b[j] == 's' || b[j] == 't')) break;
389 /* j >= 0 fixes Bug 2 */
390 if (ends("ou")) break;
392 /* takes care of -ous */
394 if (ends("ism")) break;
397 if (ends("ate")) break;
398 if (ends("iti")) break;
401 if (ends("ous")) break;
404 if (ends("ive")) break;
407 if (ends("ize")) break;
416 /* step6() removes a final -e if m() > 1. */
418 private final void step6() {
422 if (a > 1 || a == 1 && !cvc(k-1))
425 if (b[k] == 'l' && doublec(k) && m() > 1)
431 * Stem a word provided as a String. Returns the result as a String.
433 public String stem(String s) {
434 if (stem(s.toCharArray(), s.length()))
440 /** Stem a word contained in a char[]. Returns true if the stemming process
441 * resulted in a word different from the input. You can retrieve the
442 * result with getResultLength()/getResultBuffer() or toString().
444 public boolean stem(char[] word) {
445 return stem(word, word.length);
448 /** Stem a word contained in a portion of a char[] array. Returns
449 * true if the stemming process resulted in a word different from
450 * the input. You can retrieve the result with
451 * getResultLength()/getResultBuffer() or toString().
453 public boolean stem(char[] wordBuffer, int offset, int wordLen) {
455 if (b.length < wordLen) {
456 b = new char[ArrayUtil.oversize(wordLen, NUM_BYTES_CHAR)];
458 System.arraycopy(wordBuffer, offset, b, 0, wordLen);
463 /** Stem a word contained in a leading portion of a char[] array.
464 * Returns true if the stemming process resulted in a word different
465 * from the input. You can retrieve the result with
466 * getResultLength()/getResultBuffer() or toString().
468 public boolean stem(char[] word, int wordLen) {
469 return stem(word, 0, wordLen);
472 /** Stem the word placed into the Stemmer buffer through calls to add().
473 * Returns true if the stemming process resulted in a word different
474 * from the input. You can retrieve the result with
475 * getResultLength()/getResultBuffer() or toString().
477 public boolean stem() {
481 public boolean stem(int i0) {
485 step1(); step2(); step3(); step4(); step5(); step6();
487 // Also, a word is considered dirty if we lopped off letters
488 // Thanks to Ifigenia Vairelles for pointing this out.
495 /** Test program for demonstrating the Stemmer. It reads a file and
496 * stems each word, writing the result to standard out.
497 * Usage: Stemmer file-name
499 public static void main(String[] args) {
500 PorterStemmer s = new PorterStemmer();
502 for (int i = 0; i < args.length; i++) {
504 InputStream in = new FileInputStream(args[i]);
505 byte[] buffer = new byte[1024];
506 int bufferLen, offset, ch;
508 bufferLen = in.read(buffer);
513 if (offset < bufferLen)
514 ch = buffer[offset++];
516 bufferLen = in.read(buffer);
521 ch = buffer[offset++];
524 if (Character.isLetter((char) ch)) {
525 s.add(Character.toLowerCase((char) ch));
529 System.out.print(s.toString());
534 System.out.print((char) ch);
541 catch (IOException e) {
542 System.out.println("error reading " + args[i]);