porter module

Porter Stemming Algorithm This is the Porter stemming algorithm, ported to Python from the version coded up in ANSI C by the author. It may be be regarded as canonical, in that it follows the algorithm presented in

Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14, no. 3, pp 130-137,

only differing from it at the points maked –DEPARTURE– below.

See also http://www.tartarus.org/~martin/PorterStemmer

The algorithm as described in the paper could be exactly replicated by adjusting the points of DEPARTURE, but this is barely necessary, because (a) the points of DEPARTURE are definitely improvements, and (b) no encoding of the Porter stemmer I have seen is anything like as exact as this version, even with the points of DEPARTURE!

Vivake Gupta (v@nano.com)

Release 1: January 2001

Further adjustments by Santiago Bruno (bananabruno@gmail.com) to allow word input not restricted to one word per line, leading to:

release 2: July 2008

class porter.PorterStemmer

Bases: object

cons(i)

cons(i) is TRUE <=> b[i] is a consonant.

cvc(i)

cvc(i) is TRUE <=> i-2,i-1,i has the form consonant - vowel - consonant and also if the second c is not w,x or y. this is used when trying to restore an e at the end of a short e.g.

cav(e), lov(e), hop(e), crim(e), but snow, box, tray.
doublec(j)

doublec(j) is TRUE <=> j,(j-1) contain a double consonant.

ends(s)

ends(s) is TRUE <=> k0,...k ends with the string s.

m()

m() measures the number of consonant sequences between k0 and j. if c is a consonant sequence and v a vowel sequence, and <..> indicates arbitrary presence,

<c><v> gives 0 <c>vc<v> gives 1 <c>vcvc<v> gives 2 <c>vcvcvc<v> gives 3 ....
r(s)

r(s) is used further down.

setto(s)

setto(s) sets (j+1),...k to the characters in the string s, readjusting k.

stem(p, i, j)

In stem(p,i,j), p is a char pointer, and the string to be stemmed is from p[i] to p[j] inclusive. Typically i is zero and j is the offset to the last character of a string, (p[j+1] == ‘’). The stemmer adjusts the characters p[i] ... p[j] and returns the new end-point of the string, k. Stemming never increases word length, so i <= k <= j. To turn the stemmer into a module, declare ‘stem’ as extern, and delete the remainder of this file.

step1ab()

step1ab() gets rid of plurals and -ed or -ing. e.g.

caresses -> caress ponies -> poni ties -> ti caress -> caress cats -> cat

feed -> feed agreed -> agree disabled -> disable

matting -> mat mating -> mate meeting -> meet milling -> mill messing -> mess

meetings -> meet

step1c()

step1c() turns terminal y to i when there is another vowel in the stem.

step2()

step2() maps double suffices to single ones. so -ization ( = -ize plus -ation) maps to -ize etc. note that the string before the suffix must give m() > 0.

step3()

step3() dels with -ic-, -full, -ness etc. similar strategy to step2.

step4()

step4() takes off -ant, -ence etc., in context <c>vcvc<v>.

step5()

step5() removes a final -e if m() > 1, and changes -ll to -l if m() > 1.

vowelinstem()

vowelinstem() is TRUE <=> k0,...j contains a vowel