| 1 | #!/usr/bin/perl |
| 2 | # -*- Mode: Perl -*- |
| 3 | # Author : Ian Phillipps |
| 4 | # Last Modified On: Sun May 2 15:35:33 2004 |
| 5 | # Language : CPerl |
| 6 | |
| 7 | package Text::English; |
| 8 | |
| 9 | $VERSION = $VERSION = '0.01'; |
| 10 | |
| 11 | sub stem { |
| 12 | my @parms = @_; |
| 13 | foreach( @parms ) { |
| 14 | $_ = lc $_; |
| 15 | |
| 16 | # Step 0 - remove punctuation |
| 17 | s/'s$//; s/^[^a-z]+//; s/[^a-z]+$//; |
| 18 | next unless /^[a-z]+$/; |
| 19 | |
| 20 | # step1a_rules |
| 21 | if( /[^s]s$/ ) { s/sses$/ss/ || s/ies$/i/ || s/s$// } |
| 22 | |
| 23 | # step1b_rules. The business with rule==106 is embedded in the |
| 24 | # boolean expressions here. |
| 25 | (/[aeiouy][^aeiouy].*eed$/ && s/eed$/ee/ ) || |
| 26 | ( s/([aeiou].*)ed$/$1/ || s/([aeiouy].*)ing$/$1/ ) && |
| 27 | ( # step1b1_rules |
| 28 | s/at$/ate/ || s/bl$/ble/ || s/iz$/ize/ || s/bb$/b/ || |
| 29 | s/dd$/d/ || s/ff$/f/ || s/gg$/g/ || s/mm$/m/ || |
| 30 | s/nn$/n/ || s/pp$/p/ || s/rr$/r/ || s/tt$/t/ || |
| 31 | s/ww$/w/ || s/xx$/x/ || |
| 32 | # This is wordsize==1 && CVC...addanE... |
| 33 | s/^[^aeiouy]+[aeiouy][^aeiouy]$/$&e/ |
| 34 | ) |
| 35 | #DEBUG && warn "step1b1: $_\n" |
| 36 | ; |
| 37 | # step1c_rules |
| 38 | #DEBUG warn "step1c: $_\n" if |
| 39 | s/([aeiouy].*)y$/$1i/; |
| 40 | |
| 41 | # step2_rules |
| 42 | |
| 43 | if ( s/ational$/ate/ || s/tional$/tion/ || s/enci$/ence/ || |
| 44 | s/anci$/ance/ || s/izer$/ize/ || s/iser$/ise/ || |
| 45 | s/abli$/able/ || s/alli$/al/ || s/entli$/ent/ || |
| 46 | s/eli$/e/ || s/ousli$/ous/ || s/ization$/ize/ || |
| 47 | s/isation$/ise/ || s/ation$/ate/ || s/ator$/ate/ || |
| 48 | s/alism$/al/ || s/iveness$/ive/ || s/fulnes$/ful/ || |
| 49 | s/ousness$/ous/ || s/aliti$/al/ || s/iviti$/ive/ || |
| 50 | s/biliti$/ble/ |
| 51 | ) { |
| 52 | my ($l,$m) = ($`,$&); |
| 53 | #DEBUG warn "step 2: l=$l m=$m\n"; |
| 54 | $_ = $l.$m unless $l =~ /[^aeiou][aeiouy]/; |
| 55 | } |
| 56 | # step3_rules |
| 57 | if ( s/icate$/ic/ || s/ative$// || s/alize$/al/ || |
| 58 | s/iciti$/ic/ || s/ical$/ic/ || s/ful$// || |
| 59 | s/ness$// |
| 60 | ) { |
| 61 | my ($l,$m) = ($`,$&); |
| 62 | #DEBUG warn "step 3: l=$l m=$m\n"; |
| 63 | $_ = $l.$m unless $l =~ /[^aeiou][aeiouy]/; |
| 64 | } |
| 65 | |
| 66 | # step4_rules |
| 67 | if ( s/al$// || s/ance$// || s/ence$// || s/er$// || |
| 68 | s/ic$// || s/able$// || s/ible$// || s/ant$// || |
| 69 | s/ement$// || s/ment$// || s/ent$// || s/sion$/s/ || |
| 70 | s/tion$/t/ || s/ou$// || s/ism$// || s/ate$// || |
| 71 | s/iti$// || s/ous$// || s/ive$// || s/ize$// || |
| 72 | s/ise$// |
| 73 | ) { |
| 74 | my ($l,$m) = ($`,$&); |
| 75 | # Look for two consonant/vowel transitions |
| 76 | # NB simplified... |
| 77 | #DEBUG warn "step 4: l=$l m=$m\n"; |
| 78 | $_ = $l.$m unless $l =~ /[^aeiou][aeiouy].*[^aeiou][aeiouy]/; |
| 79 | } |
| 80 | |
| 81 | # step5a_rules |
| 82 | #DEBUG warn("step 5a: $_\n") && |
| 83 | s/e$// if ( /[^aeiou][aeiouy].*[^aeiou][aeiouy].*e$/ || |
| 84 | ( /[aeiou][^aeiouy].*e/ && ! /[^aeiou][aeiouy][^aeiouwxy]e$/) ); |
| 85 | |
| 86 | # step5b_rules |
| 87 | #DEBUG warn("step 5b: $_\n") && |
| 88 | s/ll$/l/ if /[^aeiou][aeiouy].*[^aeiou][aeiouy].*ll$/; |
| 89 | |
| 90 | # Cosmetic step |
| 91 | s/(.)i$/$1y/; |
| 92 | } |
| 93 | @parms; |
| 94 | } |
| 95 | |
| 96 | 1; |
| 97 | |
| 98 | __END__ |
| 99 | |
| 100 | =head1 NAME |
| 101 | |
| 102 | Text::English - Porter's stemming algorithm |
| 103 | |
| 104 | =head1 SYNOPSIS |
| 105 | |
| 106 | use Text::English; |
| 107 | @stems = Text::English::stem( @words ); |
| 108 | |
| 109 | =head1 DESCRIPTION |
| 110 | |
| 111 | This routine applies the Porter Stemming Algorithm to its parameters, |
| 112 | returning the stemmed words. |
| 113 | It is derived from the C program "stemmer.c" |
| 114 | as found in freewais and elsewhere, which contains these notes: |
| 115 | |
| 116 | Purpose: Implementation of the Porter stemming algorithm documented |
| 117 | in: Porter, M.F., "An Algorithm For Suffix Stripping," |
| 118 | Program 14 (3), July 1980, pp. 130-137. |
| 119 | Provenance: Written by B. Frakes and C. Cox, 1986. |
| 120 | |
| 121 | I have re-interpreted areas that use Frakes and Cox's "WordSize" |
| 122 | function. My version may misbehave on short words starting with "y", |
| 123 | but I can't think of any examples. |
| 124 | |
| 125 | The step numbers correspond to Frakes and Cox, and are probably in |
| 126 | Porter's article (which I've not seen). |
| 127 | Porter's algorithm still has rough spots (e.g current/currency, -ings words), |
| 128 | which I've not attempted to cure, although I have added |
| 129 | support for the British -ise suffix. |
| 130 | |
| 131 | =head1 NOTES |
| 132 | |
| 133 | This is version 0.1. I would welcome feedback, especially improvements |
| 134 | to the punctuation-stripping step. |
| 135 | |
| 136 | =head1 AUTHOR |
| 137 | |
| 138 | Ian Phillipps <[email protected]> |
| 139 | |
| 140 | =head1 COPYRIGHT |
| 141 | |
| 142 | Copyright Public IP Exchange Ltd (PIPEX). |
| 143 | Available for use under the same terms as perl. |
| 144 | |
| 145 | =cut |
| 146 |