2017-03-10 17:22:04 +00:00
|
|
|
# cython: profile=True
|
|
|
|
# cython: cdivision=True
|
|
|
|
# cython: infer_types=True
|
2015-01-24 06:29:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-03-11 17:12:01 +00:00
|
|
|
from cpython.ref cimport PyObject, Py_INCREF, Py_XDECREF
|
2015-01-24 06:29:04 +00:00
|
|
|
|
2015-06-08 23:41:09 +00:00
|
|
|
import ctypes
|
|
|
|
import os
|
|
|
|
|
2014-12-20 18:42:23 +00:00
|
|
|
from ..structs cimport TokenC
|
|
|
|
|
2015-02-21 16:06:37 +00:00
|
|
|
from .transition_system cimport do_func_t, get_cost_func_t
|
2015-06-07 01:21:29 +00:00
|
|
|
from .transition_system cimport move_cost_func_t, label_cost_func_t
|
2015-05-24 19:35:02 +00:00
|
|
|
from ..gold cimport GoldParse
|
2015-06-02 18:01:06 +00:00
|
|
|
from ..gold cimport GoldParseC
|
2016-03-28 08:40:52 +00:00
|
|
|
from ..attrs cimport TAG, HEAD, DEP, ENT_IOB, ENT_TYPE, IS_SPACE
|
2015-10-10 04:32:13 +00:00
|
|
|
from ..lexeme cimport Lexeme
|
2015-02-21 16:06:37 +00:00
|
|
|
|
2015-06-08 23:41:09 +00:00
|
|
|
from libc.stdint cimport uint32_t
|
|
|
|
from libc.string cimport memcpy
|
|
|
|
|
|
|
|
from cymem.cymem cimport Pool
|
2015-06-09 19:20:14 +00:00
|
|
|
from .stateclass cimport StateClass
|
2016-04-13 13:28:28 +00:00
|
|
|
from ._state cimport StateC, is_space_token
|
2016-05-05 13:01:10 +00:00
|
|
|
from .nonproj import PseudoProjectivity
|
2016-10-24 18:31:30 +00:00
|
|
|
from .nonproj import is_nonproj_tree
|
2015-06-08 23:41:09 +00:00
|
|
|
|
2015-01-27 16:09:45 +00:00
|
|
|
|
2014-12-20 18:42:23 +00:00
|
|
|
DEF NON_MONOTONIC = True
|
2015-06-22 22:03:30 +00:00
|
|
|
DEF USE_BREAK = True
|
2014-12-16 11:44:43 +00:00
|
|
|
|
2015-02-21 16:06:37 +00:00
|
|
|
cdef weight_t MIN_SCORE = -90000
|
2014-12-16 11:44:43 +00:00
|
|
|
|
2015-02-21 16:06:37 +00:00
|
|
|
# Break transition from here
|
|
|
|
# http://www.aclweb.org/anthology/P13-1074
|
2014-12-16 11:44:43 +00:00
|
|
|
cdef enum:
|
|
|
|
SHIFT
|
|
|
|
REDUCE
|
|
|
|
LEFT
|
|
|
|
RIGHT
|
2015-05-11 14:12:03 +00:00
|
|
|
|
2015-01-28 19:22:03 +00:00
|
|
|
BREAK
|
2015-05-11 14:12:03 +00:00
|
|
|
|
2014-12-16 11:44:43 +00:00
|
|
|
N_MOVES
|
|
|
|
|
2015-05-11 14:12:03 +00:00
|
|
|
|
2015-03-09 11:06:01 +00: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 16:18:43 +00:00
|
|
|
|
2015-06-07 01:21:29 +00:00
|
|
|
# Helper functions for the arc-eager oracle
|
|
|
|
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t push_cost(StateClass stcls, const GoldParseC* gold, int target) nogil:
|
|
|
|
cdef weight_t cost = 0
|
2015-06-09 19:20:14 +00: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
|
2016-02-01 01:58:14 +00:00
|
|
|
cost += Break.is_valid(stcls.c, -1) and Break.move_cost(stcls, gold) == 0
|
2015-06-07 01:21:29 +00:00
|
|
|
return cost
|
|
|
|
|
|
|
|
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t pop_cost(StateClass stcls, const GoldParseC* gold, int target) nogil:
|
|
|
|
cdef weight_t cost = 0
|
2015-06-09 19:20:14 +00: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
|
2016-02-01 01:58:14 +00:00
|
|
|
if Break.is_valid(stcls.c, -1) and Break.move_cost(stcls, gold) == 0:
|
2016-01-30 13:31:12 +00:00
|
|
|
cost += 1
|
2015-06-07 01:21:29 +00:00
|
|
|
return cost
|
|
|
|
|
2015-06-10 08:15:56 +00:00
|
|
|
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t arc_cost(StateClass stcls, const GoldParseC* gold, int head, int child) nogil:
|
2015-06-08 12:49:04 +00:00
|
|
|
if arc_is_gold(gold, head, child):
|
2015-06-07 01:21:29 +00:00
|
|
|
return 0
|
2015-06-09 19:20:14 +00:00
|
|
|
elif stcls.H(child) == gold.heads[child]:
|
2015-06-08 12:49:04 +00:00
|
|
|
return 1
|
2015-06-10 08:15:56 +00:00
|
|
|
# Head in buffer
|
|
|
|
elif gold.heads[child] >= stcls.B(0) and stcls.B(1) != -1:
|
2015-06-08 12:49:04 +00:00
|
|
|
return 1
|
|
|
|
else:
|
2015-06-07 01:21:29 +00:00
|
|
|
return 0
|
2015-06-08 12:49:04 +00:00
|
|
|
|
|
|
|
|
2015-06-10 04:56:35 +00:00
|
|
|
cdef bint arc_is_gold(const GoldParseC* gold, int head, int child) nogil:
|
2015-06-08 12:49:04 +00:00
|
|
|
if gold.labels[child] == -1:
|
|
|
|
return True
|
|
|
|
elif gold.heads[child] == head:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-06-10 04:56:35 +00:00
|
|
|
cdef bint label_is_gold(const GoldParseC* gold, int head, int child, int label) nogil:
|
2015-06-08 12:49:04 +00:00
|
|
|
if gold.labels[child] == -1:
|
|
|
|
return True
|
|
|
|
elif label == -1:
|
|
|
|
return True
|
2015-06-07 01:21:29 +00:00
|
|
|
elif gold.labels[child] == label:
|
2015-06-08 12:49:04 +00:00
|
|
|
return True
|
2015-06-07 01:21:29 +00:00
|
|
|
else:
|
2015-06-08 12:49:04 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-06-10 04:56:35 +00:00
|
|
|
cdef bint _is_gold_root(const GoldParseC* gold, int word) nogil:
|
2015-06-08 12:49:04 +00:00
|
|
|
return gold.labels[word] == -1 or gold.heads[word] == word
|
2015-08-09 22:09:02 +00:00
|
|
|
|
2015-06-07 01:21:29 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
cdef class Shift:
|
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef bint is_valid(const StateC* st, int label) nogil:
|
|
|
|
return st.buffer_length() >= 2 and not st.shifted[st.B(0)] and not st.B_(0).sent_start
|
2015-06-08 23:41:09 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef int transition(StateC* st, int label) nogil:
|
2015-06-10 12:08:30 +00:00
|
|
|
st.push()
|
|
|
|
st.fast_forward()
|
2014-12-16 11:44:43 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t cost(StateClass st, const GoldParseC* gold, int label) nogil:
|
2015-06-09 22:40:43 +00:00
|
|
|
return Shift.move_cost(st, gold) + Shift.label_cost(st, gold, label)
|
2015-06-07 01:21:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const GoldParseC* gold) nogil:
|
2015-06-09 22:40:43 +00:00
|
|
|
return push_cost(s, gold, s.B(0))
|
2015-02-21 16:06:37 +00:00
|
|
|
|
2015-06-07 01:21:29 +00:00
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-07 01:21:29 +00:00
|
|
|
return 0
|
|
|
|
|
2015-02-21 16:06:37 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
cdef class Reduce:
|
2015-06-08 23:41:09 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef bint is_valid(const StateC* st, int label) nogil:
|
2015-06-10 08:15:56 +00:00
|
|
|
return st.stack_depth() >= 2
|
2015-06-08 23:41:09 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef int transition(StateC* st, int label) nogil:
|
2015-06-10 08:15:56 +00:00
|
|
|
if st.has_head(st.S(0)):
|
|
|
|
st.pop()
|
|
|
|
else:
|
|
|
|
st.unshift()
|
2015-06-10 12:08:30 +00:00
|
|
|
st.fast_forward()
|
2015-06-04 20:43:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-07 01:21:29 +00:00
|
|
|
return Reduce.move_cost(s, gold) + Reduce.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t move_cost(StateClass st, const GoldParseC* gold) nogil:
|
2017-03-10 17:22:04 +00:00
|
|
|
cost = pop_cost(st, gold, st.S(0))
|
|
|
|
if not st.has_head(st.S(0)):
|
|
|
|
# Decrement cost for the arcs e save
|
|
|
|
for i in range(1, st.stack_depth()):
|
|
|
|
S_i = st.S(i)
|
|
|
|
if gold.heads[st.S(0)] == S_i:
|
|
|
|
cost -= 1
|
|
|
|
if gold.heads[S_i] == st.S(0):
|
|
|
|
cost -= 1
|
|
|
|
if Break.is_valid(st.c, -1) and Break.move_cost(st, gold) == 0:
|
|
|
|
cost -= 1
|
|
|
|
return cost
|
2015-06-07 01:21:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-07 01:21:29 +00:00
|
|
|
return 0
|
|
|
|
|
2015-01-27 16:09:45 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
cdef class LeftArc:
|
2015-06-08 23:41:09 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef bint is_valid(const StateC* st, int label) nogil:
|
2016-04-13 13:28:28 +00:00
|
|
|
return not st.B_(0).sent_start
|
2015-06-08 23:41:09 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef int transition(StateC* st, int label) nogil:
|
2015-06-10 08:15:56 +00:00
|
|
|
st.add_arc(st.B(0), st.S(0), label)
|
2015-06-09 23:35:28 +00:00
|
|
|
st.pop()
|
2015-06-10 12:08:30 +00:00
|
|
|
st.fast_forward()
|
2015-06-04 20:43:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-07 01:21:29 +00:00
|
|
|
return LeftArc.move_cost(s, gold) + LeftArc.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const GoldParseC* gold) nogil:
|
|
|
|
cdef weight_t cost = 0
|
2015-06-09 22:40:43 +00:00
|
|
|
if arc_is_gold(gold, s.B(0), s.S(0)):
|
2017-03-10 17:22:04 +00:00
|
|
|
# Have a negative cost if we 'recover' from the wrong dependency
|
|
|
|
return 0 if not s.has_head(s.S(0)) else -1
|
2015-06-08 12:49:04 +00:00
|
|
|
else:
|
2015-06-15 00:50:00 +00:00
|
|
|
# Account for deps we might lose between S0 and stack
|
|
|
|
if not s.has_head(s.S(0)):
|
|
|
|
for i in range(1, s.stack_depth()):
|
|
|
|
cost += gold.heads[s.S(i)] == s.S(0)
|
|
|
|
cost += gold.heads[s.S(0)] == s.S(i)
|
2016-05-02 22:21:08 +00:00
|
|
|
return cost + pop_cost(s, gold, s.S(0)) + arc_cost(s, gold, s.B(0), s.S(0))
|
2014-12-16 11:44:43 +00:00
|
|
|
|
2015-06-07 01:21:29 +00:00
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-09 22:40:43 +00:00
|
|
|
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 01:21:29 +00:00
|
|
|
|
2015-01-27 16:09:45 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
cdef class RightArc:
|
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef bint is_valid(const StateC* st, int label) nogil:
|
2016-04-13 13:28:28 +00:00
|
|
|
return not st.B_(0).sent_start
|
2015-06-08 23:41:09 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef int transition(StateC* st, int label) nogil:
|
2015-06-09 23:35:28 +00:00
|
|
|
st.add_arc(st.S(0), st.B(0), label)
|
|
|
|
st.push()
|
2015-06-10 12:08:30 +00:00
|
|
|
st.fast_forward()
|
2015-02-21 16:06:37 +00:00
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-07 01:21:29 +00:00
|
|
|
return RightArc.move_cost(s, gold) + RightArc.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const GoldParseC* gold) nogil:
|
2015-06-09 22:40:43 +00:00
|
|
|
if arc_is_gold(gold, s.S(0), s.B(0)):
|
2015-06-08 12:49:04 +00:00
|
|
|
return 0
|
2016-02-01 01:37:08 +00:00
|
|
|
elif s.c.shifted[s.B(0)]:
|
2015-06-10 08:15:56 +00:00
|
|
|
return push_cost(s, gold, s.B(0))
|
2015-06-08 12:49:04 +00:00
|
|
|
else:
|
2015-06-09 22:40:43 +00:00
|
|
|
return push_cost(s, gold, s.B(0)) + arc_cost(s, gold, s.S(0), s.B(0))
|
2015-06-07 01:21:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t label_cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-09 22:40:43 +00:00
|
|
|
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 16:06:37 +00:00
|
|
|
|
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
cdef class Break:
|
2015-06-08 23:41:09 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef bint is_valid(const StateC* st, int label) nogil:
|
2015-06-08 23:41:09 +00:00
|
|
|
cdef int i
|
|
|
|
if not USE_BREAK:
|
|
|
|
return False
|
2015-06-10 12:08:30 +00:00
|
|
|
elif st.at_break():
|
2015-06-08 23:41:09 +00:00
|
|
|
return False
|
|
|
|
elif st.stack_depth() < 1:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-06-04 20:43:03 +00:00
|
|
|
@staticmethod
|
2016-02-01 01:58:14 +00:00
|
|
|
cdef int transition(StateC* st, int label) nogil:
|
2016-04-25 16:39:28 +00:00
|
|
|
st.set_break(st.B_(0).l_edge)
|
2015-06-11 23:50:23 +00:00
|
|
|
st.fast_forward()
|
2015-06-04 20:43:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-09 21:23:28 +00:00
|
|
|
return Break.move_cost(s, gold) + Break.label_cost(s, gold, label)
|
2015-06-07 01:21:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const GoldParseC* gold) nogil:
|
|
|
|
cdef weight_t cost = 0
|
2015-06-22 22:03:30 +00:00
|
|
|
cdef int i, j, S_i, B_i
|
2015-06-14 15:44:03 +00:00
|
|
|
for i in range(s.stack_depth()):
|
|
|
|
S_i = s.S(i)
|
|
|
|
for j in range(s.buffer_length()):
|
|
|
|
B_i = s.B(j)
|
|
|
|
cost += gold.heads[S_i] == B_i
|
|
|
|
cost += gold.heads[B_i] == S_i
|
2017-03-15 21:40:27 +00:00
|
|
|
if cost != 0:
|
|
|
|
return cost
|
2015-06-11 23:50:23 +00:00
|
|
|
# Check for sentence boundary --- if it's here, we can't have any deps
|
|
|
|
# between stack and buffer, so rest of action is irrelevant.
|
|
|
|
s0_root = _get_root(s.S(0), gold)
|
|
|
|
b0_root = _get_root(s.B(0), gold)
|
2015-06-14 15:44:03 +00:00
|
|
|
if s0_root != b0_root or s0_root == -1 or b0_root == -1:
|
|
|
|
return cost
|
2015-06-11 23:50:23 +00:00
|
|
|
else:
|
2015-06-14 15:44:03 +00:00
|
|
|
return cost + 1
|
2015-08-08 21:31:54 +00:00
|
|
|
|
2015-06-07 01:21:29 +00:00
|
|
|
@staticmethod
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const GoldParseC* gold, int label) nogil:
|
2015-06-07 01:21:29 +00:00
|
|
|
return 0
|
2015-02-21 16:06:37 +00:00
|
|
|
|
2015-06-11 23:50:23 +00:00
|
|
|
cdef int _get_root(int word, const GoldParseC* gold) nogil:
|
|
|
|
while gold.heads[word] != word and gold.labels[word] != -1 and word >= 0:
|
|
|
|
word = gold.heads[word]
|
|
|
|
if gold.labels[word] == -1:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return word
|
2015-08-08 21:31:54 +00:00
|
|
|
|
2015-02-21 16:06:37 +00:00
|
|
|
|
2017-03-11 17:12:01 +00:00
|
|
|
cdef void* _init_state(Pool mem, int length, void* tokens) except NULL:
|
|
|
|
cdef StateClass st = StateClass.init(<const TokenC*>tokens, length)
|
|
|
|
# Ensure sent_start is set to 0 throughout
|
|
|
|
for i in range(st.c.length):
|
|
|
|
st.c._sent[i].sent_start = False
|
|
|
|
st.c._sent[i].l_edge = i
|
|
|
|
st.c._sent[i].r_edge = i
|
|
|
|
st.fast_forward()
|
|
|
|
Py_INCREF(st)
|
|
|
|
return <void*>st
|
|
|
|
|
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef class ArcEager(TransitionSystem):
|
2017-03-11 17:12:01 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
TransitionSystem.__init__(self, *args, **kwargs)
|
|
|
|
self.init_beam_state = _init_state
|
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@classmethod
|
2016-10-16 19:34:57 +00:00
|
|
|
def get_actions(cls, **kwargs):
|
2017-03-10 17:22:04 +00:00
|
|
|
actions = kwargs.get('actions',
|
2016-10-16 19:34:57 +00:00
|
|
|
{
|
|
|
|
SHIFT: {'': True},
|
|
|
|
REDUCE: {'': True},
|
|
|
|
RIGHT: {},
|
|
|
|
LEFT: {},
|
|
|
|
BREAK: {'ROOT': True}})
|
|
|
|
for label in kwargs.get('left_labels', []):
|
|
|
|
if label.upper() != 'ROOT':
|
|
|
|
actions[LEFT][label] = True
|
|
|
|
for label in kwargs.get('right_labels', []):
|
|
|
|
if label.upper() != 'ROOT':
|
|
|
|
actions[RIGHT][label] = True
|
2017-03-10 17:22:04 +00:00
|
|
|
|
2016-10-16 19:34:57 +00:00
|
|
|
for raw_text, sents in kwargs.get('gold_parses', []):
|
2015-06-05 00:27:17 +00:00
|
|
|
for (ids, words, tags, heads, labels, iob), ctnts in sents:
|
|
|
|
for child, head, label in zip(ids, heads, labels):
|
2015-06-23 02:14:03 +00:00
|
|
|
if label.upper() == 'ROOT':
|
|
|
|
label = 'ROOT'
|
|
|
|
if label != 'ROOT':
|
2015-06-05 00:27:17 +00:00
|
|
|
if head < child:
|
2016-10-16 19:34:57 +00:00
|
|
|
actions[RIGHT][label] = True
|
2015-06-05 00:27:17 +00:00
|
|
|
elif head > child:
|
2016-10-16 19:34:57 +00:00
|
|
|
actions[LEFT][label] = True
|
|
|
|
return actions
|
2015-06-05 00:27:17 +00:00
|
|
|
|
2016-01-19 18:07:43 +00:00
|
|
|
property action_types:
|
|
|
|
def __get__(self):
|
|
|
|
return (SHIFT, REDUCE, LEFT, RIGHT, BREAK)
|
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
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:
|
2015-06-23 02:14:03 +00:00
|
|
|
label = gold.labels[i]
|
|
|
|
if label.upper() == 'ROOT':
|
|
|
|
label = 'ROOT'
|
2015-06-05 00:27:17 +00:00
|
|
|
gold.c.heads[i] = gold.heads[i]
|
2015-06-23 02:14:03 +00:00
|
|
|
gold.c.labels[i] = self.strings[label]
|
2015-07-19 13:18:17 +00:00
|
|
|
# Count frequencies, for use in encoder
|
|
|
|
self.freqs[HEAD][gold.c.heads[i] - i] += 1
|
|
|
|
self.freqs[DEP][gold.c.labels[i]] += 1
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
cdef Transition lookup_transition(self, object name) except *:
|
|
|
|
if '-' in name:
|
|
|
|
move_str, label_str = name.split('-', 1)
|
2015-08-08 21:31:54 +00:00
|
|
|
label = self.strings[label_str]
|
2015-06-05 00:27:17 +00:00
|
|
|
else:
|
2015-08-08 21:31:54 +00:00
|
|
|
move_str = name
|
2015-06-05 00:27:17 +00:00
|
|
|
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
|
|
|
|
|
2016-02-01 07:34:55 +00:00
|
|
|
cdef int initialize_state(self, StateC* st) nogil:
|
2015-06-23 03:39:23 +00:00
|
|
|
# Ensure sent_start is set to 0 throughout
|
2016-02-01 07:34:55 +00:00
|
|
|
for i in range(st.length):
|
|
|
|
st._sent[i].sent_start = False
|
|
|
|
st._sent[i].l_edge = i
|
|
|
|
st._sent[i].r_edge = i
|
2015-06-10 12:08:30 +00:00
|
|
|
st.fast_forward()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
2016-02-01 07:34:55 +00:00
|
|
|
cdef int finalize_state(self, StateC* st) nogil:
|
2016-04-25 19:39:19 +00:00
|
|
|
cdef int i
|
|
|
|
for i in range(st.length):
|
|
|
|
if st._sent[i].head == 0 and st._sent[i].dep == 0:
|
|
|
|
st._sent[i].dep = self.root_label
|
2015-06-05 00:27:17 +00:00
|
|
|
|
2016-05-02 12:25:10 +00:00
|
|
|
def finalize_doc(self, doc):
|
|
|
|
doc.is_parsed = True
|
2016-05-05 13:01:10 +00:00
|
|
|
if doc.vocab.lang == 'de':
|
2016-05-05 13:10:36 +00:00
|
|
|
PseudoProjectivity.deprojectivize(doc)
|
2016-05-02 12:25:10 +00:00
|
|
|
|
2016-02-01 02:07:37 +00:00
|
|
|
cdef int set_valid(self, int* output, const StateC* st) nogil:
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef bint[N_MOVES] is_valid
|
2016-02-01 02:07:37 +00:00
|
|
|
is_valid[SHIFT] = Shift.is_valid(st, -1)
|
|
|
|
is_valid[REDUCE] = Reduce.is_valid(st, -1)
|
|
|
|
is_valid[LEFT] = LeftArc.is_valid(st, -1)
|
|
|
|
is_valid[RIGHT] = RightArc.is_valid(st, -1)
|
|
|
|
is_valid[BREAK] = Break.is_valid(st, -1)
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
output[i] = is_valid[self.c[i].move]
|
|
|
|
|
2017-03-10 17:22:04 +00:00
|
|
|
cdef int set_costs(self, int* is_valid, weight_t* costs,
|
2015-06-26 04:25:36 +00:00
|
|
|
StateClass stcls, GoldParse gold) except -1:
|
2015-06-07 01:21:29 +00: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
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef weight_t[N_MOVES] move_costs
|
2015-06-07 01:21:29 +00:00
|
|
|
for i in range(N_MOVES):
|
2017-03-10 17:22:04 +00:00
|
|
|
move_costs[i] = 9000
|
2015-06-07 01:21:29 +00:00
|
|
|
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 00:27:17 +00:00
|
|
|
cdef int* labels = gold.c.labels
|
|
|
|
cdef int* heads = gold.c.heads
|
2015-06-09 21:23:28 +00:00
|
|
|
|
2015-06-10 02:20:23 +00:00
|
|
|
n_gold = 0
|
2015-06-09 21:23:28 +00:00
|
|
|
for i in range(self.n_moves):
|
2016-02-01 01:58:14 +00:00
|
|
|
if self.c[i].is_valid(stcls.c, self.c[i].label):
|
2015-06-26 04:25:36 +00:00
|
|
|
is_valid[i] = True
|
2015-06-09 21:23:28 +00:00
|
|
|
move = self.c[i].move
|
|
|
|
label = self.c[i].label
|
2017-03-10 17:22:04 +00:00
|
|
|
if move_costs[move] == 9000:
|
2015-06-09 22:40:43 +00:00
|
|
|
move_costs[move] = move_cost_funcs[move](stcls, &gold.c)
|
2015-06-26 04:25:36 +00:00
|
|
|
costs[i] = move_costs[move] + label_cost_funcs[move](stcls, &gold.c, label)
|
2016-11-25 15:01:52 +00:00
|
|
|
n_gold += costs[i] <= 0
|
2015-06-10 04:56:35 +00:00
|
|
|
else:
|
2015-06-26 04:25:36 +00:00
|
|
|
is_valid[i] = False
|
|
|
|
costs[i] = 9000
|
2017-03-10 17:22:04 +00:00
|
|
|
if n_gold < 1:
|
2016-10-24 18:31:30 +00:00
|
|
|
# Check projectivity --- leading cause
|
|
|
|
if is_nonproj_tree(gold.heads):
|
|
|
|
raise ValueError(
|
|
|
|
"Could not find a gold-standard action to supervise the dependency "
|
|
|
|
"parser.\n"
|
|
|
|
"Likely cause: the tree is non-projective (i.e. it has crossing "
|
|
|
|
"arcs -- see spacy/syntax/nonproj.pyx for definitions)\n"
|
|
|
|
"The ArcEager transition system only supports projective trees.\n"
|
|
|
|
"To learn non-projective representations, transform the data "
|
|
|
|
"before training and after parsing. Either pass make_projective=True "
|
|
|
|
"to the GoldParse class, or use PseudoProjectivity.preprocess_training_data")
|
|
|
|
else:
|
2016-11-25 15:01:52 +00:00
|
|
|
print(gold.words)
|
|
|
|
print(gold.heads)
|
|
|
|
print(gold.labels)
|
2016-10-24 18:31:30 +00:00
|
|
|
raise ValueError(
|
|
|
|
"Could not find a gold-standard action to supervise the dependency "
|
|
|
|
"parser.\n"
|
2016-11-25 15:01:52 +00:00
|
|
|
"The GoldParse was projective.\n"
|
2017-03-10 17:22:04 +00:00
|
|
|
"The transition system has %d actions.\n"
|
2016-11-25 15:01:52 +00:00
|
|
|
"State at failure:\n"
|
|
|
|
"%s" % (self.n_moves, stcls.print_state(gold.words)))
|
2015-06-10 02:20:23 +00:00
|
|
|
assert n_gold >= 1
|