中文词频统计

中文分词

  1. 下载一中文长篇小说,并转换成UTF-8编码。
  2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
  3. 排除一些无意义词、合并同一词。
  4. 对词频统计结果做简单的解读。
    import jieba
    fo = open('C:\\Users\\Administrator\\Desktop\\l.txt','r',encoding='utf-8')
    news = fo.read()
    fo.close()
    
    words = jieba.cut(news)
    w = list(words)
    
    exp={',',' ','。','“','”','‘’',"'",'......','?','...','\n','\u3000','!'}
    
    dic={}
    keys = set(w)-exp
    for i in keys:
        if len(i)>1:
            dic[i]=w.count(i)#单词计数字典
        
        its=list(dic.items())#单词计数元组的列表
        its.sort(key=lambda x:x[1],reverse=True)#排序
        
    for i in range(20):
        print(its[i])
    

      

     分析《龙族》这部小说,路明非是主角,引出一切关于龙的传说。

 

posted on 2017-09-29 11:30  黑木-007  阅读(158)  评论(0编辑  收藏  举报