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

    .中文分词

    1. 下载一中文长篇小说,并转换成UTF-8编码。
    2. 使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。
    3. 排除一些无意义词、合并同一词。
    import jieba
    txt=open('test.txt','r',encoding='UTF-8').read()
    for i in '''.,-
    	u3000'()":“”,。‘’?''':
        txt=txt.replace(i,'')
    words=list(jieba.cut(txt))
    
    exc={'','','','','','','','','','','','','','',''}
    dic={}
    keys=set(words)
    keys=keys-exc
    for w in keys:
        dic[w]=words.count(w)
    
    wc = list(dic.items())
    wc.sort(key=lambda x:x[1],reverse=True)
    
    for w in range(20):
        print(wc[w])

    利用循环语句去除单字词

    import jieba
    txt=open('test.txt','r',encoding='UTF-8').read()
    for i in '''.,-
    	u3000'()":“”,。‘’?''':
        txt=txt.replace(i,'')
    words=list(jieba.cut(txt))
    
    
    dic={}
    for w in words:
        if len(w)==1:
            continue
        else:
            dic[w]=dic.get(w,0)+1
            
    wc = list(dic.items())
    wc.sort(key=lambda x:x[1],reverse=True)
    
    for w in range(20):
        print(wc[w])

  • 相关阅读:
    nginx公网IP无法访问浏览器
    Internet接入方式
    Adobe Photoshop Lightroom 5.3和序列号
    getopt
    printf
    scanf
    cycling -avoid the vicious cycle
    ACE handle_timeout 事件重入
    Linux查看程序端口占用
    The GNU C Library
  • 原文地址:https://www.cnblogs.com/lintingting/p/7609646.html
Copyright © 2011-2022 走看看