gpt4 book ai didi

python nltk -- 句子/短语的词干列表

转载 作者:太空宇宙 更新时间:2023-11-04 00:14:39 27 4
gpt4 key购买 nike

我在列表中有一堆句子,我想使用 nltk 库来阻止它。我可以一次提取一个句子,但是我在从列表中提取句子并将它们重新组合在一起时遇到了问题。我缺少一个步骤吗? nltk 库很新。谢谢!

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,但您不能这样做。

解决方案是将您的逻辑包装在另一个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 -- 句子/短语的词干列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330099/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com