yshda

导航

 

 一、jieba库的相关函数

二、词频统计((以下内容以百度搜索的散文为例)

步骤:

1、下载散文文本并以txt形式保存到与Python相同文件夹中

2、编写代码

#CalThreeKingdomsV1.py
import jieba
txt = open("全职高手.txt", "r", encoding='utf-8').read()
words  = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True) 
for i in range(15):
    word, count = items[i]
    print ("{0:<10}{1:>5}".format(word, count))  
 效果图

 

posted on 2020-04-05 16:07  yshda  阅读(104)  评论(0)    收藏  举报