用python实现,批量读取文件夹里的txt文件,根据停用词词表以及词表使用最大逆向算法进行分词,词表说明了哪些字的组合可以被算作一个词,停用词表说明哪些没有实意的词可以去掉,其中词表和停用词表均为excel文件,将分词结果以同名文件储存在另一个文件夹里,同时将分词结果进行词频统计并保存在另一个文件夹里,请提供完整代码
时间: 2024-03-20 08:44:05 浏览: 31
以下是一个基于 Python 的批量读取文件夹里的 txt 文件,使用停用词表和最大逆向算法进行分词,并将分词结果进行词频统计的代码:
```python
import os
import xlrd
from collections import Counter
# 读取词表和停用词表
def read_excel(file_path):
wb = xlrd.open_workbook(file_path)
sheet = wb.sheet_by_index(0)
words = []
for i in range(sheet.nrows):
words.append(sheet.cell_value(i, 0))
return words
# 最大逆向算法分词
def cut_words(text, word_list, stop_words):
words = []
i = len(text) - 1
while i >= 0:
j = i
while j >= 0:
if text[j:i+1] in word_list:
words.append(text[j:i+1])
i = j - 1
break
j -= 1
else:
if text[i] not in stop_words:
words.append(text[i])
i -= 1
else:
i -= 1
return list(reversed(words))
# 统计词频
def count_words(words):
counter = Counter(words)
return dict(counter)
# 处理单个文件
def process_file(file_path, word_list, stop_words, output_path):
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
words = cut_words(text, word_list, stop_words)
word_count = count_words(words)
output_file_path = os.path.join(output_path, os.path.basename(file_path))
with open(output_file_path, 'w', encoding='utf-8') as f:
f.write(' '.join(words))
return word_count
# 处理文件夹
def process_folder(folder_path, word_list, stop_words, output_path):
word_count = {}
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path) and file_name.endswith('.txt'):
file_word_count = process_file(file_path, word_list, stop_words, output_path)
word_count = {k: word_count.get(k, 0) + file_word_count.get(k, 0) for k in set(word_count) | set(file_word_count)}
return word_count
if __name__ == '__main__':
folder_path = '/path/to/folder' # 文件夹路径
word_list_path = '/path/to/word_list.xlsx' # 词表路径
stop_words_path = '/path/to/stop_words.xlsx' # 停用词表路径
output_path = '/path/to/output_folder' # 输出文件夹路径
word_list = read_excel(word_list_path)
stop_words = read_excel(stop_words_path)
word_count = process_folder(folder_path, word_list, stop_words, output_path)
print(word_count)
```
注意,以上代码中的 `/path/to/` 部分需要根据实际情况进行修改。此外,此代码仅提供了基本的分词和词频统计功能,还有很多可以进行优化和改进的地方,比如加入缓存机制、使用更高效的分词算法等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)