1、jieba庫安裝
(1)全自動安裝
easy-install jieba
pip install jieba
pip3 install jieba
(2)半自動安裝
首先登入https://pypi.org/project/jieba/下載安裝包
最后解壓安裝包:python setup py install
(3)手動安裝
首先登入https://pypi.org/project/jieba/下載安裝包
最后把jieba目錄放置在site-packages目錄內(nèi)
2、分詞
(1)cut
語法:
jieba.cut(sentence, cut_all=False, HMM=True, use_paddle=False)
功能描述: 將傳入的字符串參數(shù)分詞
返回情況: 返回一個生成器對象
參數(shù)說明:
sentence: 被分詞的字符串
cut_all: 分詞模式是否設(shè)置為全模式,值為False時,精準模式分詞
HMM: 是否使用 HMM 模型
(2)lcu
語法:
jieba.lcut(sentence)
功能描述: 將傳入的字符串參數(shù)分詞
返回情況: 返回一個數(shù)組
參數(shù)說明:
sentence: 被分詞的字符串
(3)cut_for_search 搜索引擎模式
語法:
jieba.cut_for_search(sentence, HMM=True)
功能描述: 將傳入的字符串參數(shù)分詞
返回情況: 返回一個生成器對象
參數(shù)說明:
sentence: 被分詞的字符串
HMM: 是否使用 HMM 模型
(4)lcut_for_search 搜索引擎模式
語法:
jieba.lcut_for_search(sentence)
功能描述: 將傳入的字符串參數(shù)分詞
返回情況: 返回一個數(shù)組
參數(shù)說明:
sentence: 被分詞的字符串
(5)分詞模式的區(qū)別
s=“我叫陳妍希,來自中國臺灣”
分詞模式 | 描述 | 結(jié)果 |
---|---|---|
全模式 | 試圖將句子最精確地切開,適合文本分析。 | [‘我’, ‘叫’, ‘陳’, ‘妍’, ‘?!? ‘,’, ‘來自’, ‘中國’, ‘中國臺灣’, ‘臺灣’] |
精準模式 | 把句子中所有可以成詞的詞語都掃描出來,速度非???,但是不能解決歧義。 | [‘我’, ‘叫’, ‘陳妍?!? ‘,’, ‘來自’, ‘中國臺灣’] |
搜索引擎模式 | 在精確模式的基礎(chǔ)上,對長詞再次切分,提高召回率,適合用于搜索引擎分詞 | [‘我’, ‘叫’, ‘陳妍?!? ‘,’, ‘來自’, ‘中國’, ‘臺灣’, ‘中國臺灣’] |
3、其他用法
(1) 添加自定義詞典
語法:
jieba.load_userdict(f)
功能描述: 添加自定義詞典
返回情況: 無返回
參數(shù)說明:
f: 詞典文件路徑。文件中一詞占一行;一行分三部分,分別為詞語,詞頻(可省略),詞性(可省略),其中分隔符為空格。
(2)添加新詞
語法:
jieba.add_word(word, freq=None, tag=None)
功能描述: 添加新的分詞到詞典中
返回情況: 無返回
參數(shù)說明:
word: 需要添加的新詞
freq: 詞頻
(2)刪除詞語
語法:
jieba.del_word(word)
功能描述: 刪除詞語
返回情況: 無返回
參數(shù)說明:
word: 需要刪除的新詞
(3)去除停用詞
[w for w in words if w not in 停用詞表]
4、詞頻統(tǒng)計
詞頻統(tǒng)計需要引入一個單獨的類,使用下面方法前需提前引入from collections import Counter
語法:
Counter(iterable=None)
功能描述: 對傳入的參數(shù)對象進行詞頻統(tǒng)計
返回情況: 返回一個collections.Counter類,類似于字典類型的數(shù)據(jù),數(shù)據(jù)格式:Counter({詞語:詞頻})
參數(shù)說明:
iterable: 被統(tǒng)計的對象,必須是可迭代對象
5、詞性提取
語法:
jieba.posseg.cut(sentence, HMM=True, use_paddle=False)
功能描述: 對傳入的數(shù)據(jù)進行詞性判斷
返回情況: 返回一個生成器,信息包含詞語及其詞性 (數(shù)據(jù)格式:詞語/詞性)
注意: 使用for遍歷時,可使用w.word和w.flag屬性提取詞語和詞性
參數(shù)說明:
sentence: 要處理的數(shù)據(jù)
HMM: 是否使用HMM模型
6、詞云繪制
本次詞云繪制主要使用wordcloud
庫,wordcloud
依賴的第三方庫主要有numpy
、Pillow
和matplotlib
。安裝時盡量使用pip install wordcloud
命令行安裝的方法安裝,繪制詞云主要使用wordcloud
庫中的WordCloud
接口。
(1)第三方庫準備工作
from scipy.misc import imread # 讀取照片,scipy1.2.0以下的版本才有imread,也可from imageio import imread
import matplotlib.pyplot as plt # 詞云圖展示
from wordcloud import WordCloud,ImageColorGenerator # 詞云圖繪制
(2)設(shè)置背景圖
img=imread(圖片文件路徑)
(3)詞云函數(shù)
(1)創(chuàng)建詞云函數(shù)
語法:
WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9, mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None, background_color='black', max_font_size=None, font_step=1, mode='RGB', relative_scaling='auto', regexp=None, collocations=True, colormap=None, normalize_plurals=True, contour_width=0, contour_color='black', repeat=False, include_numbers=False, min_word_length=0, collocation_threshold=30)
功能描述: 創(chuàng)建詞云函數(shù)
返回情況: 返回一個WordCloud函數(shù)對象
參數(shù)說明:
font_path: 字體文件。
width: 詞云畫布的寬度
height: 詞云畫布的高度
margin: 頁邊距
background_color: 詞云畫布的背景顏色
mask: 詞云遮罩性狀的照片,如果有的話會忽略width和height參數(shù)
max_words: 最多顯示的詞匯量
max_font_size: 詞云字體最小的字號
max_font_size: 詞云字體最大的字號
stopwords: 屏蔽的詞
random_state: 隨機狀態(tài)
color_func : 顏色的函數(shù),設(shè)置字體顏色
(2)生成詞云
1.generate方法
語法:
詞云函數(shù).generate(f)
功能描述: 導(dǎo)入一個文件到詞云函數(shù)中,根據(jù)詞語繪制詞云
返回情況: 返回一個函數(shù)對象
參數(shù)說明:
f: txt文件,如果傳入一個已排序好的單詞列表則需要傳入collocations=False參數(shù),避免結(jié)果出現(xiàn)重復(fù)現(xiàn)象
2.generate_from_text
語法:
詞云函數(shù).generate_from_text(f)
功能描述: 導(dǎo)入一個文件到詞云函數(shù)中,根據(jù)詞語繪制詞云
返回情況: 返回一個函數(shù)對象
參數(shù)說明:
f: txt文件,如果傳入一個已排序好的單詞列表則需要傳入collocations=False參數(shù),避免結(jié)果出現(xiàn)重復(fù)現(xiàn)象
3.generate_from_frequencies
語法:
詞云函數(shù).generate_from_frequencies(f)
功能描述: 導(dǎo)入一個文件到詞云函數(shù)中,根據(jù)詞語和詞頻繪制詞云
返回情況: 返回一個函數(shù)對象
(3)重置字體顏色
1.生成顏色函數(shù)
語法:
ImageColorGenerator(image, default_color=None)
功能描述: 生成顏色生成器
返回情況: 返回一個生成器對象
參數(shù)說明:
image: 圖片數(shù)組對象,可使用imread(圖片路徑)返回的對象
default_color: 如果畫布大于圖像,則使用回退顏色,格式為(r,g,b)。如果無,則改為引發(fā)ValueError。
2.重置字體顏色
語法:
詞云函數(shù).recolor(random_state=None, color_func=None, colormap=None)
功能描述: 重置詞云字體顏色
返回情況: 返回一個函數(shù)對象
參數(shù)說明:
random_state: 隨機狀態(tài)
color_func: 顏色函數(shù),跟WordCloud函數(shù)中color_func值一樣
colormap:
3.讀取照片
語法:
imread(uri, format=None, **kwargs)
功能描述: 從指定文件讀取圖像,生成一個數(shù)組
返回情況: 返回一個numpy數(shù)組
參數(shù)說明:
uri: 文件資源,{str, pathlib.Path, bytes, file}
format: 讀取文件的格式
(4)詞云圖展示
詞云可視化通過Pyplot 庫快來實現(xiàn),Pyplot 是 Matplotlib 的子庫,提供了很多繪畫接口。文章來源:http://www.zghlxwxcb.cn/news/detail-465931.html
plt.imshow(詞云函數(shù).recolor(color_func=ImageColorGenerator(img)))
plt.axis('off')
plt.show()
(5)保存詞云照片
語法:
詞云函數(shù).to_file(文件路徑)
|詞云函數(shù).to_image(文件路徑)
|詞云函數(shù).to_svg(文件路徑,embed_font=False, optimize_embedded_font=True, embed_image=False))
功能描述: 生成指定的文件
返回情況: 無返回,但會輸出一個文件
參數(shù)說明:
embed_font:是否在生成的SVG文件中包含字體
optimize_embedded_font:字體大小自適應(yīng)
embed_image:是否在生成的SVG文件中包含光柵化圖像
文章來源地址http://www.zghlxwxcb.cn/news/detail-465931.html
到了這里,關(guān)于Python文本分析之中文分詞(jieba庫)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!