【发布时间】:2018-01-18 22:16:15
【问题描述】:
我遇到的问题是,在我的代码中,我无法让单个单词/标记与停用词匹配以从原始文本中删除。相反,我得到了一个完整的句子,因此无法将它与停用词匹配。请告诉我一种获取单个标记的方法,然后将它们与停用词匹配并删除它们。请帮帮我。
from nltk.corpus import stopwords
import string, os
def remove_stopwords(ifile):
processed_word_list = []
stopword = stopwords.words("urdu")
text = open(ifile, 'r').readlines()
for word in text:
print(word)
if word not in stopword:
processed_word_list.append('*')
print(processed_word_list)
return processed_word_list
if __name__ == "__main__":
print ("Input file path: ")
ifile = input()
remove_stopwords(ifile)
【问题讨论】:
-
您没有得到文本中的单词的原因是因为您正在使用
readlines()函数。这为您提供了文件中的行/句子的可迭代,然后当您说for word in text:时,您将逐行获取。
标签: python python-3.x token nltk stop-words