文本词频统计3
# 文本词频统计3# threekingdomsV2:找一个三国演义的文件,保存为threekingdoms.txt,放在同一目录里面
# 记录输出出现次数最多的前十五个人物名输出
# 该版本通过设置排除非人名的词,进一步优化后精确统计:
excludes = {"将军", "荆州", "却说", "二人", "不可", "不能",\
"如此", "商议", "如何", "左右", "军士", "军马",\
"引兵", "次日", "大喜", "今日", "天下", "东吴",\
"于是", "不敢", "魏兵", "陛下", "一人", "人马",\
"不知", "汉中", "主公", "只见", "众将", "后主",\
}
import jieba
filename = "threekingdoms.txt"
txt = open(filename, "r", encoding="utf-8").read()
words = jieba.lcut(txt)
excludes = {"将军", "荆州", "却说", "二人", "不可", "不能",\
"如此", "商议", "如何", "左右", "军士", "军马",\
"引兵", "次日", "大喜", "今日", "天下", "东吴",\
"于是", "不敢", "魏兵", "陛下", "一人", "人马",\
"不知", "汉中", "主公", "只见", "众将", "后主",\
}
counts = {}
for word in words:
if len(word)==1:
continue
elif word == "诸葛亮" or word == "孔明曰":
rword = "孔明"
elif word == "玄德" or word == "玄德曰":
rword = "刘备"
elif word == "关公" or word == "云长":
rword = "关羽"
elif word == "孟德" or word == "丞相":
rword = "曹操"
else:
rword = word
counts = counts.get(rword, 0) + 1
for word in excludes:
del counts
items = list(counts.items())
items.sort(key=lambda x:x,reverse=True)
# 将出现次数最多的前十五个人物名输出
for i in range(15):
word, count = items
print("{0:<10}{1:>5}".format(word, count))
页:
[1]