2017-04-15 11:05:15 +00:00
|
|
|
# coding: utf-8
|
2017-05-06 12:22:20 +00:00
|
|
|
# cython: infer_types=True
|
2017-04-15 11:05:15 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-10-03 10:43:48 +00:00
|
|
|
import numpy
|
2017-04-15 11:05:15 +00:00
|
|
|
|
2017-05-15 19:46:08 +00:00
|
|
|
from ..tokens.doc cimport Doc
|
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):
|
2015-06-09 19:20:14 +00:00
|
|
|
cdef Pool mem = Pool()
|
|
|
|
self.mem = mem
|
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
|
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):
|
2016-04-13 13:28:28 +00:00
|
|
|
return {self.S(i) for i in range(self.c._s_i)}
|
2015-08-08 21:32:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def queue(self):
|
2016-10-16 15:04:41 +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
|
|
|
|
def token_vector_lenth(self):
|
|
|
|
return self.doc.tensor.shape[1]
|
|
|
|
|
2017-10-03 10:43:48 +00: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
|
|
|
|
|
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):
|
|
|
|
cdef StateClass new_state = StateClass.init(self.c._sent, self.c.length)
|
|
|
|
new_state.c.clone(self.c)
|
|
|
|
return new_state
|
|
|
|
|
2015-06-09 23:35:28 +00:00
|
|
|
def print_state(self, words):
|
|
|
|
words = list(words) + ['_']
|
2015-06-10 08:13:03 +00:00
|
|
|
top = words[self.S(0)] + '_%d' % self.S_(0).head
|
|
|
|
second = words[self.S(1)] + '_%d' % self.S_(1).head
|
|
|
|
third = words[self.S(2)] + '_%d' % self.S_(2).head
|
2017-04-15 11:05:15 +00:00
|
|
|
n0 = words[self.B(0)]
|
|
|
|
n1 = words[self.B(1)]
|
2015-06-14 15:44:29 +00:00
|
|
|
return ' '.join((third, second, top, '|', n0, n1))
|