2015-07-18 20:39:57 +00:00
|
|
|
from .typedefs cimport attr_t, hash_t, flags_t, len_t, tag_t
|
2015-07-15 22:58:51 +00:00
|
|
|
from .attrs cimport attr_id_t
|
2015-07-16 09:20:08 +00:00
|
|
|
from .attrs cimport ID, ORTH, LOWER, NORM, SHAPE, PREFIX, SUFFIX, LENGTH, CLUSTER
|
2015-07-15 22:58:51 +00:00
|
|
|
|
2015-01-11 23:26:22 +00:00
|
|
|
from .structs cimport LexemeC
|
2014-12-19 19:51:03 +00:00
|
|
|
from .strings cimport StringStore
|
2015-08-23 18:49:18 +00:00
|
|
|
from .vocab cimport Vocab
|
2014-10-09 08:53:30 +00:00
|
|
|
|
2015-01-17 05:21:17 +00:00
|
|
|
from numpy cimport ndarray
|
|
|
|
|
|
|
|
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef LexemeC EMPTY_LEXEME
|
2014-09-10 18:41:37 +00:00
|
|
|
|
2015-01-12 00:23:44 +00:00
|
|
|
cdef class Lexeme:
|
2015-08-22 20:04:34 +00:00
|
|
|
cdef LexemeC* c
|
|
|
|
cdef readonly Vocab vocab
|
2015-01-22 15:08:25 +00:00
|
|
|
cdef readonly attr_t orth
|
2015-01-12 00:23:44 +00:00
|
|
|
|
2015-08-23 18:49:18 +00:00
|
|
|
@staticmethod
|
|
|
|
cdef inline int set_struct_props(Vocab vocab, LexemeC* lex, dict props) except -1:
|
2015-08-22 20:04:34 +00:00
|
|
|
lex.length = props['length']
|
|
|
|
lex.orth = vocab.strings[props['orth']]
|
|
|
|
lex.lower = vocab.strings[props['lower']]
|
|
|
|
lex.norm = vocab.strings[props['norm']]
|
|
|
|
lex.shape = vocab.strings[props['shape']]
|
|
|
|
lex.prefix = vocab.strings[props['prefix']]
|
|
|
|
lex.suffix = vocab.strings[props['suffix']]
|
2015-01-12 00:23:44 +00:00
|
|
|
|
2015-08-22 20:04:34 +00:00
|
|
|
lex.cluster = props['cluster']
|
|
|
|
lex.prob = props['prob']
|
|
|
|
lex.sentiment = props['sentiment']
|
2015-01-17 05:21:17 +00:00
|
|
|
|
2015-08-22 20:04:34 +00:00
|
|
|
lex.flags = props['flags']
|
2015-01-17 05:21:17 +00:00
|
|
|
|
2015-08-22 20:04:34 +00:00
|
|
|
@staticmethod
|
|
|
|
cdef inline attr_t get_struct_attr(const LexemeC* lex, attr_id_t feat_name) nogil:
|
|
|
|
if feat_name < (sizeof(flags_t) * 8):
|
2015-09-06 15:52:32 +00:00
|
|
|
if Lexeme.check_flag(lex, feat_name):
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
2015-08-22 20:04:34 +00:00
|
|
|
elif feat_name == ID:
|
|
|
|
return lex.id
|
|
|
|
elif feat_name == ORTH:
|
|
|
|
return lex.orth
|
|
|
|
elif feat_name == LOWER:
|
|
|
|
return lex.lower
|
|
|
|
elif feat_name == NORM:
|
|
|
|
return lex.norm
|
|
|
|
elif feat_name == SHAPE:
|
|
|
|
return lex.shape
|
|
|
|
elif feat_name == PREFIX:
|
|
|
|
return lex.prefix
|
|
|
|
elif feat_name == SUFFIX:
|
|
|
|
return lex.suffix
|
|
|
|
elif feat_name == LENGTH:
|
|
|
|
return lex.length
|
|
|
|
elif feat_name == CLUSTER:
|
|
|
|
return lex.cluster
|
|
|
|
else:
|
|
|
|
return 0
|
2015-08-23 18:49:18 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline void set_struct_attr(LexemeC* lex, attr_id_t name, attr_t value) nogil:
|
|
|
|
if name < (sizeof(flags_t) * 8):
|
|
|
|
Lexeme.set_flag(lex, name, value)
|
|
|
|
elif name == ID:
|
|
|
|
lex.id = value
|
|
|
|
elif name == LOWER:
|
|
|
|
lex.lower = value
|
|
|
|
elif name == NORM:
|
|
|
|
lex.norm = value
|
|
|
|
elif name == SHAPE:
|
|
|
|
lex.shape = value
|
|
|
|
elif name == PREFIX:
|
|
|
|
lex.prefix = value
|
|
|
|
elif name == SUFFIX:
|
|
|
|
lex.suffix = value
|
|
|
|
elif name == CLUSTER:
|
|
|
|
lex.cluster = value
|
2015-08-22 20:04:34 +00:00
|
|
|
|
2015-08-23 18:49:18 +00:00
|
|
|
@staticmethod
|
2015-08-22 20:04:34 +00:00
|
|
|
cdef inline bint check_flag(const LexemeC* lexeme, attr_id_t flag_id) nogil:
|
2015-09-06 15:52:32 +00:00
|
|
|
if lexeme.flags & (1 << flag_id):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2015-08-23 18:49:18 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
cdef inline bint set_flag(LexemeC* lex, attr_id_t flag_id, int value) nogil:
|
|
|
|
cdef flags_t one = 1
|
|
|
|
if value:
|
|
|
|
lex.flags |= one << flag_id
|
|
|
|
else:
|
|
|
|
lex.flags &= ~(one << flag_id)
|