自然语言处理----词干提取器

这里主要介绍nltk中的一些现成的词干提取器Porter和Lancaster.

1. Porter

>>> import nltk
>>> porter=nltk.PorterStemmer()
>>> raw='''Listen, strange women lying in ponds distributing swords is no basis
... for a system of government. Supreme executive power derives from a mandate from
... the masses, not from some farcical aquatic'''
>>> tokens=nltk.word_tokenize(raw)
>>> [porter.stem(t) for t in tokens]
['listen', ',', u'strang', 'women', u'lie', 'in', u'pond', u'distribut', u'sword', 'is', 'no', u'basi', 'for', 'a', 'system', 'of', u'govern', '.', u'suprem', u'execut', 'power', u'deriv', 'from',
, u'mandat', 'from', 'the', u'mass', ',', 'not', 'from', 'some', u'farcic', u'aquat']

2. Lancaster

>>> lancaster=nltk.LancasterStemmer()
>>> [lancaster.stem(t) for t in tokens]
['list', ',', 'strange', 'wom', 'lying', 'in', 'pond', 'distribut', 'sword', 'is', 'no', 'bas', 'for', 'a', 'system', 'of', 'govern', '.', 'suprem', 'execut', 'pow', 'der', 'from', 'a', 'mand', 'from'
, 'the', 'mass', ',', 'not', 'from', 'som', 'farc', 'aqu']

3. 词形归并器:删除词缀产生的词, 常用的有WordNetLemmatier

>>> wnl=nltk.WordNetLemmatizer()
>>> [wnl.lemmatize(t) for t in tokens]
['Listen', ',', 'strange', u'woman', 'lying', 'in', u'pond', 'distributing', u'sword', 'is', 'no', 'basis', 'for', 'a', 'system', 'of', 'government', '.', 'Supreme', 'executive', 'power', 'derives', '
from', 'a', 'mandate', 'from', 'the', u'mass', ',', 'not', 'from', 'some', 'farcical', 'aquatic']

从上面的运行结果可以看出,Porter词干提取器的效果比较好。

4. 基于Porter词干提取算法的词干提取工具SnowballStemmer

>>> from nltk.stem import SnowballStemmer
>>> stemmer=SnowballStemmer('english')
>>> import nltk
>>> raw='''Listen, strange women lying in ponds distributing swords is no basis
... ... for a system of government. Supreme executive power derives from a mandate from
... ... the masses, not from some farcical aquatic'''
>>> tokens=nltk.word_tokenize(raw)
>>> [stemmer.stem(t) for t in tokens]
[u'listen', ',', u'strang', u'women', u'lie', 'in', u'pond', u'distribut', u'sword', 'is', 'no', u'basi', u'...', u'for', 'a', u'system', 'of', u'govern', '.', u'suprem', u'execut', u'power', u'deriv'
, u'from', 'a', u'mandat', u'from', u'...', u'the', u'mass', ',', u'not', u'from', u'some', u'farcic', u'aquat']

 

转载于:https://www.cnblogs.com/no-tears-girl/p/6964910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值