2014-12-21 23:25:43 +03:00
|
|
|
from .typedefs cimport hash_t, flags_t, id_t, len_t, tag_t, attr_t, attr_id_t
|
2015-01-12 03:23:44 +03:00
|
|
|
from .typedefs cimport ID, SIC, NORM1, NORM2, SHAPE, PREFIX, SUFFIX, LENGTH, CLUSTER
|
2015-01-12 02:26:22 +03:00
|
|
|
from .structs cimport LexemeC
|
2014-12-19 22:51:03 +03:00
|
|
|
from .strings cimport StringStore
|
2014-10-09 12:53:30 +04:00
|
|
|
|
2015-01-17 08:21:17 +03:00
|
|
|
from numpy cimport ndarray
|
|
|
|
|
|
|
|
|
2014-10-09 12:53:30 +04:00
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
cdef LexemeC EMPTY_LEXEME
|
2014-09-10 22:41:37 +04:00
|
|
|
|
2014-12-03 03:04:00 +03:00
|
|
|
|
2015-01-17 08:21:17 +03:00
|
|
|
cdef int set_lex_struct_props(LexemeC* lex, dict props, StringStore strings,
|
|
|
|
const float* empty_vec) except -1
|
2014-10-29 15:19:38 +03:00
|
|
|
|
2015-01-12 03:23:44 +03:00
|
|
|
cdef class Lexeme:
|
2015-01-17 08:21:17 +03:00
|
|
|
cdef readonly ndarray vec
|
2015-01-12 03:23:44 +03:00
|
|
|
|
|
|
|
cdef readonly flags_t flags
|
|
|
|
cdef readonly attr_t id
|
|
|
|
cdef readonly attr_t length
|
|
|
|
|
2015-01-14 16:33:16 +03:00
|
|
|
cdef readonly attr_t sic
|
2015-01-17 08:21:17 +03:00
|
|
|
cdef readonly attr_t norm1
|
|
|
|
cdef readonly attr_t norm2
|
|
|
|
cdef readonly attr_t shape
|
|
|
|
cdef readonly attr_t prefix
|
|
|
|
cdef readonly attr_t suffix
|
|
|
|
|
|
|
|
cdef readonly unicode sic_
|
|
|
|
cdef readonly unicode norm1_
|
|
|
|
cdef readonly unicode norm2_
|
|
|
|
cdef readonly unicode shape_
|
|
|
|
cdef readonly unicode prefix_
|
|
|
|
cdef readonly unicode suffix_
|
2015-01-12 03:23:44 +03:00
|
|
|
|
|
|
|
cdef readonly attr_t cluster
|
|
|
|
cdef readonly float prob
|
|
|
|
cdef readonly float sentiment
|
|
|
|
|
2015-01-17 08:21:17 +03:00
|
|
|
# Workaround for an apparent bug in the way the decorator is handled ---
|
|
|
|
# TODO: post bug report / patch to Cython.
|
|
|
|
@staticmethod
|
|
|
|
cdef inline Lexeme from_ptr(const LexemeC* ptr, StringStore strings):
|
|
|
|
cdef Lexeme py = Lexeme.__new__(Lexeme, 300)
|
|
|
|
for i in range(300):
|
2015-01-21 18:03:54 +03:00
|
|
|
py.repvec[i] = ptr.repvec[i]
|
2015-01-17 08:21:17 +03:00
|
|
|
py.flags = ptr.flags
|
|
|
|
py.id = ptr.id
|
|
|
|
py.length = ptr.length
|
|
|
|
|
|
|
|
py.sic = ptr.sic
|
|
|
|
py.norm1 = ptr.norm1
|
|
|
|
py.norm2 = ptr.norm2
|
|
|
|
py.shape = ptr.shape
|
|
|
|
py.prefix = ptr.prefix
|
|
|
|
py.suffix = ptr.suffix
|
|
|
|
|
|
|
|
py.sic_ = strings[ptr.sic]
|
|
|
|
py.norm1_ = strings[ptr.norm1]
|
|
|
|
py.norm2_ = strings[ptr.norm2]
|
|
|
|
py.shape_ = strings[ptr.shape]
|
|
|
|
py.prefix_ = strings[ptr.prefix]
|
|
|
|
py.suffix_ = strings[ptr.suffix]
|
2015-01-12 03:23:44 +03:00
|
|
|
|
2015-01-17 08:21:17 +03:00
|
|
|
py.cluster = ptr.cluster
|
|
|
|
py.prob = ptr.prob
|
|
|
|
py.sentiment = ptr.sentiment
|
|
|
|
return py
|
2015-01-12 03:23:44 +03:00
|
|
|
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
cdef inline bint check_flag(const LexemeC* lexeme, attr_id_t flag_id) nogil:
|
2014-10-23 17:59:17 +04:00
|
|
|
return lexeme.flags & (1 << flag_id)
|
2014-12-03 03:04:00 +03:00
|
|
|
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
cdef inline attr_t get_attr(const LexemeC* lex, attr_id_t feat_name) nogil:
|
2014-12-04 12:46:20 +03:00
|
|
|
if feat_name < (sizeof(flags_t) * 8):
|
|
|
|
return check_flag(lex, feat_name)
|
|
|
|
elif feat_name == ID:
|
|
|
|
return lex.id
|
|
|
|
elif feat_name == SIC:
|
|
|
|
return lex.sic
|
2015-01-12 03:23:44 +03:00
|
|
|
elif feat_name == NORM1:
|
|
|
|
return lex.norm1
|
|
|
|
elif feat_name == NORM2:
|
|
|
|
return lex.norm2
|
2014-12-04 12:46:20 +03:00
|
|
|
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
|