2015-06-02 19:36:27 +03:00
|
|
|
# cython: profile=True
|
2015-02-23 22:04:53 +03:00
|
|
|
from libc.string cimport memmove, memcpy
|
2014-12-16 14:44:43 +03:00
|
|
|
from cymem.cymem cimport Pool
|
|
|
|
|
|
|
|
from ..lexeme cimport EMPTY_LEXEME
|
2015-05-06 17:29:10 +03:00
|
|
|
from ..structs cimport TokenC, Entity, Constituent
|
2014-12-20 21:42:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
DEF PADDING = 5
|
|
|
|
DEF NON_MONOTONIC = True
|
2014-12-18 03:33:25 +03:00
|
|
|
|
|
|
|
|
2014-12-16 19:19:43 +03:00
|
|
|
cdef int add_dep(State *s, int head, int child, int label) except -1:
|
2015-04-29 22:32:18 +03:00
|
|
|
if has_head(&s.sent[child]):
|
|
|
|
del_dep(s, child + s.sent[child].head, child)
|
2014-12-20 21:42:23 +03:00
|
|
|
cdef int dist = head - child
|
|
|
|
s.sent[child].head = dist
|
2015-01-17 08:21:17 +03:00
|
|
|
s.sent[child].dep = label
|
2014-12-16 14:44:43 +03:00
|
|
|
# Keep a bit-vector tracking child dependencies. If a word has a child at
|
|
|
|
# offset i from it, set that bit (tracking left and right separately)
|
|
|
|
if child > head:
|
2014-12-20 21:42:23 +03:00
|
|
|
s.sent[head].r_kids |= 1 << (-dist)
|
2015-04-29 22:32:18 +03:00
|
|
|
s.sent[head].r_edge = child - head
|
2015-04-29 20:28:21 +03:00
|
|
|
# Walk up the tree, setting right edge
|
2015-06-02 00:06:30 +03:00
|
|
|
n_iter = 0
|
|
|
|
start = head
|
2015-04-29 22:32:18 +03:00
|
|
|
while s.sent[head].head != 0:
|
2015-04-29 20:28:21 +03:00
|
|
|
head += s.sent[head].head
|
2015-04-29 22:32:18 +03:00
|
|
|
s.sent[head].r_edge = child - head
|
2015-06-02 00:06:30 +03:00
|
|
|
n_iter += 1
|
|
|
|
if n_iter >= s.sent_len:
|
|
|
|
tree = [(i + s.sent[i].head) for i in range(s.sent_len)]
|
|
|
|
msg = "Error adding dependency (%d, %d). Could not find root of tree: %s"
|
|
|
|
msg = msg % (start, child, tree)
|
|
|
|
raise Exception(msg)
|
2014-12-16 14:44:43 +03:00
|
|
|
else:
|
2014-12-20 21:42:23 +03:00
|
|
|
s.sent[head].l_kids |= 1 << dist
|
2015-04-29 22:32:18 +03:00
|
|
|
s.sent[head].l_edge = (child + s.sent[child].l_edge) - head
|
|
|
|
|
|
|
|
|
|
|
|
cdef int del_dep(State *s, int head, int child) except -1:
|
|
|
|
cdef const TokenC* next_child
|
|
|
|
cdef int dist = head - child
|
|
|
|
if child > head:
|
|
|
|
s.sent[head].r_kids &= ~(1 << (-dist))
|
|
|
|
next_child = get_right(s, &s.sent[head], 1)
|
|
|
|
if next_child == NULL:
|
|
|
|
s.sent[head].r_edge = 0
|
|
|
|
else:
|
|
|
|
s.sent[head].r_edge = next_child.r_edge
|
|
|
|
else:
|
|
|
|
s.sent[head].l_kids &= ~(1 << dist)
|
|
|
|
next_child = get_left(s, &s.sent[head], 1)
|
|
|
|
if next_child == NULL:
|
|
|
|
s.sent[head].l_edge = 0
|
|
|
|
else:
|
|
|
|
s.sent[head].l_edge = next_child.l_edge
|
2014-12-16 14:44:43 +03:00
|
|
|
|
|
|
|
|
2014-12-16 19:19:43 +03:00
|
|
|
cdef int pop_stack(State *s) except -1:
|
2014-12-16 14:44:43 +03:00
|
|
|
assert s.stack_len >= 1
|
|
|
|
s.stack_len -= 1
|
2014-12-16 19:19:43 +03:00
|
|
|
s.stack -= 1
|
2015-06-08 15:49:04 +03:00
|
|
|
#if s.stack_len == 0 and not at_eol(s):
|
|
|
|
# push_stack(s)
|
2015-04-19 11:31:31 +03:00
|
|
|
|
2014-12-16 14:44:43 +03:00
|
|
|
|
|
|
|
cdef int push_stack(State *s) except -1:
|
|
|
|
assert s.i < s.sent_len
|
|
|
|
s.stack += 1
|
2014-12-16 19:19:43 +03:00
|
|
|
s.stack[0] = s.i
|
2014-12-16 14:44:43 +03:00
|
|
|
s.stack_len += 1
|
|
|
|
s.i += 1
|
|
|
|
|
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
cdef int children_in_buffer(const State *s, int head, const int* gold) except -1:
|
2014-12-16 14:44:43 +03:00
|
|
|
# Golds holds an array of head offsets --- the head of word i is i - golds[i]
|
|
|
|
# Iterate over the tokens of the queue, and check whether their gold head is
|
|
|
|
# our target
|
|
|
|
cdef int i
|
|
|
|
cdef int n = 0
|
|
|
|
for i in range(s.i, s.sent_len):
|
2014-12-16 19:19:43 +03:00
|
|
|
if gold[i] == head:
|
2014-12-16 14:44:43 +03:00
|
|
|
n += 1
|
2015-06-02 22:05:24 +03:00
|
|
|
elif gold[i] == i or gold[i] < head:
|
|
|
|
break
|
2014-12-16 14:44:43 +03:00
|
|
|
return n
|
|
|
|
|
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
cdef int head_in_buffer(const State *s, const int child, const int* gold) except -1:
|
2014-12-16 19:19:43 +03:00
|
|
|
return gold[child] >= s.i
|
2014-12-16 14:44:43 +03:00
|
|
|
|
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
cdef int children_in_stack(const State *s, const int head, const int* gold) except -1:
|
2014-12-16 14:44:43 +03:00
|
|
|
cdef int i
|
|
|
|
cdef int n = 0
|
|
|
|
for i in range(s.stack_len):
|
2014-12-16 19:19:43 +03:00
|
|
|
if gold[s.stack[-i]] == head:
|
2014-12-20 21:42:23 +03:00
|
|
|
if NON_MONOTONIC or not has_head(get_s0(s)):
|
|
|
|
n += 1
|
2014-12-16 14:44:43 +03:00
|
|
|
return n
|
|
|
|
|
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
cdef int head_in_stack(const State *s, const int child, const int* gold) except -1:
|
2014-12-16 14:44:43 +03:00
|
|
|
cdef int i
|
|
|
|
for i in range(s.stack_len):
|
2014-12-16 19:19:43 +03:00
|
|
|
if gold[child] == s.stack[-i]:
|
2014-12-16 14:44:43 +03:00
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2015-04-29 20:28:21 +03:00
|
|
|
cdef bint has_head(const TokenC* t) nogil:
|
|
|
|
return t.head != 0
|
|
|
|
|
|
|
|
|
2014-12-16 19:19:43 +03:00
|
|
|
cdef const TokenC* get_left(const State* s, const TokenC* head, const int idx) nogil:
|
2015-06-08 15:49:04 +03:00
|
|
|
return _new_get_left(s, head, idx)
|
|
|
|
#cdef uint32_t kids = head.l_kids
|
|
|
|
#if kids == 0:
|
|
|
|
# return NULL
|
|
|
|
#cdef int offset = _nth_significant_bit(kids, idx)
|
|
|
|
#cdef const TokenC* child = head - offset
|
|
|
|
#if child >= s.sent:
|
|
|
|
# return child
|
|
|
|
##else:
|
|
|
|
# return NULL
|
2014-12-16 14:44:43 +03:00
|
|
|
|
|
|
|
|
2014-12-16 19:19:43 +03:00
|
|
|
cdef const TokenC* get_right(const State* s, const TokenC* head, const int idx) nogil:
|
2015-06-08 15:49:04 +03:00
|
|
|
return _new_get_right(s, head, idx)
|
|
|
|
#cdef uint32_t kids = head.r_kids
|
|
|
|
#if kids == 0:
|
|
|
|
# return NULL
|
|
|
|
#cdef int offset = _nth_significant_bit(kids, idx)
|
|
|
|
#cdef const TokenC* child = head + offset
|
|
|
|
#if child < (s.sent + s.sent_len):
|
|
|
|
# return child
|
|
|
|
#else:
|
|
|
|
# return NULL
|
2014-12-16 14:44:43 +03:00
|
|
|
|
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
cdef int count_left_kids(const TokenC* head) nogil:
|
|
|
|
return _popcount(head.l_kids)
|
|
|
|
|
|
|
|
|
|
|
|
cdef int count_right_kids(const TokenC* head) nogil:
|
|
|
|
return _popcount(head.r_kids)
|
|
|
|
|
2014-12-18 01:05:31 +03:00
|
|
|
|
2015-03-10 20:00:23 +03:00
|
|
|
cdef State* new_state(Pool mem, const TokenC* sent, const int sent_len) except NULL:
|
2015-02-22 08:32:07 +03:00
|
|
|
cdef int padded_len = sent_len + PADDING + PADDING
|
2014-12-16 14:44:43 +03:00
|
|
|
cdef State* s = <State*>mem.alloc(1, sizeof(State))
|
2015-05-20 17:55:05 +03:00
|
|
|
#s.ctnt = <Constituent*>mem.alloc(padded_len, sizeof(Constituent))
|
2015-03-10 20:00:23 +03:00
|
|
|
s.ent = <Entity*>mem.alloc(padded_len, sizeof(Entity))
|
2014-12-16 19:19:43 +03:00
|
|
|
s.stack = <int*>mem.alloc(padded_len, sizeof(int))
|
2014-12-16 14:44:43 +03:00
|
|
|
for i in range(PADDING):
|
2014-12-16 19:19:43 +03:00
|
|
|
s.stack[i] = -1
|
2015-05-20 17:55:05 +03:00
|
|
|
#s.ctnt += (PADDING -1)
|
2014-12-16 19:19:43 +03:00
|
|
|
s.stack += (PADDING - 1)
|
2015-03-10 20:00:23 +03:00
|
|
|
s.ent += (PADDING - 1)
|
2014-12-16 19:19:43 +03:00
|
|
|
assert s.stack[0] == -1
|
2015-02-23 22:04:53 +03:00
|
|
|
state_sent = <TokenC*>mem.alloc(padded_len, sizeof(TokenC))
|
|
|
|
memcpy(state_sent, sent - PADDING, padded_len * sizeof(TokenC))
|
|
|
|
s.sent = state_sent + PADDING
|
2014-12-16 14:44:43 +03:00
|
|
|
s.stack_len = 0
|
|
|
|
s.i = 0
|
2015-02-22 08:32:07 +03:00
|
|
|
s.sent_len = sent_len
|
2014-12-16 14:44:43 +03:00
|
|
|
return s
|
2015-04-29 20:28:21 +03:00
|
|
|
|
|
|
|
|
2015-06-02 00:06:30 +03:00
|
|
|
cdef int copy_state(State* dest, const State* src) except -1:
|
2015-06-02 19:36:27 +03:00
|
|
|
cdef int i
|
2015-06-02 00:06:30 +03:00
|
|
|
# Copy stack --- remember stack uses pointer arithmetic, so stack[-stack_len]
|
|
|
|
# is the last word of the stack.
|
|
|
|
dest.stack += (src.stack_len - dest.stack_len)
|
|
|
|
for i in range(src.stack_len):
|
|
|
|
dest.stack[-i] = src.stack[-i]
|
|
|
|
dest.stack_len = src.stack_len
|
|
|
|
# Copy sentence (i.e. the parse), up to and including word i.
|
2015-06-02 19:36:27 +03:00
|
|
|
if src.i > dest.i:
|
|
|
|
memcpy(dest.sent, src.sent, sizeof(TokenC) * (src.i+1))
|
|
|
|
else:
|
|
|
|
memcpy(dest.sent, src.sent, sizeof(TokenC) * (dest.i+1))
|
2015-06-02 00:06:30 +03:00
|
|
|
dest.i = src.i
|
|
|
|
# Copy assigned entities --- also pointer arithmetic
|
|
|
|
dest.ent += (src.ents_len - dest.ents_len)
|
|
|
|
for i in range(src.ents_len):
|
|
|
|
dest.ent[-i] = src.ent[-i]
|
|
|
|
dest.ents_len = src.ents_len
|
|
|
|
|
|
|
|
|
2015-04-29 20:28:21 +03:00
|
|
|
# From https://en.wikipedia.org/wiki/Hamming_weight
|
|
|
|
cdef inline uint32_t _popcount(uint32_t x) nogil:
|
|
|
|
"""Find number of non-zero bits."""
|
2015-06-08 15:49:04 +03:00
|
|
|
cdef uint32_t count = 0
|
2015-04-29 20:28:21 +03:00
|
|
|
while x != 0:
|
|
|
|
x &= x - 1
|
|
|
|
count += 1
|
|
|
|
return count
|
|
|
|
|
|
|
|
|
|
|
|
cdef inline uint32_t _nth_significant_bit(uint32_t bits, int n) nogil:
|
2015-06-08 15:49:04 +03:00
|
|
|
cdef uint32_t i
|
2015-04-29 20:28:21 +03:00
|
|
|
for i in range(32):
|
|
|
|
if bits & (1 << i):
|
|
|
|
n -= 1
|
|
|
|
if n < 1:
|
|
|
|
return i
|
|
|
|
return 0
|
2015-06-08 15:49:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef const TokenC* _new_get_left(const State* s, const TokenC* target, int idx) nogil:
|
|
|
|
if idx < 1:
|
|
|
|
return NULL
|
|
|
|
cdef const TokenC* ptr = s.sent
|
|
|
|
while ptr < target:
|
|
|
|
# If this head is still to the right of us, we can skip to it
|
|
|
|
# No token that's between this token and this head could be our
|
|
|
|
# child.
|
|
|
|
if (ptr.head >= 1) and (ptr + ptr.head) < target:
|
|
|
|
ptr += ptr.head
|
|
|
|
|
|
|
|
elif ptr + ptr.head == target:
|
|
|
|
idx -= 1
|
|
|
|
if idx == 0:
|
|
|
|
return ptr
|
|
|
|
ptr += 1
|
|
|
|
else:
|
|
|
|
ptr += 1
|
|
|
|
return NULL
|
|
|
|
|
|
|
|
|
|
|
|
cdef const TokenC* _new_get_right(const State* s, const TokenC* target, int idx) nogil:
|
|
|
|
if idx < 1:
|
|
|
|
return NULL
|
|
|
|
cdef const TokenC* ptr = s.sent + (s.sent_len - 1)
|
|
|
|
while ptr > target:
|
|
|
|
# If this head is still to the right of us, we can skip to it
|
|
|
|
# No token that's between this token and this head could be our
|
|
|
|
# child.
|
|
|
|
if (ptr.head < 0) and ((ptr + ptr.head) > target):
|
|
|
|
ptr += ptr.head
|
|
|
|
elif ptr + ptr.head == target:
|
|
|
|
idx -= 1
|
|
|
|
if idx == 0:
|
|
|
|
return ptr
|
|
|
|
ptr -= 1
|
|
|
|
else:
|
|
|
|
ptr -= 1
|
|
|
|
return NULL
|