2016-10-27 19:08:13 +03:00
|
|
|
# cython: infer_types=True
|
2015-02-21 18:38:18 +03:00
|
|
|
from cymem.cymem cimport Pool
|
|
|
|
from thinc.typedefs cimport weight_t
|
2015-07-19 16:18:17 +03:00
|
|
|
from collections import defaultdict
|
2015-02-21 18:38:18 +03:00
|
|
|
|
2015-07-19 16:18:17 +03:00
|
|
|
from ..structs cimport TokenC
|
2015-06-09 22:20:14 +03:00
|
|
|
from .stateclass cimport StateClass
|
2015-07-19 16:18:17 +03:00
|
|
|
from ..attrs cimport TAG, HEAD, DEP, ENT_TYPE, ENT_IOB
|
2017-03-11 20:12:01 +03:00
|
|
|
from cpython.ref cimport PyObject, Py_INCREF, Py_XDECREF
|
2015-06-09 22:20:14 +03:00
|
|
|
|
2015-02-21 18:38:18 +03:00
|
|
|
|
|
|
|
cdef weight_t MIN_SCORE = -90000
|
|
|
|
|
|
|
|
|
2015-02-22 08:32:07 +03:00
|
|
|
class OracleError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-03-11 20:12:01 +03:00
|
|
|
cdef void* _init_state(Pool mem, int length, void* tokens) except NULL:
|
|
|
|
cdef StateClass st = StateClass.init(<const TokenC*>tokens, length)
|
|
|
|
Py_INCREF(st)
|
|
|
|
return <void*>st
|
|
|
|
|
|
|
|
|
2015-02-21 18:38:18 +03:00
|
|
|
cdef class TransitionSystem:
|
2015-10-12 11:33:11 +03:00
|
|
|
def __init__(self, StringStore string_table, dict labels_by_action, _freqs=None):
|
2015-02-21 18:38:18 +03:00
|
|
|
self.mem = Pool()
|
2015-03-14 18:06:35 +03:00
|
|
|
self.strings = string_table
|
2016-01-19 21:09:33 +03:00
|
|
|
self.n_moves = 0
|
|
|
|
self._size = 100
|
2017-03-11 20:12:01 +03:00
|
|
|
|
2016-01-19 21:09:33 +03:00
|
|
|
self.c = <Transition*>self.mem.alloc(self._size, sizeof(Transition))
|
2017-03-11 20:12:01 +03:00
|
|
|
|
2015-02-21 18:38:18 +03:00
|
|
|
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):
|
2016-01-19 21:09:33 +03:00
|
|
|
self.add_action(int(action), label_str)
|
2015-07-15 00:47:03 +03:00
|
|
|
self.root_label = self.strings['ROOT']
|
2015-10-12 11:33:11 +03:00
|
|
|
self.freqs = {} if _freqs is None else _freqs
|
2015-07-19 16:18:17 +03:00
|
|
|
for attr in (TAG, HEAD, DEP, ENT_TYPE, ENT_IOB):
|
|
|
|
self.freqs[attr] = defaultdict(int)
|
2015-07-23 02:16:27 +03:00
|
|
|
self.freqs[attr][0] = 1
|
|
|
|
# Ensure we've seen heads. Need an official dependency length limit...
|
2015-07-28 01:14:29 +03:00
|
|
|
for i in range(10024):
|
2015-07-23 02:16:27 +03:00
|
|
|
self.freqs[HEAD][i] = 1
|
|
|
|
self.freqs[HEAD][-i] = 1
|
2017-03-11 20:12:01 +03:00
|
|
|
self.init_beam_state = _init_state
|
2015-02-21 18:38:18 +03:00
|
|
|
|
2015-10-12 11:33:11 +03:00
|
|
|
def __reduce__(self):
|
2016-01-19 21:09:33 +03:00
|
|
|
labels_by_action = {}
|
|
|
|
cdef Transition t
|
|
|
|
for trans in self.c[:self.n_moves]:
|
|
|
|
label_str = self.strings[trans.label]
|
|
|
|
labels_by_action.setdefault(trans.move, []).append(label_str)
|
2015-10-12 11:33:11 +03:00
|
|
|
return (self.__class__,
|
2016-01-19 21:09:33 +03:00
|
|
|
(self.strings, labels_by_action, self.freqs),
|
2015-10-12 11:33:11 +03:00
|
|
|
None, None)
|
|
|
|
|
2016-02-01 10:34:55 +03:00
|
|
|
cdef int initialize_state(self, StateC* state) nogil:
|
2015-04-28 21:45:51 +03:00
|
|
|
pass
|
|
|
|
|
2016-02-01 10:34:55 +03:00
|
|
|
cdef int finalize_state(self, StateC* state) nogil:
|
2015-04-28 21:45:51 +03:00
|
|
|
pass
|
2015-03-10 20:00:23 +03:00
|
|
|
|
2016-05-02 15:25:10 +03:00
|
|
|
def finalize_doc(self, doc):
|
|
|
|
pass
|
|
|
|
|
2015-03-09 14:06:01 +03: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 18:38:18 +03:00
|
|
|
cdef Transition init_transition(self, int clas, int move, int label) except *:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-08-09 00:36:18 +03:00
|
|
|
def is_valid(self, StateClass stcls, move_name):
|
|
|
|
action = self.lookup_transition(move_name)
|
2016-02-01 04:58:14 +03:00
|
|
|
return action.is_valid(stcls.c, action.label)
|
2015-08-09 00:36:18 +03:00
|
|
|
|
2016-02-01 05:00:15 +03:00
|
|
|
cdef int set_valid(self, int* is_valid, const StateC* st) nogil:
|
2015-06-04 20:32:32 +03:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
2016-02-01 05:00:15 +03:00
|
|
|
is_valid[i] = self.c[i].is_valid(st, self.c[i].label)
|
2015-06-04 20:32:32 +03:00
|
|
|
|
2016-01-30 16:31:12 +03:00
|
|
|
cdef int set_costs(self, int* is_valid, weight_t* costs,
|
2015-06-26 07:25:36 +03:00
|
|
|
StateClass stcls, GoldParse gold) except -1:
|
2015-02-21 18:38:18 +03:00
|
|
|
cdef int i
|
2016-02-01 05:00:15 +03:00
|
|
|
self.set_valid(is_valid, stcls.c)
|
2015-02-21 18:38:18 +03:00
|
|
|
for i in range(self.n_moves):
|
2015-06-26 07:25:36 +03:00
|
|
|
if is_valid[i]:
|
|
|
|
costs[i] = self.c[i].get_cost(stcls, &gold.c, self.c[i].label)
|
|
|
|
else:
|
|
|
|
costs[i] = 9000
|
2016-01-19 21:09:33 +03:00
|
|
|
|
|
|
|
def add_action(self, int action, label):
|
2016-01-19 22:10:04 +03:00
|
|
|
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
|
2016-01-19 21:09:33 +03:00
|
|
|
if self.n_moves >= self._size:
|
|
|
|
self._size *= 2
|
|
|
|
self.c = <Transition*>self.mem.realloc(self.c, self._size * sizeof(self.c[0]))
|
2017-03-11 20:12:01 +03:00
|
|
|
|
2016-01-19 21:09:33 +03:00
|
|
|
self.c[self.n_moves] = self.init_transition(self.n_moves, action, label)
|
|
|
|
self.n_moves += 1
|
2016-01-19 22:10:04 +03:00
|
|
|
return 1
|