2015-02-02 05:38:52 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from ._state cimport State
|
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
from .transition_system cimport Transition
|
|
|
|
from .transition_system cimport do_func_t
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
from ..structs cimport TokenC, Entity
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
from thinc.typedefs cimport weight_t
|
2015-06-02 18:01:06 +00:00
|
|
|
from ..gold cimport GoldParseC
|
2015-05-24 19:35:02 +00:00
|
|
|
from ..gold cimport GoldParse
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2015-06-09 21:23:28 +00:00
|
|
|
from .stateclass cimport StateClass
|
|
|
|
|
2015-02-02 05:38:52 +00:00
|
|
|
|
|
|
|
cdef enum:
|
|
|
|
MISSING
|
|
|
|
BEGIN
|
|
|
|
IN
|
|
|
|
LAST
|
|
|
|
UNIT
|
|
|
|
OUT
|
|
|
|
N_MOVES
|
|
|
|
|
2015-03-23 14:34:08 +00:00
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
MOVE_NAMES = [None] * N_MOVES
|
|
|
|
MOVE_NAMES[MISSING] = 'M'
|
|
|
|
MOVE_NAMES[BEGIN] = 'B'
|
|
|
|
MOVE_NAMES[IN] = 'I'
|
|
|
|
MOVE_NAMES[LAST] = 'L'
|
|
|
|
MOVE_NAMES[UNIT] = 'U'
|
|
|
|
MOVE_NAMES[OUT] = 'O'
|
|
|
|
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
cdef do_func_t[N_MOVES] do_funcs
|
|
|
|
|
|
|
|
|
|
|
|
cdef bint entity_is_open(const State *s) except -1:
|
2015-03-10 17:00:23 +00:00
|
|
|
return s.ents_len >= 1 and s.ent.end == 0
|
2015-03-09 05:46:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef bint _entity_is_sunk(const State *s, Transition* golds) except -1:
|
|
|
|
if not entity_is_open(s):
|
|
|
|
return False
|
|
|
|
|
2015-03-11 06:25:08 +00:00
|
|
|
cdef const Transition* gold = &golds[s.ent.start]
|
2015-03-09 05:46:22 +00:00
|
|
|
if gold.move != BEGIN and gold.move != UNIT:
|
|
|
|
return True
|
|
|
|
elif gold.label != s.ent.label:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
cdef class BiluoPushDown(TransitionSystem):
|
|
|
|
@classmethod
|
|
|
|
def get_labels(cls, gold_tuples):
|
2015-03-16 12:32:23 +00:00
|
|
|
move_labels = {MISSING: {'': True}, BEGIN: {}, IN: {}, LAST: {}, UNIT: {},
|
|
|
|
OUT: {'': True}}
|
2015-03-10 17:00:23 +00:00
|
|
|
moves = ('M', 'B', 'I', 'L', 'U')
|
2015-05-29 23:25:46 +00:00
|
|
|
for raw_text, sents in gold_tuples:
|
|
|
|
for (ids, words, tags, heads, labels, biluo), _ in sents:
|
|
|
|
for i, ner_tag in enumerate(biluo):
|
|
|
|
if ner_tag != 'O' and ner_tag != '-':
|
|
|
|
if ner_tag.count('-') != 1:
|
|
|
|
raise ValueError(ner_tag)
|
|
|
|
_, label = ner_tag.split('-')
|
|
|
|
for move_str in ('B', 'I', 'L', 'U'):
|
|
|
|
move_labels[moves.index(move_str)][label] = True
|
2015-03-09 05:46:22 +00:00
|
|
|
return move_labels
|
|
|
|
|
2015-03-10 17:00:23 +00:00
|
|
|
def move_name(self, int move, int label):
|
|
|
|
if move == OUT:
|
|
|
|
return 'O'
|
|
|
|
elif move == 'MISSING':
|
|
|
|
return 'M'
|
|
|
|
else:
|
2015-03-24 03:27:20 +00:00
|
|
|
return MOVE_NAMES[move] + '-' + self.strings[label]
|
2015-03-10 17:00:23 +00:00
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
cdef int preprocess_gold(self, GoldParse gold) except -1:
|
|
|
|
for i in range(gold.length):
|
2015-06-02 18:01:06 +00:00
|
|
|
gold.c.ner[i] = self.lookup_transition(gold.ner[i])
|
2015-03-09 11:06:01 +00:00
|
|
|
|
|
|
|
cdef Transition lookup_transition(self, object name) except *:
|
2015-03-10 17:00:23 +00:00
|
|
|
if name == '-':
|
|
|
|
move_str = 'M'
|
|
|
|
label = 0
|
|
|
|
elif '-' in name:
|
2015-03-09 11:06:01 +00:00
|
|
|
move_str, label_str = name.split('-', 1)
|
2015-03-16 12:32:23 +00:00
|
|
|
label = self.strings[label_str]
|
2015-03-09 11:06:01 +00:00
|
|
|
else:
|
|
|
|
move_str = name
|
|
|
|
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]
|
2015-03-11 06:25:08 +00:00
|
|
|
else:
|
2015-04-15 23:36:22 +00:00
|
|
|
raise KeyError(name)
|
2015-03-09 11:06:01 +00:00
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
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
|
2015-06-05 00:27:17 +00:00
|
|
|
if move == MISSING:
|
|
|
|
t.is_valid = Missing.is_valid
|
|
|
|
t.do = Missing.transition
|
|
|
|
t.get_cost = Missing.cost
|
|
|
|
elif move == BEGIN:
|
|
|
|
t.is_valid = Begin.is_valid
|
|
|
|
t.do = Begin.transition
|
|
|
|
t.get_cost = Begin.cost
|
|
|
|
elif move == IN:
|
|
|
|
t.is_valid = In.is_valid
|
|
|
|
t.do = In.transition
|
|
|
|
t.get_cost = In.cost
|
|
|
|
elif move == LAST:
|
|
|
|
t.is_valid = Last.is_valid
|
|
|
|
t.do = Last.transition
|
|
|
|
t.get_cost = Last.cost
|
|
|
|
elif move == UNIT:
|
|
|
|
t.is_valid = Unit.is_valid
|
|
|
|
t.do = Unit.transition
|
|
|
|
t.get_cost = Unit.cost
|
|
|
|
elif move == OUT:
|
|
|
|
t.is_valid = Out.is_valid
|
|
|
|
t.do = Out.transition
|
|
|
|
t.get_cost = Out.cost
|
|
|
|
else:
|
|
|
|
raise Exception(move)
|
2015-03-09 05:46:22 +00:00
|
|
|
return t
|
|
|
|
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef Transition best_valid(self, const weight_t* scores, StateClass stcls) except *:
|
2015-03-09 05:46:22 +00:00
|
|
|
cdef int best = -1
|
|
|
|
cdef weight_t score = -90000
|
|
|
|
cdef const Transition* m
|
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
m = &self.c[i]
|
2015-06-09 21:23:28 +00:00
|
|
|
if m.is_valid(stcls, m.label) and scores[i] > score:
|
2015-03-09 05:46:22 +00:00
|
|
|
best = i
|
|
|
|
score = scores[i]
|
|
|
|
assert best >= 0
|
|
|
|
cdef Transition t = self.c[best]
|
|
|
|
t.score = score
|
|
|
|
return t
|
|
|
|
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef int set_valid(self, bint* output, StateClass stcls) except -1:
|
2015-06-01 21:05:25 +00:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
m = &self.c[i]
|
2015-06-09 21:23:28 +00:00
|
|
|
output[i] = m.is_valid(stcls, m.label)
|
2015-06-05 00:27:17 +00:00
|
|
|
|
2015-06-01 21:05:25 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef class Missing:
|
|
|
|
@staticmethod
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
2015-06-05 00:27:17 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int transition(State* s, int label) except -1:
|
|
|
|
raise NotImplementedError
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@staticmethod
|
|
|
|
cdef int cost(const State* s, const GoldParseC* gold, int label) except -1:
|
2015-03-09 05:46:22 +00:00
|
|
|
return 9000
|
|
|
|
|
2015-03-11 06:25:08 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef class Begin:
|
|
|
|
@staticmethod
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
|
|
|
return label != 0 and not st.entity_is_open()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int transition(State* s, int label) except -1:
|
|
|
|
s.ent += 1
|
|
|
|
s.ents_len += 1
|
|
|
|
s.ent.start = s.i
|
|
|
|
s.ent.label = label
|
|
|
|
s.ent.end = 0
|
|
|
|
s.sent[s.i].ent_iob = 3
|
|
|
|
s.sent[s.i].ent_type = label
|
|
|
|
s.i += 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int cost(const State* s, const GoldParseC* gold, int label) except -1:
|
|
|
|
cdef int g_act = gold.ner[s.i].move
|
|
|
|
cdef int g_tag = gold.ner[s.i].label
|
2015-06-05 15:11:26 +00:00
|
|
|
|
|
|
|
if g_act == MISSING:
|
|
|
|
return 0
|
2015-06-05 21:48:43 +00:00
|
|
|
elif g_act == BEGIN:
|
2015-02-02 05:38:52 +00:00
|
|
|
# B, Gold B --> Label match
|
2015-06-05 00:27:17 +00:00
|
|
|
return label != g_tag
|
2015-02-02 05:38:52 +00:00
|
|
|
else:
|
|
|
|
# B, Gold I --> False (P)
|
|
|
|
# B, Gold L --> False (P)
|
|
|
|
# B, Gold O --> False (P)
|
|
|
|
# B, Gold U --> False (P)
|
2015-06-05 00:27:17 +00:00
|
|
|
return 1
|
|
|
|
|
2015-06-09 21:23:28 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef class In:
|
|
|
|
@staticmethod
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
|
|
|
return st.entity_is_open() and label != 0 and st.E_(0).ent_type == label
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int transition(State* s, int label) except -1:
|
|
|
|
s.sent[s.i].ent_iob = 1
|
|
|
|
s.sent[s.i].ent_type = label
|
|
|
|
s.i += 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int cost(const State* s, const GoldParseC* gold, int label) except -1:
|
2015-06-05 15:11:26 +00:00
|
|
|
move = IN
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef int next_act = gold.ner[s.i+1].move if s.i < s.sent_len else OUT
|
|
|
|
cdef int g_act = gold.ner[s.i].move
|
|
|
|
cdef int g_tag = gold.ner[s.i].label
|
|
|
|
cdef bint is_sunk = _entity_is_sunk(s, gold.ner)
|
2015-06-05 15:11:26 +00:00
|
|
|
|
|
|
|
if g_act == MISSING:
|
|
|
|
return 0
|
|
|
|
elif g_act == BEGIN:
|
2015-02-02 05:38:52 +00:00
|
|
|
# I, Gold B --> True (P of bad open entity sunk, R of this entity sunk)
|
2015-06-05 00:27:17 +00:00
|
|
|
return 0
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == IN:
|
|
|
|
# I, Gold I --> True (label forced by prev, if mismatch, P and R both sunk)
|
2015-06-05 00:27:17 +00:00
|
|
|
return 0
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == LAST:
|
|
|
|
# I, Gold L --> True iff this entity sunk and next tag == O
|
2015-06-05 00:27:17 +00:00
|
|
|
return not (is_sunk and (next_act == OUT or next_act == MISSING))
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == OUT:
|
|
|
|
# I, Gold O --> True iff next tag == O
|
2015-06-05 00:27:17 +00:00
|
|
|
return not (next_act == OUT or next_act == MISSING)
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == UNIT:
|
|
|
|
# I, Gold U --> True iff next tag == O
|
2015-06-05 00:27:17 +00:00
|
|
|
return next_act != OUT
|
2015-06-05 15:11:26 +00:00
|
|
|
else:
|
|
|
|
return 1
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef class Last:
|
|
|
|
@staticmethod
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
|
|
|
return st.entity_is_open() and label != 0 and st.E_(0).ent_type == label
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int transition(State* s, int label) except -1:
|
|
|
|
s.ent.end = s.i+1
|
|
|
|
s.sent[s.i].ent_iob = 1
|
|
|
|
s.sent[s.i].ent_type = label
|
|
|
|
s.i += 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int cost(const State* s, const GoldParseC* gold, int label) except -1:
|
2015-06-05 15:11:26 +00:00
|
|
|
move = LAST
|
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef int g_act = gold.ner[s.i].move
|
|
|
|
cdef int g_tag = gold.ner[s.i].label
|
2015-06-05 15:11:26 +00:00
|
|
|
|
|
|
|
if g_act == MISSING:
|
|
|
|
return 0
|
|
|
|
elif g_act == BEGIN:
|
2015-02-02 05:38:52 +00:00
|
|
|
# L, Gold B --> True
|
2015-06-05 00:27:17 +00:00
|
|
|
return 0
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == IN:
|
|
|
|
# L, Gold I --> True iff this entity sunk
|
2015-06-05 00:27:17 +00:00
|
|
|
return not _entity_is_sunk(s, gold.ner)
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == LAST:
|
|
|
|
# L, Gold L --> True
|
2015-06-05 00:27:17 +00:00
|
|
|
return 0
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == OUT:
|
|
|
|
# L, Gold O --> True
|
2015-06-05 00:27:17 +00:00
|
|
|
return 0
|
2015-02-02 05:38:52 +00:00
|
|
|
elif g_act == UNIT:
|
|
|
|
# L, Gold U --> True
|
2015-06-05 00:27:17 +00:00
|
|
|
return 0
|
2015-06-05 15:11:26 +00:00
|
|
|
else:
|
|
|
|
return 1
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef class Unit:
|
|
|
|
@staticmethod
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
|
|
|
return label != 0 and not st.entity_is_open()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int transition(State* s, int label) except -1:
|
|
|
|
s.ent += 1
|
|
|
|
s.ents_len += 1
|
|
|
|
s.ent.start = s.i
|
|
|
|
s.ent.label = label
|
|
|
|
s.ent.end = s.i+1
|
|
|
|
s.sent[s.i].ent_iob = 3
|
|
|
|
s.sent[s.i].ent_type = label
|
|
|
|
s.i += 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int cost(const State* s, const GoldParseC* gold, int label) except -1:
|
|
|
|
cdef int g_act = gold.ner[s.i].move
|
|
|
|
cdef int g_tag = gold.ner[s.i].label
|
|
|
|
|
2015-06-05 15:11:26 +00:00
|
|
|
if g_act == MISSING:
|
|
|
|
return 0
|
|
|
|
elif g_act == UNIT:
|
2015-02-02 05:38:52 +00:00
|
|
|
# U, Gold U --> True iff tag match
|
2015-06-05 00:27:17 +00:00
|
|
|
return label != g_tag
|
2015-02-02 05:38:52 +00:00
|
|
|
else:
|
|
|
|
# U, Gold B --> False
|
|
|
|
# U, Gold I --> False
|
|
|
|
# U, Gold L --> False
|
|
|
|
# U, Gold O --> False
|
2015-06-05 00:27:17 +00:00
|
|
|
return 1
|
2015-02-02 05:38:52 +00:00
|
|
|
|
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef class Out:
|
|
|
|
@staticmethod
|
2015-06-09 21:23:28 +00:00
|
|
|
cdef bint is_valid(StateClass st, int label) except -1:
|
|
|
|
return not st.entity_is_open()
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@staticmethod
|
|
|
|
cdef int transition(State* s, int label) except -1:
|
|
|
|
s.sent[s.i].ent_iob = 2
|
|
|
|
s.i += 1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef int cost(const State* s, const GoldParseC* gold, int label) except -1:
|
|
|
|
cdef int g_act = gold.ner[s.i].move
|
|
|
|
cdef int g_tag = gold.ner[s.i].label
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2015-06-05 15:11:26 +00:00
|
|
|
|
|
|
|
if g_act == MISSING:
|
|
|
|
return 0
|
|
|
|
elif g_act == BEGIN:
|
2015-06-05 00:27:17 +00:00
|
|
|
# O, Gold B --> False
|
|
|
|
return 1
|
|
|
|
elif g_act == IN:
|
|
|
|
# O, Gold I --> True
|
|
|
|
return 0
|
|
|
|
elif g_act == LAST:
|
|
|
|
# O, Gold L --> True
|
|
|
|
return 0
|
|
|
|
elif g_act == OUT:
|
|
|
|
# O, Gold O --> True
|
|
|
|
return 0
|
|
|
|
elif g_act == UNIT:
|
|
|
|
# O, Gold U --> False
|
|
|
|
return 1
|
2015-06-05 15:11:26 +00:00
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
2015-02-02 05:38:52 +00:00
|
|
|
|
|
|
|
class OracleError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownMove(Exception):
|
|
|
|
pass
|