mirror of
https://github.com/explosion/spaCy.git
synced 2025-01-28 02:04:07 +03:00
* Add base class to do transitions
This commit is contained in:
parent
135756ac3d
commit
1cc6329b18
25
spacy/_transitions.pxd
Normal file
25
spacy/_transitions.pxd
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
cdef struct Transition:
|
||||||
|
int clas
|
||||||
|
int move
|
||||||
|
int label
|
||||||
|
|
||||||
|
weight_t score
|
||||||
|
int cost
|
||||||
|
|
||||||
|
int (*get_cost)(const Transition* self, const State* state, const TokenC* gold) except -1
|
||||||
|
|
||||||
|
int (*is_valid)(const Transition* self, const State* state) except -1
|
||||||
|
|
||||||
|
int (*do)(const Transition* self, State* state) except -1
|
||||||
|
|
||||||
|
|
||||||
|
cdef class TransitionSystem:
|
||||||
|
cdef readonly dict label_ids
|
||||||
|
cdef Pool mem
|
||||||
|
cdef const Transition* c
|
||||||
|
|
||||||
|
cdef const Transition best_valid(self, const weight_t*, const State*) except *
|
||||||
|
|
||||||
|
cdef const Transition best_gold(self, const weight_t*, const State*,
|
||||||
|
const TokenC*) except *
|
||||||
|
|
44
spacy/_transitions.pyx
Normal file
44
spacy/_transitions.pyx
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
from cymem.cymem cimport Pool
|
||||||
|
from ._state cimport State
|
||||||
|
from ..structs cimport TokenC
|
||||||
|
from thinc.typedefs cimport weight_t
|
||||||
|
|
||||||
|
|
||||||
|
cdef weight_t MIN_SCORE = -90000
|
||||||
|
|
||||||
|
cdef class TransitionSystem:
|
||||||
|
cdef Pool mem
|
||||||
|
cdef const Transition* c
|
||||||
|
|
||||||
|
def __init__(self, dict move_types):
|
||||||
|
entries = []
|
||||||
|
for move, labels in move_types.items():
|
||||||
|
entries.extend((move, label) for label in labels)
|
||||||
|
entries.sort()
|
||||||
|
moves = <Transition*>self.mem.alloc(len(entries), sizeof(Transition))
|
||||||
|
for i, (move, label_str) in enumerate(entries):
|
||||||
|
moves[i].move = move
|
||||||
|
moves[i].label = self.label_ids.setdefault(label_str, len(self.label_ids))
|
||||||
|
moves[i].clas
|
||||||
|
|
||||||
|
cdef const Transition best_valid(self, const weight_t* scores,
|
||||||
|
const State* s) except *:
|
||||||
|
cdef int best = -1
|
||||||
|
cdef weight_t score = MIN_SCORE
|
||||||
|
cdef int i
|
||||||
|
for i in range(self.n_moves):
|
||||||
|
if scores[i] > score and self.c[i].is_valid(&self.c[i], s):
|
||||||
|
best = i
|
||||||
|
score = scores[i]
|
||||||
|
return self.c[best]
|
||||||
|
|
||||||
|
cdef const Transition best_gold(self, const weight_t* scores, const State* s,
|
||||||
|
const TokenC* gold) except *:
|
||||||
|
cdef int best = -1
|
||||||
|
cdef weight_t score = MIN_SCORE
|
||||||
|
cdef int i
|
||||||
|
for i in range(self.n_moves):
|
||||||
|
if scores[i] > score and self.c[i].get_cost(&self.c[i], s, gold) == 0:
|
||||||
|
best = i
|
||||||
|
score = scores[i]
|
||||||
|
return self.c[best]
|
Loading…
Reference in New Issue
Block a user