我遇到的问题是,在我的代码中,我无法获得单个单词/标记以匹配要从原始文本中删除的停用词。相反,我得到一个完整的句子,因此无法将它与停用词匹配。请告诉我一种方法,我可以获得单独的令牌,然后匹配那些有停用词并删除它们。请帮帮我。
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)
答案 0 :(得分:2)
请改为尝试:
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string, os, ast
def remove_stopwords(ifile):
processed_word_list = []
stopword = stopwords.words("urdu")
words = ast.literal_eval(open(ifile, 'r').read())
for word in words:
print(word)
if word not in stopword:
processed_word_list.append('*')
else:
processed_word_list.append(word)
print(processed_word_list)
return processed_word_list
if __name__ == "__main__":
print ("Input file path: ")
ifile = input()
remove_stopwords(ifile)