jieba java_【NLP】【一】中文分词之jieba
聲明:本文參考jieba官方文檔而成,官方鏈接:https://github.com/fxsjy/jieba
【一】jieba安裝
pip install jieba
【二】jieba簡(jiǎn)介
簡(jiǎn)介可見(jiàn)jieba官方說(shuō)明:https://pypi.org/project/jieba/
總而言之,jieba用于中文分詞,支持的文本編碼格式為utf-8,支持的功能包括:中文分詞、關(guān)鍵字提取、詞性標(biāo)注
整體功能如下圖:
【三】結(jié)巴使用之分詞
1. 使用精確模式
# -*- coding:utf-8 -*-
import jieba
sentence = "我愛(ài)北京天安門"
seg = jieba.cut(sentence=sentence)
print("/".join(seg))
結(jié)果如下:
我/愛(ài)/北京/天安門
2. 使用全模式
import jieba
sentence = "我愛(ài)北京天安門"
seg = jieba.cut(sentence=sentence,cut_all=True)
print("/".join(seg))
結(jié)果如下:
我/愛(ài)/北京/天安/天安門
3.使用搜索模式
# -*- coding:utf-8 -*-
import jieba
sentence = "我愛(ài)北京天安門"
seg = jieba.cut_for_search(sentence=sentence)
print("/".join(seg))
結(jié)果如下:
我/愛(ài)/北京/天安/天安門
關(guān)于三種模式的區(qū)別,可見(jiàn)官方描述:
4. 分詞接口詳解
4.1 cut接口,該接口接受三個(gè)參數(shù),重點(diǎn)關(guān)注一下第一個(gè)參數(shù):要求句子編譯格式為unicode編碼。所以,如果是GBK編碼,需要先轉(zhuǎn)換為utf-8接口的編碼格式。
cut(self, sentence, cut_all=False, HMM=True)
- sentence: The str(unicode) to be segmented.
- cut_all: Model type. True for full pattern, False for accurate pattern.
- HMM: Whether to use the Hidden Markov Model.
當(dāng)我們不知道文檔的編碼格式時(shí),可以采用如下代碼:
import chardet
with open("xxx.txt",'rb') as f:
data = f.read()
print(chardet.detect(data))
輸出結(jié)果為:
{'confidence': 0.99, 'language': 'Chinese', 'encoding': 'GB2312'}
4.2?cut_for_search接口
cut_for_search(self, sentence, HMM=True)
4.3?jieba.lcut 以及 jieba.lcut_for_search 直接返回 list
print(jieba.lcut(sentence))
print(jieba.lcut_for_search(sentence))
結(jié)果如下:
['我', '愛(ài)', '北京', '天安門']
['我', '愛(ài)', '北京', '天安', '天安門']
【四】自定義詞典
1. 先看看jieba自帶的詞典長(zhǎng)啥樣
jieba/dict.txt
T恤 4 n
A座 3 n
A股 3 n
A型 3 n
A輪 3 n
可以看出,jieba的詞典組成格式為:一行一個(gè)詞語(yǔ),詞語(yǔ) 詞頻 詞性
據(jù)jieba官方介紹:
詞語(yǔ)、詞頻(可省略)、詞性(可省略),用空格隔開(kāi),順序不可顛倒
2. 自定一一個(gè)字典
我 4 n
北京 3 n
天安門
3. 使用自定義詞典
jieba.load_userdict(r"D:\jieba-0.39\my_dict.txt")
print(jieba.lcut(sentence))
print(jieba.lcut_for_search(sentence))
結(jié)果如下:
['我', '愛(ài)', '北京', '天安門']
['我', '愛(ài)', '北京', '天安', '天安門']
【五】調(diào)整詞典
jieba支持動(dòng)態(tài)調(diào)整已經(jīng)加載的詞典
有兩種方法
1. 將新詞加入詞典
2. 調(diào)整詞典中的某個(gè)詞的詞頻
使用 add_word(word, freq=None, tag=None) 和 del_word(word) 可在程序中動(dòng)態(tài)修改詞典。
使用 suggest_freq(segment, tune=True) 可調(diào)節(jié)單個(gè)詞語(yǔ)的詞頻,使其能(或不能)被分出來(lái)。
【六】關(guān)鍵詞提取
jieba分詞支持兩種關(guān)鍵詞提取算法:TF-IDF、TextRank。這兩種算法會(huì)在后面的文章結(jié)合jieba源碼進(jìn)行分析。這里先看看如何使用。
1. 基于TF-IDF進(jìn)行關(guān)鍵詞提取
print(','.join(jieba.analyse.extract_tags(sentence,topK=2)))
結(jié)果如下:
天安門,北京
1.1 接口詳解?extract_tags
extract_tags(self, sentence, topK=20, withWeight=False, allowPOS=(), withFlag=False)
sentence 為待提取的文本
topK 為返回幾個(gè) TF/IDF 權(quán)重最大的關(guān)鍵詞,默認(rèn)值為 20
withWeight 為是否一并返回關(guān)鍵詞權(quán)重值,默認(rèn)值為 False
allowPOS 僅包括指定詞性的詞,默認(rèn)值為空,即不篩選
該接口用于基于TF-IDF提取關(guān)鍵詞,可用于篩選指定詞性的關(guān)鍵詞,返回值可以帶關(guān)鍵詞的權(quán)重,也可以不帶。
2. 依據(jù)TextRank算法進(jìn)行關(guān)鍵詞提取
print(','.join(jieba.analyse.textrank(sentence,topK=2)))
結(jié)果如下:
天安門,北京
2.1 接口詳解?textrank
textrank(self, sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'), withFlag=False):
- topK: return how many top keywords. `None` for all possible words.
- withWeight: if True, return a list of (word, weight);
if False, return a list of words.
- allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v'].
if the POS of w is not in this list, it will be filtered.
- withFlag: if True, return a list of pair(word, weight) like posseg.cut
if False, return a list of words
【七】詞性標(biāo)注
words =jieba.posseg.cut(sentence)
for word, flag in words:
print('%s %s' % (word, flag))
結(jié)果如下:
我 r
愛(ài) v
北京 n
天安門 ns
總結(jié):jieba提供的中文分詞、詞性標(biāo)注、關(guān)鍵字提取等功能,使用簡(jiǎn)單,安裝方便。其實(shí)現(xiàn)不僅有python版本,還有c++ java等版本,詳情可以jieba官方鏈接:https://github.com/fxsjy/jieba
總結(jié)
以上是生活随笔為你收集整理的jieba java_【NLP】【一】中文分词之jieba的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jQuery css详解
- 下一篇: 使用input type=file 上传