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]]
|
label = labels[s.stack[0]]
|
||||||
output[i] += move.label != label and label != -1
|
output[i] += move.label != label and label != -1
|
||||||
|
|
||||||
|
|
||||||
cdef Transition best_valid(self, const weight_t* scores, const State* s) except *:
|
cdef Transition best_valid(self, const weight_t* scores, const State* s) except *:
|
||||||
cdef bint[N_MOVES] is_valid
|
cdef bint[N_MOVES] is_valid
|
||||||
is_valid[SHIFT] = _can_shift(s)
|
is_valid[SHIFT] = _can_shift(s)
|
||||||
|
@ -210,166 +209,181 @@ cdef class ArcEager(TransitionSystem):
|
||||||
return best
|
return best
|
||||||
|
|
||||||
|
|
||||||
cdef int _do_shift(const Transition* self, State* state) except -1:
|
cdef class Shift:
|
||||||
# Set the dep label, in case we need it after we reduce
|
@staticmethod
|
||||||
if NON_MONOTONIC:
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
state.sent[state.i].dep = self.label
|
return not at_eol(s)
|
||||||
push_stack(state)
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
cdef int _do_left(const Transition* self, State* state) except -1:
|
cdef int transition(State* state, int label) except -1:
|
||||||
# Interpret left-arcs from EOL as attachment to root
|
# Set the dep label, in case we need it after we reduce
|
||||||
if at_eol(state):
|
if NON_MONOTONIC:
|
||||||
add_dep(state, state.stack[0], state.stack[0], self.label)
|
state.sent[state.i].dep = 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)
|
push_stack(state)
|
||||||
|
|
||||||
cdef int _shift_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
@staticmethod
|
||||||
if not _can_shift(s):
|
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||||
return 9000
|
if not _can_shift(s):
|
||||||
cost = 0
|
return 9000
|
||||||
cost += head_in_stack(s, s.i, gold.heads)
|
cost = 0
|
||||||
cost += children_in_stack(s, s.i, gold.heads)
|
cost += head_in_stack(s, s.i, gold.heads)
|
||||||
# If we can break, and there's no cost to doing so, we should
|
cost += children_in_stack(s, s.i, gold.heads)
|
||||||
if _can_break(s) and _break_cost(self, s, gold) == 0:
|
# If we can break, and there's no cost to doing so, we should
|
||||||
cost += 1
|
if _can_break(s) and _break_cost(self, s, gold) == 0:
|
||||||
return cost
|
cost += 1
|
||||||
|
|
||||||
cdef int _right_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
|
||||||
if not _can_right(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)
|
|
||||||
return cost
|
|
||||||
|
|
||||||
|
|
||||||
cdef int _left_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
|
||||||
if not _can_left(s):
|
|
||||||
return 9000
|
|
||||||
cost = 0
|
|
||||||
if gold.heads[s.stack[0]] == s.i:
|
|
||||||
cost += self.label != -1 and self.label != gold.labels[s.stack[0]]
|
|
||||||
return cost
|
|
||||||
# If we're at EOL, then the left arc will add an arc to ROOT.
|
|
||||||
elif at_eol(s):
|
|
||||||
# Are we root?
|
|
||||||
if gold.labels[s.stack[0]] != -1:
|
|
||||||
# If we're at EOL, prefer to reduce or break over left-arc
|
|
||||||
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]]
|
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
cost += head_in_buffer(s, s.stack[0], gold.heads)
|
|
||||||
cost += children_in_buffer(s, s.stack[0], gold.heads)
|
cdef class Reduce:
|
||||||
if NON_MONOTONIC and s.stack_len >= 2:
|
@staticmethod
|
||||||
cost += gold.heads[s.stack[0]] == s.stack[-1]
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
if gold.labels[s.stack[0]] != -1:
|
if NON_MONOTONIC:
|
||||||
cost += gold.heads[s.stack[0]] == s.stack[0]
|
return s.stack_len >= 2 #and not missing_brackets(s)
|
||||||
return cost
|
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
|
||||||
|
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 _reduce_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
cdef class LeftArc:
|
||||||
if not _can_reduce(s):
|
@staticmethod
|
||||||
return 9000
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
cdef int cost = 0
|
if NON_MONOTONIC:
|
||||||
cost += children_in_buffer(s, s.stack[0], gold.heads)
|
return s.stack_len >= 1 #and not missing_brackets(s)
|
||||||
if NON_MONOTONIC:
|
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
|
||||||
|
if gold.heads[s.stack[0]] == s.i:
|
||||||
|
cost += self.label != -1 and self.label != gold.labels[s.stack[0]]
|
||||||
|
return cost
|
||||||
|
# If we're at EOL, then the left arc will add an arc to ROOT.
|
||||||
|
elif at_eol(s):
|
||||||
|
# Are we root?
|
||||||
|
if gold.labels[s.stack[0]] != -1:
|
||||||
|
# If we're at EOL, prefer to reduce or break over left-arc
|
||||||
|
if _can_reduce(s) or _can_break(s):
|
||||||
|
cost += gold.heads[s.stack[0]] != s.stack[0]
|
||||||
|
# Are we labelling correctly?
|
||||||
|
cost += label != -1 and label != gold.labels[s.stack[0]]
|
||||||
|
return cost
|
||||||
cost += head_in_buffer(s, s.stack[0], gold.heads)
|
cost += head_in_buffer(s, s.stack[0], gold.heads)
|
||||||
return cost
|
cost += children_in_buffer(s, s.stack[0], gold.heads)
|
||||||
|
if NON_MONOTONIC and s.stack_len >= 2:
|
||||||
|
cost += gold.heads[s.stack[0]] == s.stack[-1]
|
||||||
|
if gold.labels[s.stack[0]] != -1:
|
||||||
|
cost += gold.heads[s.stack[0]] == s.stack[0]
|
||||||
|
return cost
|
||||||
|
|
||||||
|
|
||||||
cdef int _break_cost(const Transition* self, const State* s, GoldParseC* gold) except -1:
|
cdef class RightArc:
|
||||||
if not _can_break(s):
|
@staticmethod
|
||||||
return 9000
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
# When we break, we Reduce all of the words on the stack.
|
return s.stack_len >= 1 and not at_eol(s)
|
||||||
cdef int cost = 0
|
|
||||||
# Number of deps between S0...Sn and N0...Nn
|
@staticmethod
|
||||||
for i in range(s.i, s.sent_len):
|
cdef int transition(State* state, int label) except -1:
|
||||||
cost += children_in_stack(s, i, gold.heads)
|
add_dep(state, state.stack[0], state.i, label)
|
||||||
cost += head_in_stack(s, i, gold.heads)
|
push_stack(state)
|
||||||
return cost
|
|
||||||
|
@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_shift(const State* s) nogil:
|
cdef class Break:
|
||||||
return not at_eol(s)
|
@staticmethod
|
||||||
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
|
cdef int i
|
||||||
|
if not USE_BREAK:
|
||||||
|
return False
|
||||||
|
elif at_eol(s):
|
||||||
|
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(s.stack_len):
|
||||||
|
if s.sent[s.stack[-i]].head == 0:
|
||||||
|
if seen_headless:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
seen_headless = True
|
||||||
|
# TODO: Constituency constraints
|
||||||
|
return True
|
||||||
|
|
||||||
|
@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 inline bint _can_right(const State* s) nogil:
|
cdef class Constituent:
|
||||||
return s.stack_len >= 1 and not at_eol(s)
|
@staticmethod
|
||||||
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
|
if s.stack_len < 1:
|
||||||
cdef inline bint _can_left(const State* s) nogil:
|
return False
|
||||||
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))
|
|
||||||
|
|
||||||
|
|
||||||
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 int i
|
|
||||||
if not USE_BREAK:
|
|
||||||
return False
|
return False
|
||||||
elif at_eol(s):
|
|
||||||
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(s.stack_len):
|
|
||||||
if s.sent[s.stack[-i]].head == 0:
|
|
||||||
if seen_headless:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
seen_headless = True
|
|
||||||
# TODO: Constituency constraints
|
|
||||||
return True
|
|
||||||
|
|
||||||
cdef inline bint _can_constituent(const State* s) nogil:
|
|
||||||
if s.stack_len < 1:
|
|
||||||
return False
|
|
||||||
return False
|
|
||||||
#else:
|
#else:
|
||||||
# # If all stack elements are popped, can't constituent
|
# # If all stack elements are popped, can't constituent
|
||||||
# for i in range(s.ctnts.stack_len):
|
# for i in range(s.ctnts.stack_len):
|
||||||
|
@ -378,112 +392,119 @@ cdef inline bint _can_constituent(const State* s) nogil:
|
||||||
# else:
|
# else:
|
||||||
# return False
|
# return False
|
||||||
|
|
||||||
cdef inline bint _can_adjust(const State* s) nogil:
|
@staticmethod
|
||||||
return False
|
cdef int transition(State* state, int label) except -1:
|
||||||
#if s.ctnts.stack_len < 2:
|
return False
|
||||||
# return False
|
#cdef Constituent* bracket = new_bracket(state.ctnts)
|
||||||
|
|
||||||
#cdef const Constituent* b1 = s.ctnts.stack[-1]
|
#bracket.parent = NULL
|
||||||
#cdef const Constituent* b0 = s.ctnts.stack[0]
|
#bracket.label = self.label
|
||||||
|
#bracket.head = get_s0(state)
|
||||||
|
#bracket.length = 0
|
||||||
|
|
||||||
#if (b1.head + b1.head.head) != b0.head:
|
#attach(bracket, state.ctnts.stack)
|
||||||
# return False
|
# Attach rightward children. They're in the brackets array somewhere
|
||||||
#elif b0.head >= b1.head:
|
# between here and B0.
|
||||||
# return False
|
#cdef Constituent* node
|
||||||
#elif b0 >= b1:
|
#cdef const TokenC* node_gov
|
||||||
# return False
|
#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:
|
@staticmethod
|
||||||
if not _can_constituent(s):
|
cdef int cost(const State* s, GoldParseC* gold, int label) except -1:
|
||||||
return 9000
|
if not Constituent.is_valid(s):
|
||||||
raise Exception("Constituent move should be disabled currently")
|
return 9000
|
||||||
# The gold standard is indexed by end, then by start, then a set of labels
|
raise Exception("Constituent move should be disabled currently")
|
||||||
#brackets = gold.brackets(get_s0(s).r_edge, {})
|
# The gold standard is indexed by end, then by start, then a set of labels
|
||||||
#if not brackets:
|
#brackets = gold.brackets(get_s0(s).r_edge, {})
|
||||||
# return 2 # 2 loss for bad bracket, only 1 for good bracket bad label
|
#if not brackets:
|
||||||
# Index the current brackets in the state
|
# return 2 # 2 loss for bad bracket, only 1 for good bracket bad label
|
||||||
#existing = set()
|
# Index the current brackets in the state
|
||||||
#for i in range(s.ctnt_len):
|
#existing = set()
|
||||||
# if ctnt.end == s.r_edge and ctnt.label == self.label:
|
#for i in range(s.ctnt_len):
|
||||||
# existing.add(ctnt.start)
|
# if ctnt.end == s.r_edge and ctnt.label == self.label:
|
||||||
#cdef int loss = 2
|
# existing.add(ctnt.start)
|
||||||
#cdef const TokenC* child
|
#cdef int loss = 2
|
||||||
#cdef const TokenC* s0 = get_s0(s)
|
#cdef const TokenC* child
|
||||||
#cdef int n_left = count_left_kids(s0)
|
#cdef const TokenC* s0 = get_s0(s)
|
||||||
# Iterate over the possible start positions, and check whether we have a
|
#cdef int n_left = count_left_kids(s0)
|
||||||
# (start, end, label) match to the gold tree
|
# Iterate over the possible start positions, and check whether we have a
|
||||||
#for i in range(1, n_left):
|
# (start, end, label) match to the gold tree
|
||||||
# child = get_left(s, s0, i)
|
#for i in range(1, n_left):
|
||||||
# if child.l_edge in brackets and child.l_edge not in existing:
|
# child = get_left(s, s0, i)
|
||||||
# if self.label in brackets[child.l_edge]
|
# if child.l_edge in brackets and child.l_edge not in existing:
|
||||||
# return 0
|
# if self.label in brackets[child.l_edge]
|
||||||
# else:
|
# return 0
|
||||||
# loss = 1 # If we see the start position, set loss to 1
|
# else:
|
||||||
#return loss
|
# 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):
|
|
||||||
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
|
|
||||||
#gold_starts = gold.brackets(get_s0(s).r_edge, {})
|
|
||||||
# Case 1: There are 0 brackets ending at this word.
|
|
||||||
# --> Cost is sunk, but must allow brackets to begin
|
|
||||||
#if not gold_starts:
|
|
||||||
# return 0
|
|
||||||
# Is the top bracket correct?
|
|
||||||
#gold_labels = gold_starts.get(s.ctnt.start, set())
|
|
||||||
# TODO: Case where we have a unary rule
|
|
||||||
# TODO: Case where two brackets end on this word, with top bracket starting
|
|
||||||
# before
|
|
||||||
|
|
||||||
#cdef const TokenC* child
|
|
||||||
#cdef const TokenC* s0 = get_s0(s)
|
|
||||||
#cdef int n_left = count_left_kids(s0)
|
|
||||||
#cdef int i
|
|
||||||
# Iterate over the possible start positions, and check whether we have a
|
|
||||||
# (start, end, label) match to the gold tree
|
|
||||||
#for i in range(1, n_left):
|
|
||||||
# child = get_left(s, s0, i)
|
|
||||||
# if child.l_edge in brackets:
|
|
||||||
# if self.label in brackets[child.l_edge]:
|
|
||||||
# return 0
|
|
||||||
# else:
|
|
||||||
# loss = 1 # If we see the start position, set loss to 1
|
|
||||||
#return loss
|
|
||||||
|
|
||||||
|
|
||||||
cdef int _do_constituent(const Transition* self, State* state) except -1:
|
cdef class Adjust:
|
||||||
return False
|
@staticmethod
|
||||||
#cdef Constituent* bracket = new_bracket(state.ctnts)
|
cdef inline bint is_valid(const State* s) nogil:
|
||||||
|
return False
|
||||||
|
#if s.ctnts.stack_len < 2:
|
||||||
|
# return False
|
||||||
|
|
||||||
#bracket.parent = NULL
|
#cdef const Constituent* b1 = s.ctnts.stack[-1]
|
||||||
#bracket.label = self.label
|
#cdef const Constituent* b0 = s.ctnts.stack[0]
|
||||||
#bracket.head = get_s0(state)
|
|
||||||
#bracket.length = 0
|
|
||||||
|
|
||||||
#attach(bracket, state.ctnts.stack)
|
#if (b1.head + b1.head.head) != b0.head:
|
||||||
# Attach rightward children. They're in the brackets array somewhere
|
# return False
|
||||||
# between here and B0.
|
#elif b0.head >= b1.head:
|
||||||
#cdef Constituent* node
|
# return False
|
||||||
#cdef const TokenC* node_gov
|
#elif b0 >= b1:
|
||||||
#for i in range(1, bracket - state.ctnts.stack):
|
# return False
|
||||||
# node = bracket - i
|
|
||||||
# node_gov = node.head + node.head.head
|
@staticmethod
|
||||||
# if node_gov == bracket.head:
|
cdef int transition(State* state) except -1:
|
||||||
# attach(bracket, node)
|
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
|
||||||
|
#gold_starts = gold.brackets(get_s0(s).r_edge, {})
|
||||||
|
# Case 1: There are 0 brackets ending at this word.
|
||||||
|
# --> Cost is sunk, but must allow brackets to begin
|
||||||
|
#if not gold_starts:
|
||||||
|
# return 0
|
||||||
|
# Is the top bracket correct?
|
||||||
|
#gold_labels = gold_starts.get(s.ctnt.start, set())
|
||||||
|
# TODO: Case where we have a unary rule
|
||||||
|
# TODO: Case where two brackets end on this word, with top bracket starting
|
||||||
|
# before
|
||||||
|
|
||||||
|
#cdef const TokenC* child
|
||||||
|
#cdef const TokenC* s0 = get_s0(s)
|
||||||
|
#cdef int n_left = count_left_kids(s0)
|
||||||
|
#cdef int i
|
||||||
|
# Iterate over the possible start positions, and check whether we have a
|
||||||
|
# (start, end, label) match to the gold tree
|
||||||
|
#for i in range(1, n_left):
|
||||||
|
# child = get_left(s, s0, i)
|
||||||
|
# if child.l_edge in brackets:
|
||||||
|
# if self.label in brackets[child.l_edge]:
|
||||||
|
# return 0
|
||||||
|
# else:
|
||||||
|
# loss = 1 # If we see the start position, set loss to 1
|
||||||
|
#return loss
|
||||||
|
|
||||||
|
|
||||||
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