【问题标题】:remove stop words (NLTK) from multiple files从多个文件中删除停用词 (NLTK)
【发布时间】:2019-01-21 09:36:32
【问题描述】:

我有几个 tousend 文本文件(本地文件夹),想从该文件夹中的每个文件中删除停用词并将新文件保存在子文件夹中。

一个文件的代码:

import io
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

stop_words = set(stopwords.words('english'))
file1 = open("1_1.txt")
line = file1.read()
words = line.split()
for r in words:
    if not r in stop_words:
        appendFile = open('subfolder/1_1.txt','a')
        appendFile.write(" "+r)
        appendFile.close()

我想我必须用 glob 试试?但我似乎不了解文档。我也许应该降低()文本?必须有一个超级简单的方法,但我只找到一个句子或一个文件的教程,而不是多个文件。

【问题讨论】:

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


    【解决方案1】:
    import io
    from nltk.corpus import stopwords
    from nltk.tokenize import word_tokenize
    
    stop_words = set(stopwords.words('english'))
    file1 = open("file1.txt")
    line = file1.read()
    words = word_tokenize(line)
    words_witout_stop_words = ["" if word in stop_words else word for word in words]
    new_words = " ".join(words_witout_stop_words).strip()
    appendFile = open('subfolder/file1.txt','w')
    appendFile.write(new_words)
    appendFile.close()
    

    现在您可以通过 localfolder 的文件名添加一个循环,一切顺利。

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 2015-01-20
      • 1970-01-01
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多