首先需要安装依赖包:pip install pyhanlp
识别人名的代码如下:
from pyhanlp import HanLP
def extract_chinese_name(string: str) -> list:
"""使用HanLP人名识别"""
if (string is None) or (string == ""):
return []
segment = HanLP.newSegment().enableNameRecognize(True)
user_list = []
for i in segment.seg(string):
split_words = str(i).split('/') # check //m
word, tag = split_words[0], split_words[-1]
if tag == 'nr':
user_list.append(word)
return user_list
if __name__ == '__main__':
user_list = extract_chinese_name("《八佰》(英語:The Eight Hundred)是一部于2020年上映的以中国历史上的战争为题材的电影,由管虎执导,黄志忠、张俊一.....")
print(user_list) # ['管虎', '黄志忠', '张俊']
这里的nr
就表示人名的含义,Hanlp使用隐马模型进行分词,词性标注表可以参考:https://www.hankcs.com/nlp/part-of-speech-tagging.html