mirror of https://github.com/explosion/spaCy.git
* Use non-joint sbd, for more simplicity and fewer classes
This commit is contained in:
parent
d05c5bf141
commit
ebf7d2fab1
|
@ -18,8 +18,7 @@ cdef enum:
|
||||||
REDUCE
|
REDUCE
|
||||||
LEFT
|
LEFT
|
||||||
RIGHT
|
RIGHT
|
||||||
BREAK_SHIFT
|
BREAK
|
||||||
BREAK_RIGHT
|
|
||||||
N_MOVES
|
N_MOVES
|
||||||
|
|
||||||
# Break transition from here
|
# Break transition from here
|
||||||
|
@ -48,35 +47,14 @@ cdef inline bint _can_reduce(const State* s) nogil:
|
||||||
return s.stack_len >= 2 and has_head(get_s0(s))
|
return s.stack_len >= 2 and has_head(get_s0(s))
|
||||||
|
|
||||||
|
|
||||||
cdef inline bint _can_break_shift(const State* s) nogil:
|
cdef inline bint _can_break(const State* s) nogil:
|
||||||
cdef int i
|
cdef int i
|
||||||
if not USE_BREAK:
|
if not USE_BREAK:
|
||||||
return False
|
return False
|
||||||
elif at_eol(s):
|
elif at_eol(s):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
# P. 757
|
# If stack is disconnected, cannot break
|
||||||
# In UPP, if Shift(F) or RightArc(F) fail to result in a single parsing
|
|
||||||
# tree, they cannot be performed as well.
|
|
||||||
seen_headless = False
|
|
||||||
for i in range(s.stack_len):
|
|
||||||
if seen_headless:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
seen_headless = True
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
cdef inline bint _can_break_right(const State* s) nogil:
|
|
||||||
cdef int i
|
|
||||||
if not USE_BREAK:
|
|
||||||
return False
|
|
||||||
elif not _can_right(s):
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
# P. 757
|
|
||||||
# In UPP, if Shift(F) or RightArc(F) fail to result in a single parsing
|
|
||||||
# tree, they cannot be performed as well.
|
|
||||||
seen_headless = False
|
seen_headless = False
|
||||||
for i in range(s.stack_len):
|
for i in range(s.stack_len):
|
||||||
if s.sent[s.stack[-i]].head == 0:
|
if s.sent[s.stack[-i]].head == 0:
|
||||||
|
@ -95,7 +73,7 @@ cdef int _shift_cost(const State* s, const int* gold) except -1:
|
||||||
if NON_MONOTONIC:
|
if NON_MONOTONIC:
|
||||||
cost += gold[s.stack[0]] == s.i
|
cost += gold[s.stack[0]] == s.i
|
||||||
# If we can break, and there's no cost to doing so, we should
|
# If we can break, and there's no cost to doing so, we should
|
||||||
if _can_break_shift(s) and _break_shift_cost(s, gold) == 0:
|
if _can_break(s) and _break_cost(s, gold) == 0:
|
||||||
cost += 1
|
cost += 1
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
|
@ -103,9 +81,6 @@ cdef int _shift_cost(const State* s, const int* gold) except -1:
|
||||||
cdef int _right_cost(const State* s, const int* gold) except -1:
|
cdef int _right_cost(const State* s, const int* gold) except -1:
|
||||||
assert s.stack_len >= 1
|
assert s.stack_len >= 1
|
||||||
cost = 0
|
cost = 0
|
||||||
# If we can break, and there's no cost to doing so, we should
|
|
||||||
if _can_break_right(s) and _break_right_cost(s, gold) == 0:
|
|
||||||
cost += 1
|
|
||||||
if gold[s.i] == s.stack[0]:
|
if gold[s.i] == s.stack[0]:
|
||||||
return cost
|
return cost
|
||||||
cost += head_in_buffer(s, s.i, gold)
|
cost += head_in_buffer(s, s.i, gold)
|
||||||
|
@ -138,11 +113,8 @@ cdef int _reduce_cost(const State* s, const int* gold) except -1:
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
|
|
||||||
cdef int _break_shift_cost(const State* s, const int* gold) except -1:
|
cdef int _break_cost(const State* s, const int* gold) except -1:
|
||||||
# When we break, we Reduce all of the words on the stack. We also remove
|
# When we break, we Reduce all of the words on the stack.
|
||||||
# the first word from the buffer.
|
|
||||||
#
|
|
||||||
# n0_cost:
|
|
||||||
cdef int cost = 0
|
cdef int cost = 0
|
||||||
# Number of deps between S0...Sn and N0...Nn
|
# Number of deps between S0...Sn and N0...Nn
|
||||||
for i in range(s.i, s.sent_len):
|
for i in range(s.i, s.sent_len):
|
||||||
|
@ -151,29 +123,6 @@ cdef int _break_shift_cost(const State* s, const int* gold) except -1:
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
|
|
||||||
cdef int _break_right_cost(const State* s, const int* gold) except -1:
|
|
||||||
cdef int cost = 0
|
|
||||||
assert s.stack_len >= 1
|
|
||||||
cdef int i
|
|
||||||
# When we break, we Reduce all of the words on the stack. We also remove
|
|
||||||
# the first word from the buffer.
|
|
||||||
#
|
|
||||||
# n0_cost:
|
|
||||||
# number of head/child deps between n0 and N0...Nn
|
|
||||||
cost += children_in_buffer(s, s.i, gold)
|
|
||||||
cost += head_in_buffer(s, s.i, gold)
|
|
||||||
# number of child deps from N0 into stack
|
|
||||||
cost += children_in_stack(s, s.i, gold)
|
|
||||||
# number of head deps to N0 from S1..Sn
|
|
||||||
for i in range(1, s.stack_len):
|
|
||||||
cost += s.stack[-i] == gold[s.i]
|
|
||||||
# Number of deps between S0...Sn and N1...Nn
|
|
||||||
for i in range(s.i+1, s.sent_len):
|
|
||||||
cost += children_in_stack(s, i, gold)
|
|
||||||
cost += head_in_stack(s, i, gold)
|
|
||||||
return cost
|
|
||||||
|
|
||||||
|
|
||||||
cdef class TransitionSystem:
|
cdef class TransitionSystem:
|
||||||
def __init__(self, list left_labels, list right_labels):
|
def __init__(self, list left_labels, list right_labels):
|
||||||
self.mem = Pool()
|
self.mem = Pool()
|
||||||
|
@ -183,7 +132,7 @@ cdef class TransitionSystem:
|
||||||
right_labels.pop(right_labels.index('ROOT'))
|
right_labels.pop(right_labels.index('ROOT'))
|
||||||
if 'ROOT' in left_labels:
|
if 'ROOT' in left_labels:
|
||||||
left_labels.pop(left_labels.index('ROOT'))
|
left_labels.pop(left_labels.index('ROOT'))
|
||||||
self.n_moves = 3 + len(left_labels) + len(right_labels) + len(right_labels)
|
self.n_moves = 3 + len(left_labels) + len(right_labels)
|
||||||
moves = <Transition*>self.mem.alloc(self.n_moves, sizeof(Transition))
|
moves = <Transition*>self.mem.alloc(self.n_moves, sizeof(Transition))
|
||||||
cdef int i = 0
|
cdef int i = 0
|
||||||
moves[i].move = SHIFT
|
moves[i].move = SHIFT
|
||||||
|
@ -210,17 +159,10 @@ cdef class TransitionSystem:
|
||||||
moves[i].label = label_id
|
moves[i].label = label_id
|
||||||
moves[i].clas = i
|
moves[i].clas = i
|
||||||
i += 1
|
i += 1
|
||||||
moves[i].move = BREAK_SHIFT
|
moves[i].move = BREAK
|
||||||
moves[i].label = 0
|
moves[i].label = 0
|
||||||
moves[i].clas = i
|
moves[i].clas = i
|
||||||
i += 1
|
i += 1
|
||||||
for label_str in right_labels:
|
|
||||||
label_str = unicode(label_str)
|
|
||||||
label_id = self.label_ids.setdefault(label_str, len(self.label_ids))
|
|
||||||
moves[i].move = BREAK_RIGHT
|
|
||||||
moves[i].label = label_id
|
|
||||||
moves[i].clas = i
|
|
||||||
i += 1
|
|
||||||
self._moves = moves
|
self._moves = moves
|
||||||
|
|
||||||
cdef int transition(self, State *s, const Transition* t) except -1:
|
cdef int transition(self, State *s, const Transition* t) except -1:
|
||||||
|
@ -239,16 +181,10 @@ cdef class TransitionSystem:
|
||||||
# TODO: Huh? Is this some weirdness from the non-monotonic?
|
# TODO: Huh? Is this some weirdness from the non-monotonic?
|
||||||
add_dep(s, s.stack[-1], s.stack[0], get_s0(s).dep)
|
add_dep(s, s.stack[-1], s.stack[0], get_s0(s).dep)
|
||||||
pop_stack(s)
|
pop_stack(s)
|
||||||
elif t.move == BREAK_RIGHT:
|
elif t.move == BREAK:
|
||||||
add_dep(s, s.stack[0], s.i, t.label)
|
|
||||||
push_stack(s)
|
|
||||||
while s.stack_len != 0:
|
|
||||||
s.stack -= 1
|
|
||||||
s.stack_len -= 1
|
|
||||||
if not at_eol(s):
|
|
||||||
push_stack(s)
|
|
||||||
elif t.move == BREAK_SHIFT:
|
|
||||||
while s.stack_len != 0:
|
while s.stack_len != 0:
|
||||||
|
if get_s0(s).head == 0:
|
||||||
|
get_s0(s).dep = 0
|
||||||
s.stack -= 1
|
s.stack -= 1
|
||||||
s.stack_len -= 1
|
s.stack_len -= 1
|
||||||
if not at_eol(s):
|
if not at_eol(s):
|
||||||
|
@ -262,8 +198,7 @@ cdef class TransitionSystem:
|
||||||
valid[LEFT] = _can_left(s)
|
valid[LEFT] = _can_left(s)
|
||||||
valid[RIGHT] = _can_right(s)
|
valid[RIGHT] = _can_right(s)
|
||||||
valid[REDUCE] = _can_reduce(s)
|
valid[REDUCE] = _can_reduce(s)
|
||||||
valid[BREAK_SHIFT] = _can_break_shift(s)
|
valid[BREAK] = _can_break(s)
|
||||||
valid[BREAK_RIGHT] = _can_break_right(s)
|
|
||||||
|
|
||||||
cdef int best = -1
|
cdef int best = -1
|
||||||
cdef weight_t score = 0
|
cdef weight_t score = 0
|
||||||
|
@ -292,8 +227,7 @@ cdef class TransitionSystem:
|
||||||
unl_costs[LEFT] = _left_cost(s, gold_heads) if _can_left(s) else -1
|
unl_costs[LEFT] = _left_cost(s, gold_heads) if _can_left(s) else -1
|
||||||
unl_costs[RIGHT] = _right_cost(s, gold_heads) if _can_right(s) else -1
|
unl_costs[RIGHT] = _right_cost(s, gold_heads) if _can_right(s) else -1
|
||||||
unl_costs[REDUCE] = _reduce_cost(s, gold_heads) if _can_reduce(s) else -1
|
unl_costs[REDUCE] = _reduce_cost(s, gold_heads) if _can_reduce(s) else -1
|
||||||
unl_costs[BREAK_SHIFT] = _break_shift_cost(s, gold_heads) if _can_break_shift(s) else -1
|
unl_costs[BREAK] = _break_cost(s, gold_heads) if _can_break(s) else -1
|
||||||
unl_costs[BREAK_RIGHT] = _break_right_cost(s, gold_heads) if _can_break_right(s) else -1
|
|
||||||
|
|
||||||
guess.cost = unl_costs[guess.move]
|
guess.cost = unl_costs[guess.move]
|
||||||
cdef Transition t
|
cdef Transition t
|
||||||
|
@ -309,7 +243,7 @@ cdef class TransitionSystem:
|
||||||
return t
|
return t
|
||||||
elif gold_heads[s.i] == s.stack[0]:
|
elif gold_heads[s.i] == s.stack[0]:
|
||||||
target_label = gold_labels[s.i]
|
target_label = gold_labels[s.i]
|
||||||
if guess.move == RIGHT or guess.move == BREAK_RIGHT:
|
if guess.move == RIGHT:
|
||||||
if unl_costs[guess.move] != 0:
|
if unl_costs[guess.move] != 0:
|
||||||
guess.cost += guess.label != target_label
|
guess.cost += guess.label != target_label
|
||||||
for i in range(self.n_moves):
|
for i in range(self.n_moves):
|
||||||
|
|
Loading…
Reference in New Issue