From 9c090945e0586a56e8f7b448837051f8bf48097b Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sun, 9 Aug 2015 02:29:58 +0200 Subject: [PATCH] * Add Parser.predict method, and clean up Parser.get_state --- spacy/syntax/parser.pxd | 1 + spacy/syntax/parser.pyx | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/spacy/syntax/parser.pxd b/spacy/syntax/parser.pxd index 411172eda..4ee30341a 100644 --- a/spacy/syntax/parser.pxd +++ b/spacy/syntax/parser.pxd @@ -16,3 +16,4 @@ cdef class Parser: cdef readonly TransitionSystem moves cdef void parse(self, StateClass stcls, ExampleC eg) nogil + cdef void predict(self, StateClass stcls, ExampleC* eg) nogil diff --git a/spacy/syntax/parser.pyx b/spacy/syntax/parser.pyx index c215bd30b..bb7e2d96a 100644 --- a/spacy/syntax/parser.pyx +++ b/spacy/syntax/parser.pyx @@ -97,11 +97,7 @@ cdef class Parser: self.model.n_feats, self.model.n_feats) for action_name in initial_actions: if action_name == '_': - memset(eg.c.scores, 0, eg.c.nr_class * sizeof(weight_t)) - self.moves.set_valid(eg.c.is_valid, stcls) - fill_context(eg.c.atoms, stcls) - self.model.set_scores(eg.c.scores, eg.c.atoms) - eg.c.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.model.n_classes) + self.predict(stcls, &eg.c) action = self.moves.c[eg.c.guess] else: action = self.moves.lookup_transition(action_name) @@ -111,13 +107,16 @@ cdef class Parser: tokens.set_parse(stcls._sent) return stcls + cdef void predict(self, StateClass stcls, ExampleC* eg) nogil: + memset(eg.scores, 0, eg.nr_class * sizeof(weight_t)) + self.moves.set_valid(eg.is_valid, stcls) + fill_context(eg.atoms, stcls) + self.model.set_scores(eg.scores, eg.atoms) + eg.guess = arg_max_if_true(eg.scores, eg.is_valid, self.model.n_classes) + cdef void parse(self, StateClass stcls, ExampleC eg) nogil: while not stcls.is_final(): - memset(eg.scores, 0, eg.nr_class * sizeof(weight_t)) - self.moves.set_valid(eg.is_valid, stcls) - fill_context(eg.atoms, stcls) - self.model.set_scores(eg.scores, eg.atoms) - eg.guess = arg_max_if_true(eg.scores, eg.is_valid, self.model.n_classes) + self.predict(stcls, &eg) self.moves.c[eg.guess].do(stcls, self.moves.c[eg.guess].label) self.moves.finalize_state(stcls) @@ -132,13 +131,9 @@ cdef class Parser: cdef Transition G while not stcls.is_final(): memset(eg.c.scores, 0, eg.c.nr_class * sizeof(weight_t)) - self.moves.set_costs(eg.c.is_valid, eg.c.costs, stcls, gold) - fill_context(eg.c.atoms, stcls) - self.model.train(eg) - G = self.moves.c[eg.c.guess] self.moves.c[eg.c.guess].do(stcls, self.moves.c[eg.c.guess].label)