请编写程序,对"凉州词.txt"文本中出现的中文词语进行统计,采用 jieba 库分词(
时间: 2023-12-01 13:01:28 浏览: 179
中文分词词典UserDict.txt
import jieba
import os
# 读取文件
file_path = "凉州词.txt"
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
# 使用jieba进行分词
seg_list = jieba.cut(content, cut_all=False)
# 统计词语出现次数
word_count = {}
for word in seg_list:
if len(word) > 1: # 只统计长度大于1的词语
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 将结果写入文件
output_path = "词语统计结果.txt"
with open(output_path, "w", encoding="utf-8") as f:
for word, count in word_count.items():
f.write(f"{word}: {count}\n")
print("统计完成,结果已写入词语统计结果.txt文件中。")
阅读全文