深入理解Python的jieba模塊
一、前言
英語單詞之間是通過空格分隔的,但是中文卻不存在空格的概念,因此需要一個模塊來解決中文的分詞問題。jieba模塊是一個python第三方中文分詞模塊,可以用于將語句中的中文詞語分離出來。
此外,全國計算機等級考試二級python語言程序設(shè)計也涉及到該模塊的相關(guān)知識。因此大家可以好好了解下該模塊。
二、模塊的安裝
jieba模塊作為python的一個第三方模塊,是需要我們自行下載安裝后才能使用的,我們主要采用pip安裝工具進(jìn)行jieba的安裝,具體步驟如下:
在windows操作系統(tǒng)中,快捷鍵win+R,
然后輸入cmd,點擊確定,打開
輸入
pip install jieba
即可安裝成功。
三、jieba模塊具體講解
3.1分詞模式
jieba模塊支持三種分詞模式:全模式、精準(zhǔn)模式以及搜索引擎模式。
①全模式:全模式可以將句子中所有可能的詞語全部提取出來,該模式提取速度快,但可能會出現(xiàn)冗余詞匯。
如圖,第一行出現(xiàn)了冗余詞匯,其采用的就是全模式,而第二行采用精準(zhǔn)模式。
②精準(zhǔn)模式:精準(zhǔn)模式通過優(yōu)化的智能算法將語句精準(zhǔn)的分隔,適用于文本分析。
③搜索引擎模式:搜索引擎模式在精準(zhǔn)模式的基礎(chǔ)上對詞語進(jìn)行再次劃分,提高召回率,適用于搜索引擎分詞。
3.2cut()、lcut()
3.2.1cut(sentence, cut_all=False, HMM=True, use_paddle=False)
參數(shù)解析:
sentence:要分割的str(unicode)。
cut_all:模型類型。True 表示全模式,F(xiàn)alse 表示精準(zhǔn)模式。其默認(rèn)為精準(zhǔn)模式。
HMM:是否使用隱馬爾可夫模型。
函數(shù)功能:
The main function that segments an entire sentence that contains Chinese characters into separated words.
將包含漢字的整個句子分割成單獨的單詞的主要功能。
import jieba sentence = 'python是世界上最好的編程語言' ls = jieba.cut(sentence, cut_all=False) print(ls) # <generator object Tokenizer.cut at 0x000001966B14EA98>
print(type(ls)) # <class 'generator'>
如圖,其是迭代器類型,可以用以下三種方式顯示結(jié)果
①' '.join()
# ①''.join ls_1 = ' '.join(ls) print(ls_1) # python 是 世界 上 最好 的 編程 編程語言 語言
②for循環(huán)遍歷
# ②for循環(huán)遍歷 for i in ls: print(i) ''' python 是 世界 上 最好 的 編程語言 '''
③列表推導(dǎo)式
# ③列表推導(dǎo)式 ls_2 = [i for i in ls] print(ls_2) # ['python', '是', '世界', '上', '最好', '的', '編程語言']
3.2.2lcut(sentence,cut_all=False)
def lcut(self, *args, **kwargs): return list(self.cut(*args, **kwargs))
查看jieba模塊,其定義lcut()函數(shù)如上,可以發(fā)現(xiàn)lcut()函數(shù)最終返回的是list(cut())
import jieba sentence = 'python是世界上最好的編程語言' ls = jieba.cut(sentence, cut_all=False) print(ls) print(list(ls)) ls1 = jieba.lcut(sentence, cut_all=True) print(ls1) ls2 = jieba.lcut(sentence) print(ls2)
結(jié)果如下
注意cut_all=False是精準(zhǔn)模式,也是其默認(rèn)的類型。
3.3cut_for_search()、lcut_for_search()
cut_for_search(sentence, HMM=True)和lcut_for_search(sentence, HMM=True)和上面所講的類似。
其都是對搜索引擎進(jìn)行更精細(xì)的細(xì)分,即采用搜索引擎模式。
import jieba sentence = 'python是世界上最好的編程語言' ls3 = jieba.cut_for_search(sentence) print(ls3) # <generator object Tokenizer.cut_for_search at 0x00000199C7A3D9A8> print(list(ls3)) # ['python', '是', '世界', '上', '最好', '的', '編程', '語言', '編程語言'] ls4 = jieba.lcut_for_search(sentence) print(ls4) # ['python', '是', '世界', '上', '最好', '的', '編程', '語言', '編程語言']
3.4add_word(self, word, freq=None, tag=None)
Add a word to dictionary.
freq and tag can be omitted, freq defaults to be a calculated value that ensures the word can be cut out.
函數(shù)功能:在字典中添加一個單詞。
參數(shù)解析:freq 和 tag 可以省略,freq 默認(rèn)是一個計算值,保證單詞可以被切掉。
import jieba sentence = 'python是世界上最好的編程語言' ls2 = jieba.lcut(sentence) print(ls2) ls5 = jieba.add_word('最好的') ls6 = jieba.lcut(sentence) print(ls6)
結(jié)果如上,最終最好的就沒有被切掉。
3.5del_word(word)
函數(shù)功能:分詞詞典中刪除詞word
import jieba sentence = 'python是世界上最好的編程語言' ls2 = jieba.lcut(sentence) print(ls2) ls7 = jieba.del_word('世界') ls8 = jieba.lcut(sentence) print(ls8)
不過經(jīng)過筆者更改word,發(fā)現(xiàn)word是編程語言時,最后就分割成了編程和語言;當(dāng)word是編程時,結(jié)果沒變化;當(dāng)word是python時,結(jié)果也沒變化。因此有些需要筆者自己去嘗試。
3.6suggest_freq(segment, tune=False)
"""
Suggest word frequency to force the characters in a word to be
joined or splitted.
Parameter:
- segment : The segments that the word is expected to be cut into,
If the word should be treated as a whole, use a str.
- tune : If True, tune the word frequency.
Note that HMM may affect the final result. If the result doesn't change,
set HMM=False.
"""
函數(shù)功能:建議詞頻,強制將單詞中的字符合并或拆分。
參數(shù)解析:
- segment :該單詞預(yù)期被切割成的片段,如果該單詞應(yīng)該被視為一個整體,則使用str。
- tune : 如果為True,則調(diào)整詞頻。
注意HMM可能會影響最終結(jié)果。如果結(jié)果不變,設(shè)置HMM=False。
3.7tokenize(unicode_sentence, mode="default", HMM=True)
"""
Tokenize a sentence and yields tuples of (word, start, end)
Parameter:
- sentence: the str(unicode) to be segmented.
- mode: "default" or "search", "search" is for finer segmentation.
- HMM: whether to use the Hidden Markov Model.
"""
函數(shù)功能:標(biāo)記一個句子并產(chǎn)生 (word, start, end) 的元組
參數(shù)解析:
unicode_sentence:要分割的 str(unicode)。
模式:"default" or "search", "search" is for finer segmentation. “默認(rèn)”或“搜索”,“搜索”用于更精細(xì)的分割。
HMM: 是否使用隱馬爾可夫模型。
四、所需代碼展示
# -*- coding: utf-8-*- import jieba sentence = 'python是世界上最好的編程語言' ls = jieba.cut(sentence, cut_all=False) # print(ls) # print(list(ls)) # # <generator object Tokenizer.cut at 0x0000019F5E44DA98> # print(type(ls)) # # <class 'generator'> # # ①''.join # ls_1 = ' '.join(ls) # print(ls_1) # # python 是 世界 上 最好 的 編程語言 # ②for循環(huán)遍歷 # for i in ls: # print(i) # ''' # python # 是 # 世界 # 上 # 最好 # 的 # 編程語言 # ''' # # ③列表推導(dǎo)式 # ls_2 = [i for i in ls] # print(ls_2) # # ['python', '是', '世界', '上', '最好', '的', '編程語言'] # ls1 = jieba.lcut(sentence, cut_all=True) # print(ls1) ls2 = jieba.lcut(sentence) print(ls2) # ls3 = jieba.cut_for_search(sentence) # print(ls3) # # <generator object Tokenizer.cut_for_search at 0x00000199C7A3D9A8> # print(list(ls3)) # # ['python', '是', '世界', '上', '最好', '的', '編程', '語言', '編程語言'] # ls4 = jieba.lcut_for_search(sentence) # print(ls4) # ['python', '是', '世界', '上', '最好', '的', '編程', '語言', '編程語言'] # ls5 = jieba.load_userdict('文案.txt') # ls6 = jieba.lcut(sentence) # print(ls6) # ls5 = jieba.add_word('最好的') # ls6 = jieba.lcut(sentence) # print(ls6) ls7 = jieba.del_word('世界') ls8 = jieba.lcut(sentence) print(ls8)
需要的可以自行復(fù)制
五、總結(jié)
①全國計算機等級考試二級python語言程序設(shè)計中涉及到的內(nèi)容一般只是分詞模式、lcut()、lcut_for_search()和add_word()這幾方面知識;
②筆者所寫的不是特別詳細(xì),要是之后有好的案例或者其他方式,會進(jìn)行添加以及完善3.6,3.7的內(nèi)容;
③該模塊的理解與使用不是特別難,希望大家自己動手試試,找?guī)讉€案例,敲敲代碼?。?/p>
到此這篇關(guān)于深入理解Python的jieba模塊的文章就介紹到這了,更多相關(guān)Python的jieba模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch之torch.nn.Identity()的作用及解釋
這篇文章主要介紹了pytorch之torch.nn.Identity()的作用及解釋,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08最新tensorflow與pytorch環(huán)境搭建的實現(xiàn)步驟
深度學(xué)習(xí)相關(guān)的熱門框架主要為Tensorflow和Pytorch,本文主要介紹了搭建最新tensorflow與pytorch環(huán)境,具有一定的參考價值,感興趣的可以了解一下2024-04-04解決pycharm無法識別本地site-packages的問題
今天小編就為大家分享一篇解決pycharm無法識別本地site-packages的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10利用Python通過獲取剪切板數(shù)據(jù)實現(xiàn)百度劃詞搜索功能
大家是不是嫌棄每次打開百度太麻煩?今天教大家利用Python通過獲取剪切板數(shù)據(jù)實現(xiàn)百度劃詞搜索功能,用程序直接打開網(wǎng)頁,需要的朋友可以參考下2021-06-06django配置DJANGO_SETTINGS_MODULE的實現(xiàn)
本文主要介紹了django配置DJANGO_SETTINGS_MODULE,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03