How to use Python to extract a person's name from a statement
This article mainly introduces how to use Python to extract the name of the person in the sentence, the article is very detailed, has a certain reference value, interested friends must read it!
Environmental dependence
Need to install two libraries, in fact, one is fine, but I have prepared two libraries to make a comparison.
The installation commands are as follows:
Pip install LAC-I https://pypi.douban.com/simplepip install ltp-I https://pypi.douban.com/simple code
No nonsense, go to the code.
#! / user/bin/env python# coding=utf-8 "" @ project: csdn@author: swordsman A Liang _ ALiang@file: extract_sentence_name_tool.py@ide: PyCharm@time: 2022-01-25 11:11:43 "" from LAC import LACfrom ltp import LTPimport time lac = LAC (mode= "lac") ltp = LTP () # sentence extraction name def extract_name (sentence: str Type='lac'): user_name_lis = [] if type= = 'lac': _ result = lac.run (sentence) for _ index, _ label in enumerate (_ result [1]): if _ label = = "PER": user_name_lis.append (_ result [0] [_ index]) elif type= =' ltp': _ seg _ hidden = ltp.seg ([sentence]) _ pos_hidden = ltp.pos (_ hidden) for _ seg_i, _ seg_v in enumerate (_ seg): _ hidden_v = _ pos_ hidden[ _ seg_i] for _ hudi _ seg_ v in enumerate (_ hidden_v): if _ hudv = = "nh": user_name_lis.append (_ seg_ v [_ hudi]) else: raise Exception ('type not suppose') return user_name_lis if _ _ name__ = =' _ _ main__': _ start_lac = time.time () lis1 = extract_name ("just because I watched the trump card show of Shen Teng and Jia Ling. So Yang Di must have stolen the topic. " , 'lac') _ end_lac = time.time () print ("LAC: {}