2015-06-02 21:01:06 +03:00
|
|
|
# cython: profile=True
|
2015-01-24 09:29:04 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-06-09 02:41:09 +03:00
|
|
|
import ctypes
|
|
|
|
import os
|
|
|
|
|
2014-12-16 14:44:43 +03:00
|
|
|
from ._state cimport State
|
2015-05-11 17:12:03 +03:00
|
|
|
from ._state cimport has_head, get_idx, get_s0, get_n0, get_left, get_right
|
2014-12-16 14:44:43 +03:00
|
|
|
from ._state cimport is_final, at_eol, pop_stack, push_stack, add_dep
|
|
|
|
from ._state cimport head_in_buffer, children_in_buffer
|
|
|
|
from ._state cimport head_in_stack, children_in_stack
|
2015-05-11 17:12:03 +03:00
|
|
|
from ._state cimport count_left_kids
|
2014-12-16 14:44:43 +03:00
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
from ..structs cimport TokenC
|
|
|
|
|
2015-02-21 19:06:37 +03:00
|
|
|
from .transition_system cimport do_func_t, get_cost_func_t
|
2015-06-07 04:21:29 +03:00
|
|
|
from .transition_system cimport move_cost_func_t, label_cost_func_t
|
2015-05-24 22:35:02 +03:00
|
|
|
from ..gold cimport GoldParse
|
2015-06-02 21:01:06 +03:00
|
|
|
from ..gold cimport GoldParseC
|
2015-02-21 19:06:37 +03:00
|
|
|
|
2015-06-09 02:41:09 +03:00
|
|
|
from libc.stdint cimport uint32_t
|
|
|
|
from libc.string cimport memcpy
|
|
|
|
|
|
|
|
from cymem.cymem cimport Pool
|
2015-06-09 22:20:14 +03:00
|
|
|
from .stateclass cimport StateClass
|
2015-06-09 02:41:09 +03:00
|
|
|
|
2015-01-27 19:09:45 +03:00
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
DEF NON_MONOTONIC = True
|
2015-01-27 19:09:45 +03:00
|
|
|
DEF USE_BREAK = True
|
2014-12-16 14:44:43 +03:00
|
|
|
|
2015-02-21 19:06:37 +03:00
|
|
|
cdef weight_t MIN_SCORE = -90000
|
2014-12-16 14:44:43 +03:00
|
|
|
|
2015-02-21 19:06:37 +03:00
|
|
|
# Break transition from here
|
|
|
|
# http://www.aclweb.org/anthology/P13-1074
|
2014-12-16 14:44:43 +03:00
|
|
|
cdef enum:
|
|
|
|
SHIFT
|
|
|
|
REDUCE
|
|
|
|
LEFT
|
|
|
|
RIGHT
|
2015-05-11 17:12:03 +03:00
|
|
|
|
2015-01-28 22:22:03 +03:00
|
|
|
BREAK
|
2015-05-11 17:12:03 +03:00
|
|
|
|
2014-12-16 14:44:43 +03:00
|
|
|
N_MOVES
|
|
|
|
|
2015-05-11 17:12:03 +03:00
|
|
|
|
2015-03-09 14:06:01 +03:00
|
|
|
MOVE_NAMES = [None] * N_MOVES
|
|
|
|
MOVE_NAMES[SHIFT] = 'S'
|
|
|
|
MOVE_NAMES[REDUCE] = 'D'
|
|
|
|
MOVE_NAMES[LEFT] = 'L'
|
|
|
|
MOVE_NAMES[RIGHT] = 'R'
|
|
|
|
MOVE_NAMES[BREAK] = 'B'
|
|
|
|
|
2015-01-27 19:18:43 +03:00
|
|
|
|
2015-06-07 04:21:29 +03:00
|
|
|
# Helper functions for the arc-eager oracle
|
|
|
|
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int push_cost(StateClass stcls, const GoldParseC* gold, int target) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
cdef int cost = 0
|
2015-06-09 22:20:14 +03:00
|
|
|
cdef int i, S_i
|
|
|
|
for i in range(stcls.stack_depth()):
|
|
|
|
S_i = stcls.S(i)
|
|
|
|
if gold.heads[target] == S_i:
|
|
|
|
cost += 1
|
|
|
|
if gold.heads[S_i] == target and (NON_MONOTONIC or not stcls.has_head(S_i)):
|
|
|
|
cost += 1
|
2015-06-10 01:40:43 +03:00
|
|
|
cost += Break.is_valid(stcls, -1) and Break.move_cost(stcls, gold) == 0
|
2015-06-07 04:21:29 +03:00
|
|
|
return cost
|
|
|
|
|
|
|
|
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int pop_cost(StateClass stcls, const GoldParseC* gold, int target) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
cdef int cost = 0
|
2015-06-09 22:20:14 +03:00
|
|
|
cdef int i, B_i
|
|
|
|
for i in range(stcls.buffer_length()):
|
|
|
|
B_i = stcls.B(i)
|
|
|
|
cost += gold.heads[B_i] == target
|
|
|
|
cost += gold.heads[target] == B_i
|
|
|
|
if gold.heads[B_i] == B_i or gold.heads[B_i] < target:
|
|
|
|
break
|
2015-06-07 04:21:29 +03:00
|
|
|
return cost
|
|
|
|
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int arc_cost(StateClass stcls, const GoldParseC* gold, int head, int child) except -1:
|
2015-06-08 15:49:04 +03:00
|
|
|
if arc_is_gold(gold, head, child):
|
2015-06-07 04:21:29 +03:00
|
|
|
return 0
|
2015-06-09 22:20:14 +03:00
|
|
|
elif stcls.H(child) == gold.heads[child]:
|
2015-06-08 15:49:04 +03:00
|
|
|
return 1
|
2015-06-09 22:20:14 +03:00
|
|
|
elif gold.heads[child] >= stcls.B(0):
|
2015-06-08 15:49:04 +03:00
|
|
|
return 1
|
|
|
|
else:
|
2015-06-07 04:21:29 +03:00
|
|
|
return 0
|
2015-06-08 15:49:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef bint arc_is_gold(const GoldParseC* gold, int head, int child) except -1:
|
|
|
|
if gold.labels[child] == -1:
|
|
|
|
return True
|
|
|
|
elif _is_gold_root(gold, head) and _is_gold_root(gold, child):
|
|
|
|
return True
|
|
|
|
elif gold.heads[child] == head:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
cdef bint label_is_gold(const GoldParseC* gold, int head, int child, int label) except -1:
|
|
|
|
if gold.labels[child] == -1:
|
|
|
|
return True
|
|
|
|
elif label == -1:
|
|
|
|
return True
|
2015-06-07 04:21:29 +03:00
|
|
|
elif gold.labels[child] == label:
|
2015-06-08 15:49:04 +03:00
|
|
|
return True
|
2015-06-07 04:21:29 +03:00
|
|
|
else:
|
2015-06-08 15:49:04 +03:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
cdef bint _is_gold_root(const GoldParseC* gold, int word) except -1:
|
|
|
|
return gold.labels[word] == -1 or gold.heads[word] == word
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
cdef class Shift:
|
|
|
|
@staticmethod
|
2015-06-10 00:23:28 +03:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
2015-06-09 02:41:09 +03:00
|
|
|
return not st.eol()
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 02:35:28 +03:00
|
|
|
cdef int transition(StateClass state, int label) except -1:
|
2015-06-04 23:43:03 +03:00
|
|
|
# Set the dep label, in case we need it after we reduce
|
|
|
|
if NON_MONOTONIC:
|
2015-06-10 02:35:28 +03:00
|
|
|
state._sent[state.B(0)].dep = label
|
|
|
|
state.push()
|
2014-12-16 14:44:43 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int cost(StateClass st, const GoldParseC* gold, int label) except -1:
|
|
|
|
return Shift.move_cost(st, gold) + Shift.label_cost(st, gold, label)
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int move_cost(StateClass s, const GoldParseC* gold) except -1:
|
|
|
|
return push_cost(s, gold, s.B(0))
|
2015-02-21 19:06:37 +03:00
|
|
|
|
2015-06-07 04:21:29 +03:00
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int label_cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
return 0
|
|
|
|
|
2015-02-21 19:06:37 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
cdef class Reduce:
|
2015-06-09 02:41:09 +03:00
|
|
|
@staticmethod
|
2015-06-10 00:23:28 +03:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
2015-06-09 02:41:09 +03:00
|
|
|
if NON_MONOTONIC:
|
|
|
|
return st.stack_depth() >= 2 #and not missing_brackets(s)
|
|
|
|
else:
|
|
|
|
return st.stack_depth() >= 2 and st.has_head(st.S(0))
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 02:35:28 +03:00
|
|
|
cdef int transition(StateClass st, int label) except -1:
|
|
|
|
if NON_MONOTONIC and not st.has_head(st.S(0)) and st.stack_depth() >= 2:
|
|
|
|
st.add_arc(st.S(1), st.S(0), st.S_(0).dep)
|
|
|
|
st.pop()
|
2015-06-04 23:43:03 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
return Reduce.move_cost(s, gold) + Reduce.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int move_cost(StateClass s, const GoldParseC* gold) except -1:
|
2015-06-04 23:43:03 +03:00
|
|
|
if NON_MONOTONIC:
|
2015-06-10 01:40:43 +03:00
|
|
|
return pop_cost(s, gold, s.S(0))
|
2015-06-07 04:21:29 +03:00
|
|
|
else:
|
2015-06-10 01:40:43 +03:00
|
|
|
return children_in_buffer(s, s.S(0), gold.heads)
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int label_cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
return 0
|
|
|
|
|
2015-01-27 19:09:45 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
cdef class LeftArc:
|
2015-06-09 02:41:09 +03:00
|
|
|
@staticmethod
|
2015-06-10 00:23:28 +03:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
2015-06-09 02:41:09 +03:00
|
|
|
if NON_MONOTONIC:
|
|
|
|
return st.stack_depth() >= 1 #and not missing_brackets(s)
|
|
|
|
else:
|
|
|
|
return st.stack_depth() >= 1 and not st.has_head(st.S(0))
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 02:35:28 +03:00
|
|
|
cdef int transition(StateClass st, int label) except -1:
|
2015-06-04 23:43:03 +03:00
|
|
|
# Interpret left-arcs from EOL as attachment to root
|
2015-06-10 02:35:28 +03:00
|
|
|
if st.eol():
|
|
|
|
st.add_arc(st.S(0), st.S(0), label)
|
2015-06-04 23:43:03 +03:00
|
|
|
else:
|
2015-06-10 02:35:28 +03:00
|
|
|
st.add_arc(st.B(0), st.S(0), label)
|
|
|
|
st.pop()
|
2015-06-04 23:43:03 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
return LeftArc.move_cost(s, gold) + LeftArc.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int move_cost(StateClass s, const GoldParseC* gold) except -1:
|
|
|
|
if arc_is_gold(gold, s.B(0), s.S(0)):
|
2015-06-08 15:49:04 +03:00
|
|
|
return 0
|
|
|
|
else:
|
2015-06-10 01:40:43 +03:00
|
|
|
return pop_cost(s, gold, s.S(0)) + arc_cost(s, gold, s.B(0), s.S(0))
|
2014-12-16 14:44:43 +03:00
|
|
|
|
2015-06-07 04:21:29 +03:00
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int label_cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
|
|
|
return arc_is_gold(gold, s.B(0), s.S(0)) and not label_is_gold(gold, s.B(0), s.S(0), label)
|
2015-06-07 04:21:29 +03:00
|
|
|
|
2015-01-27 19:09:45 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
cdef class RightArc:
|
|
|
|
@staticmethod
|
2015-06-10 00:23:28 +03:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
2015-06-09 02:41:09 +03:00
|
|
|
return st.stack_depth() >= 1 and not st.eol()
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 02:35:28 +03:00
|
|
|
cdef int transition(StateClass st, int label) except -1:
|
|
|
|
st.add_arc(st.S(0), st.B(0), label)
|
|
|
|
st.push()
|
2015-02-21 19:06:37 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
return RightArc.move_cost(s, gold) + RightArc.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int move_cost(StateClass s, const GoldParseC* gold) except -1:
|
|
|
|
if arc_is_gold(gold, s.S(0), s.B(0)):
|
2015-06-08 15:49:04 +03:00
|
|
|
return 0
|
|
|
|
else:
|
2015-06-10 01:40:43 +03:00
|
|
|
return push_cost(s, gold, s.B(0)) + arc_cost(s, gold, s.S(0), s.B(0))
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int label_cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
|
|
|
return arc_is_gold(gold, s.S(0), s.B(0)) and not label_is_gold(gold, s.S(0), s.B(0), label)
|
2015-02-21 19:06:37 +03:00
|
|
|
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
cdef class Break:
|
2015-06-09 02:41:09 +03:00
|
|
|
@staticmethod
|
2015-06-10 00:23:28 +03:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
2015-06-09 02:41:09 +03:00
|
|
|
cdef int i
|
|
|
|
if not USE_BREAK:
|
|
|
|
return False
|
|
|
|
elif st.eol():
|
|
|
|
return False
|
|
|
|
elif st.stack_depth() < 1:
|
|
|
|
return False
|
|
|
|
elif NON_MONOTONIC:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# In the Break transition paper, they have this constraint that prevents
|
|
|
|
# Break if stack is disconnected. But, if we're doing non-monotonic parsing,
|
|
|
|
# we prefer to relax this constraint. This is helpful in parsing whole
|
|
|
|
# documents, because then we don't get stuck with words on the stack.
|
|
|
|
seen_headless = False
|
|
|
|
for i in range(st.stack_depth()):
|
|
|
|
if not st.has_head(st.S(i)):
|
|
|
|
if seen_headless:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
seen_headless = True
|
|
|
|
# TODO: Constituency constraints
|
|
|
|
return True
|
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2015-06-10 02:35:28 +03:00
|
|
|
cdef int transition(StateClass st, int label) except -1:
|
|
|
|
st.set_sent_end(st.B(0)-1)
|
|
|
|
while not st.empty():
|
|
|
|
if not st.has_head(st.S(0)):
|
|
|
|
st._sent[st.S(0)].dep = label
|
|
|
|
st.pop()
|
2015-06-04 23:43:03 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-10 00:23:28 +03:00
|
|
|
return Break.move_cost(s, gold) + Break.label_cost(s, gold, label)
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int move_cost(StateClass s, const GoldParseC* gold) except -1:
|
2015-06-04 23:43:03 +03:00
|
|
|
# When we break, we Reduce all of the words on the stack.
|
|
|
|
cdef int cost = 0
|
|
|
|
# Number of deps between S0...Sn and N0...Nn
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int i, B_i, S_i
|
|
|
|
for i in range(s.buffer_length()):
|
|
|
|
B_i = s.B(i)
|
|
|
|
for j in range(s.stack_depth()):
|
|
|
|
S_i = s.S(j)
|
|
|
|
cost += gold.heads[B_i] == S_i
|
|
|
|
cost += gold.heads[S_i] == B_i
|
2015-06-04 23:43:03 +03:00
|
|
|
return cost
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int label_cost(StateClass s, const GoldParseC* gold, int label) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
return 0
|
2015-02-21 19:06:37 +03:00
|
|
|
|
|
|
|
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef class ArcEager(TransitionSystem):
|
|
|
|
@classmethod
|
|
|
|
def get_labels(cls, gold_parses):
|
|
|
|
move_labels = {SHIFT: {'': True}, REDUCE: {'': True}, RIGHT: {},
|
2015-06-10 00:23:28 +03:00
|
|
|
LEFT: {'ROOT': True}, BREAK: {'ROOT': True}}
|
2015-06-05 03:27:17 +03:00
|
|
|
for raw_text, sents in gold_parses:
|
|
|
|
for (ids, words, tags, heads, labels, iob), ctnts in sents:
|
|
|
|
for child, head, label in zip(ids, heads, labels):
|
|
|
|
if label != 'ROOT':
|
|
|
|
if head < child:
|
|
|
|
move_labels[RIGHT][label] = True
|
|
|
|
elif head > child:
|
|
|
|
move_labels[LEFT][label] = True
|
|
|
|
return move_labels
|
|
|
|
|
|
|
|
cdef int preprocess_gold(self, GoldParse gold) except -1:
|
|
|
|
for i in range(gold.length):
|
|
|
|
if gold.heads[i] is None: # Missing values
|
|
|
|
gold.c.heads[i] = i
|
|
|
|
gold.c.labels[i] = -1
|
|
|
|
else:
|
|
|
|
gold.c.heads[i] = gold.heads[i]
|
|
|
|
gold.c.labels[i] = self.strings[gold.labels[i]]
|
|
|
|
for end, brackets in gold.brackets.items():
|
|
|
|
for start, label_strs in brackets.items():
|
|
|
|
gold.c.brackets[start][end] = 1
|
|
|
|
for label_str in label_strs:
|
|
|
|
# Add the encoded label to the set
|
|
|
|
gold.brackets[end][start].add(self.strings[label_str])
|
|
|
|
|
|
|
|
cdef Transition lookup_transition(self, object name) except *:
|
|
|
|
if '-' in name:
|
|
|
|
move_str, label_str = name.split('-', 1)
|
|
|
|
label = self.label_ids[label_str]
|
|
|
|
else:
|
|
|
|
label = 0
|
|
|
|
move = MOVE_NAMES.index(move_str)
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
if self.c[i].move == move and self.c[i].label == label:
|
|
|
|
return self.c[i]
|
|
|
|
|
|
|
|
def move_name(self, int move, int label):
|
|
|
|
label_str = self.strings[label]
|
|
|
|
if label_str:
|
|
|
|
return MOVE_NAMES[move] + '-' + label_str
|
|
|
|
else:
|
|
|
|
return MOVE_NAMES[move]
|
|
|
|
|
|
|
|
cdef Transition init_transition(self, int clas, int move, int label) except *:
|
|
|
|
# TODO: Apparent Cython bug here when we try to use the Transition()
|
|
|
|
# constructor with the function pointers
|
|
|
|
cdef Transition t
|
|
|
|
t.score = 0
|
|
|
|
t.clas = clas
|
|
|
|
t.move = move
|
|
|
|
t.label = label
|
|
|
|
if move == SHIFT:
|
|
|
|
t.is_valid = Shift.is_valid
|
|
|
|
t.do = Shift.transition
|
|
|
|
t.get_cost = Shift.cost
|
|
|
|
elif move == REDUCE:
|
|
|
|
t.is_valid = Reduce.is_valid
|
|
|
|
t.do = Reduce.transition
|
|
|
|
t.get_cost = Reduce.cost
|
|
|
|
elif move == LEFT:
|
|
|
|
t.is_valid = LeftArc.is_valid
|
|
|
|
t.do = LeftArc.transition
|
|
|
|
t.get_cost = LeftArc.cost
|
|
|
|
elif move == RIGHT:
|
|
|
|
t.is_valid = RightArc.is_valid
|
|
|
|
t.do = RightArc.transition
|
|
|
|
t.get_cost = RightArc.cost
|
|
|
|
elif move == BREAK:
|
|
|
|
t.is_valid = Break.is_valid
|
|
|
|
t.do = Break.transition
|
|
|
|
t.get_cost = Break.cost
|
|
|
|
else:
|
|
|
|
raise Exception(move)
|
|
|
|
return t
|
|
|
|
|
2015-06-10 05:20:23 +03:00
|
|
|
cdef int initialize_state(self, StateClass st) except -1:
|
|
|
|
st.push()
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2015-06-10 02:35:28 +03:00
|
|
|
cdef int finalize_state(self, StateClass st) except -1:
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef int root_label = self.strings['ROOT']
|
2015-06-10 02:35:28 +03:00
|
|
|
for i in range(st.length):
|
|
|
|
if st._sent[i].head == 0 and st._sent[i].dep == 0:
|
|
|
|
st._sent[i].dep = root_label
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2015-06-10 00:23:28 +03:00
|
|
|
cdef int set_valid(self, bint* output, StateClass stcls) except -1:
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef bint[N_MOVES] is_valid
|
2015-06-10 00:23:28 +03:00
|
|
|
is_valid[SHIFT] = Shift.is_valid(stcls, -1)
|
|
|
|
is_valid[REDUCE] = Reduce.is_valid(stcls, -1)
|
|
|
|
is_valid[LEFT] = LeftArc.is_valid(stcls, -1)
|
|
|
|
is_valid[RIGHT] = RightArc.is_valid(stcls, -1)
|
|
|
|
is_valid[BREAK] = Break.is_valid(stcls, -1)
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef int i
|
2015-06-10 05:20:23 +03:00
|
|
|
n_valid = 0
|
2015-06-05 03:27:17 +03:00
|
|
|
for i in range(self.n_moves):
|
|
|
|
output[i] = is_valid[self.c[i].move]
|
2015-06-10 05:20:23 +03:00
|
|
|
n_valid += output[i]
|
|
|
|
assert n_valid >= 1
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2015-06-10 01:40:43 +03:00
|
|
|
cdef int set_costs(self, int* output, StateClass stcls, GoldParse gold) except -1:
|
2015-06-07 04:21:29 +03:00
|
|
|
cdef int i, move, label
|
|
|
|
cdef label_cost_func_t[N_MOVES] label_cost_funcs
|
|
|
|
cdef move_cost_func_t[N_MOVES] move_cost_funcs
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef int[N_MOVES] move_costs
|
2015-06-07 04:21:29 +03:00
|
|
|
for i in range(N_MOVES):
|
|
|
|
move_costs[i] = -1
|
|
|
|
move_cost_funcs[SHIFT] = Shift.move_cost
|
|
|
|
move_cost_funcs[REDUCE] = Reduce.move_cost
|
|
|
|
move_cost_funcs[LEFT] = LeftArc.move_cost
|
|
|
|
move_cost_funcs[RIGHT] = RightArc.move_cost
|
|
|
|
move_cost_funcs[BREAK] = Break.move_cost
|
|
|
|
|
|
|
|
label_cost_funcs[SHIFT] = Shift.label_cost
|
|
|
|
label_cost_funcs[REDUCE] = Reduce.label_cost
|
|
|
|
label_cost_funcs[LEFT] = LeftArc.label_cost
|
|
|
|
label_cost_funcs[RIGHT] = RightArc.label_cost
|
|
|
|
label_cost_funcs[BREAK] = Break.label_cost
|
|
|
|
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef int* labels = gold.c.labels
|
|
|
|
cdef int* heads = gold.c.heads
|
2015-06-10 00:23:28 +03:00
|
|
|
|
|
|
|
self.set_valid(self._is_valid, stcls)
|
2015-06-10 05:20:23 +03:00
|
|
|
n_gold = 0
|
2015-06-10 00:23:28 +03:00
|
|
|
for i in range(self.n_moves):
|
|
|
|
if not self._is_valid[i]:
|
|
|
|
output[i] = 9000
|
|
|
|
else:
|
|
|
|
move = self.c[i].move
|
|
|
|
label = self.c[i].label
|
|
|
|
if move_costs[move] == -1:
|
2015-06-10 01:40:43 +03:00
|
|
|
move_costs[move] = move_cost_funcs[move](stcls, &gold.c)
|
|
|
|
output[i] = move_costs[move] + label_cost_funcs[move](stcls, &gold.c, label)
|
2015-06-10 05:20:23 +03:00
|
|
|
n_gold += output[i] == 0
|
|
|
|
assert n_gold >= 1
|
2015-06-10 00:23:28 +03:00
|
|
|
|
|
|
|
cdef Transition best_valid(self, const weight_t* scores, StateClass stcls) except *:
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef bint[N_MOVES] is_valid
|
2015-06-10 00:23:28 +03:00
|
|
|
is_valid[SHIFT] = Shift.is_valid(stcls, -1)
|
|
|
|
is_valid[REDUCE] = Reduce.is_valid(stcls, -1)
|
|
|
|
is_valid[LEFT] = LeftArc.is_valid(stcls, -1)
|
|
|
|
is_valid[RIGHT] = RightArc.is_valid(stcls, -1)
|
|
|
|
is_valid[BREAK] = Break.is_valid(stcls, -1)
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef Transition best
|
|
|
|
cdef weight_t score = MIN_SCORE
|
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
if scores[i] > score and is_valid[self.c[i].move]:
|
|
|
|
best = self.c[i]
|
|
|
|
score = scores[i]
|
|
|
|
assert best.clas < self.n_moves
|
|
|
|
assert score > MIN_SCORE
|
|
|
|
# Label Shift moves with the best Right-Arc label, for non-monotonic
|
|
|
|
# actions
|
|
|
|
if best.move == SHIFT:
|
|
|
|
score = MIN_SCORE
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
if self.c[i].move == RIGHT and scores[i] > score:
|
|
|
|
best.label = self.c[i].label
|
|
|
|
score = scores[i]
|
|
|
|
return best
|