001: package org.apache.lucene.analysis;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: /*
021:
022: Porter stemmer in Java. The original paper is in
023:
024: Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
025: no. 3, pp 130-137,
026:
027: See also http://www.tartarus.org/~martin/PorterStemmer/index.html
028:
029: Bug 1 (reported by Gonzalo Parra 16/10/99) fixed as marked below.
030: Tthe words 'aed', 'eed', 'oed' leave k at 'a' for step 3, and b[k-1]
031: is then out outside the bounds of b.
032:
033: Similarly,
034:
035: Bug 2 (reported by Steve Dyrdahl 22/2/00) fixed as marked below.
036: 'ion' by itself leaves j = -1 in the test for 'ion' in step 5, and
037: b[j] is then outside the bounds of b.
038:
039: Release 3.
040:
041: [ This version is derived from Release 3, modified by Brian Goetz to
042: optimize for fewer object creations. ]
043:
044: */
045:
046: import java.io.*;
047:
048: /**
049: *
050: * Stemmer, implementing the Porter Stemming Algorithm
051: *
052: * The Stemmer class transforms a word into its root form. The input
053: * word can be provided a character at time (by calling add()), or at once
054: * by calling one of the various stem(something) methods.
055: */
056:
057: class PorterStemmer {
058: private char[] b;
059: private int i, /* offset into b */
060: j, k, k0;
061: private boolean dirty = false;
062: private static final int INC = 50; /* unit of size whereby b is increased */
063: private static final int EXTRA = 1;
064:
065: public PorterStemmer() {
066: b = new char[INC];
067: i = 0;
068: }
069:
070: /**
071: * reset() resets the stemmer so it can stem another word. If you invoke
072: * the stemmer by calling add(char) and then stem(), you must call reset()
073: * before starting another word.
074: */
075: public void reset() {
076: i = 0;
077: dirty = false;
078: }
079:
080: /**
081: * Add a character to the word being stemmed. When you are finished
082: * adding characters, you can call stem(void) to process the word.
083: */
084: public void add(char ch) {
085: if (b.length <= i + EXTRA) {
086: char[] new_b = new char[b.length + INC];
087: System.arraycopy(b, 0, new_b, 0, b.length);
088: b = new_b;
089: }
090: b[i++] = ch;
091: }
092:
093: /**
094: * After a word has been stemmed, it can be retrieved by toString(),
095: * or a reference to the internal buffer can be retrieved by getResultBuffer
096: * and getResultLength (which is generally more efficient.)
097: */
098: public String toString() {
099: return new String(b, 0, i);
100: }
101:
102: /**
103: * Returns the length of the word resulting from the stemming process.
104: */
105: public int getResultLength() {
106: return i;
107: }
108:
109: /**
110: * Returns a reference to a character buffer containing the results of
111: * the stemming process. You also need to consult getResultLength()
112: * to determine the length of the result.
113: */
114: public char[] getResultBuffer() {
115: return b;
116: }
117:
118: /* cons(i) is true <=> b[i] is a consonant. */
119:
120: private final boolean cons(int i) {
121: switch (b[i]) {
122: case 'a':
123: case 'e':
124: case 'i':
125: case 'o':
126: case 'u':
127: return false;
128: case 'y':
129: return (i == k0) ? true : !cons(i - 1);
130: default:
131: return true;
132: }
133: }
134:
135: /* m() measures the number of consonant sequences between k0 and j. if c is
136: a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
137: presence,
138:
139: <c><v> gives 0
140: <c>vc<v> gives 1
141: <c>vcvc<v> gives 2
142: <c>vcvcvc<v> gives 3
143: ....
144: */
145:
146: private final int m() {
147: int n = 0;
148: int i = k0;
149: while (true) {
150: if (i > j)
151: return n;
152: if (!cons(i))
153: break;
154: i++;
155: }
156: i++;
157: while (true) {
158: while (true) {
159: if (i > j)
160: return n;
161: if (cons(i))
162: break;
163: i++;
164: }
165: i++;
166: n++;
167: while (true) {
168: if (i > j)
169: return n;
170: if (!cons(i))
171: break;
172: i++;
173: }
174: i++;
175: }
176: }
177:
178: /* vowelinstem() is true <=> k0,...j contains a vowel */
179:
180: private final boolean vowelinstem() {
181: int i;
182: for (i = k0; i <= j; i++)
183: if (!cons(i))
184: return true;
185: return false;
186: }
187:
188: /* doublec(j) is true <=> j,(j-1) contain a double consonant. */
189:
190: private final boolean doublec(int j) {
191: if (j < k0 + 1)
192: return false;
193: if (b[j] != b[j - 1])
194: return false;
195: return cons(j);
196: }
197:
198: /* cvc(i) is true <=> i-2,i-1,i has the form consonant - vowel - consonant
199: and also if the second c is not w,x or y. this is used when trying to
200: restore an e at the end of a short word. e.g.
201:
202: cav(e), lov(e), hop(e), crim(e), but
203: snow, box, tray.
204:
205: */
206:
207: private final boolean cvc(int i) {
208: if (i < k0 + 2 || !cons(i) || cons(i - 1) || !cons(i - 2))
209: return false;
210: else {
211: int ch = b[i];
212: if (ch == 'w' || ch == 'x' || ch == 'y')
213: return false;
214: }
215: return true;
216: }
217:
218: private final boolean ends(String s) {
219: int l = s.length();
220: int o = k - l + 1;
221: if (o < k0)
222: return false;
223: for (int i = 0; i < l; i++)
224: if (b[o + i] != s.charAt(i))
225: return false;
226: j = k - l;
227: return true;
228: }
229:
230: /* setto(s) sets (j+1),...k to the characters in the string s, readjusting
231: k. */
232:
233: void setto(String s) {
234: int l = s.length();
235: int o = j + 1;
236: for (int i = 0; i < l; i++)
237: b[o + i] = s.charAt(i);
238: k = j + l;
239: dirty = true;
240: }
241:
242: /* r(s) is used further down. */
243:
244: void r(String s) {
245: if (m() > 0)
246: setto(s);
247: }
248:
249: /* step1() gets rid of plurals and -ed or -ing. e.g.
250:
251: caresses -> caress
252: ponies -> poni
253: ties -> ti
254: caress -> caress
255: cats -> cat
256:
257: feed -> feed
258: agreed -> agree
259: disabled -> disable
260:
261: matting -> mat
262: mating -> mate
263: meeting -> meet
264: milling -> mill
265: messing -> mess
266:
267: meetings -> meet
268:
269: */
270:
271: private final void step1() {
272: if (b[k] == 's') {
273: if (ends("sses"))
274: k -= 2;
275: else if (ends("ies"))
276: setto("i");
277: else if (b[k - 1] != 's')
278: k--;
279: }
280: if (ends("eed")) {
281: if (m() > 0)
282: k--;
283: } else if ((ends("ed") || ends("ing")) && vowelinstem()) {
284: k = j;
285: if (ends("at"))
286: setto("ate");
287: else if (ends("bl"))
288: setto("ble");
289: else if (ends("iz"))
290: setto("ize");
291: else if (doublec(k)) {
292: int ch = b[k--];
293: if (ch == 'l' || ch == 's' || ch == 'z')
294: k++;
295: } else if (m() == 1 && cvc(k))
296: setto("e");
297: }
298: }
299:
300: /* step2() turns terminal y to i when there is another vowel in the stem. */
301:
302: private final void step2() {
303: if (ends("y") && vowelinstem()) {
304: b[k] = 'i';
305: dirty = true;
306: }
307: }
308:
309: /* step3() maps double suffices to single ones. so -ization ( = -ize plus
310: -ation) maps to -ize etc. note that the string before the suffix must give
311: m() > 0. */
312:
313: private final void step3() {
314: if (k == k0)
315: return; /* For Bug 1 */
316: switch (b[k - 1]) {
317: case 'a':
318: if (ends("ational")) {
319: r("ate");
320: break;
321: }
322: if (ends("tional")) {
323: r("tion");
324: break;
325: }
326: break;
327: case 'c':
328: if (ends("enci")) {
329: r("ence");
330: break;
331: }
332: if (ends("anci")) {
333: r("ance");
334: break;
335: }
336: break;
337: case 'e':
338: if (ends("izer")) {
339: r("ize");
340: break;
341: }
342: break;
343: case 'l':
344: if (ends("bli")) {
345: r("ble");
346: break;
347: }
348: if (ends("alli")) {
349: r("al");
350: break;
351: }
352: if (ends("entli")) {
353: r("ent");
354: break;
355: }
356: if (ends("eli")) {
357: r("e");
358: break;
359: }
360: if (ends("ousli")) {
361: r("ous");
362: break;
363: }
364: break;
365: case 'o':
366: if (ends("ization")) {
367: r("ize");
368: break;
369: }
370: if (ends("ation")) {
371: r("ate");
372: break;
373: }
374: if (ends("ator")) {
375: r("ate");
376: break;
377: }
378: break;
379: case 's':
380: if (ends("alism")) {
381: r("al");
382: break;
383: }
384: if (ends("iveness")) {
385: r("ive");
386: break;
387: }
388: if (ends("fulness")) {
389: r("ful");
390: break;
391: }
392: if (ends("ousness")) {
393: r("ous");
394: break;
395: }
396: break;
397: case 't':
398: if (ends("aliti")) {
399: r("al");
400: break;
401: }
402: if (ends("iviti")) {
403: r("ive");
404: break;
405: }
406: if (ends("biliti")) {
407: r("ble");
408: break;
409: }
410: break;
411: case 'g':
412: if (ends("logi")) {
413: r("log");
414: break;
415: }
416: }
417: }
418:
419: /* step4() deals with -ic-, -full, -ness etc. similar strategy to step3. */
420:
421: private final void step4() {
422: switch (b[k]) {
423: case 'e':
424: if (ends("icate")) {
425: r("ic");
426: break;
427: }
428: if (ends("ative")) {
429: r("");
430: break;
431: }
432: if (ends("alize")) {
433: r("al");
434: break;
435: }
436: break;
437: case 'i':
438: if (ends("iciti")) {
439: r("ic");
440: break;
441: }
442: break;
443: case 'l':
444: if (ends("ical")) {
445: r("ic");
446: break;
447: }
448: if (ends("ful")) {
449: r("");
450: break;
451: }
452: break;
453: case 's':
454: if (ends("ness")) {
455: r("");
456: break;
457: }
458: break;
459: }
460: }
461:
462: /* step5() takes off -ant, -ence etc., in context <c>vcvc<v>. */
463:
464: private final void step5() {
465: if (k == k0)
466: return; /* for Bug 1 */
467: switch (b[k - 1]) {
468: case 'a':
469: if (ends("al"))
470: break;
471: return;
472: case 'c':
473: if (ends("ance"))
474: break;
475: if (ends("ence"))
476: break;
477: return;
478: case 'e':
479: if (ends("er"))
480: break;
481: return;
482: case 'i':
483: if (ends("ic"))
484: break;
485: return;
486: case 'l':
487: if (ends("able"))
488: break;
489: if (ends("ible"))
490: break;
491: return;
492: case 'n':
493: if (ends("ant"))
494: break;
495: if (ends("ement"))
496: break;
497: if (ends("ment"))
498: break;
499: /* element etc. not stripped before the m */
500: if (ends("ent"))
501: break;
502: return;
503: case 'o':
504: if (ends("ion") && j >= 0 && (b[j] == 's' || b[j] == 't'))
505: break;
506: /* j >= 0 fixes Bug 2 */
507: if (ends("ou"))
508: break;
509: return;
510: /* takes care of -ous */
511: case 's':
512: if (ends("ism"))
513: break;
514: return;
515: case 't':
516: if (ends("ate"))
517: break;
518: if (ends("iti"))
519: break;
520: return;
521: case 'u':
522: if (ends("ous"))
523: break;
524: return;
525: case 'v':
526: if (ends("ive"))
527: break;
528: return;
529: case 'z':
530: if (ends("ize"))
531: break;
532: return;
533: default:
534: return;
535: }
536: if (m() > 1)
537: k = j;
538: }
539:
540: /* step6() removes a final -e if m() > 1. */
541:
542: private final void step6() {
543: j = k;
544: if (b[k] == 'e') {
545: int a = m();
546: if (a > 1 || a == 1 && !cvc(k - 1))
547: k--;
548: }
549: if (b[k] == 'l' && doublec(k) && m() > 1)
550: k--;
551: }
552:
553: /**
554: * Stem a word provided as a String. Returns the result as a String.
555: */
556: public String stem(String s) {
557: if (stem(s.toCharArray(), s.length()))
558: return toString();
559: else
560: return s;
561: }
562:
563: /** Stem a word contained in a char[]. Returns true if the stemming process
564: * resulted in a word different from the input. You can retrieve the
565: * result with getResultLength()/getResultBuffer() or toString().
566: */
567: public boolean stem(char[] word) {
568: return stem(word, word.length);
569: }
570:
571: /** Stem a word contained in a portion of a char[] array. Returns
572: * true if the stemming process resulted in a word different from
573: * the input. You can retrieve the result with
574: * getResultLength()/getResultBuffer() or toString().
575: */
576: public boolean stem(char[] wordBuffer, int offset, int wordLen) {
577: reset();
578: if (b.length < wordLen) {
579: char[] new_b = new char[wordLen + EXTRA];
580: b = new_b;
581: }
582: System.arraycopy(wordBuffer, offset, b, 0, wordLen);
583: i = wordLen;
584: return stem(0);
585: }
586:
587: /** Stem a word contained in a leading portion of a char[] array.
588: * Returns true if the stemming process resulted in a word different
589: * from the input. You can retrieve the result with
590: * getResultLength()/getResultBuffer() or toString().
591: */
592: public boolean stem(char[] word, int wordLen) {
593: return stem(word, 0, wordLen);
594: }
595:
596: /** Stem the word placed into the Stemmer buffer through calls to add().
597: * Returns true if the stemming process resulted in a word different
598: * from the input. You can retrieve the result with
599: * getResultLength()/getResultBuffer() or toString().
600: */
601: public boolean stem() {
602: return stem(0);
603: }
604:
605: public boolean stem(int i0) {
606: k = i - 1;
607: k0 = i0;
608: if (k > k0 + 1) {
609: step1();
610: step2();
611: step3();
612: step4();
613: step5();
614: step6();
615: }
616: // Also, a word is considered dirty if we lopped off letters
617: // Thanks to Ifigenia Vairelles for pointing this out.
618: if (i != k + 1)
619: dirty = true;
620: i = k + 1;
621: return dirty;
622: }
623:
624: /** Test program for demonstrating the Stemmer. It reads a file and
625: * stems each word, writing the result to standard out.
626: * Usage: Stemmer file-name
627: */
628: public static void main(String[] args) {
629: PorterStemmer s = new PorterStemmer();
630:
631: for (int i = 0; i < args.length; i++) {
632: try {
633: InputStream in = new FileInputStream(args[i]);
634: byte[] buffer = new byte[1024];
635: int bufferLen, offset, ch;
636:
637: bufferLen = in.read(buffer);
638: offset = 0;
639: s.reset();
640:
641: while (true) {
642: if (offset < bufferLen)
643: ch = buffer[offset++];
644: else {
645: bufferLen = in.read(buffer);
646: offset = 0;
647: if (bufferLen < 0)
648: ch = -1;
649: else
650: ch = buffer[offset++];
651: }
652:
653: if (Character.isLetter((char) ch)) {
654: s.add(Character.toLowerCase((char) ch));
655: } else {
656: s.stem();
657: System.out.print(s.toString());
658: s.reset();
659: if (ch < 0)
660: break;
661: else {
662: System.out.print((char) ch);
663: }
664: }
665: }
666:
667: in.close();
668: } catch (IOException e) {
669: System.out.println("error reading " + args[i]);
670: }
671: }
672: }
673: }
|