2015-02-21 15:38:18 +00:00
|
|
|
from cymem.cymem cimport Pool
|
|
|
|
from ._state cimport State
|
|
|
|
from ..structs cimport TokenC
|
|
|
|
from thinc.typedefs cimport weight_t
|
|
|
|
|
|
|
|
|
|
|
|
cdef weight_t MIN_SCORE = -90000
|
|
|
|
|
|
|
|
|
2015-02-22 05:32:07 +00:00
|
|
|
class OracleError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-02-21 15:38:18 +00:00
|
|
|
cdef class TransitionSystem:
|
|
|
|
def __init__(self, dict labels_by_action):
|
|
|
|
self.mem = Pool()
|
2015-02-23 19:04:53 +00:00
|
|
|
self.n_moves = sum(len(labels) for labels in labels_by_action.values())
|
2015-02-21 15:38:18 +00:00
|
|
|
moves = <Transition*>self.mem.alloc(self.n_moves, sizeof(Transition))
|
|
|
|
cdef int i = 0
|
2015-02-23 19:04:53 +00:00
|
|
|
cdef int label_id
|
|
|
|
self.label_ids = {'ROOT': 0, 'MISSING': -1}
|
2015-02-21 15:38:18 +00:00
|
|
|
for action, label_strs in sorted(labels_by_action.items()):
|
2015-02-23 19:04:53 +00:00
|
|
|
for label_str in sorted(label_strs):
|
|
|
|
label_str = unicode(label_str)
|
|
|
|
label_id = self.label_ids.setdefault(label_str, len(self.label_ids))
|
|
|
|
moves[i] = self.init_transition(i, int(action), label_id)
|
|
|
|
i += 1
|
2015-02-21 15:38:18 +00:00
|
|
|
self.c = moves
|
|
|
|
|
|
|
|
cdef Transition init_transition(self, int clas, int move, int label) except *:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
cdef Transition best_valid(self, const weight_t* scores, const State* s) except *:
|
2015-02-21 16:06:37 +00:00
|
|
|
raise NotImplementedError
|
2015-02-21 15:38:18 +00:00
|
|
|
|
|
|
|
cdef Transition best_gold(self, const weight_t* scores, const State* s,
|
2015-02-22 05:32:07 +00:00
|
|
|
GoldParse gold) except *:
|
2015-02-21 15:38:18 +00:00
|
|
|
cdef Transition best
|
|
|
|
cdef weight_t score = MIN_SCORE
|
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
if scores[i] > score and self.c[i].get_cost(&self.c[i], s, gold) == 0:
|
|
|
|
best = self.c[i]
|
|
|
|
score = scores[i]
|
|
|
|
return best
|