jieba库的使用和好看的词元
生活随笔
收集整理的這篇文章主要介紹了
jieba库的使用和好看的词元
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.jieba庫的使用與說明
1.jieba庫基本介紹
jieba庫是優秀的中文分詞第三方庫
? ? ? ? ?-中文文本需要通過分詞獲得單個的詞語
????? ? ?- jieba是優秀的中文分詞第三方庫,需要額外安裝
????? ? ?- jieba庫提供三種分詞模式,最簡單只需掌握一個函數
2.jieba庫使用說明
? (1)、jieba分詞的三種模式
????? ? ?精確模式、全模式、搜索引擎模式
????? ? ?- 精確模式:把文本精確的切分開,不存在冗余單詞
????? ? ?- 全模式:把文本中所有可能的詞語都掃描出來,有冗余
????? ? ?- 搜索引擎模式:在精確模式基礎上,對長詞再次切分
(2)、jieba庫常用函數
?3.jieba庫的利用實例顯示
?
4.利用jieba庫統計三國演義的人物出場次數
(1)代碼如下
import jieba excludes = {"將軍","卻說","荊州","二人","不可","不能","如此","商議","軍士","如何","主公","軍馬","左右",} txt = open("./三國演義.txt", "r", encoding='utf-8').read() words = jieba.lcut(txt) counts = {} for word in words:if len(word) == 1:continueelif word == "諸葛亮" or word == "孔明曰":rword = "孔明"elif word == "關公" or word == "云長":rword = "關羽"elif word == "玄德" or word == "玄德曰":rword = "劉備"elif word == "孟德" or word == "丞相":rword = "曹操"else:rword = wordcounts[rword] = counts.get(rword,0) + 1 for word in excludes:del counts[word] items = list(counts.items()) items.sort(key=lambda x:x[1], reverse=True) for i in range(5):word, count = items[i]print ("{0:<10}{1:>5}".format(word, count))(2)結果顯示如下:
二、利用好看的詞元
1.Python的詞元圖的生成
(1)安裝庫
pip install jieba wordcloud matplotlib(2)準備
- txt文本
- 字體(simhei.ttf)
- 詞云背景圖片
由上面的三國演義的文本txt可以生成如下圖形:
Python jieba和詞元的利用到此結束....................
轉載于:https://www.cnblogs.com/psl1234/p/10652467.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的jieba库的使用和好看的词元的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Algorithms学习笔记-Chapt
- 下一篇: 大一对软件工程