pyhanlp 停用詞與使用者自定義詞典
阿新 • • 發佈:2019-02-18
hanlp的詞典模式
之前我們看了hanlp的詞性標註,現在我們就要使用自定義詞典與停用詞功能了,首先關於HanLP的詞性標註方式具體請看HanLP詞性標註集。
其核心詞典形式如下:
自定義詞典
自定義詞典有多種新增模式,首先是展示的一個小例子,展示了詞彙的動態增加與強行插入,刪除等。更復雜的內容請參考後邊的第二段程式碼。
簡單的例子
from pyhanlp import *
text = "攻城獅逆襲單身狗,迎娶白富美,走上人生巔峰" # 怎麼可能噗哈哈!
print(HanLP.segment(text))
CustomDictionary = JClass("com.hankcs.hanlp.dictionary.CustomDictionary")
CustomDictionary.add("攻城獅") # 動態增加
CustomDictionary.insert("白富美", "nz 1024") # 強行插入
CustomDictionary.remove("攻城獅"); # 刪除詞語(註釋掉試試)
CustomDictionary.add("單身狗", "nz 1024 n 1")
展示該單詞詞典中的詞頻統計 展示分詞
print(CustomDictionary.get("單身狗"))
print(HanLP.segment(text))
增加使用者詞典,對其他分詞器同樣有效
注意此處,CRF分詞器將單身狗分為了n 即使單身狗:"nz 1024 n 1"
CRFnewSegment = HanLP.newSegment("crf")
print(CRFnewSegment.seg(text))