2015-07-13 20:20:48 +03:00
|
|
|
from libc.string cimport memcpy
|
|
|
|
from cpython.mem cimport PyMem_Malloc, PyMem_Free
|
|
|
|
# Compiler crashes on memory view coercion without this. Should report bug.
|
|
|
|
from cython.view cimport array as cvarray
|
2015-07-13 21:20:58 +03:00
|
|
|
cimport numpy as np
|
|
|
|
np.import_array()
|
|
|
|
|
|
|
|
import numpy
|
|
|
|
|
|
|
|
|
2015-09-06 20:45:15 +03:00
|
|
|
from ..lexeme cimport Lexeme
|
2015-07-13 21:20:58 +03:00
|
|
|
from ..parts_of_speech import UNIV_POS_NAMES
|
|
|
|
|
2015-07-16 12:23:25 +03:00
|
|
|
from ..attrs cimport LEMMA
|
|
|
|
from ..attrs cimport ID, ORTH, NORM, LOWER, SHAPE, PREFIX, SUFFIX, LENGTH, CLUSTER
|
|
|
|
from ..attrs cimport POS, LEMMA, TAG, DEP
|
2015-07-13 21:20:58 +03:00
|
|
|
from ..parts_of_speech cimport CONJ, PUNCT
|
2015-07-13 20:20:48 +03:00
|
|
|
|
2015-07-26 17:37:16 +03:00
|
|
|
from ..attrs cimport IS_ALPHA, IS_ASCII, IS_DIGIT, IS_LOWER, IS_PUNCT, IS_SPACE
|
|
|
|
from ..attrs cimport IS_TITLE, IS_UPPER, LIKE_URL, LIKE_NUM, LIKE_EMAIL, IS_STOP
|
2015-07-27 02:50:06 +03:00
|
|
|
from ..attrs cimport IS_OOV
|
2015-07-26 17:37:16 +03:00
|
|
|
|
2015-08-23 21:49:18 +03:00
|
|
|
from ..lexeme cimport Lexeme
|
|
|
|
|
2015-07-13 20:20:48 +03:00
|
|
|
|
|
|
|
cdef class Token:
|
|
|
|
"""An individual token --- i.e. a word, a punctuation symbol, etc. Created
|
|
|
|
via Doc.__getitem__ and Doc.__iter__.
|
|
|
|
"""
|
2015-07-14 01:10:11 +03:00
|
|
|
def __cinit__(self, Vocab vocab, Doc doc, int offset):
|
2015-07-13 20:20:48 +03:00
|
|
|
self.vocab = vocab
|
2015-07-14 01:10:11 +03:00
|
|
|
self.doc = doc
|
|
|
|
self.c = &self.doc.data[offset]
|
|
|
|
self.i = offset
|
|
|
|
self.array_len = doc.length
|
2015-07-13 20:20:48 +03:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.c.lex.length
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.string
|
|
|
|
|
2015-07-24 04:49:30 +03:00
|
|
|
def __str__(self):
|
|
|
|
return self.string
|
|
|
|
|
2015-07-13 20:20:48 +03:00
|
|
|
cpdef bint check_flag(self, attr_id_t flag_id) except -1:
|
2015-09-06 20:45:15 +03:00
|
|
|
return Lexeme.check_flag(self.c.lex, flag_id)
|
2015-07-13 20:20:48 +03:00
|
|
|
|
|
|
|
def nbor(self, int i=1):
|
2015-07-14 01:10:11 +03:00
|
|
|
return self.doc[self.i+i]
|
2015-07-13 20:20:48 +03:00
|
|
|
|
|
|
|
property lex_id:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.id
|
|
|
|
|
|
|
|
property string:
|
|
|
|
def __get__(self):
|
|
|
|
cdef unicode orth = self.vocab.strings[self.c.lex.orth]
|
|
|
|
if self.c.spacy:
|
|
|
|
return orth + u' '
|
|
|
|
else:
|
|
|
|
return orth
|
|
|
|
|
2015-09-13 03:27:42 +03:00
|
|
|
property text:
|
|
|
|
def __get__(self):
|
|
|
|
return self.orth_
|
|
|
|
|
|
|
|
property text_with_ws:
|
|
|
|
def __get__(self):
|
|
|
|
cdef unicode orth = self.vocab.strings[self.c.lex.orth]
|
|
|
|
if self.c.spacy:
|
|
|
|
return orth + u' '
|
|
|
|
else:
|
|
|
|
return orth
|
|
|
|
|
2015-07-13 20:20:48 +03:00
|
|
|
property prob:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.prob
|
|
|
|
|
|
|
|
property idx:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.idx
|
|
|
|
|
|
|
|
property cluster:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.cluster
|
|
|
|
|
|
|
|
property orth:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.orth
|
|
|
|
|
|
|
|
property lower:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.lower
|
|
|
|
|
|
|
|
property norm:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.norm
|
|
|
|
|
|
|
|
property shape:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.shape
|
|
|
|
|
|
|
|
property prefix:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.prefix
|
|
|
|
|
|
|
|
property suffix:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lex.suffix
|
|
|
|
|
|
|
|
property lemma:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.lemma
|
|
|
|
|
|
|
|
property pos:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.pos
|
|
|
|
|
|
|
|
property tag:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.tag
|
|
|
|
|
|
|
|
property dep:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.dep
|
|
|
|
|
|
|
|
property repvec:
|
|
|
|
def __get__(self):
|
|
|
|
cdef int length = self.vocab.repvec_length
|
|
|
|
repvec_view = <float[:length,]>self.c.lex.repvec
|
|
|
|
return numpy.asarray(repvec_view)
|
|
|
|
|
|
|
|
property n_lefts:
|
|
|
|
def __get__(self):
|
|
|
|
cdef int n = 0
|
|
|
|
cdef const TokenC* ptr = self.c - self.i
|
|
|
|
while ptr != self.c:
|
|
|
|
if ptr + ptr.head == self.c:
|
|
|
|
n += 1
|
|
|
|
ptr += 1
|
|
|
|
return n
|
|
|
|
|
|
|
|
property n_rights:
|
|
|
|
def __get__(self):
|
|
|
|
cdef int n = 0
|
|
|
|
cdef const TokenC* ptr = self.c + (self.array_len - self.i)
|
|
|
|
while ptr != self.c:
|
|
|
|
if ptr + ptr.head == self.c:
|
|
|
|
n += 1
|
|
|
|
ptr -= 1
|
|
|
|
return n
|
|
|
|
|
|
|
|
property lefts:
|
|
|
|
def __get__(self):
|
|
|
|
"""The leftward immediate children of the word, in the syntactic
|
|
|
|
dependency parse.
|
|
|
|
"""
|
2015-09-06 11:48:36 +03:00
|
|
|
cdef const TokenC* ptr = self.c - (self.i - self.c.l_edge)
|
2015-07-13 20:20:48 +03:00
|
|
|
while ptr < self.c:
|
|
|
|
# If this head is still to the right of us, we can skip to it
|
|
|
|
# No token that's between this token and this head could be our
|
|
|
|
# child.
|
|
|
|
if (ptr.head >= 1) and (ptr + ptr.head) < self.c:
|
|
|
|
ptr += ptr.head
|
|
|
|
|
|
|
|
elif ptr + ptr.head == self.c:
|
2015-07-14 01:10:11 +03:00
|
|
|
yield self.doc[ptr - (self.c - self.i)]
|
2015-07-13 20:20:48 +03:00
|
|
|
ptr += 1
|
|
|
|
else:
|
|
|
|
ptr += 1
|
|
|
|
|
|
|
|
property rights:
|
|
|
|
def __get__(self):
|
|
|
|
"""The rightward immediate children of the word, in the syntactic
|
|
|
|
dependency parse."""
|
2015-09-06 11:48:36 +03:00
|
|
|
cdef const TokenC* ptr = self.c + (self.c.r_edge - self.i)
|
2015-07-13 20:20:48 +03:00
|
|
|
tokens = []
|
|
|
|
while ptr > self.c:
|
|
|
|
# If this head is still to the right of us, we can skip to it
|
|
|
|
# No token that's between this token and this head could be our
|
|
|
|
# child.
|
|
|
|
if (ptr.head < 0) and ((ptr + ptr.head) > self.c):
|
|
|
|
ptr += ptr.head
|
|
|
|
elif ptr + ptr.head == self.c:
|
2015-07-14 01:10:11 +03:00
|
|
|
tokens.append(self.doc[ptr - (self.c - self.i)])
|
2015-07-13 20:20:48 +03:00
|
|
|
ptr -= 1
|
|
|
|
else:
|
|
|
|
ptr -= 1
|
|
|
|
tokens.reverse()
|
|
|
|
for t in tokens:
|
|
|
|
yield t
|
|
|
|
|
|
|
|
property children:
|
|
|
|
def __get__(self):
|
|
|
|
yield from self.lefts
|
|
|
|
yield from self.rights
|
|
|
|
|
|
|
|
property subtree:
|
|
|
|
def __get__(self):
|
|
|
|
for word in self.lefts:
|
|
|
|
yield from word.subtree
|
|
|
|
yield self
|
|
|
|
for word in self.rights:
|
|
|
|
yield from word.subtree
|
|
|
|
|
|
|
|
property left_edge:
|
|
|
|
def __get__(self):
|
2015-07-14 01:10:11 +03:00
|
|
|
return self.doc[self.c.l_edge]
|
2015-08-09 00:37:44 +03:00
|
|
|
|
2015-07-13 20:20:48 +03:00
|
|
|
property right_edge:
|
|
|
|
def __get__(self):
|
2015-07-14 01:10:11 +03:00
|
|
|
return self.doc[self.c.r_edge]
|
2015-07-13 20:20:48 +03:00
|
|
|
|
|
|
|
property head:
|
|
|
|
def __get__(self):
|
|
|
|
"""The token predicted by the parser to be the head of the current token."""
|
2015-07-14 01:10:11 +03:00
|
|
|
return self.doc[self.i + self.c.head]
|
2015-08-09 00:37:44 +03:00
|
|
|
|
2015-07-13 20:20:48 +03:00
|
|
|
property conjuncts:
|
|
|
|
def __get__(self):
|
|
|
|
"""Get a list of conjoined words"""
|
|
|
|
cdef Token word
|
|
|
|
conjs = []
|
|
|
|
if self.c.pos != CONJ and self.c.pos != PUNCT:
|
|
|
|
seen_conj = False
|
|
|
|
for word in reversed(list(self.lefts)):
|
|
|
|
if word.c.pos == CONJ:
|
|
|
|
seen_conj = True
|
|
|
|
elif seen_conj and word.c.pos == self.c.pos:
|
|
|
|
conjs.append(word)
|
|
|
|
conjs.reverse()
|
|
|
|
conjs.append(self)
|
|
|
|
if seen_conj:
|
|
|
|
return conjs
|
|
|
|
elif self is not self.head and self in self.head.conjuncts:
|
|
|
|
return self.head.conjuncts
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
property ent_type:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.ent_type
|
|
|
|
|
|
|
|
property ent_iob:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.ent_iob
|
|
|
|
|
|
|
|
property ent_type_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.ent_type]
|
|
|
|
|
|
|
|
property ent_iob_:
|
|
|
|
def __get__(self):
|
|
|
|
iob_strings = ('', 'I', 'O', 'B')
|
|
|
|
return iob_strings[self.c.ent_iob]
|
|
|
|
|
|
|
|
property whitespace_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.string[self.c.lex.length:]
|
|
|
|
|
|
|
|
property orth_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lex.orth]
|
|
|
|
|
|
|
|
property lower_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lex.lower]
|
|
|
|
|
|
|
|
property norm_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lex.norm]
|
|
|
|
|
|
|
|
property shape_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lex.shape]
|
|
|
|
|
|
|
|
property prefix_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lex.prefix]
|
|
|
|
|
|
|
|
property suffix_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lex.suffix]
|
|
|
|
|
|
|
|
property lemma_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.lemma]
|
|
|
|
|
|
|
|
property pos_:
|
|
|
|
def __get__(self):
|
|
|
|
return _pos_id_to_string[self.c.pos]
|
|
|
|
|
|
|
|
property tag_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.tag]
|
|
|
|
|
|
|
|
property dep_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.dep]
|
2015-07-13 21:20:58 +03:00
|
|
|
|
2015-07-27 02:50:06 +03:00
|
|
|
property is_oov:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_OOV)
|
2015-07-27 02:50:06 +03:00
|
|
|
|
2015-07-26 17:37:16 +03:00
|
|
|
property is_alpha:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_ALPHA)
|
2015-08-09 00:37:44 +03:00
|
|
|
|
2015-07-26 17:37:16 +03:00
|
|
|
property is_ascii:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_ASCII)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_digit:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_DIGIT)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_lower:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_LOWER)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_title:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_TITLE)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_punct:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_PUNCT)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_space:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, IS_SPACE)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property like_url:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, LIKE_URL)
|
2015-08-09 00:37:44 +03:00
|
|
|
|
2015-07-26 17:37:16 +03:00
|
|
|
property like_num:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, LIKE_NUM)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property like_email:
|
2015-09-06 20:45:15 +03:00
|
|
|
def __get__(self): return Lexeme.check_flag(self.c.lex, LIKE_EMAIL)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
2015-07-13 21:20:58 +03:00
|
|
|
|
|
|
|
_pos_id_to_string = {id_: string for string, id_ in UNIV_POS_NAMES.items()}
|