Need a python module for stemming of text documents
Solution 1
You may want to try NLTK
>>> from nltk import PorterStemmer
>>> PorterStemmer().stem('complications')
Solution 2
All these stemmers that have been discussed here are algorithmic stemmer,hence they can always produce unexpected results such as
In [3]: from nltk.stem.porter import *
In [4]: stemmer = PorterStemmer()
In [5]: stemmer.stem('identified')
Out[5]: u'identifi'
In [6]: stemmer.stem('nonsensical')
Out[6]: u'nonsens'
To correctly get the root words one need a dictionary based stemmer such as Hunspell Stemmer.Here is a python implementation of it in the following link. Example code is here
>>> import hunspell
>>> hobj = hunspell.HunSpell('/usr/share/myspell/en_US.dic', '/usr/share/myspell/en_US.aff')
>>> hobj.spell('spookie')
False
>>> hobj.suggest('spookie')
['spookier', 'spookiness', 'spooky', 'spook', 'spoonbill']
>>> hobj.spell('spooky')
True
>>> hobj.analyze('linked')
[' st:link fl:D']
>>> hobj.stem('linked')
['link']
Solution 3
Python stemming module has implementations of various stemming algorithms like Porter, Porter2, Paice-Husk, and Lovins. http://pypi.python.org/pypi/stemming/1.0
>> from stemming.porter2 import stem
>> stem("factionally")
faction
Solution 4
The gensim
package for topic modelling comes with a Porter Stemmer algorithm:
>>> from gensim import parsing
>>> gensim.parsing.stem_text("trying writing nonsense")
'try write nonsens'
The PorterStemmer is the only stemming option implemented in gensim
.
An a side note: I can imagine (without further references) that most text-mining-related modules have their own implementations for simple pre-processing procedures like Porter's stemming, white-space removal and stop-word removal.
Kai
Updated on July 05, 2022Comments
-
Kai almost 2 years
I need a good python module for stemming text documents in the pre-processing stage.
I found this one
http://pypi.python.org/pypi/PyStemmer/1.0.1
but i cannot find the documentation int the link provided.
I anyone knows where to find the documentation or any other good stemming algorithm please help.
-
kalu about 10 yearsWasn't the PorterStemmer developed in the 1980s? Surely there is a more advanced option?
-
ditkin about 10 yearsYou are correct that there are other stemmers. From the preview of the Natural Language Processing with Python section on stemmers they do a simple comparison of Lancaster to Porter and then state "Stemming is not a well-defined process, and we typically pick the stemmer that best suits the application we have in mind. The Porter Stemmer is a good choice if you are indexing some texts and want to support search using alternative forms of words."
-
umop aplsdn almost 8 years-1: The object of stemmers is not to find the root word (or lemmatization, which nltk also has a module for), but rather find a shortened version of the word that other inflections would also shorten to. It doesn't matter if the stemmer does not find the root word; as long as
stem('nonsense') == stem('nonsensical') != stem('bananas')
, it's fine. -
Varun Balupuri over 6 yearsBe aware that this is a pure python implementation and performs slower at scale than things like PyStemmer which are wrappers for a fast C implementation