2017-05-06 15:22:20 +03:00
|
|
|
# cython: infer_types=True
|
2017-10-03 13:43:48 +03:00
|
|
|
import numpy
|
2020-12-13 04:08:32 +03:00
|
|
|
from libcpp.vector cimport vector
|
|
|
|
from ._state cimport ArcC
|
2017-04-15 14:05:15 +03:00
|
|
|
|
2020-07-31 00:30:54 +03:00
|
|
|
from ...tokens.doc cimport Doc
|
2015-06-09 02:39:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class StateClass:
|
2017-05-15 22:46:08 +03:00
|
|
|
def __init__(self, Doc doc=None, int offset=0):
|
2017-11-14 04:11:40 +03:00
|
|
|
self._borrowed = 0
|
2017-05-15 22:46:08 +03:00
|
|
|
if doc is not None:
|
|
|
|
self.c = new StateC(doc.c, doc.length)
|
|
|
|
self.c.offset = offset
|
2020-12-13 04:08:32 +03:00
|
|
|
self.doc = doc
|
|
|
|
else:
|
|
|
|
self.doc = None
|
2016-02-01 04:22:21 +03:00
|
|
|
|
|
|
|
def __dealloc__(self):
|
2017-11-14 04:11:40 +03:00
|
|
|
if self._borrowed != 1:
|
|
|
|
del self.c
|
2016-02-01 04:22:21 +03:00
|
|
|
|
2015-08-09 00:32:42 +03:00
|
|
|
@property
|
|
|
|
def stack(self):
|
2020-12-13 04:08:32 +03:00
|
|
|
return [self.S(i) for i in range(self.c.stack_depth())]
|
2015-08-09 00:32:42 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def queue(self):
|
2020-12-13 04:08:32 +03:00
|
|
|
return [self.B(i) for i in range(self.c.buffer_length())]
|
2015-08-09 00:32:42 +03:00
|
|
|
|
2017-05-06 15:22:20 +03:00
|
|
|
@property
|
|
|
|
def token_vector_lenth(self):
|
|
|
|
return self.doc.tensor.shape[1]
|
|
|
|
|
2017-10-03 13:43:48 +03:00
|
|
|
@property
|
2020-12-13 04:08:32 +03:00
|
|
|
def arcs(self):
|
|
|
|
cdef vector[ArcC] arcs
|
|
|
|
self.c.get_arcs(&arcs)
|
|
|
|
return list(arcs)
|
|
|
|
#py_arcs = []
|
|
|
|
#for arc in arcs:
|
|
|
|
# if arc.head != -1 and arc.child != -1:
|
|
|
|
# py_arcs.append((arc.head, arc.child, arc.label))
|
|
|
|
#return arcs
|
|
|
|
|
|
|
|
def add_arc(self, int head, int child, int label):
|
|
|
|
self.c.add_arc(head, child, label)
|
|
|
|
|
|
|
|
def del_arc(self, int head, int child):
|
|
|
|
self.c.del_arc(head, child)
|
|
|
|
|
|
|
|
def H(self, int child):
|
|
|
|
return self.c.H(child)
|
|
|
|
|
|
|
|
def L(self, int head, int idx):
|
|
|
|
return self.c.L(head, idx)
|
|
|
|
|
|
|
|
def R(self, int head, int idx):
|
|
|
|
return self.c.R(head, idx)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _b_i(self):
|
|
|
|
return self.c._b_i
|
|
|
|
|
|
|
|
@property
|
|
|
|
def length(self):
|
|
|
|
return self.c.length
|
2017-10-03 13:43:48 +03:00
|
|
|
|
2017-05-15 22:46:08 +03:00
|
|
|
def is_final(self):
|
2017-05-06 15:22:20 +03:00
|
|
|
return self.c.is_final()
|
|
|
|
|
2017-05-26 19:31:23 +03:00
|
|
|
def copy(self):
|
2020-12-13 04:08:32 +03:00
|
|
|
cdef StateClass new_state = StateClass(doc=self.doc, offset=self.c.offset)
|
2017-05-26 19:31:23 +03:00
|
|
|
new_state.c.clone(self.c)
|
|
|
|
return new_state
|
|
|
|
|
2020-12-13 04:08:32 +03:00
|
|
|
def print_state(self):
|
|
|
|
words = [token.text for token in self.doc]
|
2015-06-10 02:35:28 +03:00
|
|
|
words = list(words) + ['_']
|
2020-12-13 04:08:32 +03:00
|
|
|
bools = ["F", "T"]
|
|
|
|
sent_starts = [bools[self.c.is_sent_start(i)] for i in range(len(self.doc))]
|
|
|
|
shifted = [1 if self.c.is_unshiftable(i) else 0 for i in range(self.c.length)]
|
|
|
|
shifted.append("")
|
|
|
|
sent_starts.append("")
|
|
|
|
top = f"{self.S(0)}{words[self.S(0)]}_{words[self.H(self.S(0))]}_{shifted[self.S(0)]}"
|
|
|
|
second = f"{self.S(1)}{words[self.S(1)]}_{words[self.H(self.S(1))]}_{shifted[self.S(1)]}"
|
|
|
|
third = f"{self.S(2)}{words[self.S(2)]}_{words[self.H(self.S(2))]}_{shifted[self.S(2)]}"
|
|
|
|
n0 = f"{self.B(0)}{words[self.B(0)]}_{sent_starts[self.B(0)]}_{shifted[self.B(0)]}"
|
|
|
|
n1 = f"{self.B(1)}{words[self.B(1)]}_{sent_starts[self.B(1)]}_{shifted[self.B(1)]}"
|
|
|
|
return ' '.join((str(self.stack_depth()), str(self.buffer_length()), third, second, top, '|', n0, n1))
|
|
|
|
|
|
|
|
def S(self, int i):
|
|
|
|
return self.c.S(i)
|
|
|
|
|
|
|
|
def B(self, int i):
|
|
|
|
return self.c.B(i)
|
|
|
|
|
|
|
|
def H(self, int i):
|
|
|
|
return self.c.H(i)
|
|
|
|
|
|
|
|
def E(self, int i):
|
|
|
|
return self.c.E(i)
|
|
|
|
|
|
|
|
def L(self, int i, int idx):
|
|
|
|
return self.c.L(i, idx)
|
|
|
|
|
|
|
|
def R(self, int i, int idx):
|
|
|
|
return self.c.R(i, idx)
|
|
|
|
|
|
|
|
def S_(self, int i):
|
|
|
|
return self.doc[self.c.S(i)]
|
|
|
|
|
|
|
|
def B_(self, int i):
|
|
|
|
return self.doc[self.c.B(i)]
|
|
|
|
|
|
|
|
def H_(self, int i):
|
|
|
|
return self.doc[self.c.H(i)]
|
|
|
|
|
|
|
|
def E_(self, int i):
|
|
|
|
return self.doc[self.c.E(i)]
|
|
|
|
|
|
|
|
def L_(self, int i, int idx):
|
|
|
|
return self.doc[self.c.L(i, idx)]
|
|
|
|
|
|
|
|
def R_(self, int i, int idx):
|
|
|
|
return self.doc[self.c.R(i, idx)]
|
|
|
|
|
|
|
|
def empty(self):
|
|
|
|
return self.c.empty()
|
|
|
|
|
|
|
|
def eol(self):
|
|
|
|
return self.c.eol()
|
|
|
|
|
|
|
|
def at_break(self):
|
|
|
|
return False
|
|
|
|
#return self.c.at_break()
|
|
|
|
|
|
|
|
def has_head(self, int i):
|
|
|
|
return self.c.has_head(i)
|
|
|
|
|
|
|
|
def n_L(self, int i):
|
|
|
|
return self.c.n_L(i)
|
|
|
|
|
|
|
|
def n_R(self, int i):
|
|
|
|
return self.c.n_R(i)
|
|
|
|
|
|
|
|
def entity_is_open(self):
|
|
|
|
return self.c.entity_is_open()
|
|
|
|
|
|
|
|
def stack_depth(self):
|
|
|
|
return self.c.stack_depth()
|
|
|
|
|
|
|
|
def buffer_length(self):
|
|
|
|
return self.c.buffer_length()
|
|
|
|
|
|
|
|
def push(self):
|
|
|
|
self.c.push()
|
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
self.c.pop()
|
|
|
|
|
|
|
|
def unshift(self):
|
|
|
|
self.c.unshift()
|
|
|
|
|
|
|
|
def add_arc(self, int head, int child, attr_t label):
|
|
|
|
self.c.add_arc(head, child, label)
|
|
|
|
|
|
|
|
def del_arc(self, int head, int child):
|
|
|
|
self.c.del_arc(head, child)
|
|
|
|
|
|
|
|
def open_ent(self, attr_t label):
|
|
|
|
self.c.open_ent(label)
|
|
|
|
|
|
|
|
def close_ent(self):
|
|
|
|
self.c.close_ent()
|
|
|
|
|
|
|
|
def clone(self, StateClass src):
|
|
|
|
self.c.clone(src.c)
|