2012-04-29 38 views

回答

25

你可能想嘗試NLTK

>>> from nltk import PorterStemmer 
>>> PorterStemmer().stem('complications') 
+0

是不是80年代開發的PorterStemmer?當然有更先進的選擇? – kalu 2014-02-15 21:19:10

+1

你是正確的,有其他的詞幹。從[自然語言處理與Python部分的自然語言處理部分](http://www.nltk.org/book3/ch03.html#stemmers)中,他們對Lancaster和Porter進行了簡單比較,然後聲明「Stemming不是一個明確定義的過程,我們通常會選擇最適合我們想法的應用程序。如果您將某些文本編入索引並希望支持使用其他形式的詞彙進行搜索,Porter Stemmer是一個不錯的選擇。「 – ditkin 2014-02-15 22:23:43

5

所產生的Python模塊具有像搬運工,Porter2,Paice甲殼,洛文斯和制止各種算法實現。 已經在這裏討論http://pypi.python.org/pypi/stemming/1.0

>> from stemming.porter2 import stem 
    >> stem("factionally") 
    faction 
+0

請注意,這是一個純粹的python實現,其執行速度比PyStemmer慢,這些實現是快速C實現的包裝。 – 2018-01-10 17:31:35

2

所有這些詞幹是算法的詞幹,因此他們總能產生意想不到的結果,如

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' 

要正確獲取根字一個需要基於字典詞幹這樣作爲Hunspell Stemmer.Here在下面的link中是一個python實現。示例代碼在這裏

>>> 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'] 
+4

-1:stemmers的目標不是找到根詞(或詞法,nltk也有一個模塊),而是找到縮短版本的單詞,其他變體也將縮短。如果造詞者沒有找到根詞,這並不重要;只要'幹('廢話')==幹('無意義')!=幹('香蕉')',這很好。 – 2016-07-07 00:18:31

0

gensim package爲主題建模自帶波特施特默爾算法:

>>> from gensim import parsing 
>>> gensim.parsing.stem_text("trying writing nonsense") 
'try write nonsens' 

的PorterStemmer是gensim實現的唯一所產生的選擇。

附註:我可以想象(沒有進一步的參考),大多數與文本挖掘相關的模塊都有其自己的實現,用於簡單的預處理過程,如Porter的詞幹,空格刪除和停用詞移除。