spaCy/spacy/ner/_state.pyx

45 lines
1.0 KiB
Cython
Raw Normal View History

from .bilou_moves cimport BEGIN, UNIT
2014-11-10 08:28:56 +03:00
cdef int begin_entity(State* s, label) except -1:
2014-11-10 09:39:23 +03:00
s.curr.start = s.i
s.curr.label = label
2014-11-10 08:28:56 +03:00
cdef int end_entity(State* s) except -1:
s.curr.end = s.i
s.ents[s.j] = s.curr
s.j += 1
2014-11-10 09:39:23 +03:00
s.curr.start = 0
s.curr.label = -1
s.curr.end = 0
2014-11-10 08:28:56 +03:00
cdef State* init_state(Pool mem, int sent_length) except NULL:
s = <State*>mem.alloc(1, sizeof(State))
s.j = 0
2014-11-10 08:28:56 +03:00
s.ents = <Entity*>mem.alloc(sent_length, sizeof(Entity))
for i in range(sent_length):
s.ents[i].label = -1
2014-11-10 09:39:23 +03:00
s.curr.label = -1
2014-11-10 08:28:56 +03:00
s.tags = <int*>mem.alloc(sent_length, sizeof(int))
s.length = sent_length
return s
cdef bint entity_is_open(State *s) except -1:
2014-11-10 09:39:23 +03:00
return s.curr.label != -1
2014-11-10 08:28:56 +03:00
cdef bint entity_is_sunk(State *s, Move* golds) except -1:
if not entity_is_open(s):
return False
2014-11-10 09:39:23 +03:00
cdef Move* gold = &golds[s.curr.start]
2014-11-10 08:28:56 +03:00
if gold.action != BEGIN and gold.action != UNIT:
return True
2014-11-10 09:39:23 +03:00
elif gold.label != s.curr.label:
2014-11-10 08:28:56 +03:00
return True
else:
return False