中文的分词+词频统计

 

下载一长篇中文文章。

从文件读取待分析文本。

news = open('gzccnews.txt','r',encoding = 'utf-8')

安装与使用jieba进行中文分词。

pip install jieba

import jieba

list(jieba.lcut(news))

生成词频统计

排序

排除语法型词汇,代词、冠词、连词

输出词频最大TOP20

 

代码

import jieba
with open('novel.txt','r',encoding="utf-8") as file:
    novel = file.read()

punctuation = '。,;!?、'
for l in punctuation:
    novel = novel.replace(l,'')

no_list = list(jieba.cut(novel))
dic = dict()
for i in no_list:
    if len(i)!=1:
        dic[i] = novel.count(i)

del_word = { '\n',' '}

for i in del_word:
    if i in dic:
        del dic[i]
dic = sorted(dic.items(),key=lambda x:x[1],reverse = True)
for i in range(20):
    print(dic[i])

 

截图如下

转载于:https://www.cnblogs.com/RE148/p/8664533.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值