spaCy/spacy/syntax/transition_system.pyx

75 lines
2.6 KiB
Cython
Raw Normal View History

from cymem.cymem cimport Pool
from thinc.typedefs cimport weight_t
from collections import defaultdict
from ..structs cimport TokenC
from .stateclass cimport StateClass
from ..attrs cimport TAG, HEAD, DEP, ENT_TYPE, ENT_IOB
cdef weight_t MIN_SCORE = -90000
class OracleError(Exception):
pass
cdef class TransitionSystem:
def __init__(self, StringStore string_table, dict labels_by_action):
self.mem = Pool()
2015-02-23 22:04:53 +03:00
self.n_moves = sum(len(labels) for labels in labels_by_action.values())
2015-06-02 00:05:25 +03:00
self._is_valid = <bint*>self.mem.alloc(self.n_moves, sizeof(bint))
moves = <Transition*>self.mem.alloc(self.n_moves, sizeof(Transition))
cdef int i = 0
2015-02-23 22:04:53 +03:00
cdef int label_id
self.strings = string_table
for action, label_strs in sorted(labels_by_action.items()):
2015-02-23 22:04:53 +03:00
for label_str in sorted(label_strs):
label_id = self.strings[unicode(label_str)] if label_str else 0
2015-02-23 22:04:53 +03:00
moves[i] = self.init_transition(i, int(action), label_id)
i += 1
self.c = moves
2015-07-15 00:47:03 +03:00
self.root_label = self.strings['ROOT']
self.freqs = {}
for attr in (TAG, HEAD, DEP, ENT_TYPE, ENT_IOB):
self.freqs[attr] = defaultdict(int)
self.freqs[attr][0] = 1
# Ensure we've seen heads. Need an official dependency length limit...
for i in range(10024):
self.freqs[HEAD][i] = 1
self.freqs[HEAD][-i] = 1
cdef int initialize_state(self, StateClass state) except -1:
pass
2015-07-15 00:47:03 +03:00
cdef int finalize_state(self, StateClass state) nogil:
pass
cdef int preprocess_gold(self, GoldParse gold) except -1:
raise NotImplementedError
cdef Transition lookup_transition(self, object name) except *:
raise NotImplementedError
cdef Transition init_transition(self, int clas, int move, int label) except *:
raise NotImplementedError
def is_valid(self, StateClass stcls, move_name):
action = self.lookup_transition(move_name)
return action.is_valid(stcls, action.label)
cdef int set_valid(self, int* is_valid, StateClass stcls) nogil:
2015-06-04 20:32:32 +03:00
cdef int i
for i in range(self.n_moves):
is_valid[i] = self.c[i].is_valid(stcls, self.c[i].label)
2015-06-04 20:32:32 +03:00
cdef int set_costs(self, int* is_valid, int* costs,
StateClass stcls, GoldParse gold) except -1:
cdef int i
self.set_valid(is_valid, stcls)
for i in range(self.n_moves):
if is_valid[i]:
costs[i] = self.c[i].get_cost(stcls, &gold.c, self.c[i].label)
else:
costs[i] = 9000