nlp之HanLP

HanLP是一系列模型与算法组成的NLP工具包,目标是普及自然语言处理在生产环境中的应用。HanLP具备功能完善、性能高效、架构清晰、语料时新、可自定义的特点。

安装hanlp
step1:下载jpype1.whl,
https://www.lfd.uci.edu/~gohlke/pythonlibs/#jpype
然后Terminal或者cmd里面pip install 路径/JPype1-0.7.0-cp37-cp37m-win_amd64.whl
step2:pip install pyhanlp
step3:在python console里面 ,第一次调用:
import pyhanlp,自动下载jar包,data文件(1个G)等

HanLP提供下列功能:
中文分词

import jpype

jvm_path=jpype.getDefaultJVMPath()
hanlp_classpath="I:\soft\Anaconda3\Lib\site-packages\pyhanlp\static\hanlp-1.7.4.jar;I:\soft\Anaconda3\Lib\site-packages\pyhanlp"
jvm_arg='-Djava.class.path='+hanlp_classpath

#判断是否jvm进程开启
if not jpype.isJVMStarted():#如果没有开启进程,则需要进行开启
    jpype.startJVM(jvm_path,jvm_arg)    #开启jvm,调用java_class

HanLP=jpype.JClass('com.hankcs.hanlp.HanLP')

text = "大家好,这是第一个关于HanLP测试! "
print("模式1:标准分词",HanLP.segment(text))

HanLPTokenizer=jpype.JClass('com.hankcs.hanlp.tokenizer.NLPTokenizer')
print("模式2:NLP分词",HanLPTokenizer.segment(text))

# 2 可以支持自定义词典
text = "攻城狮逆袭单身狗,迎娶白富美,走向人生巅峰"
print("模式2:NLP分词(未定义新词)",HanLPTokenizer.segment(text))
CustomDictionary=jpype.JClass('com.hankcs.hanlp.dictionary.CustomDictionary')
CustomDictionary.add("攻城狮")
CustomDictionary.add("单身狗")
print("模式2:NLP分词(定义新词)",HanLPTokenizer.segment(text))

#3 特征词提取
paragraphs="过去几十年里,国内外的互联网巨头基本都诞生于搜索、社交、电商、衣食住行等生活和消费领域,那时的互联网也被称作消费级互联网. "

HanLP = jpype.JClass("com.hankcs.hanlp.HanLP")
print("#3 特征词提取",HanLP.extractKeyword(paragraphs,5))
print("#4 摘要提取",HanLP.extractSummary(paragraphs,4))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章