如何将词干字符串存储到单个数组中?
How to store stemmed strings into single array?
我是 python 的新手,我只是想编写一个简单的程序,将字符串中的单词词干并输出包含词干词干的 array/list,但我似乎可以得到它们成一个数组。这是我的代码,我包含了输出。在此先感谢您的帮助!
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']
您可以改用 list comprehension。
print([ps.stem(w) for w in words])
我是 python 的新手,我只是想编写一个简单的程序,将字符串中的单词词干并输出包含词干词干的 array/list,但我似乎可以得到它们成一个数组。这是我的代码,我包含了输出。在此先感谢您的帮助!
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']
您可以改用 list comprehension。
print([ps.stem(w) for w in words])