hanlp进行命名实体识别
需要安装jpype先,这个是python调用java库的桥梁。
# -*- 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) #地名识别,标注为ns def Place_Recognize(sentence_str): HanLP = jpype.JClass('com.hankcs.hanlp.HanLP') segment = HanLP.newSegment().enablePlaceRecognize(True) return HanLP.segment(sentence_str) #人名识别,标注为nr def PersonName_Recognize(sentence_str): HanLP = jpype.JClass('com.hankcs.hanlp.HanLP') segment = HanLP.newSegment().enableNameRecognize(True) return HanLP.segment(sentence_str) #机构名识别,标注为nt def 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'], 'person': [' 蓝翔/nr', ' 阳县红/nr', ' 宗成庆/nr'], 'organization': [' 中国科学院/nt'], 'time': ['2018年/t', ' 6月份/t']}
分类:
自然语言处理
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了