遍历txt文件夹,去停用词
时间: 2023-07-25 11:24:07 浏览: 77
好的,可以使用Python编写脚本来实现。您需要先安装Python和相关的库,如nltk和os。以下是可能的实现方式:
```python
import os
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
# 设置文件夹路径和输出文件名
folder_path = "path/to/folder"
output_file = "output.txt"
# 获取停用词列表
stop_words = set(stopwords.words("english"))
# 遍历文件夹
with open(output_file, "w") as f:
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
# 打开文件并读取内容
with open(file_path, "r") as file:
content = file.read()
# 分词并去除停用词
tokens = word_tokenize(content)
words = [word.lower() for word in tokens if word.isalpha() and word.lower() not in stop_words]
# 将去除停用词后的文本写入输出文件
f.write(" ".join(words) + "\n")
```
请将`path/to/folder`替换为您的文件夹路径,将`output.txt`替换为您想要的输出文件名。该脚本将遍历文件夹中的每个文件,去除停用词后将文本写入输出文件中。
阅读全文
相关推荐














