python调用Hanlp做命名实体识别以及词性标注

今日头条

1100人已加入

描述

之前需要做一个中文命名实体识别的api,看完了一些相关论文以后觉得短时间内自己实现不大现实,于是找了一些开源工具,其中哈工大的LTP效果是很好的,但是免费使用限流量,需要给钱才行; NLPIR的pynlpir似乎还不能支持命名实体识别等复杂工作,只能做一些分词之类;最后还剩下Hanlp,感谢Hanlp的作者hancks无私的将代码开源,还提供了那么详细的文档。

pyhanlp只有少数功能,其他复杂一点的功能需要使用python调用java代码来实现。
以下是api的模型部分,大多是照着文档写成的。
python调用java需要jpype库,具体安装请参考之前的博客:jpype安装的简便方法

# -*- coding: utf-8 -*-"""
Created on Thu May 10 09:19:55 2018

@author: wang小尧
"""import jpype#路径jvmPath = jpype.getDefaultJVMPath() # 获得系统的jvm路径ext_classpath = r"./ner/hanlp\hanlp-1.6.3.jar:./ner/hanlp"jvmArg = '-Djava.class.path=' + ext_classpath
jpype.startJVM(jvmPath, jvmArg, "-Xms1g", "-Xmx1g")#繁体转简体def TraditionalChinese2SimplifiedChinese(sentence_str):
    HanLP = jpype.JClass('com.hankcs.hanlp.HanLP')    return HanLP.convertToSimplifiedChinese(sentence_str)#切词&命名实体识别与词性标注(可以粗略识别)def NLP_tokenizer(sentence_str):
    NLPTokenizer = jpype.JClass('com.hankcs.hanlp.tokenizer.NLPTokenizer')    return NLPTokenizer.segment(sentence_str)#地名识别,标注为nsdef Place_Recognize(sentence_str):
    HanLP = jpype.JClass('com.hankcs.hanlp.HanLP')
    segment = HanLP.newSegment().enablePlaceRecognize(True)    return HanLP.segment(sentence_str)#人名识别,标注为nrdef PersonName_Recognize(sentence_str):
    HanLP = jpype.JClass('com.hankcs.hanlp.HanLP')
    segment = HanLP.newSegment().enableNameRecognize(True)    return HanLP.segment(sentence_str)#机构名识别,标注为ntdef Organization_Recognize(sentence_str):
    HanLP = jpype.JClass('com.hankcs.hanlp.HanLP')
    segment = HanLP.newSegment().enableOrganizationRecognize(True)    return HanLP.segment(sentence_str)#标注结果转化成列表def total_result(function_result_input):
    x = str(function_result_input)
    y = x[1:len(x)-1]
    y = y.split(',')    return y#时间实体def time_result(total_result):
    z = []    for i in range(len(total_result)):        if total_result[i][-2:] == '/t':
            z.append(total_result[i])    return z#Type_Recognition 可以选 ‘place’,‘person’,‘organization’三种实体,#返回单一实体类别的列表def single_result(Type_Recognition,total_result):
    if Type_Recognition == 'place':
        Type = '/ns'
    elif Type_Recognition == 'person':
        Type = '/nr'
    elif Type_Recognition == 'organization':
        Type = '/nt'
    else:        print ('请输入正确的参数:(place,person或organization)')
    z = []    for i in range(len(total_result)):        if total_result[i][-3:] == Type:
            z.append(total_result[i])    return z#把单一实体结果汇总成一个字典def dict_result(sentence_str):
    sentence = TraditionalChinese2SimplifiedChinese(sentence_str)
    total_dict = {}
    a = total_result(Place_Recognize(sentence))
    b = single_result('place',a)
    c = total_result(PersonName_Recognize(sentence))
    d = single_result('person',c)
    e = total_result(Organization_Recognize(sentence))
    f = single_result('organization',e)
    g = total_result(NLP_tokenizer(sentence))
    h = time_result(g)
    total_list = [i for i in [b,d,f,h]]
    total_dict.update(place = total_list[0],person = total_list[1],organization = total_list[2],time = total_list[3])
    jpype.shutdownJVM()#关闭JVM虚拟机
    return total_dict#测试test_sentence="2018年武胜县新学乡政府大楼门前锣鼓喧天,6月份蓝翔给宁夏固原市彭阳县红河镇捐赠了挖掘机,中国科学院计算技术研究所的宗成庆教授负责教授自然语言处理课程,而他的学生现在正在香港看***"print (dict_result(test_sentence))

识别结果:



{'place': [' 武胜县/ns', ' 宁夏/ns', ' 固原市/ns', ' 彭阳县/ns', ' 红河镇/ns', ' 香港/ns'], 'person': [' 宗成庆/nr'], 'organization': [' 蓝翔/nt', ' 中国科学院计算技术研究所/nt'], 'time': ['2018年/t', ' 6月份/t', ' 现在/t']}

遇到的问题:

在弄这个api时遇到了一些问题,就是当我打开java虚拟机JVM,功能正常使用完关闭了JVM,但是再打开的时候就会报错,所以得一直保持一个JVM一直打开的状态,或者重启kernel才行。网上找了找也没能找到靠谱的解决方案,这个问题只有以后慢慢解决了。如果有人知道如何处理,可以给我发私信。



文章来源于wong小尧的博客

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 相关推荐

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分