Advanced NLP with spaCy
Ines Montani
spaCy core developer

TRAINING_DATA = [
("How to preorder the iPhone X", {'entities': [(20, 28, 'GADGET')]})
# And many more examples...
]
# Loop for 10 iterations for i in range(10):# Shuffle the training data random.shuffle(TRAINING_DATA)# Create batches and iterate over them for batch in spacy.util.minibatch(TRAINING_DATA):# Split the batch in texts and annotations texts = [text for text, annotation in batch] annotations = [annotation for text, annotation in batch]# Update the model nlp.update(texts, annotations)# Save the model nlp.to_disk(path_to_model)
PERSON# Start with blank English model nlp = spacy.blank('en')# Create blank entity recognizer and add it to the pipeline ner = nlp.create_pipe('ner') nlp.add_pipe(ner)# Add a new label ner.add_label('GADGET')# Start the training nlp.begin_training()# Train for 10 iterations for itn in range(10): random.shuffle(examples)# Divide examples into batches for batch in spacy.util.minibatch(examples, size=2): texts = [text for text, annotation in batch] annotations = [annotation for text, annotation in batch]# Update the model nlp.update(texts, annotations)
Advanced NLP with spaCy