Pyhton学习之中文词频统计(三国人物出场统计、19大报告高频词汇统计)

编程入门 行业动态 更新时间:2024-10-26 17:31:04

Pyhton学习之中文<a href=https://www.elefans.com/category/jswz/34/1767675.html style=词频统计(三国人物出场统计、19大报告高频词汇统计)"/>

Pyhton学习之中文词频统计(三国人物出场统计、19大报告高频词汇统计)

用python程序实现统计三国人物的出场次数的统计,引用强大的jieba库做分词解析,然后根据我们的统计目标,做适当的统计修正。如非人物的词语就需要统计了,再如人物会有多个名称时也需要一起统计。

技术路线:jieba库,数据字典、集合等组合数据类型的应用场景

实现:

#calthtreeKingdowsV1.py
import jieba
def getTxt(paper):
    txt = open(paper,'r',encoding="utf-8").read()
    txtlist = jieba.lcut(txt)
    return txtlist


def main():
    zwtxt = getTxt("threeKingdows.txt")
    countDict = {}
    #定义一个集合,修正非人名
    excludes = {'将军','却说','三人','天下','东吴','今日','不敢','魏兵','于是','不可','荆州','二人','如此','不能','商议','如何','军士','左右','军马','引兵','次日','大喜'}
    for ch in zwtxt:
        if len(ch)==1:
            continue
        elif ch in excludes:
            continue
        elif ch == "玄德" or ch == "玄德曰" or ch == "主公":
            ch = "刘备"
        elif ch == "孔明" or ch == "孔明曰":
            ch = "诸葛亮"
        elif ch == "孟德" or ch == "孟德曰" or ch == "丞相":
            ch = "曹操"
        elif ch == "关公" or ch == "云长":
            ch = "关羽"
        countDict[ch] = countDict.get(ch,0) + 1
    itemList = list(countDict.items())
    itemList.sort(key=lambda x:x[1], reverse=True)
    for i in range(10):
        word,count = itemList[i]
        print("{}:{}".format(word,count))
        

main()

PS:没有完全修正完。

延伸,作为信息战线的党员小同志,用python自己统计下19大报告的高频词汇也是必须的。

实现:

#cal19thReportV1.py
import jieba
def getTxt(paper):
    txt = open(paper,'r',encoding="utf-8").read()
    txtlist = jieba.lcut(txt)
    return txtlist


def main():
    zwtxt = getTxt("19threport.txt")
    countDict = {}
    for ch in zwtxt:
        if len(ch)==1:
            continue
        countDict[ch] = countDict.get(ch,0) + 1
    itemList = list(countDict.items())
    itemList.sort(key=lambda x:x[1], reverse=True)
    for i in range(10):
        word,count = itemList[i]
        print("{}:{}".format(word,count))       
main()

PS:对,就是这么短,python 牛,继续。

更多推荐

Pyhton学习之中文词频统计(三国人物出场统计、19大报告高频词汇统计)

本文发布于:2024-03-08 16:33:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1721455.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词频   中文   国人   词汇   报告

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!