函數(shù) | 描述 |
---|---|
jieba.lcut(s) | 精確模式,返回一個(gè)列表類(lèi)型的分詞結(jié)果 |
jieba.lcut(s,cut_all=True) | 全模式,返回一個(gè)列表類(lèi)型的分詞結(jié)果,存在冗余 |
jieba.lcut_for_search(s) | 搜索引擎模式,返回一個(gè)列表類(lèi)型的分詞結(jié)果,存在冗余 |
jieba.lcut(s) | 精確模式,返回一個(gè)列表類(lèi)型的分詞結(jié)果 |
jieba.add_word(s) | 向分詞詞典增加新詞w |
例子:
>>> jieba.lcut("中國(guó)是一個(gè)偉大的國(guó)家") ['中國(guó)', '是', '一個(gè)', '偉大', '的', '國(guó)家'] >>> jieba.lcut("中國(guó)是一個(gè)偉大的國(guó)家", cut_all=True) ['中國(guó)', '國(guó)是', '一個(gè)', '偉大', '的', '國(guó)家'] >>> jieba.lcut_for_search("中華人民共和國(guó)是偉大的") ['中華', '華人', '人民', '共和', '共和國(guó)', '中華人民共和國(guó)', '是', '偉大', '的']
問(wèn)題分析
https://python123.io/resources/pye/hamlet.txt
https://python123.io/resources/pye/threekingdoms.txt
代碼如下:
def getText(): # 打開(kāi) hamlet.txt 這個(gè)文件 txt = open("hamlet.txt", "r").read() # 避免大小寫(xiě)對(duì)詞頻統(tǒng)計(jì)的干擾,將所有單詞轉(zhuǎn)換為小寫(xiě) txt = txt.lower() # 將文中出現(xiàn)的所有特殊字符替換為空格 for ch in '|"#$%^*()_+-=\\`~{}[];:>?/': txt = txt.replace(ch, " ") # 返回一個(gè)所以后單詞都是小寫(xiě)的,單詞間以空格間隔的文本 return txt hamletTxt = getText() # split() 默認(rèn)使用空格作為分隔符 words = hamletTxt.split() counts = {} for word in words: counts[word] = counts.get(word,0) + 1 items = list(counts.items()) items.sort(key=lambda x:x[1], reverse=True) for i in range(10): word, count = items[i] print("{0:10}{1:>5}".format(word,count))
上面代碼中的
items.sort(key=lambda x:x[1], reverse=True)
是根據(jù)單詞出現(xiàn)的次數(shù)進(jìn)行排序,其中使用了 lambda 函數(shù)。更多解釋請(qǐng)看:
https://www.runoob.com/python/att-list-sort.html
下面使用 jieba 庫(kù)來(lái)統(tǒng)計(jì)《三國(guó)演義》中任務(wù)出場(chǎng)的次數(shù):
import jieba txt = open("threekingdoms.txt","r",encoding="utf-8").read() words = jieba.lcut(txt) counts = {} for word in words: if len(word) == 1: continue else: counts[word] = counts.get(word, 0) + 1 items = list(counts.items()) items.sort(key=lambda x:x[1], reverse=True) for i in range(15): word, count = items[i] print("{0:10}{1:>5}".format(word,count))
運(yùn)行結(jié)果:
曹操 953 孔明 836 將軍 772 卻說(shuō) 656 玄德 585 關(guān)公 510 丞相 491 二人 469 不可 440 荊州 425 玄德曰 390 孔明曰 390 不能 384 如此 378 張飛 358
我們可以看到得出的結(jié)果與我們想象的有些差異,比如
所以我們需要對(duì)上面代碼進(jìn)行優(yōu)化,在詞頻統(tǒng)計(jì)的基礎(chǔ)上,面向問(wèn)題改造我們的程序。
下面是《三國(guó)演義》人物數(shù)量統(tǒng)計(jì)代碼的升級(jí)版,升級(jí)版中對(duì)于某些確定不是人名的詞,即使做了詞頻統(tǒng)計(jì),也要將它刪除掉。使用寄一個(gè)集合excludes來(lái)接收一些確定不是人名但是又排序比較靠前的單詞列進(jìn)去。
import jieba txt = open("threekingdoms.txt","r",encoding="utf-8").read() excludes = {"將軍","卻說(shuō)","荊州","二人","不可","不能","如此"} words = jieba.lcut(txt) counts = {} for word in words: if len(word) == 1: continue elif word == "諸葛亮" or word == "孔明曰": rword == "孔明" elif word == "關(guān)公" or word == "云長(zhǎng)": rword == "關(guān)羽" elif word == "玄德" or word == "玄德曰": rword == "劉備" elif word == "孟德" or word == "丞相": rword == "曹操" else: rword = word counts[rword] = counts.get(rword, 0) + 1 items = list(counts.items()) items.sort(key=lambda x:x[1], reverse=True) for i in range(15): word, count = items[i] print("{0:10}{1:>5}".format(word,count))
運(yùn)行結(jié)果:
曹操 963 孔明 847 張飛 366 商議 359 如何 352 主公 340 軍士 320 呂布 303 左右 298 軍馬 297 趙云 283 劉備 282 引兵 279 次日 278 大喜 274
可以看出還是有像“商議”、“如何”等不是人物的詞出現(xiàn)在統(tǒng)計(jì)結(jié)果,我們將這些詞加入到 excludes 中,多次運(yùn)行程序后最后得到《三國(guó)演義》任務(wù)出場(chǎng)順序前20:
應(yīng)用問(wèn)題擴(kuò)展
以上內(nèi)容資料均來(lái)源于中國(guó)大學(xué)MOOC網(wǎng)-北京理工大學(xué)Python語(yǔ)言程序設(shè)計(jì)課程
課程地址:https://www.icourse163.org/course/BIT-268001
以上就是python jieba庫(kù)的基本使用的詳細(xì)內(nèi)容,更多關(guān)于python jieba庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:錫林郭勒盟 梅州 文山 石家莊 西寧 懷化 浙江 昆明
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python jieba庫(kù)的基本使用》,本文關(guān)鍵詞 python,jieba,庫(kù),的,基本,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。