分詞,用jiaba
# 分詞
import jieba
doc = '我喜歡上海東方明珠'
# 全模式;精準模式;搜尋引擎模式
w1 = jieba.cut(doc,cut_all=False) # 參數1:資料 參數2:模式 有三種模式,這裡使用了精準模式
for item in w1:
print(item)
運作結果:
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\pc\AppData\Local\Temp\jieba.cache
我
喜歡
上海
東方明珠
Loading model cost 0.752 seconds.
Prefix dict has been built succesfully.
擷取詞語的詞性
import jieba.posseg
doc = '我喜歡上海東方明珠'
w2 = jieba.posseg.cut(doc)
# flag詞性
# word詞語
for item in w2:
print(item.flag)
運作結果:
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\pc\AppData\Local\Temp\jieba.cache
Loading model cost 0.745 seconds.
Prefix dict has been built succesfully.
r
v
ns
nr
a:形容詞
c:連詞
d:副詞
e:歎詞
f:方位詞
i:成語
m:數詞
n:名詞
nr:人名
ns:地名
nt:機構團體
nz:其他專有名詞
p:介詞
r:代詞
t:時間
u:助詞
v:動詞
vn:動名詞
w:标點符号
un:未知詞語
詞典的加載
jieba.load_userdict('檔案名')