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

标签 python nltk porter-stemmer stem

我在列表中有一堆句子,我想使用 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/

相关文章:

Python,导入错误 : cannot import name AbstractLazySequence

python textblob和文本分类

java - 是否有 Porter2 词干分析器的 java 实现

java - java中的停用词和词干分析器

python - 在Python中使用lxml解析ODF

python初学者-如何将多个文件的内容读入唯一列表?

jupyter-notebook - 如何解决 google colab 上没有名为 'nltk.translate.meteor_score' 的模块

java - 在java中读取使用python的struct.pack方法编写的整数时出现问题

python - 模型方法关注点分离

java - java opennlp工具包中内置Porter Stemmer