本文介绍了python nltk -- 句子/短语的词干列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个列表中有一堆句子,我想使用 nltk 库来阻止它.我可以一次提取一个句子,但是我在从列表中提取句子并将它们重新组合在一起时遇到了问题.有没有我遗漏的步骤?nltk 库很新.谢谢!
I have bunch of sentences in a list and I wanted to use nltk library to stem it. I am able to stem one sentence at a time, however I am having issues stemming sentences from a list and joining them back together. Is there a step I am missing? Quite new to nltk library. Thanks!
import nltk
from nltk.stem import PorterStemmer
ps = PorterStemmer()
# Success: one sentences at a time
data = 'the gamers playing games'
words = word_tokenize(data)
for w in words:
print(ps.stem(w))
# Fails:
data_list = ['the gamers playing games',
'higher scores',
'sports']
words = word_tokenize(data_list)
for w in words:
print(ps.stem(w))
# Error: TypeError: expected string or bytes-like object
# result should be:
['the gamer play game',
'higher score',
'sport']
推荐答案
您正在将列表传递给 word_tokenize
,而您不能这样做.
You're passing a list to word_tokenize
which you can't.
解决方案是将您的逻辑包装在另一个for-loop
中,
The solution is to wrap your logic in another for-loop
,
data_list = ['the gamers playing games','higher scores','sports']
for words in data_list:
words = tokenize.word_tokenize(words)
for w in words:
print(ps.stem(w))
>>>>the
gamer
play
game
higher
score
sport
这篇关于python nltk -- 句子/短语的词干列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!