使用Spacy从文本文件中提取名称

时间:2018-07-24 04:46:08

标签: python-3.x nlp nltk spacy data-extraction

我有一个文本文件,其中包含如下所示的行:

Electronically signed : Wes Scott, M.D.; Jun 26 2010 11:10AM CST

The patient was referred by Dr. Jacob Austin.  

Electronically signed by Robert Clowson, M.D.; Janury 15 2015 11:13AM CST

Electronically signed by Dr. John Douglas, M.D.; Jun 16 2017 11:13AM CST

The patient was referred by
Dr. Jayden Green Olivia.  

我想使用Spacy提取所有名称。我正在使用Spacy的语音标记和实体识别部分,但无法获得成功。 请问该怎么做?任何帮助都是可以的

我以这种方式使用一些代码:

import spacy
nlp = spacy.load('en')
 document_string= " Electronically signed by stupid: Dr. John Douglas, M.D.; 
 Jun 13 2018 11:13AM CST"
doc = nlp(document_string)
 for sentence in doc.ents:
     print(sentence, sentence.label_) 

2 个答案:

答案 0 :(得分:1)

尝试一下:

import spacy
en = spacy.load('en')

sents = en(open('input.txt').read())
people = [ee for ee in sents.ents if ee.label_ == 'PERSON']

答案 1 :(得分:0)

模型准确性问题

所有模型的问题在于它们没有100%的准确性,即使使用更大的模型也无法识别日期。 Here是NER模型的准确度值(F分数,精确度,召回率),均在86%左右。

document_string = """ 
Electronically signed : Wes Scott, M.D.; Jun 26 2010 11:10AM CST 
 The patient was referred by Dr. Jacob Austin.   
Electronically signed by Robert Clowson, M.D.; Janury 15 2015 11:13AM CST 
Electronically signed by Dr. John Douglas, M.D.; Jun 16 2017 11:13AM CST 
The patient was referred by 
Dr. Jayden Green Olivia.   
"""  

在小型模型中,两个日期项标记为“ PERSON”:

import spacy                                                                                                                            

nlp = spacy.load('en')                                                                                                                  
sents = nlp(document_string) 
 [ee for ee in sents.ents if ee.label_ == 'PERSON']                                                                                      
# Out:
# [Wes Scott,
#  Jun 26,
#  Jacob Austin,
#  Robert Clowson,
#  John Douglas,
#  Jun 16 2017,
#  Jayden Green Olivia]

使用更大的模型en_core_web_md,由于存在三个错误分类的实体,结果的准确性甚至更差。

nlp = spacy.load('en_core_web_md')                                                                                                                  
sents = nlp(document_string) 
# Out:
#[Wes Scott,
# Jun 26,
# Jacob Austin,
# Robert Clowson,
# Janury,
# John Douglas,
# Jun 16 2017,
# Jayden Green Olivia]

我还尝试了其他模型(xx_ent_wiki_smen_core_web_md),它们也没有带来任何改进。

如何使用规则提高准确性?

在小示例中,不仅文档看起来结构清晰,而且错误分类的实体都是日期。那么,为什么不将初始模型与基于规则的组件结合起来呢?

好消息是在Spacy:

  

有可能将统计和基于规则的组件组合在一起   多种方式。基于规则的组件可用于改进   统计模型的准确性

(来自https://spacy.io/usage/rule-based-matching#models-rules

因此,通过遵循示例并使用dateparser库(用于人类可读日期的解析器),我整理了一个基于规则的组件,该组件在此示例中效果很好:

from spacy.tokens import Span
import dateparser

def expand_person_entities(doc):
    new_ents = []
    for ent in doc.ents:
        # Only check for title if it's a person and not the first token
        if ent.label_ == "PERSON":
            if ent.start != 0:
                # if person preceded by title, include title in entity
                prev_token = doc[ent.start - 1]
                if prev_token.text in ("Dr", "Dr.", "Mr", "Mr.", "Ms", "Ms."):
                    new_ent = Span(doc, ent.start - 1, ent.end, label=ent.label)
                    new_ents.append(new_ent)
                else:
                    # if entity can be parsed as a date, it's not a person
                    if dateparser.parse(ent.text) is None:
                        new_ents.append(ent) 
        else:
            new_ents.append(ent)
    doc.ents = new_ents
    return doc

# Add the component after the named entity recognizer
# nlp.remove_pipe('expand_person_entities')
nlp.add_pipe(expand_person_entities, after='ner')

doc = nlp(document_string)
[(ent.text, ent.label_) for ent in doc.ents if ent.label_=='PERSON']
# Out:
# [(‘Wes Scott', 'PERSON'),
#  ('Dr. Jacob Austin', 'PERSON'),
#  ('Robert Clowson', 'PERSON'),
#  ('Dr. John Douglas', 'PERSON'),
#  ('Dr. Jayden Green Olivia', 'PERSON')]