Module: Emoninja::Stemmable
- Included in:
- String
- Defined in:
- lib/emoninja/porter_stemmer.rb
Overview
There are numerous strategies and algorithms for stemming. A widely-used algorithm for English stemming is the Porter stemming algorithm,
written by Martin Porter in 1980. The Porter stemmer follows a strategy
of suffix stripping, which basically uses a set of rules to strip away suffixes.
For example, a word that ends with ‘-ed’ might be suffix-stripped to remove the ‘-ed’. The Porter stemmer follows a sequence of steps in stripping suffixes.
tartarus.org/~martin/PorterStemmer/ruby.txt rubocop:disable Metrics/ModuleLength
Constant Summary collapse
- STEP_2_LIST =
{ 'ational' => 'ate', 'tional' => 'tion', 'enci' => 'ence', 'anci' => 'ance', 'izer' => 'ize', 'bli' => 'ble', 'alli' => 'al', 'entli' => 'ent', 'eli' => 'e', 'ousli' => 'ous', 'ization' => 'ize', 'ation' => 'ate', 'ator' => 'ate', 'alism' => 'al', 'iveness' => 'ive', 'fulness' => 'ful', 'ousness' => 'ous', 'aliti' => 'al', 'iviti' => 'ive', 'biliti' => 'ble', 'logi' => 'log' }.freeze
- STEP_3_LIST =
{ 'icate' => 'ic', 'ative' => '', 'alize' => 'al', 'iciti' => 'ic', 'ical' => 'ic', 'ful' => '', 'ness' => '' }.freeze
- SUFFIX_1_REGEXP =
/( ational | tional | enci | anci | izer | bli | alli | entli | eli | ousli | ization | ation | ator | alism | iveness | fulness | ousness | aliti | iviti | biliti | logi)$/x
- SUFFIX_2_REGEXP =
/( al | ance | ence | er | ic | able | ible | ant | ement | ment | ent | ou | ism | ate | iti | ous | ive | ize)$/x
- C =
'[^aeiou]'.freeze
- V =
'[aeiouy]'.freeze
- CC =
"#{C}(?>[^aeiouy]*)".freeze
- VV =
"#{V}(?>[aeiou]*)".freeze
- MGR0 =
[cc]vvcc… is m>0
/^(#{CC})?#{VV}#{CC}/o
- MEQ1 =
- cc]vvcc[vv
-
is m=1
/^(#{CC})?#{VV}#{CC}(#{VV})?$/o
- MGR1 =
[cc]vvccvvcc… is m>1
/^(#{CC})?#{VV}#{CC}#{VV}#{CC}/o
- VOWEL_IN_STEM =
vowel in stem
/^(#{CC})?#{V}/o
Instance Method Summary collapse
-
#stem_porter ⇒ Object
(also: #stem)
rubocop:disable Metrics/MethodLength rubocop:disable Style/PerlBackrefs.
Instance Method Details
#stem_porter ⇒ Object Also known as: stem
rubocop:disable Metrics/MethodLength rubocop:disable Style/PerlBackrefs
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/emoninja/porter_stemmer.rb', line 97 def stem_porter # make a copy of the given object and convert it to a string. w = dup.to_str return w if w.length < 3 # now map initial y to Y so that the patterns never treat it as vowel w[0] = 'Y' if w[0] == 'y' # Step 1a case w when /(ss|i)es$/ then w = $` + $1 when /([^s])s$/ then w = $` + $1 end # Step 1b if w =~ /eed$/ w.chop! if $` =~ MGR0 elsif w =~ /(ed|ing)$/ stem = $` if stem =~ VOWEL_IN_STEM w = stem case w when /(at|bl|iz)$/ then w << 'e' when /([^aeiouylsz])\1$/ then w.chop! when /^#{CC}#{V}[^aeiouwxy]$/o then w << 'e' end end end if w =~ /y$/ stem = $` w = stem + 'i' if stem =~ VOWEL_IN_STEM end # Step 2 if w =~ SUFFIX_1_REGEXP stem = $` suffix = $1 # print "stem= " + stem + "\n" + "suffix=" + suffix + "\n" w = stem + STEP_2_LIST[suffix] if stem =~ MGR0 end # Step 3 if w =~ /(icate|ative|alize|iciti|ical|ful|ness)$/ stem = $` suffix = $1 w = stem + STEP_3_LIST[suffix] if stem =~ MGR0 end # Step 4 if w =~ SUFFIX_2_REGEXP stem = $` w = stem if stem =~ MGR1 elsif w =~ /(s|t)(ion)$/ stem = $` + $1 w = stem if stem =~ MGR1 end # Step 5 if w =~ /e$/ stem = $` w = stem if (stem =~ MGR1) || (stem =~ MEQ1 && stem !~ /^#{CC}#{V}[^aeiouwxy]$/o) end w.chop! if w =~ /ll$/ && w =~ MGR1 # and turn initial Y back to y w[0] = 'y' if w[0] == 'Y' w end |