如何将带词干的字符串存储到单个数组中?

2024-04-26 09:29:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,只是想编写一个简单的程序,将词干放在一个字符串中,并输出一个数组/列表,其中包含词干单词,但我似乎可以将它们放在一个数组中。这是我的代码,我包括了输出。提前感谢您的帮助!你知道吗

from nltk.stem import PorterStemmer
from nltk.tokenize import sent_tokenize, word_tokenize

ps = PorterStemmer()

new_text = "My two friends are getting married tomorrow and I could not be 
more excited for them"


words = word_tokenize(new_text)

for w in words:
    stems = [ps.stem(w)]
    print(stems)

我的输出:

['My']
['two']
['friend']
['are']
['get']
['marri']
['tomorrow']
['and']
['I']
['could']
['not']
['be']
['more']
['excit']
['for']
['them']

Tags: textfromimportnewformy数组word