2014-12-30 10:22:00 +00:00
|
|
|
# cython: embedsignature=True
|
2015-01-05 06:54:13 +00:00
|
|
|
from cython.view cimport array as cvarray
|
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-22 15:08:25 +00:00
|
|
|
from .typedefs cimport ID, ORTH, NORM1, NORM2, SHAPE, PREFIX, SUFFIX, LENGTH, CLUSTER
|
2014-12-24 06:42:00 +00:00
|
|
|
from .typedefs cimport POS, LEMMA
|
|
|
|
|
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
|
|
|
|
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-12 00:23:44 +00:00
|
|
|
elif feat_name == NORM1:
|
|
|
|
return lex.norm1
|
|
|
|
elif feat_name == NORM2:
|
|
|
|
return lex.norm2
|
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:
|
2014-12-27 07:45:16 +00:00
|
|
|
"""Access and set annotations onto some text.
|
2014-09-10 16:11:13 +00:00
|
|
|
"""
|
2015-01-21 07:56:32 +00:00
|
|
|
def __init__(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
|
2014-10-14 07:22:41 +00:00
|
|
|
|
2015-01-19 14:16:29 +00:00
|
|
|
def sentences(self):
|
|
|
|
cdef int i
|
|
|
|
sentences = []
|
2015-01-21 07:56:32 +00:00
|
|
|
cdef Tokens sent = Tokens(self.vocab, self._string[self.data[0].idx:])
|
2015-01-19 14:16:29 +00:00
|
|
|
cdef attr_t period = self.vocab.strings['.']
|
|
|
|
cdef attr_t question = self.vocab.strings['?']
|
|
|
|
cdef attr_t exclamation = self.vocab.strings['!']
|
2015-01-22 11:24:44 +00:00
|
|
|
spans = []
|
|
|
|
start = None
|
2015-01-19 14:16:29 +00:00
|
|
|
for i in range(self.length):
|
2015-01-22 11:24:44 +00:00
|
|
|
if start is None:
|
|
|
|
start = i
|
2015-01-22 15:08:25 +00:00
|
|
|
if self.data[i].lex.orth == period or self.data[i].lex.orth == exclamation or \
|
|
|
|
self.data[i].lex.orth == question:
|
2015-01-22 11:24:44 +00:00
|
|
|
spans.append((start, i+1))
|
|
|
|
start = None
|
|
|
|
if start is not None:
|
|
|
|
spans.append((start, self.length))
|
|
|
|
return spans
|
2015-01-19 14:16:29 +00:00
|
|
|
|
2014-09-11 19:37:32 +00:00
|
|
|
def __getitem__(self, i):
|
2014-12-27 07:45:16 +00:00
|
|
|
"""Retrieve a token.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
token (Token):
|
|
|
|
"""
|
2015-01-19 14:16:29 +00:00
|
|
|
if i < 0:
|
|
|
|
i = self.length - i
|
2014-10-22 14:57:59 +00:00
|
|
|
bounds_check(i, self.length, PADDING)
|
2015-01-13 13:03:48 +00:00
|
|
|
return Token(self, i)
|
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):
|
|
|
|
yield self[i]
|
|
|
|
|
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-01-21 15:05:37 +00:00
|
|
|
def __str__(self):
|
|
|
|
return unidecode(unicode(self))
|
|
|
|
|
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
|
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-05 06:54:13 +00:00
|
|
|
cpdef long[:,:] to_array(self, object 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-05 06:54:13 +00:00
|
|
|
cdef long[:,:] output = cvarray(shape=(self.length, len(attr_ids)),
|
|
|
|
itemsize=sizeof(long), format="l")
|
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-13 13:03:48 +00:00
|
|
|
@cython.freelist(64)
|
2014-10-23 13:59:17 +00:00
|
|
|
cdef class Token:
|
2015-01-21 15:05:37 +00:00
|
|
|
"""An individual token."""
|
2015-01-13 13:03:48 +00:00
|
|
|
def __init__(self, Tokens tokens, int i):
|
|
|
|
self._seq = tokens
|
|
|
|
self.i = i
|
2015-01-14 16:51:47 +00:00
|
|
|
cdef const TokenC* t = &tokens.data[i]
|
|
|
|
self.idx = t.idx
|
|
|
|
self.cluster = t.lex.cluster
|
|
|
|
self.length = t.lex.length
|
2015-01-22 15:08:25 +00:00
|
|
|
self.orth = t.lex.orth
|
2015-01-14 16:51:47 +00:00
|
|
|
self.norm1 = t.lex.norm1
|
|
|
|
self.norm2 = t.lex.norm2
|
|
|
|
self.shape = t.lex.shape
|
|
|
|
self.prefix = t.lex.prefix
|
|
|
|
self.suffix = t.lex.suffix
|
|
|
|
self.prob = t.lex.prob
|
|
|
|
self.sentiment = t.lex.sentiment
|
|
|
|
self.flags = t.lex.flags
|
|
|
|
self.lemma = t.lemma
|
2015-01-17 05:21:17 +00:00
|
|
|
self.tag = t.tag
|
|
|
|
self.dep = t.dep
|
2015-01-21 15:05:37 +00:00
|
|
|
self.repvec = numpy.asarray(<float[:300,]> t.lex.repvec)
|
2015-01-19 08:59:55 +00:00
|
|
|
|
2015-01-13 13:03:48 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
cdef const TokenC* t = &self._seq.data[self.i]
|
|
|
|
cdef int end_idx = t.idx + t.lex.length
|
|
|
|
if self.i + 1 == self._seq.length:
|
|
|
|
return self.string
|
|
|
|
if end_idx == t[1].idx:
|
|
|
|
return self.string
|
|
|
|
else:
|
|
|
|
return self.string + ' '
|
2014-12-24 06:42:00 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2014-12-27 07:45:16 +00:00
|
|
|
"""The number of unicode code-points in the original string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
length (int):
|
|
|
|
"""
|
2015-01-13 13:03:48 +00:00
|
|
|
return self._seq.data[self.i].lex.length
|
|
|
|
|
2015-01-17 05:21:17 +00:00
|
|
|
def check_flag(self, attr_id_t flag):
|
2015-01-21 15:05:37 +00:00
|
|
|
return self.flags & (1 << flag)
|
2015-01-17 05:21:17 +00:00
|
|
|
|
|
|
|
def is_pos(self, univ_tag_t pos):
|
2015-01-21 07:56:32 +00:00
|
|
|
return self.tag == pos
|
2015-01-17 05:21:17 +00:00
|
|
|
|
2015-01-14 16:51:47 +00:00
|
|
|
property head:
|
|
|
|
"""The token predicted by the parser to be the head of the current token."""
|
2015-01-13 13:03:48 +00:00
|
|
|
def __get__(self):
|
2015-01-14 16:51:47 +00:00
|
|
|
cdef const TokenC* t = &self._seq.data[self.i]
|
|
|
|
return Token(self._seq, self.i + t.head)
|
2015-01-13 13:03:48 +00:00
|
|
|
|
|
|
|
property string:
|
|
|
|
"""The unicode string of the word, with no whitespace padding."""
|
|
|
|
def __get__(self):
|
|
|
|
cdef const TokenC* t = &self._seq.data[self.i]
|
2015-01-22 15:08:25 +00:00
|
|
|
if t.lex.orth == 0:
|
2015-01-13 13:03:48 +00:00
|
|
|
return ''
|
2015-01-22 15:08:25 +00:00
|
|
|
cdef unicode py_ustr = self._seq.vocab.strings[t.lex.orth]
|
2015-01-13 13:03:48 +00:00
|
|
|
return py_ustr
|
|
|
|
|
2015-01-22 15:08:25 +00:00
|
|
|
property orth_:
|
2015-01-14 16:51:47 +00:00
|
|
|
def __get__(self):
|
2015-01-22 15:08:25 +00:00
|
|
|
return self._seq.vocab.strings[self.orth]
|
2015-01-14 16:51:47 +00:00
|
|
|
|
|
|
|
property norm1_:
|
|
|
|
def __get__(self):
|
|
|
|
return self._seq.vocab.strings[self.norm1]
|
|
|
|
|
|
|
|
property norm2_:
|
|
|
|
def __get__(self):
|
|
|
|
return self._seq.vocab.strings[self.norm2]
|
|
|
|
|
|
|
|
property shape_:
|
|
|
|
def __get__(self):
|
|
|
|
return self._seq.vocab.strings[self.shape]
|
|
|
|
|
|
|
|
property prefix_:
|
|
|
|
def __get__(self):
|
|
|
|
return self._seq.vocab.strings[self.prefix]
|
|
|
|
|
|
|
|
property suffix_:
|
|
|
|
def __get__(self):
|
|
|
|
return self._seq.vocab.strings[self.suffix]
|
|
|
|
|
|
|
|
property lemma_:
|
2015-01-13 13:03:48 +00:00
|
|
|
def __get__(self):
|
|
|
|
cdef const TokenC* t = &self._seq.data[self.i]
|
|
|
|
if t.lemma == 0:
|
|
|
|
return self.string
|
|
|
|
cdef unicode py_ustr = self._seq.vocab.strings[t.lemma]
|
|
|
|
return py_ustr
|
|
|
|
|
2015-01-17 05:21:17 +00:00
|
|
|
property tag_:
|
2015-01-13 13:03:48 +00:00
|
|
|
def __get__(self):
|
2015-01-17 05:21:17 +00:00
|
|
|
return self._seq.tag_names[self.tag]
|
2015-01-13 13:03:48 +00:00
|
|
|
|
2015-01-17 05:21:17 +00:00
|
|
|
property dep_:
|
2015-01-13 13:03:48 +00:00
|
|
|
def __get__(self):
|
2015-01-17 05:21:17 +00:00
|
|
|
return self._seq.dep_names[self.dep]
|