2014-11-10 06:39:23 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-11-10 05:28:56 +00:00
|
|
|
from ._state cimport init_state
|
|
|
|
from ._state cimport entity_is_open
|
2014-11-11 13:54:25 +00:00
|
|
|
from .bilou_moves cimport fill_moves
|
|
|
|
from .bilou_moves cimport transition
|
|
|
|
from .bilou_moves cimport set_accept_if_valid, set_accept_if_oracle
|
|
|
|
from .bilou_moves import get_n_moves
|
|
|
|
from .bilou_moves import ACTION_NAMES
|
2014-11-10 05:28:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef class PyState:
|
|
|
|
def __init__(self, tag_names, n_tokens):
|
|
|
|
self.mem = Pool()
|
2014-11-11 06:17:54 +00:00
|
|
|
self.tag_names = tag_names
|
|
|
|
self.n_classes = len(tag_names)
|
2014-11-10 05:28:56 +00:00
|
|
|
assert self.n_classes != 0
|
|
|
|
self._moves = <Move*>self.mem.alloc(self.n_classes, sizeof(Move))
|
2014-11-11 06:17:54 +00:00
|
|
|
fill_moves(self._moves, tag_names)
|
2014-11-10 05:28:56 +00:00
|
|
|
self._s = init_state(self.mem, n_tokens)
|
2014-11-10 11:15:18 +00:00
|
|
|
self._golds = <Move*>self.mem.alloc(n_tokens, sizeof(Move))
|
2014-11-10 06:39:23 +00:00
|
|
|
|
|
|
|
cdef Move* _get_move(self, unicode move_name) except NULL:
|
2014-11-11 06:17:54 +00:00
|
|
|
return &self._moves[self.tag_names.index(move_name)]
|
2014-11-10 05:28:56 +00:00
|
|
|
|
2014-11-10 11:15:18 +00:00
|
|
|
def set_golds(self, list gold_names):
|
|
|
|
cdef Move* m
|
|
|
|
for i, name in enumerate(gold_names):
|
|
|
|
m = self._get_move(name)
|
|
|
|
self._golds[i] = m[0]
|
|
|
|
|
2014-11-10 05:28:56 +00:00
|
|
|
def transition(self, unicode move_name):
|
2014-11-10 06:39:23 +00:00
|
|
|
cdef Move* m = self._get_move(move_name)
|
2014-11-10 05:28:56 +00:00
|
|
|
transition(self._s, m)
|
|
|
|
|
|
|
|
def is_valid(self, unicode move_name):
|
2014-11-10 06:39:23 +00:00
|
|
|
cdef Move* m = self._get_move(move_name)
|
|
|
|
set_accept_if_valid(self._moves, self.n_classes, self._s)
|
|
|
|
return m.accept
|
2014-11-10 05:28:56 +00:00
|
|
|
|
|
|
|
def is_gold(self, unicode move_name):
|
2014-11-10 11:15:18 +00:00
|
|
|
cdef Move* m = self._get_move(move_name)
|
2014-11-11 06:17:54 +00:00
|
|
|
set_accept_if_oracle(self._moves, self._golds, self.n_classes, self._s)
|
2014-11-10 11:15:18 +00:00
|
|
|
return m.accept
|
2014-11-10 05:28:56 +00:00
|
|
|
|
|
|
|
property ent:
|
|
|
|
def __get__(self):
|
2014-11-10 11:15:18 +00:00
|
|
|
return self._s.curr
|
2014-11-10 05:28:56 +00:00
|
|
|
|
|
|
|
property n_ents:
|
|
|
|
def __get__(self):
|
2014-11-10 11:15:18 +00:00
|
|
|
return self._s.j
|
2014-11-10 05:28:56 +00:00
|
|
|
|
|
|
|
property i:
|
|
|
|
def __get__(self):
|
|
|
|
return self._s.i
|
|
|
|
|
|
|
|
property open_entity:
|
|
|
|
def __get__(self):
|
|
|
|
return entity_is_open(self._s)
|