Python实现三国武将出场次数排序

双字及以上搜索:快,但不够精确,比如“懿”,“瑜”,“郃(武将张郃)”的未被查找带来排序误差。

上代码1:

import jieba
txt=open('threekingdoms.txt','r',encoding='utf-8').read()
words=jieba.lcut(txt)
counts={} #创建空字典
#excludes集合由多次迭代运行逐步完善至不影响前15名结果
excludes={'将军', '却说', '二人', '不可', '荆州', '不能','如此', '商议', '如何',\
         '主公', '军士', '左右','引兵','次日', '军马', '赵云', '大喜', '孙权', \
         '天下', '东吴', '于是', '今日','不敢', '魏兵', '陛下', '一人', '都督', \
         '人马', '不知','汉中', '只见', '众将', '后主','蜀兵', '上马','大叫',\
         '太守','此人','夫人', '先主', '后人', '背后', '城中', '天子', '一面',\
         '何不','大军', '忽报', '先生', '百姓', '何故', '然后', '先锋', '不如',\
         '赶来', '原来','令人', '江东', '下马', '喊声', '正是','徐州', '忽然', \
         '因此', '成都', '不见', '未知', '大败','大事', '之后', '一军', '引军',\
         '起兵', '军中', '接应', '进兵', '大惊','可以'}
for word in words:
    if len(word)==1:
        continue
    elif word =='丞相':  #这里忘记打第二个word==会出现keyerror:excludes里面的元素
        rword='曹操'
    elif word=='玄德曰' or word == '玄德':
        rword='刘备'
    elif word=='孔明曰' or word == '诸葛亮':
        rword='孔明'
    elif word=='云长':
        rword='关公'  #出现'关羽'次数极少
    else:
        rword=word
    counts[rword]=counts.get(rword,0)+1
for word in excludes:
    del counts[word]
items=list(counts.items()) #应该是key value item
items.sort(key=lambda x:x[1],reverse=True)
print('出场次数详情(17个):')
for i in range(17):  #无限次循环直到满足条件停要用while;遍历范围用for,都不用if,if是单次用的!!!!!!!!!!
    word,count=items[i]
    print('{2}th:{0:<10}:{1:>6}'.format(word,count,i+1)) #忽略1st表达,,排名比索引+1
print('出场次数排行TOP15:')
for i in range(15):
    print(items[i][0],end=',')
print('\n双字及以上粗略迭代寻找需要手动清除{0}个干扰项'.format(len(excludes)))

输出:

出场次数详情(17个):
1th:曹操 : 1444
2th:孔明 : 1383
3th:刘备 : 1252
4th:关公 : 775
5th:张飞 : 358
6th:吕布 : 300
7th:司马懿 : 221
8th:周瑜 : 217
9th:袁绍 : 191
10th:马超 : 185
11th:魏延 : 180
12th:黄忠 : 168
13th:姜维 : 151
14th:马岱 : 127
15th:庞德 : 122
16th:孟获 : 122
17th:以为 : 121
出场次数排行TOP15:
曹操,孔明,刘备,关公,张飞,吕布,司马懿,周瑜,袁绍,马超,魏延,黄忠,姜维,马岱,庞德,
双字及以上粗略迭代寻找需要手动清除78个干扰项

单字搜索:相对双字更精确,但更麻烦费时,由此可能造成更大误差,比如此程序中“诸葛亮”这一重要元素没有被迭代到造成极大误差,要想查找到需要更往后,这将消耗大量人力。但是这并不能说明精确度相对第一种差,因为在足够多的迭代次数下,这种方法无疑比第一种更精确。

上代码2:

import jieba
txt=open('threekingdoms.txt','r',encoding='utf-8').read()
words=jieba.lcut(txt)
counts={} #创建空字典
excludes={'', '', '"', '', '', '', '', '', '', '', '', '', '', '',\
          '','\n', '', '', '', '', '', '', '', '', '', '', '', '',\
          '', '','', '','', '','将军', '', '', '', '', '', '却说',\
          '', '','便','', '', '', '', '', '', '', '', '', '', '',\
          '','','','', '', '', '二人', '', '使',' ','不可','荆州','', '',\
          '', '', '', '', '', '', '不能', '', '如此', '', '', \
         '','', '', '', '', '商议', '', '如何', '', '主公', '', \
          '军士', '', '', '', '','左右', '军马', '', '', '', \
          '引兵', '', '次日', '', '大喜', '', '', '', '', '', \
          '', '', '', '', '天下', '', '', '东吴', '于是', '退', \
          '', '', '','今日','不敢','','魏兵', '', '', '', '',\
          '陛下', '', '一人','人马', '不知', '', '', '汉中', '', '',\
          '', '只见', '众将', '', '', '', '', '', '', '后主',\
          '', '', '', '', '', '', '', '', '蜀兵', '', \
          '上马', '', '大叫', '', '', '','太守', '此人', '夫人',\
          '先主', '后人','', '背后', '城中', '', '天子', '', '一面',\
          '何不', ''}
for word in words:
    if word=='' or word=='丞相':  #这里'操刀便砍'等里面的'操'可能会涉及,\
        # 但三国多用'提刀','拔刀'等,故忽略,不忽略也没办法,因为方法没那么智能,不能因小失大
        rword='曹操'
    elif word=='玄德曰' or word=='玄德':
        rword='刘备'
    elif word=='孔明曰':
        rword='孔明'
    elif word=='云长':
        rword='关公'
    elif word=='':
        rword='周瑜'
    elif word=='':
        rword='司马懿'
    else:
        rword=word
    counts[rword]=counts.get(rword,0)+1
for word in excludes:
    del counts[word]
items=list(counts.items()) #应该是key value item
items.sort(key=lambda x:x[1],reverse=True)
liword=[]
print('出场次数详情:')
for i in range(16):  #无限次循环直到满足条件停要用while!!!不用if,if是单次用的!!!!!!!!!!
    word,count=items[i]
    print('{2}th:{0:<10}:{1:>6}'.format(word,count,i+1))
print('出场次数排行榜TOP15:')
for i in range(15):
    print(items[i][0],end=',')
print('\n单字精确迭代寻找需要手动清除{0}个干扰项'.format(len(excludes)))

输出:

出场次数详情:
1th:曹操 : 2132
2th:刘备 : 1252
3th:孔明 : 1226
4th:关公 : 775
5th:周瑜 : 494
6th:司马懿 : 462
7th:张飞 : 358
8th:吕布 : 300
9th:赵云 : 278
10th:孙权 : 264
11th:郃 : 253
12th:都督 : 221
13th:袁绍 : 191
14th:马超 : 185
15th:魏延 : 180
16th:黄忠 : 168
出场次数排行榜TOP15:
曹操,刘备,孔明,关公,周瑜,司马懿,张飞,吕布,赵云,孙权,郃,都督,袁绍,马超,魏延,
单字精确迭代寻找需要手动清除182个干扰项

猜你喜欢

转载自www.cnblogs.com/zhangziyan/p/9208571.html