2016-12-18 21:33:53 +00:00
|
|
|
|
# cython: infer_types=True
|
2017-04-15 11:05:15 +00:00
|
|
|
|
# coding: utf8
|
2016-12-18 21:33:53 +00:00
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2015-07-13 17:20:48 +00: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 18:20:58 +00:00
|
|
|
|
cimport numpy as np
|
|
|
|
|
np.import_array()
|
|
|
|
|
import numpy
|
|
|
|
|
|
2016-09-23 12:22:06 +00:00
|
|
|
|
from ..typedefs cimport hash_t
|
2015-09-06 17:45:15 +00:00
|
|
|
|
from ..lexeme cimport Lexeme
|
2015-10-10 06:55:55 +00:00
|
|
|
|
from .. import parts_of_speech
|
2015-07-26 14:37:16 +00:00
|
|
|
|
from ..attrs cimport IS_ALPHA, IS_ASCII, IS_DIGIT, IS_LOWER, IS_PUNCT, IS_SPACE
|
2017-10-27 13:41:45 +00:00
|
|
|
|
from ..attrs cimport IS_BRACKET, IS_QUOTE, IS_LEFT_PUNCT, IS_RIGHT_PUNCT
|
2018-02-11 17:55:48 +00:00
|
|
|
|
from ..attrs cimport IS_OOV, IS_TITLE, IS_UPPER, IS_CURRENCY, LIKE_URL, LIKE_NUM, LIKE_EMAIL
|
2017-10-27 13:41:45 +00:00
|
|
|
|
from ..attrs cimport IS_STOP, ID, ORTH, NORM, LOWER, SHAPE, PREFIX, SUFFIX
|
|
|
|
|
from ..attrs cimport LENGTH, CLUSTER, LEMMA, POS, TAG, DEP
|
2017-04-15 11:05:15 +00:00
|
|
|
|
from ..compat import is_config
|
2018-04-03 13:50:31 +00:00
|
|
|
|
from ..errors import Errors
|
2017-11-01 15:49:42 +00:00
|
|
|
|
from .. import util
|
2018-04-03 16:30:17 +00:00
|
|
|
|
from .underscore import Underscore, get_ext_args
|
2015-08-23 18:49:18 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
cdef class Token:
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""An individual token – i.e. a word, punctuation symbol, whitespace,
|
|
|
|
|
etc."""
|
2017-10-07 16:56:01 +00:00
|
|
|
|
@classmethod
|
2018-04-03 16:30:17 +00:00
|
|
|
|
def set_extension(cls, name, **kwargs):
|
|
|
|
|
if cls.has_extension(name) and not kwargs.get('force', False):
|
|
|
|
|
raise ValueError(Errors.E090.format(name=name, obj='Token'))
|
|
|
|
|
Underscore.token_extensions[name] = get_ext_args(**kwargs)
|
2017-10-07 16:56:01 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_extension(cls, name):
|
2018-03-27 11:36:20 +00:00
|
|
|
|
return Underscore.token_extensions.get(name)
|
2017-10-07 16:56:01 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def has_extension(cls, name):
|
2018-03-27 11:36:20 +00:00
|
|
|
|
return name in Underscore.token_extensions
|
2017-10-07 16:56:01 +00:00
|
|
|
|
|
2018-04-28 21:33:09 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def remove_extension(cls, name):
|
|
|
|
|
if not cls.has_extension(name):
|
|
|
|
|
raise ValueError(Errors.E046.format(name=name))
|
|
|
|
|
return Underscore.token_extensions.pop(name)
|
|
|
|
|
|
2015-07-13 22:10:11 +00:00
|
|
|
|
def __cinit__(self, Vocab vocab, Doc doc, int offset):
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""Construct a `Token` object.
|
|
|
|
|
|
|
|
|
|
vocab (Vocab): A storage container for lexical types.
|
|
|
|
|
doc (Doc): The parent document.
|
|
|
|
|
offset (int): The index of the token within the document.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
self.vocab = vocab
|
2015-07-13 22:10:11 +00:00
|
|
|
|
self.doc = doc
|
2015-11-03 13:15:14 +00:00
|
|
|
|
self.c = &self.doc.c[offset]
|
2015-07-13 22:10:11 +00:00
|
|
|
|
self.i = offset
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2017-01-16 12:27:57 +00:00
|
|
|
|
def __hash__(self):
|
|
|
|
|
return hash((self.doc, self.i))
|
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __len__(self):
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""The number of unicode characters in the token, i.e. `token.text`.
|
|
|
|
|
|
|
|
|
|
RETURNS (int): The number of unicode characters in the token.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
return self.c.lex.length
|
|
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-10-17 12:02:47 +00:00
|
|
|
|
return self.text
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2015-11-02 18:22:18 +00:00
|
|
|
|
def __bytes__(self):
|
2016-10-17 12:02:47 +00:00
|
|
|
|
return self.text.encode('utf8')
|
2015-11-02 18:22:18 +00:00
|
|
|
|
|
2015-07-24 01:49:30 +00:00
|
|
|
|
def __str__(self):
|
2017-04-15 11:05:15 +00:00
|
|
|
|
if is_config(python3=True):
|
2015-11-02 18:22:18 +00:00
|
|
|
|
return self.__unicode__()
|
|
|
|
|
return self.__bytes__()
|
2015-07-24 01:49:30 +00:00
|
|
|
|
|
2015-10-21 11:11:46 +00:00
|
|
|
|
def __repr__(self):
|
2015-11-02 18:22:18 +00:00
|
|
|
|
return self.__str__()
|
2015-10-21 11:11:46 +00:00
|
|
|
|
|
2017-01-11 12:03:32 +00:00
|
|
|
|
def __richcmp__(self, Token other, int op):
|
2017-01-09 18:30:31 +00:00
|
|
|
|
# http://cython.readthedocs.io/en/latest/src/userguide/special_methods.html
|
2018-01-15 14:51:25 +00:00
|
|
|
|
if other is None:
|
|
|
|
|
if op in (0, 1, 2):
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2017-08-19 14:39:32 +00:00
|
|
|
|
cdef Doc my_doc = self.doc
|
|
|
|
|
cdef Doc other_doc = other.doc
|
2017-01-09 18:30:31 +00:00
|
|
|
|
my = self.idx
|
2018-01-15 14:51:25 +00:00
|
|
|
|
their = other.idx
|
2017-01-09 18:30:31 +00:00
|
|
|
|
if op == 0:
|
|
|
|
|
return my < their
|
|
|
|
|
elif op == 2:
|
2017-08-19 14:39:32 +00:00
|
|
|
|
if my_doc is other_doc:
|
|
|
|
|
return my == their
|
|
|
|
|
else:
|
|
|
|
|
return False
|
2017-01-09 18:30:31 +00:00
|
|
|
|
elif op == 4:
|
|
|
|
|
return my > their
|
|
|
|
|
elif op == 1:
|
|
|
|
|
return my <= their
|
|
|
|
|
elif op == 3:
|
2017-08-19 14:39:32 +00:00
|
|
|
|
if my_doc is other_doc:
|
|
|
|
|
return my != their
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2017-01-09 18:30:31 +00:00
|
|
|
|
elif op == 5:
|
|
|
|
|
return my >= their
|
|
|
|
|
else:
|
2018-04-03 13:50:31 +00:00
|
|
|
|
raise ValueError(Errors.E041.format(op=op))
|
2017-01-09 18:30:31 +00:00
|
|
|
|
|
2017-10-07 16:56:01 +00:00
|
|
|
|
@property
|
|
|
|
|
def _(self):
|
|
|
|
|
return Underscore(Underscore.token_extensions, self,
|
|
|
|
|
start=self.idx, end=None)
|
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
cpdef bint check_flag(self, attr_id_t flag_id) except -1:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""Check the value of a boolean flag.
|
|
|
|
|
|
|
|
|
|
flag_id (int): The ID of the flag attribute.
|
|
|
|
|
RETURNS (bool): Whether the flag is set.
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
EXAMPLE:
|
|
|
|
|
>>> from spacy.attrs import IS_TITLE
|
|
|
|
|
>>> doc = nlp(u'Give it back! He pleaded.')
|
|
|
|
|
>>> token = doc[0]
|
|
|
|
|
>>> token.check_flag(IS_TITLE)
|
|
|
|
|
True
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-09-15 03:06:18 +00:00
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, flag_id)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
def nbor(self, int i=1):
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""Get a neighboring token.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
i (int): The relative position of the token to get. Defaults to 1.
|
|
|
|
|
RETURNS (Token): The token at position `self.doc[self.i+i]`.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2017-10-24 10:10:39 +00:00
|
|
|
|
if self.i+i < 0 or (self.i+i >= len(self.doc)):
|
2018-04-03 13:50:31 +00:00
|
|
|
|
raise IndexError(Errors.E042.format(i=self.i, j=i, length=len(self.doc)))
|
2015-07-13 22:10:11 +00:00
|
|
|
|
return self.doc[self.i+i]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2015-09-14 07:49:58 +00:00
|
|
|
|
def similarity(self, other):
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""Make a semantic similarity estimate. The default estimate is cosine
|
|
|
|
|
similarity using an average of word vectors.
|
|
|
|
|
|
|
|
|
|
other (object): The object to compare with. By default, accepts `Doc`,
|
|
|
|
|
`Span`, `Token` and `Lexeme` objects.
|
|
|
|
|
RETURNS (float): A scalar similarity score. Higher is more similar.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2016-10-19 18:54:03 +00:00
|
|
|
|
if 'similarity' in self.doc.user_token_hooks:
|
2017-05-30 21:35:17 +00:00
|
|
|
|
return self.doc.user_token_hooks['similarity'](self)
|
2018-05-03 16:40:46 +00:00
|
|
|
|
if hasattr(other, '__len__') and len(other) == 1 and hasattr(other, "__getitem__"):
|
2018-01-15 15:29:48 +00:00
|
|
|
|
if self.c.lex.orth == getattr(other[0], 'orth', None):
|
|
|
|
|
return 1.0
|
|
|
|
|
elif hasattr(other, 'orth'):
|
|
|
|
|
if self.c.lex.orth == other.orth:
|
|
|
|
|
return 1.0
|
2018-09-05 19:50:23 +00:00
|
|
|
|
self_norm = self.vector_norm
|
|
|
|
|
other_norm = other.vector_norm
|
|
|
|
|
if self_norm == 0 or other_norm == 0:
|
2015-09-22 00:10:01 +00:00
|
|
|
|
return 0.0
|
2017-10-27 15:07:26 +00:00
|
|
|
|
return (numpy.dot(self.vector, other.vector) /
|
2018-09-05 19:50:23 +00:00
|
|
|
|
(self_norm * other_norm))
|
2015-09-14 07:49:58 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property lex_id:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (int): Sequential ID of the token's lexical type."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.id
|
|
|
|
|
|
2015-11-08 15:18:25 +00:00
|
|
|
|
property rank:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (int): Sequential ID of the token's lexical type, used to
|
|
|
|
|
index into tables, e.g. for word vectors."""
|
2015-11-08 15:18:25 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.id
|
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property string:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""Deprecated: Use Token.text_with_ws instead."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-01-16 16:14:34 +00:00
|
|
|
|
return self.text_with_ws
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2015-09-13 00:27:42 +00:00
|
|
|
|
property text:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The original verbatim text of the token."""
|
2015-09-13 00:27:42 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.orth_
|
|
|
|
|
|
|
|
|
|
property text_with_ws:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The text content of the span (with trailing
|
2017-10-27 13:41:45 +00:00
|
|
|
|
whitespace).
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""
|
2015-09-13 00:27:42 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
cdef unicode orth = self.vocab.strings[self.c.lex.orth]
|
2015-09-13 00:27:42 +00:00
|
|
|
|
if self.c.spacy:
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return orth + u' '
|
2015-09-13 00:27:42 +00:00
|
|
|
|
else:
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return orth
|
2015-09-13 00:27:42 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property prob:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (float): Smoothed log probability estimate of token type."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.prob
|
|
|
|
|
|
2016-10-19 18:54:03 +00:00
|
|
|
|
property sentiment:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (float): A scalar value indicating the positivity or
|
|
|
|
|
negativity of the token."""
|
2016-10-19 18:54:03 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
if 'sentiment' in self.doc.user_token_hooks:
|
|
|
|
|
return self.doc.user_token_hooks['sentiment'](self)
|
|
|
|
|
return self.c.lex.sentiment
|
|
|
|
|
|
2016-03-10 12:01:34 +00:00
|
|
|
|
property lang:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the language of the parent document's
|
|
|
|
|
vocabulary.
|
|
|
|
|
"""
|
2016-03-10 12:01:34 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.lang
|
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property idx:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (int): The character offset of the token within the parent
|
|
|
|
|
document.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.idx
|
|
|
|
|
|
|
|
|
|
property cluster:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (int): Brown cluster ID."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.cluster
|
|
|
|
|
|
|
|
|
|
property orth:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the verbatim text content."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.orth
|
|
|
|
|
|
|
|
|
|
property lower:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the lowercase token text."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.lower
|
|
|
|
|
|
|
|
|
|
property norm:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the token's norm, i.e. a normalised form of
|
|
|
|
|
the token text. Usually set in the language's tokenizer exceptions
|
|
|
|
|
or norm exceptions.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.norm
|
|
|
|
|
|
|
|
|
|
property shape:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the token's shape, a transform of the
|
|
|
|
|
tokens's string, to show orthographic features (e.g. "Xxxx", "dd").
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.shape
|
|
|
|
|
|
|
|
|
|
property prefix:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of a length-N substring from the start of the
|
|
|
|
|
token. Defaults to `N=1`.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.prefix
|
|
|
|
|
|
|
|
|
|
property suffix:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of a length-N substring from the end of the
|
|
|
|
|
token. Defaults to `N=3`.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.lex.suffix
|
|
|
|
|
|
|
|
|
|
property lemma:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the base form of the word, with no
|
|
|
|
|
inflectional suffixes.
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2017-11-18 02:33:31 +00:00
|
|
|
|
if self.c.lemma == 0:
|
2018-02-27 18:50:01 +00:00
|
|
|
|
lemma_ = self.vocab.morphology.lemmatizer.lookup(self.orth_)
|
|
|
|
|
return self.vocab.strings[lemma_]
|
2017-11-18 02:33:31 +00:00
|
|
|
|
else:
|
|
|
|
|
return self.c.lemma
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2017-05-28 13:10:22 +00:00
|
|
|
|
def __set__(self, attr_t lemma):
|
2017-04-16 16:07:53 +00:00
|
|
|
|
self.c.lemma = lemma
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property pos:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of coarse-grained part-of-speech tag."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.pos
|
|
|
|
|
|
|
|
|
|
property tag:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of fine-grained part-of-speech tag."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.tag
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2017-05-28 13:10:22 +00:00
|
|
|
|
def __set__(self, attr_t tag):
|
2016-11-03 23:29:07 +00:00
|
|
|
|
self.vocab.morphology.assign_tag(self.c, tag)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property dep:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of syntactic dependency label."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.dep
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2017-05-28 13:10:22 +00:00
|
|
|
|
def __set__(self, attr_t label):
|
2016-03-11 16:31:06 +00:00
|
|
|
|
self.c.dep = label
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2015-09-21 09:52:43 +00:00
|
|
|
|
property has_vector:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""A boolean value indicating whether a word vector is associated with
|
|
|
|
|
the object.
|
|
|
|
|
|
|
|
|
|
RETURNS (bool): Whether a word vector is associated with the object.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-09-21 09:52:43 +00:00
|
|
|
|
def __get__(self):
|
2016-10-19 18:54:03 +00:00
|
|
|
|
if 'has_vector' in self.doc.user_token_hooks:
|
|
|
|
|
return self.doc.user_token_hooks['has_vector'](self)
|
2017-11-03 19:56:33 +00:00
|
|
|
|
if self.vocab.vectors.size == 0 and self.doc.tensor.size != 0:
|
|
|
|
|
return True
|
2017-06-04 19:19:58 +00:00
|
|
|
|
return self.vocab.has_vector(self.c.lex.orth)
|
2015-09-21 09:52:43 +00:00
|
|
|
|
|
2015-09-14 07:49:58 +00:00
|
|
|
|
property vector:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""A real-valued meaning representation.
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
RETURNS (numpy.ndarray[ndim=1, dtype='float32']): A 1D numpy array
|
|
|
|
|
representing the token's semantics.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-10-19 18:54:03 +00:00
|
|
|
|
if 'vector' in self.doc.user_token_hooks:
|
|
|
|
|
return self.doc.user_token_hooks['vector'](self)
|
2017-11-03 19:56:33 +00:00
|
|
|
|
if self.vocab.vectors.size == 0 and self.doc.tensor.size != 0:
|
|
|
|
|
return self.doc.tensor[self.i]
|
|
|
|
|
else:
|
|
|
|
|
return self.vocab.get_vector(self.c.lex.orth)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2015-09-14 07:49:58 +00:00
|
|
|
|
property vector_norm:
|
2017-05-20 13:13:33 +00:00
|
|
|
|
"""The L2 norm of the token's vector representation.
|
2017-05-19 16:47:56 +00:00
|
|
|
|
|
|
|
|
|
RETURNS (float): The L2 norm of the vector representation.
|
|
|
|
|
"""
|
2015-09-14 07:49:58 +00:00
|
|
|
|
def __get__(self):
|
2016-10-19 18:54:03 +00:00
|
|
|
|
if 'vector_norm' in self.doc.user_token_hooks:
|
|
|
|
|
return self.doc.user_token_hooks['vector_norm'](self)
|
2017-10-10 02:15:14 +00:00
|
|
|
|
vector = self.vector
|
2017-05-28 09:46:10 +00:00
|
|
|
|
return numpy.sqrt((vector ** 2).sum())
|
2015-09-14 07:49:58 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property n_lefts:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (int): The number of leftward immediate children of the
|
|
|
|
|
word, in the syntactic dependency parse.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-03-21 15:10:25 +00:00
|
|
|
|
return self.c.l_kids
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property n_rights:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (int): The number of rightward immediate children of the
|
|
|
|
|
word, in the syntactic dependency parse.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-03-21 15:10:25 +00:00
|
|
|
|
return self.c.r_kids
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2018-07-06 13:54:15 +00:00
|
|
|
|
property sent:
|
|
|
|
|
"""RETURNS (Span): The sentence span that the token is a part of."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
if 'sent' in self.doc.user_token_hooks:
|
|
|
|
|
return self.doc.user_token_hooks['sent'](self)
|
|
|
|
|
return self.doc[self.i : self.i+1].sent
|
|
|
|
|
|
2016-05-05 09:53:20 +00:00
|
|
|
|
property sent_start:
|
|
|
|
|
def __get__(self):
|
2018-04-03 13:50:31 +00:00
|
|
|
|
# Raising a deprecation warning here causes errors for autocomplete
|
2017-11-01 12:27:14 +00:00
|
|
|
|
# Handle broken backwards compatibility case: doc[0].sent_start
|
|
|
|
|
# was False.
|
|
|
|
|
if self.i == 0:
|
|
|
|
|
return False
|
|
|
|
|
else:
|
2018-01-14 14:02:15 +00:00
|
|
|
|
return self.c.sent_start
|
2017-11-01 12:27:14 +00:00
|
|
|
|
|
|
|
|
|
def __set__(self, value):
|
|
|
|
|
self.is_sent_start = value
|
|
|
|
|
|
|
|
|
|
property is_sent_start:
|
|
|
|
|
"""RETURNS (bool / None): Whether the token starts a sentence.
|
|
|
|
|
None if unknown.
|
|
|
|
|
"""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
if self.c.sent_start == 0:
|
|
|
|
|
return None
|
|
|
|
|
elif self.c.sent_start < 0:
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2016-05-05 09:53:20 +00:00
|
|
|
|
|
2017-10-08 21:51:58 +00:00
|
|
|
|
def __set__(self, value):
|
2016-05-05 09:53:20 +00:00
|
|
|
|
if self.doc.is_parsed:
|
2018-04-03 13:50:31 +00:00
|
|
|
|
raise ValueError(Errors.E043)
|
2017-10-08 21:51:58 +00:00
|
|
|
|
if value is None:
|
|
|
|
|
self.c.sent_start = 0
|
|
|
|
|
elif value is True:
|
|
|
|
|
self.c.sent_start = 1
|
|
|
|
|
elif value is False:
|
|
|
|
|
self.c.sent_start = -1
|
|
|
|
|
else:
|
2018-04-03 13:50:31 +00:00
|
|
|
|
raise ValueError(Errors.E044.format(value=value))
|
2016-05-05 09:53:20 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property lefts:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""The leftward immediate children of the word, in the syntactic
|
|
|
|
|
dependency parse.
|
|
|
|
|
|
|
|
|
|
YIELDS (Token): A left-child of the token.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-01-16 15:18:17 +00:00
|
|
|
|
cdef int nr_iter = 0
|
2015-09-06 08:48:36 +00:00
|
|
|
|
cdef const TokenC* ptr = self.c - (self.i - self.c.l_edge)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
while ptr < self.c:
|
2016-03-01 09:09:08 +00:00
|
|
|
|
if ptr + ptr.head == self.c:
|
2015-07-13 22:10:11 +00:00
|
|
|
|
yield self.doc[ptr - (self.c - self.i)]
|
2016-03-01 09:09:08 +00:00
|
|
|
|
ptr += 1
|
2016-01-16 15:18:17 +00:00
|
|
|
|
nr_iter += 1
|
|
|
|
|
# This is ugly, but it's a way to guard out infinite loops
|
|
|
|
|
if nr_iter >= 10000000:
|
2018-04-03 13:50:31 +00:00
|
|
|
|
raise RuntimeError(Errors.E045.format(attr='token.lefts'))
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property rights:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""The rightward immediate children of the word, in the syntactic
|
|
|
|
|
dependency parse.
|
|
|
|
|
|
|
|
|
|
YIELDS (Token): A right-child of the token.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-09-06 08:48:36 +00:00
|
|
|
|
cdef const TokenC* ptr = self.c + (self.c.r_edge - self.i)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
tokens = []
|
2016-01-16 15:18:17 +00:00
|
|
|
|
cdef int nr_iter = 0
|
2015-07-13 17:20:48 +00:00
|
|
|
|
while ptr > self.c:
|
2016-03-01 09:09:08 +00:00
|
|
|
|
if ptr + ptr.head == self.c:
|
2015-07-13 22:10:11 +00:00
|
|
|
|
tokens.append(self.doc[ptr - (self.c - self.i)])
|
2016-03-01 09:09:08 +00:00
|
|
|
|
ptr -= 1
|
|
|
|
|
nr_iter += 1
|
2016-01-16 15:18:17 +00:00
|
|
|
|
if nr_iter >= 10000000:
|
2018-04-03 13:50:31 +00:00
|
|
|
|
raise RuntimeError(Errors.E045.format(attr='token.rights'))
|
2015-07-13 17:20:48 +00:00
|
|
|
|
tokens.reverse()
|
|
|
|
|
for t in tokens:
|
|
|
|
|
yield t
|
|
|
|
|
|
|
|
|
|
property children:
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""A sequence of the token's immediate syntactic children.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-10-27 13:41:45 +00:00
|
|
|
|
YIELDS (Token): A child token such that child.head==self
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
yield from self.lefts
|
|
|
|
|
yield from self.rights
|
|
|
|
|
|
|
|
|
|
property subtree:
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""A sequence of all the token's syntactic descendents.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-10-27 13:41:45 +00:00
|
|
|
|
YIELDS (Token): A descendent token such that
|
|
|
|
|
`self.is_ancestor(descendent)`.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
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:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""The leftmost token of this token's syntactic descendents.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
RETURNS (Token): The first token such that `self.is_ancestor(token)`.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-07-13 22:10:11 +00:00
|
|
|
|
return self.doc[self.c.l_edge]
|
2015-08-08 21:37:44 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property right_edge:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""The rightmost token of this token's syntactic descendents.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
RETURNS (Token): The last token such that `self.is_ancestor(token)`.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-07-13 22:10:11 +00:00
|
|
|
|
return self.doc[self.c.r_edge]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2016-03-11 16:31:06 +00:00
|
|
|
|
property ancestors:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""A sequence of this token's syntactic ancestors.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
YIELDS (Token): A sequence of ancestor tokens such that
|
|
|
|
|
`ancestor.is_ancestor(self)`.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2016-03-11 16:31:06 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
cdef const TokenC* head_ptr = self.c
|
2017-02-26 21:27:11 +00:00
|
|
|
|
# guard against infinite loop, no token can have
|
2016-03-11 16:31:06 +00:00
|
|
|
|
# more ancestors than tokens in the tree
|
|
|
|
|
cdef int i = 0
|
|
|
|
|
while head_ptr.head != 0 and i < self.doc.length:
|
|
|
|
|
head_ptr += head_ptr.head
|
|
|
|
|
yield self.doc[head_ptr - (self.c - self.i)]
|
|
|
|
|
i += 1
|
|
|
|
|
|
2016-11-01 11:25:36 +00:00
|
|
|
|
def is_ancestor(self, descendant):
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""Check whether this token is a parent, grandparent, etc. of another
|
2016-11-01 11:25:36 +00:00
|
|
|
|
in the dependency tree.
|
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
descendant (Token): Another token.
|
|
|
|
|
RETURNS (bool): Whether this token is the ancestor of the descendant.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2016-11-01 12:28:00 +00:00
|
|
|
|
if self.doc is not descendant.doc:
|
2016-11-01 11:25:36 +00:00
|
|
|
|
return False
|
2017-10-27 15:07:26 +00:00
|
|
|
|
return any(ancestor.i == self.i for ancestor in descendant.ancestors)
|
2016-03-11 16:31:06 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property head:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""The syntactic parent, or "governor", of this token.
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
2017-10-27 15:07:26 +00:00
|
|
|
|
RETURNS (Token): The token predicted by the parser to be the head of
|
|
|
|
|
the current token.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-07-13 22:10:11 +00:00
|
|
|
|
return self.doc[self.i + self.c.head]
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2016-03-11 16:31:06 +00:00
|
|
|
|
def __set__(self, Token new_head):
|
|
|
|
|
# this function sets the head of self to new_head
|
|
|
|
|
# and updates the counters for left/right dependents
|
|
|
|
|
# and left/right corner for the new and the old head
|
|
|
|
|
|
|
|
|
|
# do nothing if old head is new head
|
|
|
|
|
if self.i + self.c.head == new_head.i:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
cdef Token old_head = self.head
|
|
|
|
|
cdef int rel_newhead_i = new_head.i - self.i
|
|
|
|
|
|
|
|
|
|
# is the new head a descendant of the old head
|
2017-05-19 18:23:40 +00:00
|
|
|
|
cdef bint is_desc = old_head.is_ancestor(new_head)
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
2016-03-11 16:31:06 +00:00
|
|
|
|
cdef int new_edge
|
2016-03-14 12:43:48 +00:00
|
|
|
|
cdef Token anc, child
|
2016-03-11 16:31:06 +00:00
|
|
|
|
|
|
|
|
|
# update number of deps of old head
|
2017-10-27 15:07:26 +00:00
|
|
|
|
if self.c.head > 0: # left dependent
|
2016-03-11 16:31:06 +00:00
|
|
|
|
old_head.c.l_kids -= 1
|
|
|
|
|
if self.c.l_edge == old_head.c.l_edge:
|
2017-10-27 13:41:45 +00:00
|
|
|
|
# the token dominates the left edge so the left edge of
|
|
|
|
|
# the head may change when the token is reattached, it may
|
|
|
|
|
# not change if the new head is a descendant of the current
|
|
|
|
|
# head
|
2016-03-11 16:31:06 +00:00
|
|
|
|
|
|
|
|
|
new_edge = self.c.l_edge
|
2017-10-27 13:41:45 +00:00
|
|
|
|
# the new l_edge is the left-most l_edge on any of the
|
|
|
|
|
# other dependents where the l_edge is left of the head,
|
|
|
|
|
# otherwise it is the head
|
2016-03-11 16:31:06 +00:00
|
|
|
|
if not is_desc:
|
2016-03-14 12:43:48 +00:00
|
|
|
|
new_edge = old_head.i
|
|
|
|
|
for child in old_head.children:
|
|
|
|
|
if child == self:
|
|
|
|
|
continue
|
|
|
|
|
if child.c.l_edge < new_edge:
|
|
|
|
|
new_edge = child.c.l_edge
|
|
|
|
|
old_head.c.l_edge = new_edge
|
|
|
|
|
|
2017-10-27 13:41:45 +00:00
|
|
|
|
# walk up the tree from old_head and assign new l_edge to
|
|
|
|
|
# ancestors until an ancestor already has an l_edge that's
|
|
|
|
|
# further left
|
2016-03-11 16:31:06 +00:00
|
|
|
|
for anc in old_head.ancestors:
|
|
|
|
|
if anc.c.l_edge <= new_edge:
|
|
|
|
|
break
|
|
|
|
|
anc.c.l_edge = new_edge
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
2017-10-27 13:41:45 +00:00
|
|
|
|
elif self.c.head < 0: # right dependent
|
2016-03-11 16:31:06 +00:00
|
|
|
|
old_head.c.r_kids -= 1
|
|
|
|
|
# do the same thing as for l_edge
|
|
|
|
|
if self.c.r_edge == old_head.c.r_edge:
|
|
|
|
|
new_edge = self.c.r_edge
|
2016-03-14 12:43:48 +00:00
|
|
|
|
|
2016-03-11 16:31:06 +00:00
|
|
|
|
if not is_desc:
|
2016-03-14 12:43:48 +00:00
|
|
|
|
new_edge = old_head.i
|
|
|
|
|
for child in old_head.children:
|
|
|
|
|
if child == self:
|
|
|
|
|
continue
|
|
|
|
|
if child.c.r_edge > new_edge:
|
|
|
|
|
new_edge = child.c.r_edge
|
|
|
|
|
old_head.c.r_edge = new_edge
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
2016-03-11 16:31:06 +00:00
|
|
|
|
for anc in old_head.ancestors:
|
|
|
|
|
if anc.c.r_edge >= new_edge:
|
|
|
|
|
break
|
|
|
|
|
anc.c.r_edge = new_edge
|
|
|
|
|
|
|
|
|
|
# update number of deps of new head
|
2017-10-27 13:41:45 +00:00
|
|
|
|
if rel_newhead_i > 0: # left dependent
|
2016-03-11 16:31:06 +00:00
|
|
|
|
new_head.c.l_kids += 1
|
|
|
|
|
# walk up the tree from new head and set l_edge to self.l_edge
|
|
|
|
|
# until you hit a token with an l_edge further to the left
|
|
|
|
|
if self.c.l_edge < new_head.c.l_edge:
|
2016-03-14 12:43:48 +00:00
|
|
|
|
new_head.c.l_edge = self.c.l_edge
|
2016-03-11 16:31:06 +00:00
|
|
|
|
for anc in new_head.ancestors:
|
2016-03-14 12:43:48 +00:00
|
|
|
|
if anc.c.l_edge <= self.c.l_edge:
|
2016-03-11 16:31:06 +00:00
|
|
|
|
break
|
2016-03-14 12:43:48 +00:00
|
|
|
|
anc.c.l_edge = self.c.l_edge
|
2016-03-11 16:31:06 +00:00
|
|
|
|
|
2017-10-27 13:41:45 +00:00
|
|
|
|
elif rel_newhead_i < 0: # right dependent
|
2016-03-11 16:31:06 +00:00
|
|
|
|
new_head.c.r_kids += 1
|
|
|
|
|
# do the same as for l_edge
|
|
|
|
|
if self.c.r_edge > new_head.c.r_edge:
|
2016-03-14 12:43:48 +00:00
|
|
|
|
new_head.c.r_edge = self.c.r_edge
|
2016-03-11 16:31:06 +00:00
|
|
|
|
for anc in new_head.ancestors:
|
2016-03-14 12:43:48 +00:00
|
|
|
|
if anc.c.r_edge >= self.c.r_edge:
|
2016-03-11 16:31:06 +00:00
|
|
|
|
break
|
2016-03-14 12:43:48 +00:00
|
|
|
|
anc.c.r_edge = self.c.r_edge
|
2016-03-11 16:31:06 +00:00
|
|
|
|
|
|
|
|
|
# set new head
|
|
|
|
|
self.c.head = rel_newhead_i
|
2015-08-08 21:37:44 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property conjuncts:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""A sequence of coordinated tokens, including the token itself.
|
2016-11-01 11:25:36 +00:00
|
|
|
|
|
2017-05-19 16:47:56 +00:00
|
|
|
|
YIELDS (Token): A coordinated token.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-10-14 16:34:57 +00:00
|
|
|
|
"""Get a list of conjoined words."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
cdef Token word
|
2016-10-19 18:54:03 +00:00
|
|
|
|
if 'conjuncts' in self.doc.user_token_hooks:
|
|
|
|
|
yield from self.doc.user_token_hooks['conjuncts'](self)
|
2016-10-17 00:44:49 +00:00
|
|
|
|
else:
|
|
|
|
|
if self.dep_ != 'conj':
|
|
|
|
|
for word in self.rights:
|
|
|
|
|
if word.dep_ == 'conj':
|
|
|
|
|
yield word
|
|
|
|
|
yield from word.conjuncts
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property ent_type:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): Named entity type."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.ent_type
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2017-05-28 13:10:22 +00:00
|
|
|
|
def __set__(self, ent_type):
|
|
|
|
|
self.c.ent_type = ent_type
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property ent_iob:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""IOB code of named entity tag. `1="I", 2="O", 3="B"`. 0 means no tag
|
|
|
|
|
is assigned.
|
|
|
|
|
|
2017-05-28 13:10:22 +00:00
|
|
|
|
RETURNS (uint64): IOB code of named entity tag.
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return self.c.ent_iob
|
|
|
|
|
|
|
|
|
|
property ent_type_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): Named entity type."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.ent_type]
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2017-05-28 13:10:22 +00:00
|
|
|
|
def __set__(self, ent_type):
|
|
|
|
|
self.c.ent_type = self.vocab.strings.add(ent_type)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property ent_iob_:
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""IOB code of named entity tag. "B" means the token begins an entity,
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"I" means it is inside an entity, "O" means it is outside an entity,
|
|
|
|
|
and "" means no entity tag is set.
|
2017-05-19 16:47:56 +00:00
|
|
|
|
|
|
|
|
|
RETURNS (unicode): IOB code of named entity tag.
|
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
iob_strings = ('', 'I', 'O', 'B')
|
|
|
|
|
return iob_strings[self.c.ent_iob]
|
|
|
|
|
|
2016-09-21 12:54:55 +00:00
|
|
|
|
property ent_id:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (uint64): ID of the entity the token is an instance of,
|
|
|
|
|
if any.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2016-09-21 12:54:55 +00:00
|
|
|
|
def __get__(self):
|
2016-09-23 13:07:07 +00:00
|
|
|
|
return self.c.ent_id
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
|
|
|
|
def __set__(self, hash_t key):
|
2017-03-31 12:00:14 +00:00
|
|
|
|
self.c.ent_id = key
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
|
|
|
|
property ent_id_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): ID of the entity the token is an instance of,
|
|
|
|
|
if any.
|
2017-04-15 11:05:15 +00:00
|
|
|
|
"""
|
2016-09-21 12:54:55 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.ent_id]
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
2017-03-31 12:00:14 +00:00
|
|
|
|
def __set__(self, name):
|
2017-05-28 13:10:22 +00:00
|
|
|
|
self.c.ent_id = self.vocab.strings.add(name)
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property whitespace_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The trailing whitespace character, if present.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-10-18 06:21:11 +00:00
|
|
|
|
return ' ' if self.c.spacy else ''
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property orth_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): Verbatim text content (identical to
|
2018-09-21 08:45:11 +00:00
|
|
|
|
`Token.text`). Exists mostly for consistency with the other
|
2017-10-27 15:07:26 +00:00
|
|
|
|
attributes.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.orth]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property lower_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The lowercase token text. Equivalent to
|
|
|
|
|
`Token.text.lower()`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.lower]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property norm_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The token's norm, i.e. a normalised form of the
|
|
|
|
|
token text. Usually set in the language's tokenizer exceptions or
|
|
|
|
|
norm exceptions.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.norm]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property shape_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): Transform of the tokens's string, to show
|
|
|
|
|
orthographic features. For example, "Xxxx" or "dd".
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.shape]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property prefix_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): A length-N substring from the start of the token.
|
|
|
|
|
Defaults to `N=1`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.prefix]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property suffix_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): A length-N substring from the end of the token.
|
|
|
|
|
Defaults to `N=3`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.suffix]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
2016-03-10 12:01:34 +00:00
|
|
|
|
property lang_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): Language of the parent document's vocabulary,
|
|
|
|
|
e.g. 'en'.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2016-03-10 12:01:34 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.lex.lang]
|
2016-03-10 12:01:34 +00:00
|
|
|
|
|
2015-07-13 17:20:48 +00:00
|
|
|
|
property lemma_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The token lemma, i.e. the base form of the word,
|
|
|
|
|
with no inflectional suffixes.
|
2017-05-19 16:47:56 +00:00
|
|
|
|
"""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2017-11-18 02:33:31 +00:00
|
|
|
|
if self.c.lemma == 0:
|
|
|
|
|
return self.vocab.morphology.lemmatizer.lookup(self.orth_)
|
|
|
|
|
else:
|
|
|
|
|
return self.vocab.strings[self.c.lemma]
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2017-04-16 16:07:53 +00:00
|
|
|
|
def __set__(self, unicode lemma_):
|
2017-05-28 13:10:22 +00:00
|
|
|
|
self.c.lemma = self.vocab.strings.add(lemma_)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property pos_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): Coarse-grained part-of-speech tag."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2015-10-10 06:55:55 +00:00
|
|
|
|
return parts_of_speech.NAMES[self.c.pos]
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property tag_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): Fine-grained part-of-speech tag."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.tag]
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2016-11-02 22:28:59 +00:00
|
|
|
|
def __set__(self, tag):
|
2017-05-28 13:10:22 +00:00
|
|
|
|
self.tag = self.vocab.strings.add(tag)
|
2015-07-13 17:20:48 +00:00
|
|
|
|
|
|
|
|
|
property dep_:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (unicode): The syntactic dependency label."""
|
2015-07-13 17:20:48 +00:00
|
|
|
|
def __get__(self):
|
2016-09-30 18:20:22 +00:00
|
|
|
|
return self.vocab.strings[self.c.dep]
|
2017-10-27 15:07:26 +00:00
|
|
|
|
|
2016-03-11 16:31:06 +00:00
|
|
|
|
def __set__(self, unicode label):
|
2017-05-28 13:10:22 +00:00
|
|
|
|
self.c.dep = self.vocab.strings.add(label)
|
2015-07-13 18:20:58 +00:00
|
|
|
|
|
2015-07-26 23:50:06 +00:00
|
|
|
|
property is_oov:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is out-of-vocabulary."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_OOV)
|
2015-07-26 23:50:06 +00:00
|
|
|
|
|
2015-09-14 07:49:58 +00:00
|
|
|
|
property is_stop:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is a stop word, i.e. part of a
|
|
|
|
|
"stop list" defined by the language data.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_STOP)
|
2015-09-14 07:49:58 +00:00
|
|
|
|
|
2015-07-26 14:37:16 +00:00
|
|
|
|
property is_alpha:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token consists of alpha characters.
|
|
|
|
|
Equivalent to `token.text.isalpha()`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_ALPHA)
|
2015-08-08 21:37:44 +00:00
|
|
|
|
|
2015-07-26 14:37:16 +00:00
|
|
|
|
property is_ascii:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token consists of ASCII characters.
|
|
|
|
|
Equivalent to `[any(ord(c) >= 128 for c in token.text)]`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_ASCII)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
|
|
|
|
property is_digit:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token consists of digits. Equivalent to
|
|
|
|
|
`token.text.isdigit()`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_DIGIT)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
|
|
|
|
property is_lower:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is in lowercase. Equivalent to
|
|
|
|
|
`token.text.islower()`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_LOWER)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
2017-10-27 13:41:45 +00:00
|
|
|
|
property is_upper:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is in uppercase. Equivalent to
|
|
|
|
|
`token.text.isupper()`
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_UPPER)
|
2017-10-27 13:41:45 +00:00
|
|
|
|
|
2015-07-26 14:37:16 +00:00
|
|
|
|
property is_title:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is in titlecase. Equivalent to
|
|
|
|
|
`token.text.istitle()`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_TITLE)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
|
|
|
|
property is_punct:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is punctuation."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_PUNCT)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
2017-02-26 21:27:11 +00:00
|
|
|
|
property is_space:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token consists of whitespace characters.
|
|
|
|
|
Equivalent to `token.text.isspace()`.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_SPACE)
|
2017-02-26 21:27:11 +00:00
|
|
|
|
|
|
|
|
|
property is_bracket:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is a bracket."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_BRACKET)
|
2016-02-04 12:04:16 +00:00
|
|
|
|
|
2017-02-26 21:27:11 +00:00
|
|
|
|
property is_quote:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is a quotation mark."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_QUOTE)
|
2016-02-04 12:04:16 +00:00
|
|
|
|
|
2017-02-26 21:27:11 +00:00
|
|
|
|
property is_left_punct:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is a left punctuation mark."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_LEFT_PUNCT)
|
2016-02-04 12:04:16 +00:00
|
|
|
|
|
2017-02-26 21:27:11 +00:00
|
|
|
|
property is_right_punct:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token is a left punctuation mark."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_RIGHT_PUNCT)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
2018-02-11 17:55:48 +00:00
|
|
|
|
property is_currency:
|
|
|
|
|
"""RETURNS (bool): Whether the token is a currency symbol."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, IS_CURRENCY)
|
|
|
|
|
|
2015-07-26 14:37:16 +00:00
|
|
|
|
property like_url:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token resembles a URL."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, LIKE_URL)
|
2015-08-08 21:37:44 +00:00
|
|
|
|
|
2015-07-26 14:37:16 +00:00
|
|
|
|
property like_num:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token resembles a number, e.g. "10.9",
|
|
|
|
|
"10", "ten", etc.
|
2017-10-27 13:41:45 +00:00
|
|
|
|
"""
|
2017-10-27 15:07:26 +00:00
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, LIKE_NUM)
|
2015-07-26 14:37:16 +00:00
|
|
|
|
|
|
|
|
|
property like_email:
|
2017-10-27 15:07:26 +00:00
|
|
|
|
"""RETURNS (bool): Whether the token resembles an email address."""
|
|
|
|
|
def __get__(self):
|
|
|
|
|
return Lexeme.c_check_flag(self.c.lex, LIKE_EMAIL)
|