From 04d0686b26883a97fbc8e664695c63c85592f500 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 19 Jan 2016 20:10:04 +0100 Subject: [PATCH] * Make TransitionSystem.add_action idempotent, i.e. ignore duplicate added actions. --- spacy/syntax/transition_system.pyx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/spacy/syntax/transition_system.pyx b/spacy/syntax/transition_system.pyx index ef12e0074..34ad26697 100644 --- a/spacy/syntax/transition_system.pyx +++ b/spacy/syntax/transition_system.pyx @@ -82,12 +82,17 @@ cdef class TransitionSystem: costs[i] = 9000 def add_action(self, int action, label): + if not isinstance(label, int): + label = self.strings[label] + # Check we're not creating a move we already have, so that this is + # idempotent + for trans in self.c[:self.n_moves]: + if trans.move == action and trans.label == label: + return 0 if self.n_moves >= self._size: self._size *= 2 self.c = self.mem.realloc(self.c, self._size * sizeof(self.c[0])) - - if not isinstance(label, int): - label = self.strings[label] self.c[self.n_moves] = self.init_transition(self.n_moves, action, label) self.n_moves += 1 + return 1