pythonnlpspacynamed-entity-extraction

how to predict from manually trained spacy model


I have the following code to create and train a new spacy model. i don't know how to predict entities from the new text?

can anybody help?

TRAIN_DATA = [
    ("Uber blew through $1 million a week", [(0, 4, 'ORG')]),
    ("Android Pay expands to Canada", [(0, 11, 'PRODUCT'), (23, 30, 'GPE')]),
    ("Spotify steps up Asia expansion", [(0, 8, "ORG"), (17, 21, "LOC")]),
    ("Google Maps launches location sharing", [(0, 11, "PRODUCT")]),
    ("Google rebrands its business apps", [(0, 6, "ORG")]),
    ("look what i found on google!", [(21, 27, "PRODUCT")])]

nlp = spacy.blank("en")
optimizer = nlp.begin_training()
from spacy.gold import GoldParse  #<--- add this

for i in range(20):
    random.shuffle(TRAIN_DATA)
    for text, annotations in TRAIN_DATA:
        text = nlp.make_doc(text)
        gold = GoldParse(text, entities=annotations)  #<--- add this
        nlp.update([text], [gold], sgd=optimizer)

Solution

  • You need to save the trained model to disk. Then, instead of spacy.blank("en"), you would use spacy.load("path/to/model/"). Once you do that, you will be able to use the model you saved.