NLTK 词干化

NLTK 词干化

在NLP中,我们对一句话或一个文档分词之后,一般要进行词干化处理。词干化处理就是把一些名词的复数去掉,动词的不同时态去掉等等类似的处理。

对于切词得到的英文单词要进行词干化处理,主要包括将名词的复数变为单数和将动词的其他形态变为基本形态

在nltk当中有两种方法做词干化处理:“porter” “snowball”

import nltk


word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
words = word_data.split(" ")

porterStemmer = nltk.stem.PorterStemmer()
snowballStemmer = nltk.stem.SnowballStemmer('english')

def stem_tokens(tokens, stemmer):
    stemmed = []
    for token in tokens:
        stemmed.append(stemmer.stem(token))
    return stemmed

print(word_data)
print(stem_tokens(words,porterStemmer))
print(stem_tokens(words,snowballStemmer))
It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms
['It', 'origin', 'from', 'the', 'idea', 'that', 'there', 'are', 'reader', 'who', 'prefer', 'learn', 'new', 'skill', 'from', 'the', 'comfort', 'of', 'their', 'draw', 'room']
['it', 'origin', 'from', 'the', 'idea', 'that', 'there', 'are', 'reader', 'who', 'prefer', 'learn', 'new', 'skill', 'from', 'the', 'comfort', 'of', 'their', 'draw', 'room']
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值