Python实现 文本词频统计(嵩天老师) |
您所在的位置:网站首页 › python实现文本文件的词频统计 › Python实现 文本词频统计(嵩天老师) |
实例10:文本词频统计
引用文本 英文文本:Hamet https://python123.io/resources/pye/hamlet.txt 中文文本:《三国演义》 https://python123.io/resources/pye/threekingdoms.txt 代码(哈姆雷特): #CalHamlet1.py def getText(): txt=open("hamlet.txt","r").read() txt=txt.lower() for ch in '!"#$%&()*+,-./:;?@[\\]^_{|}.~’‘': txt=txt.replace(ch,"") return txt hamletTxt=getText() words=hamletTxt.split() counts={} for word in words: counts[word]=counts.get(word,0)+1 items=list(counts.items()) items.sort(key=lambda x:x[1],reverse=True) for i in range(10): word,count=items[i] print("{0: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |