如何在Python中逐个读取文件中的标记?

时间:2017-08-10 15:12:44

标签: python python-3.x token nltk stop-words

我遇到的问题是,在我的代码中,我无法获得单个单词/标记以匹配要从原始文本中删除的停用词。相反,我得到一个完整的句子,因此无法将它与停用词匹配。请告诉我一种方法,我可以获得单独的令牌,然后匹配那些有停用词并删除它们。请帮帮我。

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)

1 个答案:

答案 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)