首页 > 代码库 > 自然语言处理----词干提取器
自然语言处理----词干提取器
这里主要介绍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词干提取器的效果比较好。
自然语言处理----词干提取器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。