1.安装hanlp
pip install hanlp
2.使用
import hanlp
#识别实体
recognizer = hanlp.load(hanlp.pretrained.ner.MSRA_NER_BERT_BASE_ZH)
#解析人名
def extract_name(text:str):
if not text:
return ""
result = parse_name_hanlp(text)
if result:
return result
def parse_name_hanlp(text:str):
entityx = recognizer.predict(list(text))
for param in entityx:
if param[1] == "NR":
print("name=",param[0])
return param[0]
if __name__ == "__main__":
info_str = "梁传凯办公电话:3822268"
extract_name(info_str)
结果: