python - 从多个文件中删除停用词 (NLTK)
问题描述
我有几个 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 试试?但我似乎不了解文档。我也许应该降低()文本?必须有一个超级简单的方法,但我只找到一个句子或一个文件的教程,而不是多个文件。
解决方案
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
,你很高兴。
推荐阅读
- html - 背景已链接但不会显示
- python - Plotly:如何设置y轴的范围?
- c# - Unity 2018.3.12f1(最新)的 MonoDeveloper 是否支持 .NET Framework 4.7.2?
- autodesk-forge - 请告诉我在将 DWG 文件转换为 PDF 时应用 ctb 的脚本
- python - 如何修复在里面切片列表?
- android - 打开片段布局时黑屏
- java - 在 JBoss EAP 7 的集群环境中,MDB 消费者不消费来自 JMS 队列的消息
- swift - 命名布尔属性的快捷方式
- azure - Azure 未使用正确的证书
- typescript - 如何从类型中省略属性?