* Make TransitionSystem.add_action idempotent, i.e. ignore duplicate added actions.

This commit is contained in:
Matthew Honnibal 2016-01-19 20:10:04 +01:00
parent c4a89d56bd
commit 04d0686b26
1 changed files with 8 additions and 3 deletions

View File

@ -82,12 +82,17 @@ cdef class TransitionSystem:
costs[i] = 9000 costs[i] = 9000
def add_action(self, int action, label): 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: if self.n_moves >= self._size:
self._size *= 2 self._size *= 2
self.c = <Transition*>self.mem.realloc(self.c, self._size * sizeof(self.c[0])) self.c = <Transition*>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.c[self.n_moves] = self.init_transition(self.n_moves, action, label)
self.n_moves += 1 self.n_moves += 1
return 1