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 -*-
    # -*- author : onexiaofeng -*-
    import jieba
    jieba.add_word('路明非')
    news=open('longzu.txt','r',encoding='utf-8')
    notes=news.read()
    notelist=list(jieba.lcut(notes))
    
    Word={}
    for i in set(notelist):    
        Word[i]=notelist.count(i)
    
    delete_word={'',' ','','', '', '', '', '', '', '', '', '','','','','','','','','?','','',
               '','使','','','','','','','', '
    ','','','','','','','','','','','','','',
                 '','','','便','','','','','','','','','','','使','','','','','','','','','','一个','','','','就是'}
    
    for i in delete_word:        
        if i in Word:
            del Word[i]
    
    sort_word = sorted(Word.items(), key= lambda d:d[1], reverse = True)  
    for i in range(20):  
        print(sort_word[i])

     截图:

  • 相关阅读:
    随机色块
    JQ命令汇总
    JQ选择器
    cookie
    tab切换
    Ajax跨域
    RocksDB介绍:一个比LevelDB更彪悍的引擎
    谷歌的诀窍:如何取消验证码
    Ruby on Rails创始人DHH谈如何进行混合移动APP开发
    SequoiaDB 架构指南
  • 原文地址:https://www.cnblogs.com/170he/p/8665316.html
Copyright © 2011-2022 走看看