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])

     截图:

     

  • 相关阅读:
    消息机制
    窗口!窗口!- Windows程序设计(SDK)003
    内联函数的作用
    结构体变量用 . 结构体指针用-> 的原因
    &a和a的区别
    分布电容
    介电常数
    天线
    封装的思想
    关于中断标志位
  • 原文地址:https://www.cnblogs.com/ashh/p/8664779.html
Copyright © 2011-2022 走看看