2014-09-14 23:31:44 +00:00
|
|
|
# cython: profile=True
|
2014-09-11 14:57:08 +00:00
|
|
|
from libc.stdlib cimport calloc, free, realloc
|
|
|
|
|
|
|
|
from spacy.word cimport Lexeme
|
|
|
|
from spacy.lexeme cimport lexeme_check_flag
|
|
|
|
from spacy.lexeme cimport lexeme_string_view
|
|
|
|
|
|
|
|
|
2014-09-14 23:31:44 +00:00
|
|
|
cdef enum Flags:
|
|
|
|
Flag_IsAlpha
|
|
|
|
Flag_IsAscii
|
|
|
|
Flag_IsDigit
|
|
|
|
Flag_IsLower
|
|
|
|
Flag_IsPunct
|
|
|
|
Flag_IsSpace
|
|
|
|
Flag_IsTitle
|
|
|
|
Flag_IsUpper
|
|
|
|
|
|
|
|
Flag_CanAdj
|
|
|
|
Flag_CanAdp
|
|
|
|
Flag_CanAdv
|
|
|
|
Flag_CanConj
|
|
|
|
Flag_CanDet
|
|
|
|
Flag_CanNoun
|
|
|
|
Flag_CanNum
|
|
|
|
Flag_CanPdt
|
|
|
|
Flag_CanPos
|
|
|
|
Flag_CanPron
|
|
|
|
Flag_CanPrt
|
|
|
|
Flag_CanPunct
|
|
|
|
Flag_CanVerb
|
|
|
|
|
|
|
|
Flag_OftLower
|
|
|
|
Flag_OftTitle
|
|
|
|
Flag_OftUpper
|
|
|
|
Flag_N
|
|
|
|
|
|
|
|
|
|
|
|
cdef enum Views:
|
|
|
|
View_CanonForm
|
|
|
|
View_WordShape
|
|
|
|
View_NonSparse
|
|
|
|
View_Asciied
|
|
|
|
View_N
|
|
|
|
|
|
|
|
|
2014-09-10 16:11:13 +00:00
|
|
|
cdef class Tokens:
|
|
|
|
"""A sequence of references to Lexeme objects.
|
|
|
|
|
|
|
|
The Tokens class provides fast and memory-efficient access to lexical features,
|
|
|
|
and can efficiently export the data to a numpy array. Specific languages
|
|
|
|
create their own Tokens subclasses, to provide more convenient access to
|
|
|
|
language-specific features.
|
|
|
|
|
|
|
|
>>> from spacy.en import EN
|
|
|
|
>>> tokens = EN.tokenize('An example sentence.')
|
|
|
|
>>> tokens.string(0)
|
|
|
|
'An'
|
|
|
|
>>> tokens.prob(0) > tokens.prob(1)
|
|
|
|
True
|
|
|
|
>>> tokens.can_noun(0)
|
|
|
|
False
|
|
|
|
>>> tokens.can_noun(1)
|
|
|
|
True
|
|
|
|
"""
|
2014-09-11 22:18:31 +00:00
|
|
|
def __cinit__(self, string_length=0):
|
|
|
|
size = int(string_length / 3) if string_length >= 3 else 1
|
2014-09-15 01:22:40 +00:00
|
|
|
self.v.reserve(size)
|
2014-09-11 22:18:31 +00:00
|
|
|
|
2014-09-11 19:37:32 +00:00
|
|
|
def __getitem__(self, i):
|
2014-09-15 01:22:40 +00:00
|
|
|
return Lexeme(<size_t>self.v.at(i))
|
2014-09-11 19:37:32 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2014-09-15 01:22:40 +00:00
|
|
|
return self.v.size()
|
2014-09-11 19:37:32 +00:00
|
|
|
|
2014-09-11 14:57:08 +00:00
|
|
|
def append(self, Lexeme lexeme):
|
2014-09-15 01:22:40 +00:00
|
|
|
self.v.push_back(lexeme._c)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef unicode string(self, size_t i):
|
2014-09-15 01:22:40 +00:00
|
|
|
cdef bytes utf8_string = self.v[i].string[:self.v[i].length]
|
2014-09-14 23:31:44 +00:00
|
|
|
cdef unicode string = utf8_string.decode('utf8')
|
|
|
|
return string
|
2014-09-10 16:11:13 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef size_t id(self, size_t i) except 0:
|
|
|
|
return <size_t>&self.v.at(i).string
|
2014-09-12 13:49:36 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef double prob(self, size_t i) except 1:
|
|
|
|
return self.v.at(i).prob
|
2014-09-10 16:11:13 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef size_t cluster(self, size_t i) except *:
|
|
|
|
return self.v.at(i).cluster
|
2014-09-10 16:11:13 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint check_flag(self, size_t i, size_t flag_id) except *:
|
|
|
|
return lexeme_check_flag(self.v[i], flag_id)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef unicode string_view(self, size_t i, size_t view_id):
|
2014-09-15 01:22:40 +00:00
|
|
|
return lexeme_string_view(self.v.at(i), view_id)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
|
|
|
# Provide accessor methods for the features supported by the language.
|
|
|
|
# Without these, clients have to use the underlying string_view and check_flag
|
|
|
|
# methods, which requires them to know the IDs.
|
|
|
|
cpdef unicode canon_string(self, size_t i):
|
2014-09-15 01:22:40 +00:00
|
|
|
return lexeme_string_view(self.v.at(i), View_CanonForm)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
|
|
|
cpdef unicode shape_string(self, size_t i):
|
2014-09-15 01:22:40 +00:00
|
|
|
return lexeme_string_view(self.v.at(i), View_WordShape)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
|
|
|
cpdef unicode non_sparse_string(self, size_t i):
|
2014-09-15 01:22:40 +00:00
|
|
|
return lexeme_string_view(self.v.at(i), View_NonSparse)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
|
|
|
cpdef unicode asciied_string(self, size_t i):
|
2014-09-15 01:22:40 +00:00
|
|
|
return lexeme_string_view(self.v.at(i), View_Asciied)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef size_t canon(self, size_t i) except *:
|
|
|
|
return id(self.v.at(i).views[<size_t>View_CanonForm])
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef size_t shape(self, size_t i) except *:
|
|
|
|
return id(self.v.at(i).views[<size_t>View_WordShape])
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef size_t non_sparse(self, size_t i) except *:
|
|
|
|
return id(self.v.at(i).views[<size_t>View_NonSparse])
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef size_t asciied(self, size_t i) except *:
|
|
|
|
return id(self.v.at(i).views[<size_t>View_Asciied])
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_alpha(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsAlpha)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_ascii(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsAscii)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_digit(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsDigit)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_lower(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsLower)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_punct(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsPunct)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_space(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsSpace)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_title(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsTitle)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint is_upper(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_IsUpper)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_adj(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanAdj)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_adp(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanAdp)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_adv(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanAdv)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_conj(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanConj)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_det(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanDet)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_noun(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanNoun)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_num(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanNum)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_pdt(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanPdt)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_pos(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanPos)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_pron(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanPron)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_prt(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanPrt)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_punct(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanPunct)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint can_verb(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_CanVerb)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint oft_lower(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_OftLower)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint oft_title(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_OftTitle)
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
cpdef bint oft_upper(self, size_t i) except *:
|
|
|
|
return lexeme_check_flag(self.v.at(i), Flag_OftUpper)
|