2017-10-26 13:15:37 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding: utf8
|
2017-10-31 23:43:22 +00:00
|
|
|
"""Example of training spaCy dependency parser, starting off with an existing
|
|
|
|
model or a blank model. For more details, see the documentation:
|
2017-10-26 13:15:37 +00:00
|
|
|
* Training: https://alpha.spacy.io/usage/training
|
|
|
|
* Dependency Parse: https://alpha.spacy.io/usage/linguistic-features#dependency-parse
|
|
|
|
|
2017-11-07 00:22:30 +00:00
|
|
|
Compatible with: spaCy v2.0.0+
|
2017-10-26 13:15:37 +00:00
|
|
|
"""
|
2016-10-16 15:05:55 +00:00
|
|
|
from __future__ import unicode_literals, print_function
|
2017-10-26 13:15:37 +00:00
|
|
|
|
2017-10-26 14:11:20 +00:00
|
|
|
import plac
|
2016-10-16 15:05:55 +00:00
|
|
|
import random
|
2017-10-26 13:15:37 +00:00
|
|
|
from pathlib import Path
|
2016-10-16 15:05:55 +00:00
|
|
|
import spacy
|
|
|
|
|
|
|
|
|
2017-10-26 13:15:37 +00:00
|
|
|
# training data
|
|
|
|
TRAIN_DATA = [
|
2017-11-06 22:14:04 +00:00
|
|
|
("They trade mortgage-backed securities.", {
|
|
|
|
'heads': [1, 1, 4, 4, 5, 1, 1],
|
|
|
|
'deps': ['nsubj', 'ROOT', 'compound', 'punct', 'nmod', 'dobj', 'punct']
|
|
|
|
}),
|
|
|
|
("I like London and Berlin", {
|
|
|
|
'heads': [1, 1, 1, 2, 2, 1],
|
|
|
|
'deps': ['nsubj', 'ROOT', 'dobj', 'cc', 'conj', 'punct']
|
|
|
|
})
|
2017-10-26 13:15:37 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-10-26 14:11:20 +00:00
|
|
|
@plac.annotations(
|
|
|
|
model=("Model name. Defaults to blank 'en' model.", "option", "m", str),
|
|
|
|
output_dir=("Optional output directory", "option", "o", Path),
|
|
|
|
n_iter=("Number of training iterations", "option", "n", int))
|
2017-11-06 22:14:04 +00:00
|
|
|
def main(model=None, output_dir=None, n_iter=10):
|
2017-10-26 14:11:20 +00:00
|
|
|
"""Load the model, set up the pipeline and train the parser."""
|
2017-10-26 13:15:37 +00:00
|
|
|
if model is not None:
|
|
|
|
nlp = spacy.load(model) # load existing spaCy model
|
|
|
|
print("Loaded model '%s'" % model)
|
|
|
|
else:
|
|
|
|
nlp = spacy.blank('en') # create blank Language class
|
|
|
|
print("Created blank 'en' model")
|
|
|
|
|
|
|
|
# add the parser to the pipeline if it doesn't exist
|
|
|
|
# nlp.create_pipe works for built-ins that are registered with spaCy
|
|
|
|
if 'parser' not in nlp.pipe_names:
|
|
|
|
parser = nlp.create_pipe('parser')
|
|
|
|
nlp.add_pipe(parser, first=True)
|
|
|
|
# otherwise, get it, so we can add labels to it
|
|
|
|
else:
|
|
|
|
parser = nlp.get_pipe('parser')
|
|
|
|
|
|
|
|
# add labels to the parser
|
2017-11-06 22:14:04 +00:00
|
|
|
for _, annotations in TRAIN_DATA:
|
|
|
|
for dep in annotations.get('deps', []):
|
2017-10-26 13:15:37 +00:00
|
|
|
parser.add_label(dep)
|
|
|
|
|
|
|
|
# get names of other pipes to disable them during training
|
|
|
|
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'parser']
|
2017-10-26 22:31:30 +00:00
|
|
|
with nlp.disable_pipes(*other_pipes): # only train parser
|
2017-11-01 12:14:31 +00:00
|
|
|
optimizer = nlp.begin_training()
|
2017-10-26 13:15:37 +00:00
|
|
|
for itn in range(n_iter):
|
|
|
|
random.shuffle(TRAIN_DATA)
|
|
|
|
losses = {}
|
2017-11-06 22:14:04 +00:00
|
|
|
for text, annotations in TRAIN_DATA:
|
|
|
|
nlp.update([text], [annotations], sgd=optimizer, losses=losses)
|
2017-10-26 13:15:37 +00:00
|
|
|
print(losses)
|
|
|
|
|
|
|
|
# test the trained model
|
|
|
|
test_text = "I like securities."
|
|
|
|
doc = nlp(test_text)
|
|
|
|
print('Dependencies', [(t.text, t.dep_, t.head.text) for t in doc])
|
|
|
|
|
|
|
|
# save model to output directory
|
|
|
|
if output_dir is not None:
|
|
|
|
output_dir = Path(output_dir)
|
|
|
|
if not output_dir.exists():
|
|
|
|
output_dir.mkdir()
|
|
|
|
nlp.to_disk(output_dir)
|
|
|
|
print("Saved model to", output_dir)
|
|
|
|
|
2017-10-27 01:55:04 +00:00
|
|
|
# test the saved model
|
2017-10-26 13:15:37 +00:00
|
|
|
print("Loading from", output_dir)
|
|
|
|
nlp2 = spacy.load(output_dir)
|
|
|
|
doc = nlp2(test_text)
|
|
|
|
print('Dependencies', [(t.text, t.dep_, t.head.text) for t in doc])
|
2016-10-16 15:05:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-10-26 13:15:37 +00:00
|
|
|
plac.call(main)
|
|
|
|
|
|
|
|
# expected result:
|
|
|
|
# [
|
|
|
|
# ('I', 'nsubj', 'like'),
|
|
|
|
# ('like', 'ROOT', 'like'),
|
|
|
|
# ('securities', 'dobj', 'like'),
|
|
|
|
# ('.', 'punct', 'like')
|
|
|
|
# ]
|