close

一、前言

jieba怎麼用?如何用jieba進行分詞,如何用jieba+wordcloud來繪製文字雲!?

如何用python爬取網路新聞,字數統計後繪製直接查看當前最熱的關鍵字!!而後繪製成文字雲wordcloud,

這邊case by case帶你走下去,5分鐘看完,廢話不多說我們開始吧!

大致分成下面幾個步驟(最下面附完整程式碼)

(1) 設定分詞庫資料庫、(2) 設定自訂常用詞、(3) 斷句的方法及如何統計詞出現的次數、(4) 移除停用詞 

 

二、jieba實戰

還沒安裝的先pip install一下,還不會用pip可以看 這篇文章 pip安裝大法一次掌握

pip install jieba # 安裝jieba

這邊抓取Yahoo新聞的的某一段來進行Case By Case教學,*懶人包完整程式碼在最下面!

先匯入套件包,並且抓取Yahoo新聞的某篇文章的某一段內文,有興趣的朋友可以自己去看

import pandas, numpy
import requests # 訪問
from bs4 import BeautifulSoup # 網頁解析
import jieba
from collections import Counter # 次數統計

# 抓取yahoo新聞的某篇某段當範例
url = 'https://tw.news.yahoo.com/%E6%99%B6%E5%9C%93%E5%BB%A0%E6%89%8D%E6%98%AF%E7%9C%9F%E6%AD%A3%E7%9A%84%E9%87%91%E9%9B%9E%E6%AF%8D-%E6%A5%8A%E6%87%89%E8%B6%85%E7%82%BA%E8%8B%B1%E7%89%B9%E7%88%BE%E7%B7%A9%E9%A0%B0-%E4%BB%A3%E5%B7%A5%E7%9A%84%E5%8F%B0%E7%A9%8D%E9%9B%BB%E6%80%8E%E9%BA%BC%E8%BE%A6-114410893.html'
re = requests.get(url)
soup = BeautifulSoup(re.text, 'html.parser')
texts = soup.find_all('p', class_='M(0)')
text = texts[0].text

(1) 設定分詞庫資料庫

設定分詞資料庫,裡面已經有常用的詞,複製註解網址右鍵另存一個txt檔就可以了

這邊使用jieba.set_dictionary('分詞資料庫')

# 設定分詞資料庫
# https://raw.githubusercontent.com/fxsjy/jieba/master/extra_dict/dict.txt.big 右鍵另存放目錄下
jieba.set_dictionary('dict.txt.big.txt')

(2) 設定自訂常用詞

加入自己定義的文字,這邊有兩種方法,一個是先打下來再txt檔裡面, 另者直接用語法新增刪除。

這邊我用jieba.load_userdict來讀取我放常用的詞txt檔

# 將自己常用的詞加入字典
jieba.load_userdict('finance_dict.txt')

# 新增及刪除常用詞
jieba.add_word('英特爾') # 加入英特爾
jieba.add_word('美元') # 加入英特爾
jieba.del_word('元') # 刪除電風扇

finance_dict.txt裡面長這樣,用換行個開每個詞就可以了很簡單

image

(3) 斷句的方法及如何統計詞出現的次數

jieba常用的方法有下面幾種,你可以想像全切模式就是一個詞可能可以切出很多種組合,大部分用預設模式就可以了

jieba.cut與jieba.lcut的差別在jieba.cut要用print(','.join(segs))去看裡面的東西,jieba.lcut直接返回list給你

最後用Counter(seg_list)就可以知道每個詞在list中出現果幾次了

# 斷句方式
# 用jieba.lcut(text, cut_all=False)直接返回list
segs = jieba.cut(text, cut_all=True) # 全切模式 切的很碎 用print(','.join(segs))檢視
segs = jieba.cut(text, cut_all=False) # 預設模式
seg_list = jieba.lcut(text, cut_all=False) # lcut直接返回list

# 統計分詞出現次數
dictionary = Counter(seg_list)

image

image

image

(4) 移除停用詞

總要把 空白,()...。「」之類的弄掉不去統計吧!

# 移除停用詞
stopword = [' ', ',', '(', ')', '...', '。', '「', '」']  # 定義停用詞
[dictionary.pop(x, None) for x in stopword] # 存字典裡刪除停用詞

 

完整程式碼

import pandas, numpy
import requests # 訪問
from bs4 import BeautifulSoup # 網頁解析
import jieba
from collections import Counter # 次數統計

# 抓取yahoo新聞的某篇某段當範例
url = 'https://tw.news.yahoo.com/%E6%99%B6%E5%9C%93%E5%BB%A0%E6%89%8D%E6%98%AF%E7%9C%9F%E6%AD%A3%E7%9A%84%E9%87%91%E9%9B%9E%E6%AF%8D-%E6%A5%8A%E6%87%89%E8%B6%85%E7%82%BA%E8%8B%B1%E7%89%B9%E7%88%BE%E7%B7%A9%E9%A0%B0-%E4%BB%A3%E5%B7%A5%E7%9A%84%E5%8F%B0%E7%A9%8D%E9%9B%BB%E6%80%8E%E9%BA%BC%E8%BE%A6-114410893.html'
re = requests.get(url)
soup = BeautifulSoup(re.text, 'html.parser')
texts = soup.find_all('p', class_='M(0)')
text = texts[0].text

# 設定分詞資料庫
# https://raw.githubusercontent.com/fxsjy/jieba/master/extra_dict/dict.txt.big 右鍵另存放目錄下
jieba.set_dictionary('dict.txt.big.txt')

# 將自己常用的詞加入字典
jieba.load_userdict('finance_dict.txt')

# 新增及刪除常用詞
jieba.add_word('英特爾') # 加入英特爾
jieba.add_word('美元') # 加入英特爾
jieba.del_word('元') # 刪除電風扇

# 斷句方式
# 用jieba.lcut(text, cut_all=False)直接返回list
segs = jieba.cut(text, cut_all=True) # 全切模式 切的很碎
segs = jieba.cut(text, cut_all=False) # 預設模式
seg_list = jieba.lcut(text, cut_all=False) # lcut直接返回list

# 統計分詞出現次數
dictionary = Counter(seg_list)

# 移除停用詞
stopword = [' ', ',', '(', ')', '...', '。', '「', '」']  # 定義停用詞
[dictionary.pop(x, None) for x in stopword] # 存字典裡刪除停用詞

 

三、後記

聰明的小伙舉一反三,可以一次收集一整個網站的內文或是標題來統計,

下一篇來告訴大家如何繪製文字雲wordcloud!,看新聞熱點、資料視覺化及告白神器!

【wordcloud】用python繪製文字雲:抓取yahoo新聞用jieba+wordcloud繪製自己的文字雲 看完文章5分鐘馬上會寫code

 

arrow
arrow

    恩哥Python 發表在 痞客邦 留言(0) 人氣()