当代码成功执行时,程序将输出文本中出现频率最高的前10个人物名称及他们的出现次数,展示了对中文文本中特定人物的词频统计结果。import jieba #导入中文分词的库jieba#这些都不是人物的称号,但都是出现次数比较多的,需先列出来excludes = ("将军","却说","荆州","二人","不可","不能","如此","如何","左右","商议","主公","军士","军马","引兵","次日","大喜","天下","东吴","于是","今日","不敢","魏兵","陛下","一人","都督","人马","不知")txt = open("./三国演义.txt","r",encoding='utf-8').read() #读取文件words = ______ (txt) #用jieba库来中文分词#用字典来存储,各个人物的出场次数counts = (}for word in ______ : 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[rword] = counts.get(rword,0) + 1#不是人物就从字典里删除for word in excludes: ______ counts[word]#将字典类型的数据转化为list类型items = list(counts.items())#排序,按出现的次数从大到小排好序items. ______ (key=lambda x:x[1],reverse=True)#打印出现次数前10的人物名字for i in ______ : word, count = items[i] t = "{0:{2)<10}(1:>5)" #chr(12288)是中文字的空格 print(t.format(word,count,chr(12288)))
当代码成功执行时,程序将输出文本中出现频率最高的前10个人物名称及他们的出现次数,展示了对中文文本中特定人物的词频统计结果。
import jieba #导入中文分词的库jieba
#这些都不是人物的称号,但都是出现次数比较多的,需先列出来
excludes = {"将军","却说","荆州","二人","不可","不能","如此","如何","左右","商议","主公",
"军士","军马","引兵","次日","大喜","天下","东吴","于是","今日","不敢","魏兵",
"陛下","一人","都督","人马","不知"}
txt = open("./三国演义.txt","r",encoding='utf-8').read() #读取文件
words = ______ (txt) #用jieba库来中文分词
#用字典来存储,各个人物的出场次数
counts = {}
for word in ______ :
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[rword] = counts.get(rword,0) + 1
#不是人物就从字典里删除
for word in excludes:
______ counts[word]
#将字典类型的数据转化为list类型
items = list(counts.items())
#排序,按出现的次数从大到小排好序
items. ______ (key=lambda x:x[1],reverse=True)
#打印出现次数前10的人物名字
for i in ______ :
word, count = items[i]
t = "{0:{2}<10}{1:>5}"
#chr(12288)是中文字的空格
print(t.format(word,count,chr(12288)))