2020-06-26 17:34:12 +00:00
|
|
|
from libc.stdint cimport int32_t
|
|
|
|
from cymem.cymem cimport Pool
|
2017-04-15 11:05:15 +00:00
|
|
|
|
2020-10-04 09:16:31 +00:00
|
|
|
from collections import Counter
|
|
|
|
|
2020-07-30 21:30:54 +00:00
|
|
|
from ...typedefs cimport weight_t, attr_t
|
|
|
|
from ...lexeme cimport Lexeme
|
|
|
|
from ...attrs cimport IS_SPACE
|
2020-09-09 08:31:03 +00:00
|
|
|
from ...training.example cimport Example
|
2017-04-15 11:05:15 +00:00
|
|
|
from .stateclass cimport StateClass
|
|
|
|
from ._state cimport StateC
|
2020-07-30 21:30:54 +00:00
|
|
|
from .transition_system cimport Transition, do_func_t
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2020-10-04 09:16:31 +00:00
|
|
|
from ...errors import Errors
|
|
|
|
|
2015-02-02 05:38:52 +00:00
|
|
|
|
|
|
|
cdef enum:
|
|
|
|
MISSING
|
|
|
|
BEGIN
|
|
|
|
IN
|
|
|
|
LAST
|
|
|
|
UNIT
|
|
|
|
OUT
|
2017-04-14 21:52:17 +00:00
|
|
|
ISNT
|
2015-02-02 05:38:52 +00:00
|
|
|
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'
|
2017-04-14 21:52:17 +00:00
|
|
|
MOVE_NAMES[ISNT] = 'x'
|
2015-03-09 11:06:01 +00:00
|
|
|
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef struct GoldNERStateC:
|
|
|
|
Transition* ner
|
|
|
|
int32_t length
|
|
|
|
|
|
|
|
|
|
|
|
cdef class BiluoGold:
|
|
|
|
cdef Pool mem
|
|
|
|
cdef GoldNERStateC c
|
|
|
|
|
|
|
|
def __init__(self, BiluoPushDown moves, StateClass stcls, Example example):
|
|
|
|
self.mem = Pool()
|
|
|
|
self.c = create_gold_state(self.mem, moves, stcls, example)
|
|
|
|
|
|
|
|
def update(self, StateClass stcls):
|
|
|
|
update_gold_state(&self.c, stcls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cdef GoldNERStateC create_gold_state(
|
|
|
|
Pool mem,
|
|
|
|
BiluoPushDown moves,
|
|
|
|
StateClass stcls,
|
|
|
|
Example example
|
|
|
|
) except *:
|
|
|
|
cdef GoldNERStateC gs
|
|
|
|
gs.ner = <Transition*>mem.alloc(example.x.length, sizeof(Transition))
|
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
for i, ner_tag in enumerate(ner_tags):
|
|
|
|
gs.ner[i] = moves.lookup_transition(ner_tag)
|
|
|
|
return gs
|
|
|
|
|
|
|
|
|
|
|
|
cdef void update_gold_state(GoldNERStateC* gs, StateClass stcls) except *:
|
|
|
|
# We don't need to update each time, unlike the parser.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
cdef do_func_t[N_MOVES] do_funcs
|
|
|
|
|
|
|
|
|
2015-06-10 05:09:17 +00:00
|
|
|
cdef bint _entity_is_sunk(StateClass st, Transition* golds) nogil:
|
2015-06-09 22:40:43 +00:00
|
|
|
if not st.entity_is_open():
|
2015-03-09 05:46:22 +00:00
|
|
|
return False
|
|
|
|
|
2015-06-09 22:40:43 +00:00
|
|
|
cdef const Transition* gold = &golds[st.E(0)]
|
2015-03-09 05:46:22 +00:00
|
|
|
if gold.move != BEGIN and gold.move != UNIT:
|
|
|
|
return True
|
2015-06-09 22:40:43 +00:00
|
|
|
elif gold.label != st.E_(0).ent_type:
|
2015-03-09 05:46:22 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-08-09 00:31:53 +00:00
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
cdef class BiluoPushDown(TransitionSystem):
|
2017-05-27 20:50:21 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
TransitionSystem.__init__(self, *args, **kwargs)
|
|
|
|
|
2015-03-09 05:46:22 +00:00
|
|
|
@classmethod
|
2016-10-16 19:34:57 +00:00
|
|
|
def get_actions(cls, **kwargs):
|
2018-03-27 17:23:02 +00:00
|
|
|
actions = {
|
|
|
|
MISSING: Counter(),
|
|
|
|
BEGIN: Counter(),
|
|
|
|
IN: Counter(),
|
|
|
|
LAST: Counter(),
|
|
|
|
UNIT: Counter(),
|
|
|
|
OUT: Counter()
|
|
|
|
}
|
2019-09-18 19:37:17 +00:00
|
|
|
actions[OUT][''] = 1 # Represents a token predicted to be outside of any entity
|
|
|
|
actions[UNIT][''] = 1 # Represents a token prohibited to be in an entity
|
2016-10-16 19:34:57 +00:00
|
|
|
for entity_type in kwargs.get('entity_types', []):
|
|
|
|
for action in (BEGIN, IN, LAST, UNIT):
|
2018-03-27 17:23:02 +00:00
|
|
|
actions[action][entity_type] = 1
|
2015-03-10 17:00:23 +00:00
|
|
|
moves = ('M', 'B', 'I', 'L', 'U')
|
2020-06-26 17:34:12 +00:00
|
|
|
for example in kwargs.get('examples', []):
|
|
|
|
for token in example.y:
|
|
|
|
ent_type = token.ent_type_
|
|
|
|
if ent_type:
|
2019-11-25 15:03:28 +00:00
|
|
|
for action in (BEGIN, IN, LAST, UNIT):
|
2020-06-26 17:34:12 +00:00
|
|
|
actions[action][ent_type] += 1
|
2016-10-16 19:34:57 +00:00
|
|
|
return actions
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2019-03-11 14:59:09 +00:00
|
|
|
@property
|
|
|
|
def action_types(self):
|
|
|
|
return (BEGIN, IN, LAST, UNIT, OUT)
|
2016-01-19 18:07:43 +00:00
|
|
|
|
2017-05-28 12:06:40 +00:00
|
|
|
def move_name(self, int move, attr_t label):
|
2015-03-10 17:00:23 +00:00
|
|
|
if move == OUT:
|
|
|
|
return 'O'
|
2017-05-27 20:50:21 +00:00
|
|
|
elif move == MISSING:
|
2015-03-10 17:00:23 +00:00
|
|
|
return 'M'
|
|
|
|
else:
|
2016-09-30 18:11:49 +00:00
|
|
|
return MOVE_NAMES[move] + '-' + self.strings[label]
|
2015-03-10 17:00:23 +00:00
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
def init_gold_batch(self, examples):
|
|
|
|
all_states = self.init_batch([eg.predicted for eg in examples])
|
|
|
|
golds = []
|
|
|
|
states = []
|
|
|
|
for state, eg in zip(all_states, examples):
|
|
|
|
if self.has_gold(eg) and not state.is_final():
|
|
|
|
golds.append(self.init_gold(state, eg))
|
|
|
|
states.append(state)
|
|
|
|
n_steps = sum([len(s.queue) for s in states])
|
|
|
|
return states, golds, n_steps
|
2017-07-29 19:59:02 +00:00
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
cdef Transition lookup_transition(self, object name) except *:
|
2017-05-28 16:09:27 +00:00
|
|
|
cdef attr_t label
|
2019-02-27 11:06:32 +00:00
|
|
|
if name == '-' or name == '' or name is None:
|
2017-09-14 14:59:45 +00:00
|
|
|
return Transition(clas=0, move=MISSING, label=0, score=0)
|
2017-07-19 22:18:49 +00:00
|
|
|
elif name == '!O':
|
|
|
|
return Transition(clas=0, move=ISNT, label=0, score=0)
|
2015-03-10 17:00:23 +00:00
|
|
|
elif '-' in name:
|
2015-03-09 11:06:01 +00:00
|
|
|
move_str, label_str = name.split('-', 1)
|
2017-04-14 21:52:17 +00:00
|
|
|
# Hacky way to denote 'not this entity'
|
|
|
|
if label_str.startswith('!'):
|
|
|
|
label_str = label_str[1:]
|
|
|
|
move_str = 'x'
|
2017-05-28 12:06:40 +00:00
|
|
|
label = self.strings.add(label_str)
|
2015-03-09 11:06:01 +00:00
|
|
|
else:
|
|
|
|
move_str = name
|
|
|
|
label = 0
|
|
|
|
move = MOVE_NAMES.index(move_str)
|
2017-04-14 21:52:17 +00:00
|
|
|
if move == ISNT:
|
|
|
|
return Transition(clas=0, move=ISNT, label=label, score=0)
|
2015-03-09 11:06:01 +00:00
|
|
|
for i in range(self.n_moves):
|
|
|
|
if self.c[i].move == move and self.c[i].label == label:
|
|
|
|
return self.c[i]
|
2019-09-18 19:37:17 +00:00
|
|
|
raise KeyError(Errors.E022.format(name=name))
|
2015-03-09 11:06:01 +00:00
|
|
|
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef Transition init_transition(self, int clas, int move, attr_t label) except *:
|
2015-03-09 05:46:22 +00: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
|
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:
|
2018-04-03 13:50:31 +00:00
|
|
|
raise ValueError(Errors.E019.format(action=move, src='ner'))
|
2015-03-09 05:46:22 +00:00
|
|
|
return t
|
|
|
|
|
2018-03-27 17:23:02 +00:00
|
|
|
def add_action(self, int action, label_name, freq=None):
|
2017-10-07 17:02:40 +00:00
|
|
|
cdef attr_t label_id
|
|
|
|
if not isinstance(label_name, (int, long)):
|
|
|
|
label_id = self.strings.add(label_name)
|
|
|
|
else:
|
|
|
|
label_id = label_name
|
|
|
|
if action == OUT and label_id != 0:
|
2018-12-10 05:31:00 +00:00
|
|
|
return None
|
2017-10-07 17:02:40 +00:00
|
|
|
if action == MISSING or action == ISNT:
|
2018-12-10 05:31:00 +00:00
|
|
|
return None
|
2017-10-07 17:02:40 +00:00
|
|
|
# Check we're not creating a move we already have, so that this is
|
|
|
|
# idempotent
|
|
|
|
for trans in self.c[:self.n_moves]:
|
|
|
|
if trans.move == action and trans.label == label_id:
|
|
|
|
return 0
|
|
|
|
if self.n_moves >= self._size:
|
2018-12-10 05:31:00 +00:00
|
|
|
self._size = self.n_moves
|
2017-10-07 17:02:40 +00:00
|
|
|
self._size *= 2
|
|
|
|
self.c = <Transition*>self.mem.realloc(self.c, self._size * sizeof(self.c[0]))
|
|
|
|
self.c[self.n_moves] = self.init_transition(self.n_moves, action, label_id)
|
|
|
|
self.n_moves += 1
|
2018-03-27 17:23:02 +00:00
|
|
|
if self.labels.get(action, []):
|
|
|
|
freq = min(0, min(self.labels[action].values()))
|
|
|
|
self.labels[action][label_name] = freq-1
|
|
|
|
else:
|
|
|
|
self.labels[action] = Counter()
|
|
|
|
self.labels[action][label_name] = -1
|
2017-10-07 17:02:40 +00:00
|
|
|
return 1
|
2017-09-14 14:16:41 +00:00
|
|
|
|
2016-10-27 16:01:55 +00:00
|
|
|
cdef int initialize_state(self, StateC* st) nogil:
|
2017-03-11 17:12:01 +00:00
|
|
|
# This is especially necessary when we use limited training data.
|
2016-10-27 16:01:55 +00:00
|
|
|
for i in range(st.length):
|
|
|
|
if st._sent[i].ent_type != 0:
|
|
|
|
with gil:
|
|
|
|
self.add_action(BEGIN, st._sent[i].ent_type)
|
|
|
|
self.add_action(IN, st._sent[i].ent_type)
|
|
|
|
self.add_action(UNIT, st._sent[i].ent_type)
|
|
|
|
self.add_action(LAST, st._sent[i].ent_type)
|
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
def init_gold(self, StateClass state, Example example):
|
|
|
|
return BiluoGold(self, state, example)
|
|
|
|
|
|
|
|
def has_gold(self, Example eg, start=0, end=None):
|
|
|
|
for word in eg.y[start:end]:
|
|
|
|
if word.ent_iob != 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_cost(self, StateClass stcls, gold, int i):
|
|
|
|
if not isinstance(gold, BiluoGold):
|
2020-10-04 09:16:31 +00:00
|
|
|
raise TypeError(Errors.E909.format(name="BiluoGold"))
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef BiluoGold gold_ = gold
|
|
|
|
gold_state = gold_.c
|
|
|
|
n_gold = 0
|
|
|
|
if self.c[i].is_valid(stcls.c, self.c[i].label):
|
|
|
|
cost = self.c[i].get_cost(stcls, &gold_state, self.c[i].label)
|
|
|
|
else:
|
|
|
|
cost = 9000
|
|
|
|
return cost
|
|
|
|
|
|
|
|
cdef int set_costs(self, int* is_valid, weight_t* costs,
|
|
|
|
StateClass stcls, gold) except -1:
|
|
|
|
if not isinstance(gold, BiluoGold):
|
2020-10-04 09:16:31 +00:00
|
|
|
raise TypeError(Errors.E909.format(name="BiluoGold"))
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef BiluoGold gold_ = gold
|
|
|
|
gold_.update(stcls)
|
|
|
|
gold_state = gold_.c
|
|
|
|
n_gold = 0
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
if self.c[i].is_valid(stcls.c, self.c[i].label):
|
|
|
|
is_valid[i] = 1
|
|
|
|
costs[i] = self.c[i].get_cost(stcls, &gold_state, self.c[i].label)
|
|
|
|
n_gold += costs[i] <= 0
|
|
|
|
else:
|
|
|
|
is_valid[i] = 0
|
|
|
|
costs[i] = 9000
|
|
|
|
if n_gold < 1:
|
|
|
|
raise ValueError
|
|
|
|
|
2015-06-01 21:05:25 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef class Missing:
|
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-06-05 00:27:17 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef int transition(StateC* s, attr_t label) nogil:
|
2015-06-10 05:09:17 +00:00
|
|
|
pass
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@staticmethod
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
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
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-08-05 22:35:40 +00:00
|
|
|
cdef int preset_ent_iob = st.B_(0).ent_iob
|
2019-09-04 11:42:42 +00:00
|
|
|
cdef attr_t preset_ent_label = st.B_(0).ent_type
|
2019-03-10 13:53:03 +00:00
|
|
|
# If we're the last token of the input, we can't B -- must U or O.
|
|
|
|
if st.B(1) == -1:
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
elif st.entity_is_open():
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
elif label == 0:
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
2019-09-18 19:37:17 +00:00
|
|
|
elif preset_ent_iob == 1:
|
2019-03-10 13:53:03 +00:00
|
|
|
# Ensure we don't clobber preset entities. If no entity preset,
|
|
|
|
# ent_iob is 0
|
2015-11-08 15:19:12 +00:00
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
elif preset_ent_iob == 3:
|
|
|
|
# Okay, we're in a preset entity.
|
|
|
|
if label != preset_ent_label:
|
|
|
|
# If label isn't right, reject
|
|
|
|
return False
|
|
|
|
elif st.B_(1).ent_iob != 1:
|
|
|
|
# If next token isn't marked I, we need to make U, not B.
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Otherwise, force acceptance, even if we're across a sentence
|
|
|
|
# boundary or the token is whitespace.
|
|
|
|
return True
|
2019-09-18 19:37:17 +00:00
|
|
|
elif st.B_(1).ent_iob == 3:
|
|
|
|
# If the next word is B, we can't B now
|
2015-11-11 17:48:23 +00:00
|
|
|
return False
|
2018-02-08 14:25:41 +00:00
|
|
|
elif st.B_(1).sent_start == 1:
|
2019-03-10 13:53:03 +00:00
|
|
|
# Don't allow entities to extend across sentence boundaries
|
2015-11-06 18:30:44 +00:00
|
|
|
return False
|
2018-12-07 00:12:12 +00:00
|
|
|
# Don't allow entities to start on whitespace
|
|
|
|
elif Lexeme.get_struct_attr(st.B_(0).lex, IS_SPACE):
|
|
|
|
return False
|
2015-08-05 22:35:40 +00:00
|
|
|
else:
|
2019-03-10 13:53:03 +00:00
|
|
|
return True
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-09 23:35:28 +00:00
|
|
|
st.open_ent(label)
|
|
|
|
st.set_ent_tag(st.B(0), 3, label)
|
|
|
|
st.push()
|
|
|
|
st.pop()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <GoldNERStateC*>_gold
|
2015-06-10 04:57:41 +00:00
|
|
|
cdef int g_act = gold.ner[s.B(0)].move
|
2017-05-28 16:09:27 +00:00
|
|
|
cdef attr_t g_tag = gold.ner[s.B(0)].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
|
2017-04-14 21:52:17 +00:00
|
|
|
# Support partial supervision in the form of "not this label"
|
|
|
|
elif g_act == ISNT:
|
|
|
|
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
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-08-05 22:35:40 +00:00
|
|
|
cdef int preset_ent_iob = st.B_(0).ent_iob
|
2019-09-18 19:37:17 +00:00
|
|
|
cdef attr_t preset_ent_label = st.B_(0).ent_type
|
2019-03-10 13:53:03 +00:00
|
|
|
if label == 0:
|
|
|
|
return False
|
|
|
|
elif st.E_(0).ent_type != label:
|
|
|
|
return False
|
|
|
|
elif not st.entity_is_open():
|
|
|
|
return False
|
|
|
|
elif st.B(1) == -1:
|
|
|
|
# If we're at the end, we can't I.
|
|
|
|
return False
|
2015-08-05 22:35:40 +00:00
|
|
|
elif preset_ent_iob == 3:
|
|
|
|
return False
|
2019-09-18 19:37:17 +00:00
|
|
|
elif st.B_(1).ent_iob == 3:
|
|
|
|
# If we know the next word is B, we can't be I (must be L)
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
2019-09-18 19:37:17 +00:00
|
|
|
elif preset_ent_iob == 1:
|
|
|
|
if st.B_(1).ent_iob in (0, 2):
|
|
|
|
# if next preset is missing or O, this can't be I (must be L)
|
|
|
|
return False
|
|
|
|
elif label != preset_ent_label:
|
|
|
|
# If label isn't right, reject
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Otherwise, force acceptance, even if we're across a sentence
|
|
|
|
# boundary or the token is whitespace.
|
|
|
|
return True
|
2018-12-09 07:06:45 +00:00
|
|
|
elif st.B(1) != -1 and st.B_(1).sent_start == 1:
|
2019-03-10 13:53:03 +00:00
|
|
|
# Don't allow entities to extend across sentence boundaries
|
2015-11-06 18:30:44 +00:00
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
else:
|
|
|
|
return True
|
2017-03-11 17:12:01 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-09 23:35:28 +00:00
|
|
|
st.set_ent_tag(st.B(0), 1, label)
|
|
|
|
st.push()
|
|
|
|
st.pop()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <GoldNERStateC*>_gold
|
2015-06-05 15:11:26 +00:00
|
|
|
move = IN
|
2018-10-26 22:46:30 +00:00
|
|
|
cdef int next_act = gold.ner[s.B(1)].move if s.B(1) >= 0 else OUT
|
2015-06-10 04:57:41 +00:00
|
|
|
cdef int g_act = gold.ner[s.B(0)].move
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef attr_t g_tag = gold.ner[s.B(0)].label
|
2015-06-05 00:27:17 +00:00
|
|
|
cdef bint is_sunk = _entity_is_sunk(s, gold.ner)
|
2017-03-11 17:12:01 +00:00
|
|
|
|
2015-06-05 15:11:26 +00:00
|
|
|
if g_act == MISSING:
|
|
|
|
return 0
|
|
|
|
elif g_act == BEGIN:
|
2017-10-27 17:45:57 +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:
|
2017-10-27 17:45:57 +00:00
|
|
|
# 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
|
2017-07-19 22:18:49 +00:00
|
|
|
# Support partial supervision in the form of "not this label"
|
|
|
|
elif g_act == ISNT:
|
|
|
|
return 0
|
2015-06-05 15:11:26 +00:00
|
|
|
else:
|
|
|
|
return 1
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
cdef class Last:
|
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2019-09-18 19:37:17 +00:00
|
|
|
cdef int preset_ent_iob = st.B_(0).ent_iob
|
|
|
|
cdef attr_t preset_ent_label = st.B_(0).ent_type
|
2019-03-10 13:53:03 +00:00
|
|
|
if label == 0:
|
|
|
|
return False
|
|
|
|
elif not st.entity_is_open():
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
2019-09-18 19:37:17 +00:00
|
|
|
elif preset_ent_iob == 1 and st.B_(1).ent_iob != 1:
|
2019-09-04 11:42:42 +00:00
|
|
|
# If a preset entity has I followed by not-I, is L
|
2019-09-18 19:37:17 +00:00
|
|
|
if label != preset_ent_label:
|
|
|
|
# If label isn't right, reject
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Otherwise, force acceptance, even if we're across a sentence
|
|
|
|
# boundary or the token is whitespace.
|
|
|
|
return True
|
2019-03-10 13:53:03 +00:00
|
|
|
elif st.E_(0).ent_type != label:
|
|
|
|
return False
|
|
|
|
elif st.B_(1).ent_iob == 1:
|
|
|
|
# If a preset entity has I next, we can't L here.
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-09 23:35:28 +00:00
|
|
|
st.close_ent()
|
2015-11-06 19:51:41 +00:00
|
|
|
st.set_ent_tag(st.B(0), 1, label)
|
2015-06-09 23:35:28 +00:00
|
|
|
st.push()
|
|
|
|
st.pop()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <GoldNERStateC*>_gold
|
2015-06-05 15:11:26 +00:00
|
|
|
move = LAST
|
|
|
|
|
2015-06-10 04:57:41 +00:00
|
|
|
cdef int g_act = gold.ner[s.B(0)].move
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef attr_t g_tag = gold.ner[s.B(0)].label
|
2017-03-11 17:12:01 +00:00
|
|
|
|
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
|
2017-07-19 22:18:49 +00:00
|
|
|
# Support partial supervision in the form of "not this label"
|
|
|
|
elif g_act == ISNT:
|
|
|
|
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
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-08-05 22:35:40 +00:00
|
|
|
cdef int preset_ent_iob = st.B_(0).ent_iob
|
2019-03-10 13:53:03 +00:00
|
|
|
cdef attr_t preset_ent_label = st.B_(0).ent_type
|
|
|
|
if label == 0:
|
2019-09-18 19:37:17 +00:00
|
|
|
# this is only allowed if it's a preset blocked annotation
|
|
|
|
if preset_ent_label == 0 and preset_ent_iob == 3:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
elif st.entity_is_open():
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
|
|
|
elif st.B_(1).ent_iob == 1:
|
2019-03-10 13:53:03 +00:00
|
|
|
# If next token is In, we can't be Unit -- must be Begin
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
elif preset_ent_iob == 3:
|
|
|
|
# Okay, there's a preset entity here
|
|
|
|
if label != preset_ent_label:
|
|
|
|
# Require labels to match
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Otherwise return True, ignoring the whitespace constraint.
|
|
|
|
return True
|
2018-12-07 00:12:12 +00:00
|
|
|
elif Lexeme.get_struct_attr(st.B_(0).lex, IS_SPACE):
|
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
else:
|
|
|
|
return True
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-09 23:35:28 +00:00
|
|
|
st.open_ent(label)
|
|
|
|
st.close_ent()
|
|
|
|
st.set_ent_tag(st.B(0), 3, label)
|
|
|
|
st.push()
|
|
|
|
st.pop()
|
2015-06-05 00:27:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <GoldNERStateC*>_gold
|
2015-06-10 04:57:41 +00:00
|
|
|
cdef int g_act = gold.ner[s.B(0)].move
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef attr_t g_tag = gold.ner[s.B(0)].label
|
2015-06-05 00:27:17 +00:00
|
|
|
|
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
|
2017-04-14 21:52:17 +00:00
|
|
|
# Support partial supervision in the form of "not this label"
|
|
|
|
elif g_act == ISNT:
|
|
|
|
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
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef bint is_valid(const StateC* st, attr_t label) nogil:
|
2015-08-05 22:35:40 +00:00
|
|
|
cdef int preset_ent_iob = st.B_(0).ent_iob
|
2019-03-10 13:53:03 +00:00
|
|
|
if st.entity_is_open():
|
|
|
|
return False
|
|
|
|
elif preset_ent_iob == 3:
|
2015-08-05 22:35:40 +00:00
|
|
|
return False
|
|
|
|
elif preset_ent_iob == 1:
|
|
|
|
return False
|
2019-03-10 13:53:03 +00:00
|
|
|
else:
|
|
|
|
return True
|
2015-02-02 05:38:52 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@staticmethod
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef int transition(StateC* st, attr_t label) nogil:
|
2015-06-09 23:35:28 +00:00
|
|
|
st.set_ent_tag(st.B(0), 2, 0)
|
|
|
|
st.push()
|
|
|
|
st.pop()
|
2017-03-11 17:12:01 +00:00
|
|
|
|
2015-06-05 00:27:17 +00:00
|
|
|
@staticmethod
|
2020-06-26 17:34:12 +00:00
|
|
|
cdef weight_t cost(StateClass s, const void* _gold, attr_t label) nogil:
|
|
|
|
gold = <GoldNERStateC*>_gold
|
2015-06-10 04:57:41 +00:00
|
|
|
cdef int g_act = gold.ner[s.B(0)].move
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef attr_t g_tag = gold.ner[s.B(0)].label
|
2015-03-09 05:46:22 +00:00
|
|
|
|
2017-07-19 22:18:49 +00:00
|
|
|
if g_act == ISNT and g_tag == 0:
|
|
|
|
return 1
|
|
|
|
elif g_act == MISSING or g_act == ISNT:
|
2015-06-05 15:11:26 +00:00
|
|
|
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
|