我遇到的問題是,在我的代碼中,我無法獲取單個詞/標記以匹配停用詞從原始文本中刪除。相反,我得到了一整句話,因此無法將它與停用詞相匹配。請告訴我一種方法,我可以獲取個人令牌,然後用停用詞匹配並刪除它們。請幫幫我。如何在Python中逐個讀取文件中的令牌?
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方法()'函數。這給你一個文件中的行/句子的迭代,然後當你說'文本中的單詞'時,你會逐一獲取這些行。 –