2017-05-06 12:22:20 +00:00
|
|
|
# cython: infer_types=True
|
2023-09-12 06:49:41 +00:00
|
|
|
# cython: profile=False
|
2020-12-13 01:08:32 +00:00
|
|
|
from libcpp.vector cimport vector
|
2017-04-15 11:05:15 +00:00
|
|
|
|
2020-07-30 21:30:54 +00:00
|
|
|
from ...tokens.doc cimport Doc
|
2023-06-14 15:48:41 +00:00
|
|
|
from ._state cimport ArcC
|
2015-06-08 23:39:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef class StateClass:
|
2017-05-15 19:46:08 +00:00
|
|
|
def __init__(self, Doc doc=None, int offset=0):
|
2017-11-14 01:11:40 +00:00
|
|
|
self._borrowed = 0
|
2017-05-15 19:46:08 +00:00
|
|
|
if doc is not None:
|
|
|
|
self.c = new StateC(doc.c, doc.length)
|
|
|
|
self.c.offset = offset
|
2020-12-13 01:08:32 +00:00
|
|
|
self.doc = doc
|
|
|
|
else:
|
|
|
|
self.doc = None
|
2016-02-01 01:22:21 +00:00
|
|
|
|
|
|
|
def __dealloc__(self):
|
2017-11-14 01:11:40 +00:00
|
|
|
if self._borrowed != 1:
|
|
|
|
del self.c
|
2016-02-01 01:22:21 +00:00
|
|
|
|
2015-08-08 21:32:42 +00:00
|
|
|
@property
|
|
|
|
def stack(self):
|
2020-12-13 01:08:32 +00:00
|
|
|
return [self.S(i) for i in range(self.c.stack_depth())]
|
2015-08-08 21:32:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def queue(self):
|
2020-12-13 01:08:32 +00:00
|
|
|
return [self.B(i) for i in range(self.c.buffer_length())]
|
2015-08-08 21:32:42 +00:00
|
|
|
|
2017-05-06 12:22:20 +00:00
|
|
|
@property
|
2024-01-16 20:54:54 +00:00
|
|
|
def token_vector_length(self):
|
2017-05-06 12:22:20 +00:00
|
|
|
return self.doc.tensor.shape[1]
|
|
|
|
|
2017-10-03 10:43:48 +00:00
|
|
|
@property
|
2020-12-13 01:08:32 +00:00
|
|
|
def arcs(self):
|
|
|
|
cdef vector[ArcC] arcs
|
|
|
|
self.c.get_arcs(&arcs)
|
|
|
|
return list(arcs)
|
2023-07-19 10:03:31 +00:00
|
|
|
# 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
|
2020-12-13 01:08:32 +00:00
|
|
|
|
|
|
|
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)
|
2023-07-19 10:03:31 +00:00
|
|
|
|
2020-12-13 01:08:32 +00:00
|
|
|
def L(self, int head, int idx):
|
|
|
|
return self.c.L(head, idx)
|
2023-07-19 10:03:31 +00:00
|
|
|
|
2020-12-13 01:08:32 +00:00
|
|
|
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 10:43:48 +00:00
|
|
|
|
2017-05-15 19:46:08 +00:00
|
|
|
def is_final(self):
|
2017-05-06 12:22:20 +00:00
|
|
|
return self.c.is_final()
|
|
|
|
|
2017-05-26 16:31:23 +00:00
|
|
|
def copy(self):
|
2020-12-13 01:08:32 +00:00
|
|
|
cdef StateClass new_state = StateClass(doc=self.doc, offset=self.c.offset)
|
2017-05-26 16:31:23 +00:00
|
|
|
new_state.c.clone(self.c)
|
|
|
|
return new_state
|
|
|
|
|
2020-12-13 01:08:32 +00:00
|
|
|
def print_state(self):
|
|
|
|
words = [token.text for token in self.doc]
|
2015-06-09 23:35:28 +00:00
|
|
|
words = list(words) + ['_']
|
2020-12-13 01:08:32 +00: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)
|
2023-07-19 10:03:31 +00:00
|
|
|
|
2020-12-13 01:08:32 +00:00
|
|
|
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)]
|
2023-07-19 10:03:31 +00:00
|
|
|
|
2020-12-13 01:08:32 +00:00
|
|
|
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)]
|
2023-07-19 10:03:31 +00:00
|
|
|
|
2020-12-13 01:08:32 +00:00
|
|
|
def empty(self):
|
|
|
|
return self.c.empty()
|
|
|
|
|
|
|
|
def eol(self):
|
|
|
|
return self.c.eol()
|
|
|
|
|
|
|
|
def at_break(self):
|
|
|
|
return False
|
2023-07-19 10:03:31 +00:00
|
|
|
# return self.c.at_break()
|
2020-12-13 01:08:32 +00:00
|
|
|
|
|
|
|
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)
|