天天看點

Python:使用pyhanlp 進行分詞

pyhanlp調用java實作的HanLP接口進行分詞

安裝

pip install pyhanlp      

pyhanlp:

https://github.com/hankcs/pyhanlp

HanLP:

https://github.com/hankcs/HanLP

代碼示例

# -*- coding: utf-8 -*-

from pyhanlp import HanLP

document = "你好,歡迎在Python中調用HanLP的API"

# 分詞
print(HanLP.segment(document))
"""
[你好/vl, ,/w, 歡迎/v, 在/p, Python/nx, 中/f, 調用/v, HanLP/nx, 的/ude1, API/nx]
"""

# 關鍵詞提取
print(HanLP.extractKeyword(document, 2))
# [中, 調用]

# 自動摘要
print(HanLP.extractSummary(document, 3))
# [歡迎在Python中調用HanLP的API]