zoukankan      html  css  js  c++  java
  • 中文词频统计及词云制作 25

    a、下载一中文长篇小说,并转换成UTF-8编码。

    复制代码
    lr = open('test.txt','w')
    lr.write('''At the moment, the sky is dark, the air is fresh factor after just rained.
    Suddenly thought of blue plaid shirt; Those were broken into various shapes of
    stationery;From the corner at the beginning of deep friendship; Have declared the
    end of the encounter that haven't start planning... Those years, those days of do,
    finally, like youth, will end in our life.''')
    lr.close()
    lo = open('test.txt','r')
    lo = lo.read()
    lo = lo.lower()
    for i in ',.':
        lo = lo.replace(i,' ')
    words = lo.split(' ')
    exp = {'','the','and','to','on','of','a','is'}
    dic = {}
    
    keys = set(words) - exp
    for i in keys:
        dic[i]=words.count(i)
    
    t = sorted(dic.items())
    
    dd = list(dic.items())
    dd.sort(key = lambda x:x[1],reverse = True)
    
    for i in range(10):
        print(dd[i])
    复制代码

      b、使用jieba库,进行中文词频统计,输出TOP20的词及出现次数。

    复制代码
    import jieba
    
    lr = open('llf.txt','w')
    lr.write('''朋友居五伦之末,其实朋友是极重要的一伦。所谓友谊实即人与人之间的一种良好的关系,其中包括了解、
                欣赏、信任、容忍、牺牲……诸多美德。如果以友谊作基础,则其他的各种关系如父子夫妇兄弟之类均可
                圆满地建立起来。当然父子兄弟是无可选择的永久关系,夫妇虽有选择余地,但一经结合便以不再仳离为原则,
                而朋友则是有聚有散可合可分的。不过,说穿了,父子夫妇兄弟都是朋友关系,不过形式性质稍有不同罢了。
                严格地讲,凡是充分具备一个好朋友的人,他一定也是一个好父亲、好儿子、好丈夫、好妻子、好哥哥、好弟弟。
                反过来亦然)
    ''')
    lr.close()
    
    txt = open('llf.txt','r',encoding='GBK').read()
    words = jieba.cut(txt)
    
    
    dic = {}
    for word in words:
        if len(word) == 1:
            continue
        else:
            reword = word
            dic[word] = dic.get(word,0) +1
    
    keys = set(word) 
    
    
    t = sorted(dic.items())
    
    dd = list(dic.items())
    dd.sort(key = lambda x:x[1],reverse = True)
    
    for i in range(20):
        print(dd[i])
    复制代码

     c、**使用wordcloud库绘制一个词云。

    复制代码
    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    lr = open('llf1.txt','w')
    lr.write('''朋友 朋友 朋友 朋友 朋友 朋友 朋友 朋友
    关系 关系 关系 关系 关系 关系 关
    父子 父子 父子 父子 父子
    夫妇 夫妇 夫妇 夫妇
    兄弟  兄弟 兄弟 兄弟
    友谊 友谊 友谊
    不过 不过
    一个  一个 五伦 之末 其实 重要 一伦 所谓 实即 之间 一种 良好 其中
    ''')
    lr.close()
    lo = open('llf1.txt','r',encoding='GBK').read()
    mywc = WordCloud().generate(lo)
    plt.imshow(mywc)
    plt.show()
    复制代码

  • 相关阅读:
    P1052 过河
    P1004 方格取数
    自定义事件
    自定义单选,多选按钮
    构造函数+原型的js混合模式
    图标
    格式化
    时间 ---- 时间简史
    居中
    插入DOM元素
  • 原文地址:https://www.cnblogs.com/kinoko/p/7684991.html
Copyright © 2011-2022 走看看