2014-12-30 10:22:00 +00:00
|
|
|
# cython: embedsignature=True
|
2014-12-27 07:45:16 +00:00
|
|
|
|
2014-12-04 09:46:55 +00:00
|
|
|
from preshed.maps cimport PreshMap
|
|
|
|
from preshed.counter cimport PreshCounter
|
|
|
|
|
2014-12-24 06:42:00 +00:00
|
|
|
from .vocab cimport EMPTY_LEXEME
|
2014-12-21 20:25:43 +00:00
|
|
|
from .typedefs cimport attr_id_t, attr_t
|
|
|
|
from .typedefs cimport LEMMA
|
2015-01-23 19:17:03 +00:00
|
|
|
from .typedefs cimport ID, ORTH, NORM, LOWER, SHAPE, PREFIX, SUFFIX, LENGTH, CLUSTER
|
2014-12-24 06:42:00 +00:00
|
|
|
from .typedefs cimport POS, LEMMA
|
2015-01-25 05:31:07 +00:00
|
|
|
from .parts_of_speech import UNIV_POS_NAMES
|
2015-03-26 14:06:26 +00:00
|
|
|
from .lexeme cimport check_flag
|
2014-12-24 06:42:00 +00:00
|
|
|
|
2015-01-21 15:05:37 +00:00
|
|
|
from unidecode import unidecode
|
|
|
|
|
2015-01-19 08:59:55 +00:00
|
|
|
cimport numpy
|
|
|
|
import numpy
|
|
|
|
|
2014-10-22 01:55:42 +00:00
|
|
|
cimport cython
|
2014-12-03 00:05:15 +00:00
|
|
|
|
2015-02-11 23:05:06 +00:00
|
|
|
from cpython.mem cimport PyMem_Malloc, PyMem_Free
|
|
|
|
from libc.string cimport memcpy
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-10-22 14:57:59 +00:00
|
|
|
DEF PADDING = 5
|
|
|
|
|
2014-11-04 16:42:14 +00:00
|
|
|
|
2014-10-22 14:57:59 +00:00
|
|
|
cdef int bounds_check(int i, int length, int padding) except -1:
|
|
|
|
if (i + padding) < 0:
|
|
|
|
raise IndexError
|
|
|
|
if (i - padding) >= length:
|
|
|
|
raise IndexError
|
|
|
|
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-12-24 06:42:00 +00:00
|
|
|
cdef attr_t get_token_attr(const TokenC* token, attr_id_t feat_name) nogil:
|
|
|
|
if feat_name == LEMMA:
|
|
|
|
return token.lemma
|
|
|
|
elif feat_name == POS:
|
|
|
|
return token.pos
|
|
|
|
else:
|
|
|
|
return get_lex_attr(token.lex, feat_name)
|
|
|
|
|
|
|
|
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef attr_t get_lex_attr(const LexemeC* lex, attr_id_t feat_name) nogil:
|
2014-12-24 06:42:00 +00:00
|
|
|
if feat_name < (sizeof(flags_t) * 8):
|
|
|
|
return check_flag(lex, feat_name)
|
|
|
|
elif feat_name == ID:
|
|
|
|
return lex.id
|
2015-01-22 15:08:25 +00:00
|
|
|
elif feat_name == ORTH:
|
|
|
|
return lex.orth
|
2015-01-23 19:17:03 +00:00
|
|
|
elif feat_name == LOWER:
|
|
|
|
return lex.lower
|
|
|
|
elif feat_name == NORM:
|
|
|
|
return lex.norm
|
2014-12-24 06:42:00 +00: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
|
|
|
|
|
|
|
|
|
2014-09-10 16:11:13 +00:00
|
|
|
cdef class Tokens:
|
|
|
|
"""
|
2015-01-27 07:57:52 +00:00
|
|
|
Container class for annotated text. Constructed via English.__call__ or
|
|
|
|
Tokenizer.__call__.
|
|
|
|
"""
|
|
|
|
def __cinit__(self, Vocab vocab, unicode string):
|
2014-12-21 20:25:43 +00:00
|
|
|
self.vocab = vocab
|
2015-01-21 07:56:32 +00:00
|
|
|
self._string = string
|
|
|
|
string_length = len(string)
|
2014-10-22 14:57:59 +00:00
|
|
|
if string_length >= 3:
|
|
|
|
size = int(string_length / 3.0)
|
|
|
|
else:
|
|
|
|
size = 5
|
|
|
|
self.mem = Pool()
|
|
|
|
# Guarantee self.lex[i-x], for any i >= 0 and x < padding is in bounds
|
|
|
|
# However, we need to remember the true starting places, so that we can
|
|
|
|
# realloc.
|
2014-12-05 05:31:30 +00:00
|
|
|
data_start = <TokenC*>self.mem.alloc(size + (PADDING*2), sizeof(TokenC))
|
2014-10-22 17:01:17 +00:00
|
|
|
cdef int i
|
|
|
|
for i in range(size + (PADDING*2)):
|
2014-12-07 11:07:41 +00:00
|
|
|
data_start[i].lex = &EMPTY_LEXEME
|
2014-12-05 05:31:30 +00:00
|
|
|
self.data = data_start + PADDING
|
2014-10-22 14:57:59 +00:00
|
|
|
self.max_length = size
|
|
|
|
self.length = 0
|
2015-01-25 04:33:54 +00:00
|
|
|
self.is_tagged = False
|
|
|
|
self.is_parsed = False
|
2015-01-31 02:42:58 +00:00
|
|
|
self._py_tokens = []
|
2014-10-14 07:22:41 +00:00
|
|
|
|
2015-01-31 02:42:58 +00:00
|
|
|
def __getitem__(self, object i):
|
2014-12-27 07:45:16 +00:00
|
|
|
"""Retrieve a token.
|
|
|
|
|
2015-01-30 07:04:41 +00:00
|
|
|
The Python Token objects are created lazily from internal C data, and
|
|
|
|
cached in _py_tokens
|
|
|
|
|
2014-12-27 07:45:16 +00:00
|
|
|
Returns:
|
|
|
|
token (Token):
|
|
|
|
"""
|
2015-01-19 14:16:29 +00:00
|
|
|
if i < 0:
|
2015-02-07 17:58:46 +00:00
|
|
|
i = self.length + i
|
2014-10-22 14:57:59 +00:00
|
|
|
bounds_check(i, self.length, PADDING)
|
2015-01-31 11:10:22 +00:00
|
|
|
return Token.cinit(self.vocab, self._string,
|
2015-01-31 02:42:58 +00:00
|
|
|
&self.data[i], i, self.length,
|
2015-03-14 18:53:50 +00:00
|
|
|
self)
|
2014-09-11 19:37:32 +00:00
|
|
|
|
2014-11-03 14:07:08 +00:00
|
|
|
def __iter__(self):
|
2014-12-27 07:45:16 +00:00
|
|
|
"""Iterate over the tokens.
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
token (Token):
|
|
|
|
"""
|
2014-11-03 14:07:08 +00:00
|
|
|
for i in range(self.length):
|
2015-01-31 11:10:22 +00:00
|
|
|
yield Token.cinit(self.vocab, self._string,
|
2015-01-31 02:42:58 +00:00
|
|
|
&self.data[i], i, self.length,
|
2015-03-14 18:53:50 +00:00
|
|
|
self)
|
2014-11-03 14:07:08 +00:00
|
|
|
|
2014-09-11 19:37:32 +00:00
|
|
|
def __len__(self):
|
2014-10-22 14:57:59 +00:00
|
|
|
return self.length
|
2014-09-11 19:37:32 +00:00
|
|
|
|
2015-01-21 07:56:32 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
cdef const TokenC* last = &self.data[self.length - 1]
|
|
|
|
return self._string[:last.idx + last.lex.length]
|
|
|
|
|
2015-03-10 17:00:23 +00:00
|
|
|
property ents:
|
|
|
|
def __get__(self):
|
|
|
|
cdef int i
|
|
|
|
cdef const TokenC* token
|
|
|
|
cdef int start = -1
|
|
|
|
cdef object label = None
|
|
|
|
for i in range(self.length):
|
|
|
|
token = &self.data[i]
|
|
|
|
if token.ent_iob == 1:
|
|
|
|
assert start != -1
|
|
|
|
pass
|
|
|
|
elif token.ent_iob == 2:
|
|
|
|
if start != -1:
|
|
|
|
yield (start, i, label)
|
|
|
|
start = -1
|
|
|
|
label = None
|
|
|
|
elif token.ent_iob == 3:
|
|
|
|
start = i
|
2015-03-14 15:06:35 +00:00
|
|
|
label = self.vocab.strings[token.ent_type]
|
2015-03-10 17:00:23 +00:00
|
|
|
if start != -1:
|
|
|
|
yield (start, self.length, label)
|
|
|
|
|
2014-12-09 05:50:01 +00:00
|
|
|
cdef int push_back(self, int idx, LexemeOrToken lex_or_tok) except -1:
|
2014-10-22 14:57:59 +00:00
|
|
|
if self.length == self.max_length:
|
|
|
|
self._realloc(self.length * 2)
|
2014-12-05 04:56:14 +00:00
|
|
|
cdef TokenC* t = &self.data[self.length]
|
2014-12-09 05:50:01 +00:00
|
|
|
if LexemeOrToken is TokenC_ptr:
|
|
|
|
t[0] = lex_or_tok[0]
|
|
|
|
else:
|
|
|
|
t.lex = lex_or_tok
|
2014-12-24 06:42:00 +00:00
|
|
|
t.idx = idx
|
2014-10-22 14:57:59 +00:00
|
|
|
self.length += 1
|
2015-01-30 07:04:41 +00:00
|
|
|
self._py_tokens.append(None)
|
2014-12-09 05:50:01 +00:00
|
|
|
return idx + t.lex.length
|
2014-10-14 04:21:03 +00:00
|
|
|
|
2014-12-04 09:46:55 +00:00
|
|
|
@cython.boundscheck(False)
|
2015-01-24 06:29:04 +00:00
|
|
|
cpdef long[:,:] to_array(self, object py_attr_ids):
|
2014-12-27 10:46:04 +00:00
|
|
|
"""Given a list of M attribute IDs, export the tokens to a numpy ndarray
|
|
|
|
of shape N*M, where N is the length of the sentence.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
attr_ids (list[int]): A list of attribute ID ints.
|
|
|
|
|
|
|
|
Returns:
|
2015-01-14 13:33:16 +00:00
|
|
|
feat_array (numpy.ndarray[long, ndim=2]):
|
|
|
|
A feature matrix, with one row per word, and one column per attribute
|
|
|
|
indicated in the input attr_ids.
|
2014-12-27 10:46:04 +00:00
|
|
|
"""
|
2014-12-02 12:48:05 +00:00
|
|
|
cdef int i, j
|
2014-12-04 09:46:55 +00:00
|
|
|
cdef attr_id_t feature
|
2015-01-24 06:29:04 +00:00
|
|
|
cdef numpy.ndarray[long, ndim=2] output
|
|
|
|
# Make an array from the attributes --- otherwise our inner loop is Python
|
|
|
|
# dict iteration.
|
|
|
|
cdef numpy.ndarray[long, ndim=1] attr_ids = numpy.asarray(py_attr_ids)
|
|
|
|
output = numpy.ndarray(shape=(self.length, len(attr_ids)), dtype=numpy.int)
|
2014-12-02 12:48:05 +00:00
|
|
|
for i in range(self.length):
|
2014-12-04 09:46:55 +00:00
|
|
|
for j, feature in enumerate(attr_ids):
|
2014-12-24 06:42:00 +00:00
|
|
|
output[i, j] = get_token_attr(&self.data[i], feature)
|
2014-12-02 12:48:05 +00:00
|
|
|
return output
|
|
|
|
|
2015-01-21 15:05:37 +00:00
|
|
|
def count_by(self, attr_id_t attr_id, exclude=None):
|
2014-12-27 10:46:04 +00:00
|
|
|
"""Produce a dict of {attribute (int): count (ints)} frequencies, keyed
|
|
|
|
by the values of the given attribute ID.
|
|
|
|
|
|
|
|
>>> from spacy.en import English, attrs
|
|
|
|
>>> nlp = English()
|
|
|
|
>>> tokens = nlp(u'apple apple orange banana')
|
2015-01-22 15:08:25 +00:00
|
|
|
>>> tokens.count_by(attrs.ORTH)
|
2014-12-27 10:46:04 +00:00
|
|
|
{12800L: 1, 11880L: 2, 7561L: 1}
|
2015-01-22 15:08:25 +00:00
|
|
|
>>> tokens.to_array([attrs.ORTH])
|
2014-12-27 10:46:04 +00:00
|
|
|
array([[11880],
|
|
|
|
[11880],
|
|
|
|
[ 7561],
|
|
|
|
[12800]])
|
|
|
|
"""
|
2014-12-04 09:46:55 +00:00
|
|
|
cdef int i
|
|
|
|
cdef attr_t attr
|
|
|
|
cdef size_t count
|
|
|
|
|
|
|
|
cdef PreshCounter counts = PreshCounter(2 ** 8)
|
|
|
|
for i in range(self.length):
|
2015-01-21 15:05:37 +00:00
|
|
|
if exclude is not None and exclude(self[i]):
|
|
|
|
continue
|
2014-12-24 06:42:00 +00:00
|
|
|
attr = get_token_attr(&self.data[i], attr_id)
|
2014-12-04 09:46:55 +00:00
|
|
|
counts.inc(attr, 1)
|
|
|
|
return dict(counts)
|
|
|
|
|
2014-10-23 13:59:17 +00:00
|
|
|
def _realloc(self, new_size):
|
|
|
|
self.max_length = new_size
|
|
|
|
n = new_size + (PADDING * 2)
|
2014-12-07 11:07:41 +00:00
|
|
|
# What we're storing is a "padded" array. We've jumped forward PADDING
|
|
|
|
# places, and are storing the pointer to that. This way, we can access
|
|
|
|
# words out-of-bounds, and get out-of-bounds markers.
|
|
|
|
# Now that we want to realloc, we need the address of the true start,
|
|
|
|
# so we jump the pointer back PADDING places.
|
2014-12-05 05:31:30 +00:00
|
|
|
cdef TokenC* data_start = self.data - PADDING
|
|
|
|
data_start = <TokenC*>self.mem.realloc(data_start, n * sizeof(TokenC))
|
|
|
|
self.data = data_start + PADDING
|
2014-12-05 04:56:14 +00:00
|
|
|
cdef int i
|
2014-10-23 13:59:17 +00:00
|
|
|
for i in range(self.length, self.max_length + PADDING):
|
2014-12-07 11:07:41 +00:00
|
|
|
self.data[i].lex = &EMPTY_LEXEME
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2015-01-31 02:42:58 +00:00
|
|
|
@property
|
|
|
|
def sents(self):
|
|
|
|
"""This is really only a place-holder for a proper solution."""
|
|
|
|
cdef int i
|
|
|
|
cdef Tokens sent = Tokens(self.vocab, self._string[self.data[0].idx:])
|
|
|
|
start = None
|
|
|
|
for i in range(self.length):
|
|
|
|
if start is None:
|
|
|
|
start = i
|
2015-02-02 01:17:32 +00:00
|
|
|
if self.data[i].sent_end:
|
2015-03-13 23:21:16 +00:00
|
|
|
yield Span(self, start, i+1)
|
2015-01-31 02:42:58 +00:00
|
|
|
start = None
|
|
|
|
if start is not None:
|
2015-03-13 23:21:16 +00:00
|
|
|
yield Span(self, start, self.length)
|
2015-01-31 02:42:58 +00:00
|
|
|
|
2015-03-14 18:53:50 +00:00
|
|
|
cdef int set_parse(self, const TokenC* parsed) except -1:
|
2015-03-24 03:31:20 +00:00
|
|
|
# TODO: This method is fairly misleading atm. It's used by GreedyParser
|
|
|
|
# to actually apply the parse calculated. Need to rethink this.
|
2015-02-21 05:43:52 +00:00
|
|
|
self._py_tokens = [None] * self.length
|
|
|
|
self.is_parsed = True
|
|
|
|
for i in range(self.length):
|
|
|
|
self.data[i] = parsed[i]
|
|
|
|
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2015-03-13 23:21:16 +00:00
|
|
|
cdef class Span:
|
|
|
|
"""A slice from a Tokens object."""
|
|
|
|
def __cinit__(self, Tokens tokens, int start, int end):
|
|
|
|
self._seq = tokens
|
|
|
|
self.start = start
|
|
|
|
self.end = end
|
|
|
|
|
2015-03-14 15:06:35 +00:00
|
|
|
def __richcmp__(self, Span other, int op):
|
|
|
|
# Eq
|
|
|
|
if op in (1, 2, 5):
|
|
|
|
if self._seq is other._seq and \
|
|
|
|
self.start == other.start and \
|
|
|
|
self.end == other.end:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2015-03-13 23:21:16 +00:00
|
|
|
def __len__(self):
|
|
|
|
if self.end < self.start:
|
|
|
|
return 0
|
|
|
|
return self.end - self.start
|
|
|
|
|
|
|
|
def __getitem__(self, int i):
|
|
|
|
return self._seq[self.start + i]
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for i in range(self.start, self.end):
|
|
|
|
yield self._seq[i]
|
|
|
|
|
|
|
|
|
2014-10-23 13:59:17 +00:00
|
|
|
cdef class Token:
|
2015-02-11 23:05:06 +00:00
|
|
|
"""An individual token --- i.e. a word, a punctuation symbol, etc. Created
|
|
|
|
via Tokens.__getitem__ and Tokens.__iter__.
|
|
|
|
"""
|
2015-01-31 11:10:22 +00:00
|
|
|
def __cinit__(self, Vocab vocab, unicode string):
|
2015-01-31 02:42:58 +00:00
|
|
|
self.vocab = vocab
|
|
|
|
self._string = string
|
2014-12-24 06:42:00 +00:00
|
|
|
|
2015-02-11 23:05:06 +00:00
|
|
|
def __dealloc__(self):
|
|
|
|
if self._owns_c_data:
|
|
|
|
# Cast through const, if we own the data
|
|
|
|
PyMem_Free(<void*>self.c)
|
|
|
|
|
2014-12-24 06:42:00 +00:00
|
|
|
def __len__(self):
|
2015-01-31 02:42:58 +00:00
|
|
|
return self.c.lex.length
|
2015-01-13 13:03:48 +00:00
|
|
|
|
2015-02-11 23:05:06 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.string
|
|
|
|
|
2015-03-26 14:06:26 +00:00
|
|
|
cpdef bint check_flag(self, attr_id_t flag_id) except -1:
|
|
|
|
return check_flag(self.c.lex, flag_id)
|
|
|
|
|
|
|
|
|
2015-02-11 23:05:06 +00:00
|
|
|
cdef int take_ownership_of_c_data(self) except -1:
|
|
|
|
owned_data = <TokenC*>PyMem_Malloc(sizeof(TokenC) * self.array_len)
|
|
|
|
memcpy(owned_data, self.c, sizeof(TokenC) * self.array_len)
|
|
|
|
self.c = owned_data
|
|
|
|
self._owns_c_data = True
|
|
|
|
|
2015-01-24 06:29:04 +00:00
|
|
|
def nbor(self, int i=1):
|
2015-01-31 11:10:22 +00:00
|
|
|
return Token.cinit(self.vocab, self._string,
|
2015-01-31 02:42:58 +00:00
|
|
|
self.c, self.i, self.array_len,
|
2015-03-14 18:53:50 +00:00
|
|
|
self._seq)
|
2015-01-31 02:42:58 +00:00
|
|
|
|
2015-02-11 23:05:06 +00:00
|
|
|
property string:
|
|
|
|
def __get__(self):
|
|
|
|
cdef int next_idx = (self.c + 1).idx
|
|
|
|
if next_idx < self.c.idx:
|
|
|
|
next_idx = self.c.idx + self.c.lex.length
|
|
|
|
return self._string[self.c.idx:next_idx]
|
|
|
|
|
|
|
|
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):
|
|
|
|
return numpy.asarray(<float[:300,]> self.c.lex.repvec)
|
|
|
|
|
|
|
|
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
|
2015-02-18 03:07:44 +00:00
|
|
|
ptr += 1
|
2015-02-11 23:05:06 +00:00
|
|
|
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
|
2015-01-31 02:42:58 +00:00
|
|
|
ptr -= 1
|
2015-02-11 23:05:06 +00:00
|
|
|
return n
|
|
|
|
|
|
|
|
property lefts:
|
|
|
|
def __get__(self):
|
|
|
|
"""The leftward immediate children of the word, in the syntactic
|
|
|
|
dependency parse.
|
|
|
|
"""
|
|
|
|
cdef const TokenC* ptr = self.c - self.i
|
|
|
|
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:
|
|
|
|
yield Token.cinit(self.vocab, self._string,
|
|
|
|
ptr, ptr - (self.c - self.i), self.array_len,
|
2015-03-14 18:53:50 +00:00
|
|
|
self._seq)
|
2015-02-11 23:05:06 +00:00
|
|
|
ptr += 1
|
|
|
|
else:
|
|
|
|
ptr += 1
|
|
|
|
|
|
|
|
property rights:
|
|
|
|
def __get__(self):
|
|
|
|
"""The rightward immediate children of the word, in the syntactic
|
|
|
|
dependency parse."""
|
|
|
|
cdef const TokenC* ptr = (self.c - self.i) + (self.array_len - 1)
|
2015-03-22 15:35:00 +00:00
|
|
|
tokens = []
|
2015-02-11 23:05:06 +00: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 < 0) and ((ptr + ptr.head) > self.c):
|
|
|
|
ptr += ptr.head
|
|
|
|
elif ptr + ptr.head == self.c:
|
2015-03-22 15:35:00 +00:00
|
|
|
tokens.append(Token.cinit(self.vocab, self._string,
|
2015-02-11 23:05:06 +00:00
|
|
|
ptr, ptr - (self.c - self.i), self.array_len,
|
2015-03-22 15:35:00 +00:00
|
|
|
self._seq))
|
2015-02-11 23:05:06 +00:00
|
|
|
ptr -= 1
|
|
|
|
else:
|
|
|
|
ptr -= 1
|
2015-03-22 15:35:00 +00:00
|
|
|
tokens.reverse()
|
|
|
|
for t in tokens:
|
|
|
|
yield t
|
2015-02-11 23:05:06 +00:00
|
|
|
|
2015-03-03 09:18:41 +00:00
|
|
|
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
|
|
|
|
|
2015-02-11 23:05:06 +00:00
|
|
|
property head:
|
|
|
|
def __get__(self):
|
|
|
|
"""The token predicted by the parser to be the head of the current token."""
|
|
|
|
return Token.cinit(self.vocab, self._string,
|
|
|
|
self.c + self.c.head, self.i + self.c.head, self.array_len,
|
2015-03-14 18:53:50 +00:00
|
|
|
self._seq)
|
2015-02-11 23:05:06 +00:00
|
|
|
|
|
|
|
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):
|
2015-03-14 18:53:50 +00:00
|
|
|
return self.vocab.strings[self.c.tag]
|
2015-02-11 23:05:06 +00:00
|
|
|
|
|
|
|
property dep_:
|
|
|
|
def __get__(self):
|
2015-03-14 15:06:35 +00:00
|
|
|
return self.vocab.strings[self.c.dep]
|
2015-01-31 02:42:58 +00:00
|
|
|
|
2015-01-25 04:33:54 +00:00
|
|
|
|
2015-01-31 02:42:58 +00:00
|
|
|
_pos_id_to_string = {id_: string for string, id_ in UNIV_POS_NAMES.items()}
|
2015-01-25 04:33:54 +00:00
|
|
|
|
2015-01-30 07:04:41 +00:00
|
|
|
_parse_unset_error = """Text has not been parsed, so cannot be accessed.
|
2015-01-25 04:33:54 +00:00
|
|
|
|
2015-01-30 07:04:41 +00:00
|
|
|
Check that the parser data is installed. Run "python -m spacy.en.download" if not.
|
|
|
|
Check whether parse=False in the call to English.__call__
|
2015-01-25 04:33:54 +00:00
|
|
|
"""
|
|
|
|
|