spaCy/spacy/syntax/stateclass.pyx

55 lines
1.4 KiB
Cython
Raw Normal View History

# cython: infer_types=True
2017-10-03 13:43:48 +03:00
import numpy
2017-04-15 14:05:15 +03:00
from ..tokens.doc cimport Doc
cdef class StateClass:
def __init__(self, Doc doc=None, int offset=0):
cdef Pool mem = Pool()
self.mem = mem
2017-11-14 04:11:40 +03:00
self._borrowed = 0
if doc is not None:
self.c = new StateC(doc.c, doc.length)
self.c.offset = offset
def __dealloc__(self):
2017-11-14 04:11:40 +03:00
if self._borrowed != 1:
del self.c
@property
def stack(self):
return {self.S(i) for i in range(self.c._s_i)}
@property
def queue(self):
return {self.B(i) for i in range(self.c.buffer_length())}
@property
def token_vector_lenth(self):
return self.doc.tensor.shape[1]
2017-10-03 13:43:48 +03:00
@property
def history(self):
hist = numpy.ndarray((8,), dtype='i')
for i in range(8):
hist[i] = self.c.get_hist(i+1)
return hist
def is_final(self):
return self.c.is_final()
2017-05-26 19:31:23 +03:00
def copy(self):
cdef StateClass new_state = StateClass.init(self.c._sent, self.c.length)
new_state.c.clone(self.c)
return new_state
def print_state(self, words):
words = list(words) + ['_']
2019-12-25 19:59:52 +03:00
top = f"{words[self.S(0)]}_{self.S_(0).head}"
second = f"{words[self.S(1)]}_{self.S_(1).head}"
third = f"{words[self.S(2)]}_{self.S_(2).head}"
2017-04-15 14:05:15 +03:00
n0 = words[self.B(0)]
n1 = words[self.B(1)]
2015-06-14 18:44:29 +03:00
return ' '.join((third, second, top, '|', n0, n1))