词频统计(三国人物出场统计、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大报告高频词汇统计)
发布评论