我使用这个官方教程训练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:
...
发布于 2018-08-02 15:41:28
doc = nlp(String)
对于doc.ents:
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
消息可以是任何字符串。
https://stackoverflow.com/questions/-100001879
复制相似问题