mirror of https://github.com/explosion/spaCy.git
* Refactoring arc_eager, grouping oracle functions into transitions
This commit is contained in:
parent
4433396005
commit
36a34d544b
|
@ -180,7 +180,6 @@ cdef class ArcEager(TransitionSystem):
|
|||
label = labels[s.stack[0]]
|
||||
output[i] += move.label != label and label != -1
|
||||
|
||||
|
||||
cdef Transition best_valid(self, const weight_t* scores, const State* s) except *:
|
||||
cdef bint[N_MOVES] is_valid
|
||||
is_valid[SHIFT] = _can_shift(s)
|
||||
|
@ -210,44 +209,20 @@ cdef class ArcEager(TransitionSystem):
|
|||
return best
|
||||
|
||||
|
||||
cdef int _do_shift(const Transition* self, State* state) except -1:
|
||||
cdef class Shift:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
return not at_eol(s)
|
||||
|
||||
@staticmethod
|
||||
cdef int transition(State* state, int label) except -1:
|
||||
# Set the dep label, in case we need it after we reduce
|
||||
if NON_MONOTONIC:
|
||||
state.sent[state.i].dep = self.label
|
||||
state.sent[state.i].dep = label
|
||||
push_stack(state)
|
||||
|
||||
|
||||
cdef int _do_left(const Transition* self, State* state) except -1:
|
||||
# Interpret left-arcs from EOL as attachment to root
|
||||
if at_eol(state):
|
||||
add_dep(state, state.stack[0], state.stack[0], self.label)
|
||||
else:
|
||||
add_dep(state, state.i, state.stack[0], self.label)
|
||||
pop_stack(state)
|
||||
|
||||
|
||||
cdef int _do_right(const Transition* self, State* state) except -1:
|
||||
add_dep(state, state.stack[0], state.i, self.label)
|
||||
push_stack(state)
|
||||
|
||||
|
||||
cdef int _do_reduce(const Transition* self, State* state) except -1:
|
||||
if NON_MONOTONIC and not has_head(get_s0(state)):
|
||||
add_dep(state, state.stack[-1], state.stack[0], get_s0(state).dep)
|
||||
pop_stack(state)
|
||||
|
||||
|
||||
cdef int _do_break(const Transition* self, State* state) except -1:
|
||||
state.sent[state.i-1].sent_end = True
|
||||
while state.stack_len != 0:
|
||||
if get_s0(state).head == 0:
|
||||
get_s0(state).dep = self.label
|
||||
state.stack -= 1
|
||||
state.stack_len -= 1
|
||||
if not at_eol(state):
|
||||
push_stack(state)
|
||||
|
||||
cdef int _shift_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not _can_shift(s):
|
||||
return 9000
|
||||
cost = 0
|
||||
|
@ -258,22 +233,51 @@ cdef int _shift_cost(const Transition* self, const State* s, GoldParseC* gold) e
|
|||
cost += 1
|
||||
return cost
|
||||
|
||||
cdef int _right_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
if not _can_right(s):
|
||||
|
||||
cdef class Reduce:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
if NON_MONOTONIC:
|
||||
return s.stack_len >= 2 #and not missing_brackets(s)
|
||||
else:
|
||||
return s.stack_len >= 2 and has_head(get_s0(s))
|
||||
|
||||
@staticmethod
|
||||
cdef int transition(State* state, int label) except -1:
|
||||
if NON_MONOTONIC and not has_head(get_s0(state)):
|
||||
add_dep(state, state.stack[-1], state.stack[0], get_s0(state).dep)
|
||||
pop_stack(state)
|
||||
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not Reduce.is_valid(s):
|
||||
return 9000
|
||||
cost = 0
|
||||
if gold.heads[s.i] == s.stack[0]:
|
||||
cost += self.label != -1 and self.label != gold.labels[s.i]
|
||||
return cost
|
||||
# This indicates missing head
|
||||
if gold.labels[s.i] != -1:
|
||||
cost += head_in_buffer(s, s.i, gold.heads)
|
||||
cost += children_in_stack(s, s.i, gold.heads)
|
||||
cost += head_in_stack(s, s.i, gold.heads)
|
||||
cdef int cost = 0
|
||||
cost += children_in_buffer(s, s.stack[0], gold.heads)
|
||||
if NON_MONOTONIC:
|
||||
cost += head_in_buffer(s, s.stack[0], gold.heads)
|
||||
return cost
|
||||
|
||||
|
||||
cdef int _left_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
cdef class LeftArc:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
if NON_MONOTONIC:
|
||||
return s.stack_len >= 1 #and not missing_brackets(s)
|
||||
else:
|
||||
return s.stack_len >= 1 and not has_head(get_s0(s))
|
||||
|
||||
@staticmethod
|
||||
cdef int transition(State* state, int label) except -1:
|
||||
# Interpret left-arcs from EOL as attachment to root
|
||||
if at_eol(state):
|
||||
add_dep(state, state.stack[0], state.stack[0], label)
|
||||
else:
|
||||
add_dep(state, state.i, state.stack[0], label)
|
||||
pop_stack(state)
|
||||
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not _can_left(s):
|
||||
return 9000
|
||||
cost = 0
|
||||
|
@ -288,9 +292,8 @@ cdef int _left_cost(const Transition* self, const State* s, GoldParseC* gold) ex
|
|||
if _can_reduce(s) or _can_break(s):
|
||||
cost += gold.heads[s.stack[0]] != s.stack[0]
|
||||
# Are we labelling correctly?
|
||||
cost += self.label != -1 and self.label != gold.labels[s.stack[0]]
|
||||
cost += label != -1 and label != gold.labels[s.stack[0]]
|
||||
return cost
|
||||
|
||||
cost += head_in_buffer(s, s.stack[0], gold.heads)
|
||||
cost += children_in_buffer(s, s.stack[0], gold.heads)
|
||||
if NON_MONOTONIC and s.stack_len >= 2:
|
||||
|
@ -300,50 +303,35 @@ cdef int _left_cost(const Transition* self, const State* s, GoldParseC* gold) ex
|
|||
return cost
|
||||
|
||||
|
||||
cdef int _reduce_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
if not _can_reduce(s):
|
||||
return 9000
|
||||
cdef int cost = 0
|
||||
cost += children_in_buffer(s, s.stack[0], gold.heads)
|
||||
if NON_MONOTONIC:
|
||||
cost += head_in_buffer(s, s.stack[0], gold.heads)
|
||||
return cost
|
||||
|
||||
|
||||
cdef int _break_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
if not _can_break(s):
|
||||
return 9000
|
||||
# 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
|
||||
for i in range(s.i, s.sent_len):
|
||||
cost += children_in_stack(s, i, gold.heads)
|
||||
cost += head_in_stack(s, i, gold.heads)
|
||||
return cost
|
||||
|
||||
|
||||
cdef inline bint _can_shift(const State* s) nogil:
|
||||
return not at_eol(s)
|
||||
|
||||
|
||||
cdef inline bint _can_right(const State* s) nogil:
|
||||
cdef class RightArc:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
return s.stack_len >= 1 and not at_eol(s)
|
||||
|
||||
@staticmethod
|
||||
cdef int transition(State* state, int label) except -1:
|
||||
add_dep(state, state.stack[0], state.i, label)
|
||||
push_stack(state)
|
||||
|
||||
cdef inline bint _can_left(const State* s) nogil:
|
||||
if NON_MONOTONIC:
|
||||
return s.stack_len >= 1 #and not missing_brackets(s)
|
||||
else:
|
||||
return s.stack_len >= 1 and not has_head(get_s0(s))
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not RightArc.is_valid(s):
|
||||
return 9000
|
||||
cost = 0
|
||||
if gold.heads[s.i] == s.stack[0]:
|
||||
cost += label != -1 and self.label != gold.labels[s.i]
|
||||
return cost
|
||||
# This indicates missing head
|
||||
if gold.labels[s.i] != -1:
|
||||
cost += head_in_buffer(s, s.i, gold.heads)
|
||||
cost += children_in_stack(s, s.i, gold.heads)
|
||||
cost += head_in_stack(s, s.i, gold.heads)
|
||||
return cost
|
||||
|
||||
|
||||
cdef inline bint _can_reduce(const State* s) nogil:
|
||||
if NON_MONOTONIC:
|
||||
return s.stack_len >= 2 #and not missing_brackets(s)
|
||||
else:
|
||||
return s.stack_len >= 2 and has_head(get_s0(s))
|
||||
|
||||
cdef inline bint _can_break(const State* s) nogil:
|
||||
cdef class Break:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
cdef int i
|
||||
if not USE_BREAK:
|
||||
return False
|
||||
|
@ -366,7 +354,33 @@ cdef inline bint _can_break(const State* s) nogil:
|
|||
# TODO: Constituency constraints
|
||||
return True
|
||||
|
||||
cdef inline bint _can_constituent(const State* s) nogil:
|
||||
@staticmethod
|
||||
cdef int transition(State* state, int label) except -1:
|
||||
state.sent[state.i-1].sent_end = True
|
||||
while state.stack_len != 0:
|
||||
if get_s0(state).head == 0:
|
||||
get_s0(state).dep = label
|
||||
state.stack -= 1
|
||||
state.stack_len -= 1
|
||||
if not at_eol(state):
|
||||
push_stack(state)
|
||||
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not Break.is_valid(s):
|
||||
return 9000
|
||||
# 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
|
||||
for i in range(s.i, s.sent_len):
|
||||
cost += children_in_stack(s, i, gold.heads)
|
||||
cost += head_in_stack(s, i, gold.heads)
|
||||
return cost
|
||||
|
||||
|
||||
cdef class Constituent:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
if s.stack_len < 1:
|
||||
return False
|
||||
return False
|
||||
|
@ -378,23 +392,30 @@ cdef inline bint _can_constituent(const State* s) nogil:
|
|||
# else:
|
||||
# return False
|
||||
|
||||
cdef inline bint _can_adjust(const State* s) nogil:
|
||||
@staticmethod
|
||||
cdef int transition(State* state, int label) except -1:
|
||||
return False
|
||||
#if s.ctnts.stack_len < 2:
|
||||
# return False
|
||||
#cdef Constituent* bracket = new_bracket(state.ctnts)
|
||||
|
||||
#cdef const Constituent* b1 = s.ctnts.stack[-1]
|
||||
#cdef const Constituent* b0 = s.ctnts.stack[0]
|
||||
#bracket.parent = NULL
|
||||
#bracket.label = self.label
|
||||
#bracket.head = get_s0(state)
|
||||
#bracket.length = 0
|
||||
|
||||
#if (b1.head + b1.head.head) != b0.head:
|
||||
# return False
|
||||
#elif b0.head >= b1.head:
|
||||
# return False
|
||||
#elif b0 >= b1:
|
||||
# return False
|
||||
#attach(bracket, state.ctnts.stack)
|
||||
# Attach rightward children. They're in the brackets array somewhere
|
||||
# between here and B0.
|
||||
#cdef Constituent* node
|
||||
#cdef const TokenC* node_gov
|
||||
#for i in range(1, bracket - state.ctnts.stack):
|
||||
# node = bracket - i
|
||||
# node_gov = node.head + node.head.head
|
||||
# if node_gov == bracket.head:
|
||||
# attach(bracket, node)
|
||||
|
||||
cdef int _constituent_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
if not _can_constituent(s):
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not Constituent.is_valid(s):
|
||||
return 9000
|
||||
raise Exception("Constituent move should be disabled currently")
|
||||
# The gold standard is indexed by end, then by start, then a set of labels
|
||||
|
@ -421,8 +442,42 @@ cdef int _constituent_cost(const Transition* self, const State* s, GoldParseC* g
|
|||
# loss = 1 # If we see the start position, set loss to 1
|
||||
#return loss
|
||||
|
||||
cdef int _adjust_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
||||
if not _can_adjust(s):
|
||||
|
||||
cdef class Adjust:
|
||||
@staticmethod
|
||||
cdef inline bint is_valid(const State* s) nogil:
|
||||
return False
|
||||
#if s.ctnts.stack_len < 2:
|
||||
# return False
|
||||
|
||||
#cdef const Constituent* b1 = s.ctnts.stack[-1]
|
||||
#cdef const Constituent* b0 = s.ctnts.stack[0]
|
||||
|
||||
#if (b1.head + b1.head.head) != b0.head:
|
||||
# return False
|
||||
#elif b0.head >= b1.head:
|
||||
# return False
|
||||
#elif b0 >= b1:
|
||||
# return False
|
||||
|
||||
@staticmethod
|
||||
cdef int transition(State* state) except -1:
|
||||
return False
|
||||
#cdef Constituent* b0 = state.ctnts.stack[0]
|
||||
#cdef Constituent* b1 = state.ctnts.stack[1]
|
||||
|
||||
#assert (b1.head + b1.head.head) == b0.head
|
||||
#assert b0.head < b1.head
|
||||
#assert b0 < b1
|
||||
|
||||
#attach(b0, b1)
|
||||
## Pop B1 from stack, but keep B0 on top
|
||||
#state.ctnts.stack -= 1
|
||||
#state.ctnts.stack[0] = b0
|
||||
|
||||
@staticmethod
|
||||
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||
if not Adjust.is_valid(s):
|
||||
return 9000
|
||||
raise Exception("Adjust move should be disabled currently")
|
||||
# The gold standard is indexed by end, then by start, then a set of labels
|
||||
|
@ -453,37 +508,3 @@ cdef int _adjust_cost(const Transition* self, const State* s, GoldParseC* gold)
|
|||
#return loss
|
||||
|
||||
|
||||
cdef int _do_constituent(const Transition* self, State* state) except -1:
|
||||
return False
|
||||
#cdef Constituent* bracket = new_bracket(state.ctnts)
|
||||
|
||||
#bracket.parent = NULL
|
||||
#bracket.label = self.label
|
||||
#bracket.head = get_s0(state)
|
||||
#bracket.length = 0
|
||||
|
||||
#attach(bracket, state.ctnts.stack)
|
||||
# Attach rightward children. They're in the brackets array somewhere
|
||||
# between here and B0.
|
||||
#cdef Constituent* node
|
||||
#cdef const TokenC* node_gov
|
||||
#for i in range(1, bracket - state.ctnts.stack):
|
||||
# node = bracket - i
|
||||
# node_gov = node.head + node.head.head
|
||||
# if node_gov == bracket.head:
|
||||
# attach(bracket, node)
|
||||
|
||||
|
||||
cdef int _do_adjust(const Transition* self, State* state) except -1:
|
||||
return False
|
||||
#cdef Constituent* b0 = state.ctnts.stack[0]
|
||||
#cdef Constituent* b1 = state.ctnts.stack[1]
|
||||
|
||||
#assert (b1.head + b1.head.head) == b0.head
|
||||
#assert b0.head < b1.head
|
||||
#assert b0 < b1
|
||||
|
||||
#attach(b0, b1)
|
||||
## Pop B1 from stack, but keep B0 on top
|
||||
#state.ctnts.stack -= 1
|
||||
#state.ctnts.stack[0] = b0
|
||||
|
|
Loading…
Reference in New Issue