无法从受过训练的spacy获得实体

时间:2017-10-06 17:13:29

标签: spacy

我使用此官方tutorial对我的数据进行了spacy培训。 但在此之后,我不知道如何使用这种受过训练的spacy。

for ent in doc.ents:

返回空列表。我认为这是因为这部分代码,当我更新spacy时:

nlp = spacy.load('en', entity=False, parser=False)

有部分" entity = False"我打开这个模块后就不能了。 这是我的更新代码:

nlp = spacy.load('en', entity=False, parser=False)
ner = EntityRecognizer(nlp.vocab, entity_types=['ORGANIZATION', 'PERSON', 'MONEY'])

for itn in range(5):
    print("round")
    random.shuffle(train_data)
    for raw_text, entity_offsets in train_data:
        doc = nlp.make_doc(raw_text)
        gold = GoldParse(doc, entities=entity_offsets)

        nlp.tagger(doc)
        ner.update(doc, gold)
ner.model.end_training()

我可以帮助一下,如何在更新后从nlp获取实体?使用如下:

doc = nlp(String)

for ent in doc.ents:
    ...

1 个答案:

答案 0 :(得分:0)

doc = nlp(String)

用于文档中的ent:

print ("{} is of entity type {}".format(ent.label_, ent.text) )

这是我的代码示例:

import spacy 
nlp= spacy.load('en')
include_entities = ['DATE', 'ORG', 'PERSON']

# Define extract_entities()
def extract_entities(message):
# Create a dict to hold the entities
ents = dict.fromkeys(include_entities)
# Create a spacy document
doc = nlp(message)
for ent in doc.ents:
    if ent.label_ in include_entities :
        # Save interesting entities
        ents[ent.label_] = ent.text

return ents

消息可以是任何字符串。