2020-03-02 13:48:10 +03:00
|
|
|
# cython: profile=True, cdivision=True, infer_types=True
|
2017-10-27 20:45:57 +03:00
|
|
|
from cpython.ref cimport Py_INCREF
|
2015-06-09 02:41:09 +03:00
|
|
|
from cymem.cymem cimport Pool
|
2020-06-14 18:45:46 +03:00
|
|
|
from libc.stdint cimport int32_t
|
2020-03-02 13:48:10 +03:00
|
|
|
|
|
|
|
from collections import defaultdict, Counter
|
2018-03-27 20:23:02 +03:00
|
|
|
import json
|
2017-04-15 14:05:15 +03:00
|
|
|
|
2018-04-08 16:20:16 +03:00
|
|
|
from ..typedefs cimport hash_t, attr_t
|
|
|
|
from ..strings cimport hash_string
|
2020-03-02 13:48:10 +03:00
|
|
|
from ..structs cimport TokenC
|
|
|
|
from ..tokens.doc cimport Doc, set_children_from_heads
|
2015-06-09 22:20:14 +03:00
|
|
|
from .stateclass cimport StateClass
|
2017-10-27 20:45:57 +03:00
|
|
|
from ._state cimport StateC
|
2017-04-15 14:05:15 +03:00
|
|
|
from .transition_system cimport move_cost_func_t, label_cost_func_t
|
2020-06-14 18:45:46 +03:00
|
|
|
from ..gold.example cimport Example
|
2020-03-02 13:48:10 +03:00
|
|
|
|
2018-04-03 16:50:31 +03:00
|
|
|
from ..errors import Errors
|
2020-03-02 13:48:10 +03:00
|
|
|
from .nonproj import is_nonproj_tree
|
|
|
|
from . import nonproj
|
|
|
|
|
2015-06-09 02:41:09 +03:00
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
# Calculate cost as gold/not gold. We don't use scalar value anyway.
|
|
|
|
cdef int BINARY_COSTS = 1
|
2018-04-08 16:20:16 +03:00
|
|
|
cdef weight_t MIN_SCORE = -90000
|
2019-12-22 03:53:56 +03:00
|
|
|
cdef attr_t SUBTOK_LABEL = hash_string(u'subtok')
|
2015-01-27 19:09:45 +03:00
|
|
|
|
2014-12-20 21:42:23 +03:00
|
|
|
DEF NON_MONOTONIC = True
|
2015-06-23 01:03:30 +03:00
|
|
|
DEF USE_BREAK = True
|
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
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef enum:
|
2020-06-14 18:45:46 +03:00
|
|
|
HEAD_IN_STACK = 0
|
2020-06-14 18:22:14 +03:00
|
|
|
HEAD_IN_BUFFER
|
|
|
|
HEAD_UNKNOWN
|
|
|
|
|
|
|
|
|
|
|
|
cdef struct GoldParseStateC:
|
|
|
|
char* state_bits
|
|
|
|
attr_t* labels
|
|
|
|
int32_t* heads
|
|
|
|
int32_t* n_kids_in_buffer
|
2020-06-14 18:45:46 +03:00
|
|
|
int32_t* n_kids_in_stack
|
2020-06-14 18:22:14 +03:00
|
|
|
int32_t length
|
|
|
|
int32_t stride
|
|
|
|
|
2020-06-15 19:10:19 +03:00
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef GoldParseStateC create_gold_state(Pool mem, StateClass stcls, Example example) except *:
|
|
|
|
cdef GoldParseStateC gs
|
2020-06-15 19:10:19 +03:00
|
|
|
gs.length = example.x.length
|
|
|
|
gs.stride = 1
|
|
|
|
gs.state_bits = <char*>mem.alloc(gs.length, sizeof(gs.state_bits[0]))
|
|
|
|
gs.labels = <attr_t*>mem.alloc(gs.length, sizeof(gs.labels[0]))
|
|
|
|
gs.heads = <int32_t*>mem.alloc(gs.length, sizeof(gs.heads[0]))
|
|
|
|
gs.n_kids_in_buffer = <int32_t*>mem.alloc(gs.length, sizeof(gs.n_kids_in_buffer[0]))
|
|
|
|
gs.n_kids_in_stack = <int32_t*>mem.alloc(gs.length, sizeof(gs.n_kids_in_stack[0]))
|
|
|
|
|
|
|
|
cand_to_gold = example.alignment.cand_to_gold
|
2020-06-20 22:49:46 +03:00
|
|
|
gold_to_cand = example.alignment.cand_to_gold
|
2020-06-15 19:10:19 +03:00
|
|
|
cdef TokenC ref_tok
|
|
|
|
for cand_i in range(example.x.length):
|
|
|
|
gold_i = cand_to_gold[cand_i]
|
2020-06-20 22:49:46 +03:00
|
|
|
if gold_i is not None: # Alignment found
|
2020-06-15 19:10:19 +03:00
|
|
|
ref_tok = example.y.c[gold_i]
|
2020-06-21 02:01:09 +03:00
|
|
|
gold_head = gold_to_cand[gold_i + ref_tok.head]
|
2020-06-20 22:49:46 +03:00
|
|
|
if gold_head is not None:
|
|
|
|
gs.heads[cand_i] = gold_head
|
|
|
|
gs.labels[cand_i] = ref_tok.dep
|
|
|
|
gs.state_bits[cand_i] = set_state_flag(
|
|
|
|
gs.state_bits[cand_i],
|
|
|
|
HEAD_UNKNOWN,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
gs.state_bits[cand_i] = set_state_flag(
|
|
|
|
gs.state_bits[cand_i],
|
|
|
|
HEAD_UNKNOWN,
|
|
|
|
1
|
|
|
|
)
|
2020-06-15 19:10:19 +03:00
|
|
|
else:
|
|
|
|
gs.state_bits[cand_i] = set_state_flag(
|
|
|
|
gs.state_bits[cand_i],
|
|
|
|
HEAD_UNKNOWN,
|
|
|
|
1
|
|
|
|
)
|
|
|
|
stack_words = set()
|
|
|
|
for i in range(stcls.stack_depth()):
|
|
|
|
s_i = stcls.S(i)
|
2020-06-21 02:01:09 +03:00
|
|
|
head = gs.heads[s_i]
|
2020-06-15 19:10:19 +03:00
|
|
|
gs.n_kids_in_stack[head] += 1
|
|
|
|
stack_words.add(s_i)
|
|
|
|
buffer_words = set()
|
|
|
|
for i in range(stcls.buffer_length()):
|
|
|
|
b_i = stcls.B(i)
|
2020-06-21 02:01:09 +03:00
|
|
|
head = gs.heads[b_i]
|
2020-06-15 19:10:19 +03:00
|
|
|
gs.n_kids_in_buffer[head] += 1
|
|
|
|
buffer_words.add(b_i)
|
|
|
|
for i in range(gs.length):
|
2020-06-21 02:01:09 +03:00
|
|
|
head = gs.heads[i]
|
2020-06-15 19:10:19 +03:00
|
|
|
if head in stack_words:
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_STACK,
|
|
|
|
1
|
|
|
|
)
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_BUFFER,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
elif head in buffer_words:
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_STACK,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_BUFFER,
|
|
|
|
1
|
|
|
|
)
|
2020-06-14 18:45:46 +03:00
|
|
|
return gs
|
2020-06-14 18:22:14 +03:00
|
|
|
|
2020-06-19 01:11:59 +03:00
|
|
|
|
2020-06-21 02:01:09 +03:00
|
|
|
cdef void update_gold_state(GoldParseStateC* gs, StateClass stcls) except *:
|
|
|
|
for i in range(gs.length):
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_BUFFER,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_STACK,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
gs.n_kids_in_stack[i] = 0
|
|
|
|
gs.n_kids_in_buffer[i] = 0
|
|
|
|
stack_words = set()
|
|
|
|
for i in range(stcls.stack_depth()):
|
|
|
|
s_i = stcls.S(i)
|
|
|
|
head = gs.heads[s_i]
|
|
|
|
gs.n_kids_in_stack[head] += 1
|
|
|
|
stack_words.add(s_i)
|
|
|
|
buffer_words = set()
|
|
|
|
for i in range(stcls.buffer_length()):
|
|
|
|
b_i = stcls.B(i)
|
|
|
|
head = gs.heads[b_i]
|
|
|
|
gs.n_kids_in_buffer[head] += 1
|
|
|
|
buffer_words.add(b_i)
|
|
|
|
for i in range(gs.length):
|
|
|
|
head = gs.heads[i]
|
|
|
|
if head in stack_words:
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_STACK,
|
|
|
|
1
|
|
|
|
)
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_BUFFER,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
elif head in buffer_words:
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_STACK,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
gs.state_bits[i] = set_state_flag(
|
|
|
|
gs.state_bits[i],
|
|
|
|
HEAD_IN_BUFFER,
|
|
|
|
1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-19 03:29:16 +03:00
|
|
|
cdef class ArcEagerGold:
|
2020-06-19 01:11:59 +03:00
|
|
|
cdef GoldParseStateC c
|
2020-06-20 22:49:46 +03:00
|
|
|
cdef Pool mem
|
|
|
|
|
2020-06-19 03:29:16 +03:00
|
|
|
def __init__(self, ArcEager moves, StateClass stcls, Example example):
|
2020-06-19 01:11:59 +03:00
|
|
|
self.mem = Pool()
|
|
|
|
self.c = create_gold_state(self.mem, stcls, example)
|
|
|
|
|
2020-06-21 02:01:09 +03:00
|
|
|
def update(self, StateClass stcls):
|
|
|
|
update_gold_state(&self.c, stcls)
|
|
|
|
|
2020-06-19 01:11:59 +03:00
|
|
|
|
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef int check_state_gold(char state_bits, char flag) nogil:
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef char one = 1
|
|
|
|
return state_bits & (one << flag)
|
|
|
|
|
|
|
|
|
|
|
|
cdef int set_state_flag(char state_bits, char flag, int value) nogil:
|
|
|
|
cdef char one = 1
|
|
|
|
if value:
|
|
|
|
return state_bits | (one << flag)
|
|
|
|
else:
|
|
|
|
return state_bits & ~(one << flag)
|
|
|
|
|
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef int is_head_in_stack(const GoldParseStateC* gold, int i) nogil:
|
|
|
|
return check_state_gold(gold.state_bits[i], HEAD_IN_STACK)
|
2020-06-14 18:22:14 +03:00
|
|
|
|
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef int is_head_in_buffer(const GoldParseStateC* gold, int i) nogil:
|
2020-06-14 18:22:14 +03:00
|
|
|
return check_state_gold(gold.state_bits[i], HEAD_IN_BUFFER)
|
|
|
|
|
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef int is_head_unknown(const GoldParseStateC* gold, int i) nogil:
|
2020-06-14 18:22:14 +03:00
|
|
|
return check_state_gold(gold.state_bits[i], HEAD_UNKNOWN)
|
|
|
|
|
2015-06-07 04:21:29 +03:00
|
|
|
# Helper functions for the arc-eager oracle
|
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef weight_t push_cost(StateClass stcls, const void* _gold, int target) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2016-01-30 16:31:12 +03:00
|
|
|
cdef weight_t cost = 0
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_in_stack(gold, target):
|
2020-06-14 18:22:14 +03:00
|
|
|
cost += 1
|
2020-06-21 02:04:02 +03:00
|
|
|
cost += gold.n_kids_in_stack[target]
|
2020-06-14 18:22:14 +03:00
|
|
|
if Break.is_valid(stcls.c, 0) and Break.move_cost(stcls, gold) == 0:
|
|
|
|
cost += 1
|
2015-06-07 04:21:29 +03:00
|
|
|
return cost
|
|
|
|
|
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef weight_t pop_cost(StateClass stcls, const void* _gold, int target) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2016-01-30 16:31:12 +03:00
|
|
|
cdef weight_t cost = 0
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_in_buffer(gold, target):
|
2020-06-14 18:22:14 +03:00
|
|
|
cost += 1
|
|
|
|
cost += gold[0].n_kids_in_buffer[target]
|
2017-05-30 21:37:24 +03:00
|
|
|
if Break.is_valid(stcls.c, 0) and Break.move_cost(stcls, gold) == 0:
|
2016-01-30 16:31:12 +03:00
|
|
|
cost += 1
|
2015-06-07 04:21:29 +03:00
|
|
|
return cost
|
|
|
|
|
2015-06-10 11:15:56 +03:00
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef weight_t arc_cost(StateClass stcls, const void* _gold, int head, int child) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
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-10 11:15:56 +03:00
|
|
|
# Head in buffer
|
2017-05-30 21:37:24 +03:00
|
|
|
elif gold.heads[child] >= stcls.B(0) and stcls.B(1) != 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
|
|
|
|
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef bint arc_is_gold(const GoldParseStateC* gold, int head, int child) nogil:
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_unknown(gold, child):
|
2015-06-08 15:49:04 +03:00
|
|
|
return True
|
|
|
|
elif gold.heads[child] == head:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef bint label_is_gold(const GoldParseStateC* gold, int head, int child, attr_t label) nogil:
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_unknown(gold, child):
|
2018-05-16 01:31:52 +03:00
|
|
|
return True
|
2017-05-30 21:37:24 +03:00
|
|
|
elif label == 0:
|
2015-06-08 15:49:04 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef bint _is_gold_root(const GoldParseStateC* gold, int word) nogil:
|
2020-06-14 18:45:46 +03:00
|
|
|
return gold.heads[word] == word or is_head_unknown(gold, word)
|
2020-06-14 18:22:14 +03:00
|
|
|
|
2015-06-07 04:21:29 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
cdef class Shift:
|
|
|
|
@staticmethod
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2018-03-27 20:23:02 +03:00
|
|
|
sent_start = st._sent[st.B_(0).l_edge].sent_start
|
|
|
|
return st.buffer_length() >= 2 and not st.shifted[st.B(0)] and sent_start != 1
|
2015-06-09 02:41:09 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-10 15:08:30 +03:00
|
|
|
st.push()
|
|
|
|
st.fast_forward()
|
2014-12-16 14:44:43 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef weight_t cost(StateClass st, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2015-06-10 01:40:43 +03:00
|
|
|
return Shift.move_cost(st, gold) + Shift.label_cost(st, gold, label)
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const void* _gold) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2015-06-10 01:40:43 +03:00
|
|
|
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
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const void* _gold, attr_t label) nogil:
|
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
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-06-10 11:15:56 +03:00
|
|
|
return st.stack_depth() >= 2
|
2015-06-09 02:41:09 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-10 11:15:56 +03:00
|
|
|
if st.has_head(st.S(0)):
|
|
|
|
st.pop()
|
|
|
|
else:
|
|
|
|
st.unshift()
|
2015-06-10 15:08:30 +03:00
|
|
|
st.fast_forward()
|
2015-06-04 23:43:03 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2015-06-07 04:21:29 +03:00
|
|
|
return Reduce.move_cost(s, gold) + Reduce.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t move_cost(StateClass st, const void* _gold) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2020-06-14 18:22:14 +03:00
|
|
|
s0 = st.S(0)
|
|
|
|
cost = pop_cost(st, gold, s0)
|
|
|
|
return_to_buffer = not st.has_head(s0)
|
|
|
|
if return_to_buffer:
|
|
|
|
# Decrement cost for the arcs we save, as we'll be putting this
|
|
|
|
# back to the buffer
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_in_stack(gold, s0):
|
2020-06-14 18:22:14 +03:00
|
|
|
cost -= 1
|
|
|
|
cost -= gold.n_kids_in_stack[s0]
|
2017-05-30 21:37:24 +03:00
|
|
|
if Break.is_valid(st.c, 0) and Break.move_cost(st, gold) == 0:
|
2017-03-10 20:22:04 +03:00
|
|
|
cost -= 1
|
|
|
|
return cost
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const void* gold, attr_t label) nogil:
|
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
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2018-04-08 16:20:16 +03:00
|
|
|
if label == SUBTOK_LABEL and st.S(0) != (st.B(0)-1):
|
2018-04-08 15:50:21 +03:00
|
|
|
return 0
|
2018-03-27 20:23:02 +03:00
|
|
|
sent_start = st._sent[st.B_(0).l_edge].sent_start
|
|
|
|
return sent_start != 1
|
2015-06-09 02:41:09 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-10 11:15:56 +03:00
|
|
|
st.add_arc(st.B(0), st.S(0), label)
|
2015-06-10 02:35:28 +03:00
|
|
|
st.pop()
|
2015-06-10 15:08:30 +03:00
|
|
|
st.fast_forward()
|
2015-06-04 23:43:03 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
2020-06-14 18:22:14 +03:00
|
|
|
gold = <const GoldParseStateC*>_gold
|
2020-06-21 02:01:09 +03:00
|
|
|
return LeftArc.move_cost(s, gold) + LeftArc.label_cost(s, gold, label)
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-21 02:01:09 +03:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const GoldParseStateC* gold) nogil:
|
|
|
|
cdef weight_t cost = 0
|
|
|
|
s0 = s.S(0)
|
|
|
|
b0 = s.B(0)
|
|
|
|
if arc_is_gold(gold, b0, s0):
|
|
|
|
# Have a negative cost if we 'recover' from the wrong dependency
|
|
|
|
return 0 if not s.has_head(s0) else -1
|
2015-06-08 15:49:04 +03:00
|
|
|
else:
|
2020-06-21 02:01:09 +03:00
|
|
|
# Account for deps we might lose between S0 and stack
|
|
|
|
if not s.has_head(s0):
|
|
|
|
cost += gold.n_kids_in_stack[s0]
|
|
|
|
if is_head_in_buffer(gold, s0):
|
|
|
|
cost += 1
|
|
|
|
return cost + pop_cost(s, gold, s.S(0)) + arc_cost(s, gold, s.B(0), s.S(0))
|
2015-06-07 04:21:29 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-21 02:01:09 +03:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const GoldParseStateC* gold, attr_t label) nogil:
|
|
|
|
return arc_is_gold(gold, s.B(0), s.S(0)) and not label_is_gold(gold, s.B(0), s.S(0), label)
|
2020-06-14 18:45:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class RightArc:
|
|
|
|
@staticmethod
|
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
|
|
|
# If there's (perhaps partial) parse pre-set, don't allow cycle.
|
|
|
|
if label == SUBTOK_LABEL and st.S(0) != (st.B(0)-1):
|
|
|
|
return 0
|
|
|
|
sent_start = st._sent[st.B_(0).l_edge].sent_start
|
|
|
|
return sent_start != 1 and st.H(st.S(0)) != st.B(0)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
|
|
|
st.add_arc(st.S(0), st.B(0), label)
|
|
|
|
st.push()
|
|
|
|
st.fast_forward()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
|
|
|
return RightArc.move_cost(s, gold) + RightArc.label_cost(s, gold, label)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline weight_t move_cost(StateClass s, const void* _gold) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
|
|
|
if arc_is_gold(gold, s.S(0), s.B(0)):
|
|
|
|
return 0
|
|
|
|
elif s.c.shifted[s.B(0)]:
|
|
|
|
return push_cost(s, gold, s.B(0))
|
|
|
|
else:
|
|
|
|
return push_cost(s, gold, s.B(0)) + arc_cost(s, gold, s.S(0), s.B(0))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef weight_t label_cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2015-06-10 01:40:43 +03: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 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
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-06-09 02:41:09 +03:00
|
|
|
cdef int i
|
|
|
|
if not USE_BREAK:
|
|
|
|
return False
|
2015-06-10 15:08:30 +03:00
|
|
|
elif st.at_break():
|
2015-06-09 02:41:09 +03:00
|
|
|
return False
|
|
|
|
elif st.stack_depth() < 1:
|
|
|
|
return False
|
2017-10-09 00:53:34 +03:00
|
|
|
elif st.B_(0).l_edge < 0:
|
|
|
|
return False
|
2017-10-09 01:02:45 +03:00
|
|
|
elif st._sent[st.B_(0).l_edge].sent_start < 0:
|
|
|
|
return False
|
2015-06-09 02:41:09 +03:00
|
|
|
else:
|
2017-08-26 03:50:55 +03:00
|
|
|
return True
|
2015-06-09 02:41:09 +03:00
|
|
|
|
2015-06-04 23:43:03 +03:00
|
|
|
@staticmethod
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2016-04-25 19:39:28 +03:00
|
|
|
st.set_break(st.B_(0).l_edge)
|
2015-06-12 02:50:23 +03:00
|
|
|
st.fast_forward()
|
2015-06-04 23:43:03 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
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
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t move_cost(StateClass s, const void* _gold) nogil:
|
|
|
|
gold = <const GoldParseStateC*>_gold
|
2016-01-30 16:31:12 +03:00
|
|
|
cdef weight_t cost = 0
|
2015-06-23 01:03:30 +03:00
|
|
|
cdef int i, j, S_i, B_i
|
2015-06-14 18:44:03 +03:00
|
|
|
for i in range(s.stack_depth()):
|
|
|
|
S_i = s.S(i)
|
2020-06-14 18:22:14 +03:00
|
|
|
cost += gold.n_kids_in_buffer[S_i]
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_in_buffer(gold, S_i):
|
2020-06-14 18:22:14 +03:00
|
|
|
cost += 1
|
2015-06-12 02:50:23 +03: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.
|
2018-05-16 01:31:52 +03:00
|
|
|
s0_root = _get_root(s.S(0), gold)
|
|
|
|
b0_root = _get_root(s.B(0), gold)
|
|
|
|
if s0_root != b0_root or s0_root == -1 or b0_root == -1:
|
2015-06-14 18:44:03 +03:00
|
|
|
return cost
|
2015-06-12 02:50:23 +03:00
|
|
|
else:
|
2018-05-16 01:31:52 +03:00
|
|
|
return cost + 1
|
2015-08-09 00:31:54 +03:00
|
|
|
|
2015-06-07 04:21:29 +03:00
|
|
|
@staticmethod
|
2020-06-14 18:45:46 +03:00
|
|
|
cdef inline weight_t label_cost(StateClass s, const void* gold, attr_t label) nogil:
|
2015-06-07 04:21:29 +03:00
|
|
|
return 0
|
2015-02-21 19:06:37 +03:00
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
cdef int _get_root(int word, const GoldParseStateC* gold) nogil:
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_unknown(gold, word):
|
2015-06-12 02:50:23 +03:00
|
|
|
return -1
|
2020-06-14 18:22:14 +03:00
|
|
|
while gold.heads[word] != word and word >= 0:
|
|
|
|
word = gold.heads[word]
|
2020-06-14 18:45:46 +03:00
|
|
|
if is_head_unknown(gold, word):
|
2020-06-14 18:22:14 +03:00
|
|
|
return -1
|
2015-06-12 02:50:23 +03:00
|
|
|
else:
|
|
|
|
return word
|
2015-08-09 00:31:54 +03:00
|
|
|
|
2015-02-21 19:06:37 +03:00
|
|
|
|
2017-03-11 20:12:01 +03:00
|
|
|
cdef void* _init_state(Pool mem, int length, void* tokens) except NULL:
|
2017-11-14 04:11:40 +03:00
|
|
|
st = new StateC(<const TokenC*>tokens, length)
|
|
|
|
for i in range(st.length):
|
|
|
|
if st._sent[i].dep == 0:
|
|
|
|
st._sent[i].l_edge = i
|
|
|
|
st._sent[i].r_edge = i
|
|
|
|
st._sent[i].head = 0
|
|
|
|
st._sent[i].dep = 0
|
|
|
|
st._sent[i].l_kids = 0
|
|
|
|
st._sent[i].r_kids = 0
|
2017-03-11 20:12:01 +03:00
|
|
|
st.fast_forward()
|
|
|
|
return <void*>st
|
|
|
|
|
|
|
|
|
2019-12-10 15:23:27 +03:00
|
|
|
cdef int _del_state(Pool mem, void* state, void* x) except -1:
|
|
|
|
cdef StateC* st = <StateC*>state
|
|
|
|
del st
|
|
|
|
|
|
|
|
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef class ArcEager(TransitionSystem):
|
2017-03-11 20:12:01 +03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
TransitionSystem.__init__(self, *args, **kwargs)
|
|
|
|
|
2015-06-05 03:27:17 +03:00
|
|
|
@classmethod
|
2016-10-16 22:34:57 +03:00
|
|
|
def get_actions(cls, **kwargs):
|
2018-03-27 20:23:02 +03:00
|
|
|
min_freq = kwargs.get('min_freq', None)
|
|
|
|
actions = defaultdict(lambda: Counter())
|
|
|
|
actions[SHIFT][''] = 1
|
|
|
|
actions[REDUCE][''] = 1
|
2016-10-16 22:34:57 +03:00
|
|
|
for label in kwargs.get('left_labels', []):
|
2018-03-27 20:23:02 +03:00
|
|
|
actions[LEFT][label] = 1
|
|
|
|
actions[SHIFT][label] = 1
|
2016-10-16 22:34:57 +03:00
|
|
|
for label in kwargs.get('right_labels', []):
|
2018-03-27 20:23:02 +03:00
|
|
|
actions[RIGHT][label] = 1
|
|
|
|
actions[REDUCE][label] = 1
|
2019-11-11 19:35:27 +03:00
|
|
|
for example in kwargs.get('gold_parses', []):
|
2020-06-19 12:31:01 +03:00
|
|
|
heads, labels = nonproj.projectivize(example.get_aligned("HEAD"),
|
|
|
|
example.get_aligned("DEP"))
|
|
|
|
for child, head, label in zip(example.get_aligned("ID"), heads, labels):
|
2019-11-25 18:03:28 +03:00
|
|
|
if label.upper() == 'ROOT' :
|
|
|
|
label = 'ROOT'
|
|
|
|
if head == child:
|
|
|
|
actions[BREAK][label] += 1
|
|
|
|
elif head < child:
|
|
|
|
actions[RIGHT][label] += 1
|
|
|
|
actions[REDUCE][''] += 1
|
|
|
|
elif head > child:
|
|
|
|
actions[LEFT][label] += 1
|
|
|
|
actions[SHIFT][''] += 1
|
2018-03-27 20:23:02 +03:00
|
|
|
if min_freq is not None:
|
|
|
|
for action, label_freqs in actions.items():
|
|
|
|
for label, freq in list(label_freqs.items()):
|
|
|
|
if freq < min_freq:
|
|
|
|
label_freqs.pop(label)
|
|
|
|
# Ensure these actions are present
|
|
|
|
actions[BREAK].setdefault('ROOT', 0)
|
2019-08-23 18:54:00 +03:00
|
|
|
if kwargs.get("learn_tokens") is True:
|
|
|
|
actions[RIGHT].setdefault('subtok', 0)
|
|
|
|
actions[LEFT].setdefault('subtok', 0)
|
2018-03-27 20:23:02 +03:00
|
|
|
# Used for backoff
|
|
|
|
actions[RIGHT].setdefault('dep', 0)
|
|
|
|
actions[LEFT].setdefault('dep', 0)
|
2016-10-16 22:34:57 +03:00
|
|
|
return actions
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2019-03-11 17:59:09 +03:00
|
|
|
@property
|
|
|
|
def action_types(self):
|
|
|
|
return (SHIFT, REDUCE, LEFT, RIGHT, BREAK)
|
2016-01-19 21:07:43 +03:00
|
|
|
|
2020-06-14 18:45:46 +03:00
|
|
|
def get_cost(self, StateClass state, Example gold, action):
|
2020-06-14 18:22:14 +03:00
|
|
|
raise NotImplementedError
|
2018-04-01 11:41:28 +03:00
|
|
|
|
|
|
|
def transition(self, StateClass state, action):
|
|
|
|
cdef Transition t = self.lookup_transition(action)
|
|
|
|
t.do(state.c, t.label)
|
|
|
|
return state
|
2019-03-11 17:59:09 +03:00
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
def is_gold_parse(self, StateClass state, gold):
|
|
|
|
raise NotImplementedError
|
2017-05-26 19:31:23 +03:00
|
|
|
|
2020-06-14 18:22:14 +03:00
|
|
|
def has_gold(self, gold, start=0, end=None):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-06-18 12:19:22 +03:00
|
|
|
def preprocess_gold(self, example):
|
2020-06-14 18:22:14 +03:00
|
|
|
raise NotImplementedError
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2020-06-19 01:11:59 +03:00
|
|
|
def init_gold_batch(self, examples):
|
|
|
|
states = self.init_batch([eg.predicted for eg in examples])
|
|
|
|
keeps = [i for i, s in enumerate(states) if not s.is_final()]
|
|
|
|
states = [states[i] for i in keeps]
|
2020-06-19 03:29:16 +03:00
|
|
|
golds = [ArcEagerGold(self, states[i], examples[i]) for i in keeps]
|
2020-06-20 03:36:12 +03:00
|
|
|
n_steps = sum([len(s.queue) * 4 for s in states])
|
2020-06-19 03:29:16 +03:00
|
|
|
return states, golds, n_steps
|
2020-06-19 01:11:59 +03:00
|
|
|
|
2018-04-01 11:41:28 +03:00
|
|
|
cdef Transition lookup_transition(self, object name_or_id) except *:
|
|
|
|
if isinstance(name_or_id, int):
|
|
|
|
return self.c[name_or_id]
|
|
|
|
name = name_or_id
|
2015-06-05 03:27:17 +03:00
|
|
|
if '-' in name:
|
|
|
|
move_str, label_str = name.split('-', 1)
|
2015-08-09 00:31:54 +03:00
|
|
|
label = self.strings[label_str]
|
2015-06-05 03:27:17 +03:00
|
|
|
else:
|
2015-08-09 00:31:54 +03:00
|
|
|
move_str = name
|
2015-06-05 03:27:17 +03: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]
|
2017-08-18 23:23:03 +03:00
|
|
|
return Transition(clas=0, move=MISSING, label=0)
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2017-05-28 15:06:40 +03:00
|
|
|
def move_name(self, int move, attr_t label):
|
2015-06-05 03:27:17 +03:00
|
|
|
label_str = self.strings[label]
|
|
|
|
if label_str:
|
|
|
|
return MOVE_NAMES[move] + '-' + label_str
|
|
|
|
else:
|
|
|
|
return MOVE_NAMES[move]
|
|
|
|
|
2018-04-01 11:41:28 +03:00
|
|
|
def class_name(self, int i):
|
|
|
|
return self.move_name(self.c[i].move, self.c[i].label)
|
|
|
|
|
2017-05-28 15:06:40 +03:00
|
|
|
cdef Transition init_transition(self, int clas, int move, attr_t label) except *:
|
2015-06-05 03:27:17 +03:00
|
|
|
# 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:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E019.format(action=move, src='arc_eager'))
|
2015-06-05 03:27:17 +03:00
|
|
|
return t
|
|
|
|
|
2016-02-01 10:34:55 +03:00
|
|
|
cdef int initialize_state(self, StateC* st) nogil:
|
|
|
|
for i in range(st.length):
|
2017-10-20 17:24:48 +03:00
|
|
|
if st._sent[i].dep == 0:
|
|
|
|
st._sent[i].l_edge = i
|
|
|
|
st._sent[i].r_edge = i
|
|
|
|
st._sent[i].head = 0
|
|
|
|
st._sent[i].dep = 0
|
|
|
|
st._sent[i].l_kids = 0
|
|
|
|
st._sent[i].r_kids = 0
|
2015-06-10 15:08:30 +03:00
|
|
|
st.fast_forward()
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2016-02-01 10:34:55 +03:00
|
|
|
cdef int finalize_state(self, StateC* st) nogil:
|
2016-04-25 22:39:19 +03:00
|
|
|
cdef int i
|
|
|
|
for i in range(st.length):
|
2017-10-20 17:24:48 +03:00
|
|
|
if st._sent[i].head == 0:
|
2016-04-25 22:39:19 +03:00
|
|
|
st._sent[i].dep = self.root_label
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2018-05-02 17:19:22 +03:00
|
|
|
def finalize_doc(self, Doc doc):
|
2016-05-02 15:25:10 +03:00
|
|
|
doc.is_parsed = True
|
2018-05-02 17:19:22 +03:00
|
|
|
set_children_from_heads(doc.c, doc.length)
|
2016-05-02 15:25:10 +03:00
|
|
|
|
2016-02-01 05:07:37 +03:00
|
|
|
cdef int set_valid(self, int* output, const StateC* st) nogil:
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef bint[N_MOVES] is_valid
|
2017-05-30 21:37:24 +03:00
|
|
|
is_valid[SHIFT] = Shift.is_valid(st, 0)
|
|
|
|
is_valid[REDUCE] = Reduce.is_valid(st, 0)
|
|
|
|
is_valid[LEFT] = LeftArc.is_valid(st, 0)
|
|
|
|
is_valid[RIGHT] = RightArc.is_valid(st, 0)
|
|
|
|
is_valid[BREAK] = Break.is_valid(st, 0)
|
2015-06-05 03:27:17 +03:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
2018-04-08 16:20:16 +03:00
|
|
|
if self.c[i].label == SUBTOK_LABEL:
|
|
|
|
output[i] = self.c[i].is_valid(st, self.c[i].label)
|
|
|
|
else:
|
|
|
|
output[i] = is_valid[self.c[i].move]
|
2015-06-05 03:27:17 +03:00
|
|
|
|
2017-03-10 20:22:04 +03:00
|
|
|
cdef int set_costs(self, int* is_valid, weight_t* costs,
|
2020-06-20 22:49:46 +03:00
|
|
|
StateClass stcls, gold) except -1:
|
2020-06-21 02:01:09 +03:00
|
|
|
if not isinstance(gold, ArcEagerGold):
|
|
|
|
raise TypeError("Expected ArcEagerGold")
|
|
|
|
cdef ArcEagerGold gold_ = gold
|
|
|
|
gold_.update(stcls)
|
|
|
|
gold_state = gold_.c
|
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):
|
2016-02-01 04:58:14 +03:00
|
|
|
if self.c[i].is_valid(stcls.c, self.c[i].label):
|
2015-06-26 07:25:36 +03:00
|
|
|
is_valid[i] = True
|
2020-06-21 02:01:09 +03:00
|
|
|
costs[i] = self.c[i].get_cost(stcls, &gold_state, self.c[i].label)
|
|
|
|
n_gold += 1
|
2015-06-10 07:56:35 +03:00
|
|
|
else:
|
2015-06-26 07:25:36 +03:00
|
|
|
is_valid[i] = False
|
|
|
|
costs[i] = 9000
|
2017-03-10 20:22:04 +03:00
|
|
|
if n_gold < 1:
|
2020-06-20 22:49:46 +03:00
|
|
|
raise ValueError
|
|
|
|
#failure_state = stcls.print_state([t.text for t in example])
|
|
|
|
#raise ValueError(
|
|
|
|
# Errors.E021.format(n_actions=self.n_moves, state=failure_state))
|