【发布时间】:2021-08-14 20:51:32
【问题描述】:
我一直在构建一个停用词删除功能,但我不确定自己做错了什么。问题是它返回的是单个字符而不是单词:
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
def remove_stop_words(list):
no_stop_list = []
for sentence in files:
for word in sentence:
no_stops = [word for word in sentence if str(word) not in stop_words]
no_stop_list.append(no_stops)
return no_stop_list
no_stops = remove_stop_words(processed_text)
print(no_stops)
根据我的阅读,这似乎应该有效。我有一个列表,其中包含标记的句子,并且在这些标记的句子中是标记的单词。在这个函数中,我的目标是创建一个新列表,遍历列表中的句子,然后遍历句子中的单词,检查它们是否不是停用词,并在新列表中附加非停用词标记。尽管感觉这很合乎逻辑,但它仍然不起作用。
为澄清起见,processed_text 只是一个变量,其中包含已规范化的文本(删除了新换行符、删除了标点符号等)
【问题讨论】:
-
不工作?你能再描述一下吗?
-
不应将内置函数用作变量名。请为
list选择其他名称。 -
什么是
processed_text?能提供一个简单的demo吗? -
假设
processed_text是一个包含句子的列表,你需要sentence.split(" ")才能得到word。 -
@Ghoti:OP 说该列表是标记化的句子。所以他们已经分裂了。