Update train_parser example

This commit is contained in:
Matthew Honnibal 2016-10-16 21:41:14 +02:00
parent f787cd29fe
commit 3fba897e0f

View File

@ -18,7 +18,7 @@ def train_parser(nlp, train_data, left_labels, right_labels):
random.shuffle(train_data) random.shuffle(train_data)
loss = 0 loss = 0
for words, heads, deps in train_data: for words, heads, deps in train_data:
doc = nlp.make_doc(words) doc = Doc(nlp.vocab, words=words)
gold = GoldParse(doc, heads=heads, deps=deps) gold = GoldParse(doc, heads=heads, deps=deps)
loss += parser.update(doc, gold) loss += parser.update(doc, gold)
parser.model.end_training() parser.model.end_training()
@ -33,7 +33,6 @@ def main(model_dir=None):
assert model_dir.isdir() assert model_dir.isdir()
nlp = spacy.load('en', tagger=False, parser=False, entity=False, vectors=False) nlp = spacy.load('en', tagger=False, parser=False, entity=False, vectors=False)
nlp.make_doc = lambda words: Doc(nlp.vocab, zip(words, [True]*len(words)))
train_data = [ train_data = [
( (
@ -57,7 +56,7 @@ def main(model_dir=None):
right_labels.add(dep) right_labels.add(dep)
parser = train_parser(nlp, train_data, sorted(left_labels), sorted(right_labels)) parser = train_parser(nlp, train_data, sorted(left_labels), sorted(right_labels))
doc = nlp.make_doc(['I', 'like', 'securities', '.']) doc = Doc(nlp.vocab, words=['I', 'like', 'securities', '.'])
parser(doc) parser(doc)
for word in doc: for word in doc:
print(word.text, word.dep_, word.head.text) print(word.text, word.dep_, word.head.text)