2016-10-14 01:24:13 +00:00
|
|
|
from numpy cimport ndarray
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2016-11-25 10:35:17 +00:00
|
|
|
from ..attrs cimport *
|
2023-06-14 15:48:41 +00:00
|
|
|
from ..lexeme cimport Lexeme
|
2016-11-25 11:41:47 +00:00
|
|
|
from ..parts_of_speech cimport univ_pos_t
|
2023-06-14 15:48:41 +00:00
|
|
|
from ..structs cimport TokenC
|
|
|
|
from ..typedefs cimport attr_t, flags_t
|
|
|
|
from ..vocab cimport Vocab
|
2015-07-13 22:10:11 +00:00
|
|
|
from .doc cimport Doc
|
2020-03-02 10:48:10 +00:00
|
|
|
|
2018-04-03 13:50:31 +00:00
|
|
|
from ..errors import Errors
|
2015-07-13 17:20:48 +00:00
|
|
|
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2021-01-13 13:20:05 +00:00
|
|
|
cdef int MISSING_DEP = 0
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
cdef class Token:
|
2016-09-21 12:54:55 +00:00
|
|
|
cdef readonly Vocab vocab
|
2016-03-11 16:31:06 +00:00
|
|
|
cdef TokenC* c
|
2015-07-13 17:20:48 +00:00
|
|
|
cdef readonly int i
|
2015-07-13 22:10:11 +00:00
|
|
|
cdef readonly Doc doc
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2015-07-13 22:10:11 +00:00
|
|
|
cdef inline Token cinit(Vocab vocab, const TokenC* token, int offset, Doc doc):
|
|
|
|
if offset < 0 or offset >= doc.length:
|
2018-04-03 13:50:31 +00:00
|
|
|
raise IndexError(Errors.E040.format(i=offset, max_length=doc.length))
|
2015-07-13 22:10:11 +00:00
|
|
|
cdef Token self = Token.__new__(Token, vocab, doc, offset)
|
2015-07-13 17:20:48 +00:00
|
|
|
return self
|
|
|
|
|
2016-11-25 10:35:17 +00:00
|
|
|
#cdef inline TokenC struct_from_attrs(Vocab vocab, attrs):
|
|
|
|
# cdef TokenC token
|
|
|
|
# attrs = normalize_attrs(attrs)
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
cpdef bint check_flag(self, attr_id_t flag_id) except -1
|
2016-11-25 10:35:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline attr_t get_struct_attr(const TokenC* token, attr_id_t feat_name) nogil:
|
|
|
|
if feat_name < (sizeof(flags_t) * 8):
|
|
|
|
return Lexeme.c_check_flag(token.lex, feat_name)
|
|
|
|
elif feat_name == LEMMA:
|
|
|
|
return token.lemma
|
2018-12-08 09:49:10 +00:00
|
|
|
elif feat_name == NORM:
|
|
|
|
if token.norm == 0:
|
|
|
|
return token.lex.norm
|
|
|
|
else:
|
|
|
|
return token.norm
|
2016-11-25 10:35:17 +00:00
|
|
|
elif feat_name == POS:
|
|
|
|
return token.pos
|
|
|
|
elif feat_name == TAG:
|
|
|
|
return token.tag
|
2020-01-29 16:45:46 +00:00
|
|
|
elif feat_name == MORPH:
|
|
|
|
return token.morph
|
2016-11-25 10:35:17 +00:00
|
|
|
elif feat_name == DEP:
|
|
|
|
return token.dep
|
|
|
|
elif feat_name == HEAD:
|
|
|
|
return token.head
|
|
|
|
elif feat_name == SPACY:
|
|
|
|
return token.spacy
|
|
|
|
elif feat_name == ENT_IOB:
|
|
|
|
return token.ent_iob
|
|
|
|
elif feat_name == ENT_TYPE:
|
|
|
|
return token.ent_type
|
2020-01-06 13:57:34 +00:00
|
|
|
elif feat_name == ENT_ID:
|
|
|
|
return token.ent_id
|
2019-06-25 13:28:51 +00:00
|
|
|
elif feat_name == ENT_KB_ID:
|
|
|
|
return token.ent_kb_id
|
2018-01-28 18:50:42 +00:00
|
|
|
elif feat_name == SENT_START:
|
|
|
|
return token.sent_start
|
2016-11-25 10:35:17 +00:00
|
|
|
else:
|
|
|
|
return Lexeme.get_struct_attr(token.lex, feat_name)
|
2016-11-25 11:41:47 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline attr_t set_struct_attr(TokenC* token, attr_id_t feat_name,
|
|
|
|
attr_t value) nogil:
|
|
|
|
if feat_name == LEMMA:
|
|
|
|
token.lemma = value
|
2018-12-08 09:49:10 +00:00
|
|
|
elif feat_name == NORM:
|
|
|
|
token.norm = value
|
2016-11-25 11:41:47 +00:00
|
|
|
elif feat_name == POS:
|
|
|
|
token.pos = <univ_pos_t>value
|
|
|
|
elif feat_name == TAG:
|
|
|
|
token.tag = value
|
2020-01-29 16:45:46 +00:00
|
|
|
elif feat_name == MORPH:
|
|
|
|
token.morph = value
|
2016-11-25 11:41:47 +00:00
|
|
|
elif feat_name == DEP:
|
|
|
|
token.dep = value
|
|
|
|
elif feat_name == HEAD:
|
|
|
|
token.head = value
|
|
|
|
elif feat_name == SPACY:
|
|
|
|
token.spacy = value
|
|
|
|
elif feat_name == ENT_IOB:
|
|
|
|
token.ent_iob = value
|
|
|
|
elif feat_name == ENT_TYPE:
|
|
|
|
token.ent_type = value
|
2020-01-06 13:57:34 +00:00
|
|
|
elif feat_name == ENT_ID:
|
|
|
|
token.ent_id = value
|
2019-06-25 13:28:51 +00:00
|
|
|
elif feat_name == ENT_KB_ID:
|
|
|
|
token.ent_kb_id = value
|
2018-01-28 18:50:42 +00:00
|
|
|
elif feat_name == SENT_START:
|
|
|
|
token.sent_start = value
|
2021-01-13 12:47:25 +00:00
|
|
|
|
2021-01-13 13:20:05 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline int missing_dep(const TokenC* token) nogil:
|
|
|
|
return token.dep == MISSING_DEP
|
|
|
|
|
|
|
|
|
2021-01-13 12:47:25 +00:00
|
|
|
@staticmethod
|
|
|
|
cdef inline int missing_head(const TokenC* token) nogil:
|
2021-01-13 13:20:05 +00:00
|
|
|
return Token.missing_dep(token)
|