2015-02-21 15:38:18 +00:00
|
|
|
from cymem.cymem cimport Pool
|
|
|
|
from ..structs cimport TokenC
|
|
|
|
from thinc.typedefs cimport weight_t
|
|
|
|
|
2015-06-09 19:20:14 +00:00
|
|
|
from .stateclass cimport StateClass
|
|
|
|
|
2015-02-21 15:38:18 +00:00
|
|
|
|
|
|
|
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:
|
2015-03-14 15:06:35 +00:00
|
|
|
def __init__(self, StringStore string_table, dict labels_by_action):
|
2015-02-21 15:38:18 +00:00
|
|
|
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-06-01 21:05:25 +00:00
|
|
|
self._is_valid = <bint*>self.mem.alloc(self.n_moves, sizeof(bint))
|
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
|
2015-03-14 15:06:35 +00:00
|
|
|
self.strings = string_table
|
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):
|
2015-03-16 12:32:23 +00:00
|
|
|
label_id = self.strings[unicode(label_str)] if label_str else 0
|
2015-02-23 19:04:53 +00:00
|
|
|
moves[i] = self.init_transition(i, int(action), label_id)
|
|
|
|
i += 1
|
2015-02-21 15:38:18 +00:00
|
|
|
self.c = moves
|
|
|
|
|
2015-06-10 02:20:23 +00:00
|
|
|
cdef int initialize_state(self, StateClass state) except -1:
|
2015-04-28 18:45:51 +00:00
|
|
|
pass
|
|
|
|
|
2015-06-09 23:35:28 +00:00
|
|
|
cdef int finalize_state(self, StateClass state) except -1:
|
2015-04-28 18:45:51 +00:00
|
|
|
pass
|
2015-03-10 17:00:23 +00:00
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
cdef int preprocess_gold(self, GoldParse gold) except -1:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
cdef Transition lookup_transition(self, object name) except *:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-02-21 15:38:18 +00:00
|
|
|
cdef Transition init_transition(self, int clas, int move, int label) except *:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef Transition best_valid(self, const weight_t* scores, StateClass s) except *:
|
2015-02-21 16:06:37 +00:00
|
|
|
raise NotImplementedError
|
2015-06-01 21:05:25 +00:00
|
|
|
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef int set_valid(self, bint* output, StateClass state) except -1:
|
2015-06-01 21:05:25 +00:00
|
|
|
raise NotImplementedError
|
2015-02-21 15:38:18 +00:00
|
|
|
|
2015-06-09 22:40:43 +00:00
|
|
|
cdef int set_costs(self, int* output, StateClass stcls, GoldParse gold) except -1:
|
2015-06-04 17:32:32 +00:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
2015-06-09 22:40:43 +00:00
|
|
|
if self.c[i].is_valid(stcls, self.c[i].label):
|
|
|
|
output[i] = self.c[i].get_cost(stcls, &gold.c, self.c[i].label)
|
|
|
|
else:
|
|
|
|
output[i] = 9000
|
2015-06-04 17:32:32 +00:00
|
|
|
|
2015-06-09 22:40:43 +00:00
|
|
|
cdef Transition best_gold(self, const weight_t* scores, StateClass stcls,
|
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):
|
2015-06-09 21:23:28 +00:00
|
|
|
if self.c[i].is_valid(stcls, self.c[i].label):
|
2015-06-09 22:40:43 +00:00
|
|
|
cost = self.c[i].get_cost(stcls, &gold.c, self.c[i].label)
|
2015-06-09 21:23:28 +00:00
|
|
|
if scores[i] > score and cost == 0:
|
|
|
|
best = self.c[i]
|
|
|
|
score = scores[i]
|
2015-03-08 05:15:20 +00:00
|
|
|
assert score > MIN_SCORE
|
2015-02-21 15:38:18 +00:00
|
|
|
return best
|