<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          教你利用Python繪制酷炫的詞云圖。

          共 6369字,需瀏覽 13分鐘

           ·

          2021-07-31 12:51

          1. 效果展示

          詞云圖想必大家都見過,是一種形式新穎的查看文本中出現(xiàn)最多詞匯的圖。

          我使用Python的第三方庫stylecloud來分別生成了 2 張?jiān)~云圖,讀者可以猜一猜以下詞云圖的出處來自于哪里。

          詞云圖 1
          詞云圖 2

          2. 實(shí)現(xiàn)過程

          2.1 導(dǎo)入庫

          import pandas as pd
          import stylecloud
          import jieba
          from collections import Counter

          2.2 導(dǎo)入文本

          with open('./三體.txt',encoding='utf-8') as f:
              txt = f.read()
          txt = txt.split()

          2.3 去除停用詞

          def stopwordslist():
              stopwords = [line.strip() for line in open('./常見中文停用詞表.txt''r', encoding='gbk').readlines()]
              stopwords.append(' '# 自定義添加停用詞
              return stopwords

          def movestopwords(sentence):
              stopwords = stopwordslist()  # 加載停用詞的路徑
              santi_words =[x for x in sentence if len(x) >1 and x not in stopwords]
              return ' '.join(santi_words)

          data_cut = jieba.lcut(str(txt))
          word_list = movestopwords(data_cut)
          # print(word_list.split(' '))

          2.4 統(tǒng)計(jì)詞頻

          mycount = {}
          for word in word_list.split(' '):
              mycount[word] = mycount.get(word,0)+1
          counts_df = pd.DataFrame(mycount.items(), columns=['label''counts'])
          counts_df.sort_values(by='counts',inplace=True, ascending = False)
          counts_df.to_csv('./詞頻統(tǒng)計(jì).csv',encoding='utf-8')
          print('輸出詞頻統(tǒng)計(jì) 成功!!')
          print(counts_df.iloc[:10]) # 輸出詞頻前 10 的詞匯

          2.5 生成詞云圖

          stylecloud.gen_stylecloud(
                                      text=word_list,
                                      palette='tableau.BlueRed_6',
                                      icon_name='fas fa-apple-alt',
                                      font_path='./田英章楷書3500字.ttf',
                                      output_name='《三體》詞云圖.png',
                                      # custom_stopwords=stopwords
                                      )

          3. API詳解

          3.1 stylecloud.gen_stylecloud() 參數(shù)詳解

          gen_stylecloud(text=None,  # 輸入文本(不含詞頻數(shù))
                         file_path=None,   # 輸入文本/CSV 的文件路徑 (可以含詞頻數(shù))
                         size=512,  # stylecloud 的大小(長度和寬度)
                         icon_name='fas fa-flag',  # stylecloud 形狀的圖標(biāo)名稱(如 fas fa-grin)。[default: fas fa-flag]
                         palette='cartocolors.qualitative.Bold_5',  # 調(diào)色板(通過 palettable 實(shí)現(xiàn))。[default: cartocolors.qualitative.Bold_6]
                         colors=None, # 自定義十六進(jìn)制的字體顏色
                         background_color="white",  # 背景顏色
                         max_font_size=200,  # stylecloud 中的最大字號(hào)
                         max_words=2000,  # stylecloud 可包含的最大單詞數(shù)
                         stopwords=True,  # 布爾值,用于篩除常見禁用詞
                         custom_stopwords=STOPWORDS, # 去除停用詞
                         icon_dir='.temp',
                         output_name='stylecloud.png',   # stylecloud 的輸出文本名
                         gradient=None,  # 梯度方向
                         font_path=os.path.join(STATIC_PATH,'Staatliches-Regular.ttf'), # stylecloud 所用字體
                         random_state=None,  # 控制單詞和顏色的隨機(jī)狀態(tài)
                         collocations=True,
                         invert_mask=False,
                         pro_icon_path=None,
                         pro_css_path=None)

          3.2 palette (調(diào)色板)

          ?

          參考網(wǎng)站:「https://jiffyclub.github.io/palettable/」

          ?

          3.3 icon_name (圖標(biāo)名稱)

          ?

          參考網(wǎng)站:「https://fa5.dashgame.com/#/%E5%9B%BE%E6%A0%87」

          ?

          4. 遺留的小問題

          • 中文博大精深,jieba對(duì)中文切詞的準(zhǔn)確性問題,可通過自定義添加詞匯解決。
          • 重復(fù)無意義的詞匯反復(fù)、連續(xù)出現(xiàn),可通過機(jī)械壓縮詞匯的方法解決。

          5. 資料下載

          我已將以上配套數(shù)據(jù)文件和代碼文件打包上傳至我的 Github 和 Gitee,感興趣的讀者可以下載學(xué)習(xí)和練手。

          • 「Github 項(xiàng)目地址」

          「https://github.com/don2vito/wechat_project/tree/master/詞云」

          • 「Gitee 項(xiàng)目地址」

          「https://gitee.com/don2vito/wechat_official_account/blob/master/038_詞云」


          歡迎關(guān)注

          點(diǎn)分享
          點(diǎn)收藏
          點(diǎn)點(diǎn)贊
          點(diǎn)在看
          瀏覽 123
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  曰韩欧美综合黄色 | 日本成人在线不卡视频 | 亚洲人成在线观看 | 在线成人av| www.99爱 |