From 80baa2e3dbd1341f0154f81c32364fd6256107a0 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 20 Nov 2014 19:49:33 +1100 Subject: [PATCH] * Work on beam parser --- setup.py | 2 +- spacy/ner/_state.pxd | 4 +++- spacy/ner/_state.pyx | 32 ++++++++++++++++++-------------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 10ba5b1ae..c67bed4a1 100644 --- a/setup.py +++ b/setup.py @@ -57,7 +57,7 @@ exts = [ Extension("spacy.ner._state", ["spacy/ner/_state.pyx"], language="c++", include_dirs=includes), Extension("spacy.ner.bilou_moves", ["spacy/ner/bilou_moves.pyx"], language="c++", include_dirs=includes), Extension("spacy.ner.io_moves", ["spacy/ner/io_moves.pyx"], language="c++", include_dirs=includes), - Extension("spacy.ner.greedy_parser", ["spacy/ner/greedy_parser.pyx"], language="c++", include_dirs=includes), + #Extension("spacy.ner.greedy_parser", ["spacy/ner/greedy_parser.pyx"], language="c++", include_dirs=includes), Extension("spacy.ner.pystate", ["spacy/ner/pystate.pyx"], language="c++", include_dirs=includes), Extension("spacy.ner.context", ["spacy/ner/context.pyx"], language="c++", include_dirs=includes), Extension("spacy.ner.feats", ["spacy/ner/feats.pyx"], language="c++", include_dirs=includes), diff --git a/spacy/ner/_state.pxd b/spacy/ner/_state.pxd index d9ec1e8f1..2c5815799 100644 --- a/spacy/ner/_state.pxd +++ b/spacy/ner/_state.pxd @@ -10,4 +10,6 @@ cdef int copy_state(Pool mem, State* dest, State* source) except -1 cdef bint entity_is_open(State *s) except -1 -cdef bint entity_is_sunk(State *s, Move* golds) except -1 +cdef int entity_is_sunk(State *s, Move* golds) except -1 + +cdef int is_done(State* s) except -1 diff --git a/spacy/ner/_state.pyx b/spacy/ner/_state.pyx index 7d42cc799..0a63c4d43 100644 --- a/spacy/ner/_state.pyx +++ b/spacy/ner/_state.pyx @@ -1,11 +1,14 @@ -cdef void begin_entity(State* s, label): +from libc.string cimport memcpy + + +cdef int begin_entity(State* s, label) except -1: s.j += 1 s.ents[s.j].start = s.i s.ents[s.j].tag = label s.ents[s.j].end = s.i + 1 -cdef void end_entity(State* s): +cdef int end_entity(State* s) except -1: s.ents[s.j].end = s.i + 1 @@ -16,22 +19,22 @@ cdef State* init_state(Pool mem, int sent_length) except NULL: s.length = sent_length -cdef bint entity_is_open(State *s): +cdef bint entity_is_open(State *s) except -1: return s.ents[s.j].start != 0 -cdef bint entity_is_sunk(State *s, Move* golds): +cdef int entity_is_sunk(State *s, Move* golds) except -1: if not entity_is_open(s): return False - - cdef Entity* ent = &s.ents[s.j] - cdef Move* gold = &golds[ent.start] - if gold.action != BEGIN and gold.action != UNIT: - return True - elif gold.label != ent.label: - return True - else: - return False + raise StandardError + #cdef Entity* ent = &s.ents[s.j] + #cdef Move* gold = &golds[ent.start] + #if gold.action != BEGIN and gold.action != UNIT: + # return True + #elif gold.label != ent.label: + # return True + #else: + # return False cdef int copy_state(Pool mem, State* dest, State* source) except -1: @@ -47,4 +50,5 @@ cdef int copy_state(Pool mem, State* dest, State* source) except -1: dest.curr = source.curr - +cdef int is_done(State* s) except -1: + return s.i >= s.length and not entity_is_open(s)