zoukankan      html  css  js  c++  java
  • 中文词频统计

    下载一长篇中文文章。

    从文件读取待分析文本。

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

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

    pip install jieba

    import jieba

    list(jieba.lcut(news))

    生成词频统计

    排序

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

    输出词频最大TOP20

    将代码与运行结果截图发布在博客上。

    # -*- coding: UTF-8 -*-# -*-
    import  jieba
    fo = open('我们仨.txt','r',encoding = 'utf-8')
    novel = fo.read()
    novelList=list(jieba.lcut(novel))
    
    exclude = {'',',', '','', '', '',' ','u3000','
    ','',
               '', '', '', '', '', '', '', '',
               '', '', '', '', '', '', '', '',
               '', '', '', '', '','', '', '', '',
               '', '', '', '', '','', '', '', '',
               '', '', '一个', '', '', '', '', '',
               '', '', '', '', '', '', '', '没有',
               '','','','','','','','','什么','因为'}
    
    novelDict = {}
    novelSet = set(novelList)-exclude
    for s in novelSet:
        novelDict[s] = novelList.count(s)  
    
        dictList = list(novelDict.items())
        dictList.sort(key=lambda x: x[1], reverse=True)
    
    for i in range(20):
            print(dictList[i])

     截图:

     

  • 相关阅读:
    Hibernate学习之缓存机制
    Hibernate学习之hibernate状态
    Ajax学习之小结
    Hibernate学习之hibernate执行顺序
    Svn入门
    Svn服务启动的两种方式
    Eclipse安装Svn插件
    一种给力的带背景的超链接的写法
    转载:IE下div使用margin:0px auto不居中的原因
    github上的Lua in Erlang
  • 原文地址:https://www.cnblogs.com/ashh/p/8664779.html
Copyright © 2011-2022 走看看