2015-01-14 16:33:16 +03:00
|
|
|
# cython: embedsignature=True
|
2014-09-15 05:22:40 +04:00
|
|
|
from cpython.ref cimport Py_INCREF
|
2014-09-18 01:09:24 +04:00
|
|
|
from cymem.cymem cimport Pool
|
2014-10-29 15:19:38 +03:00
|
|
|
from murmurhash.mrmr cimport hash64
|
2014-09-15 05:22:40 +04:00
|
|
|
|
2015-09-14 10:49:58 +03:00
|
|
|
# Compiler crashes on memory view coercion without this. Should report bug.
|
|
|
|
from cython.view cimport array as cvarray
|
|
|
|
cimport numpy as np
|
|
|
|
np.import_array()
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-22 18:57:59 +04:00
|
|
|
from libc.string cimport memset
|
|
|
|
|
2015-01-05 10:49:19 +03:00
|
|
|
from .orth cimport word_shape
|
2015-07-01 19:50:37 +03:00
|
|
|
from .typedefs cimport attr_t, flags_t
|
2015-01-17 08:21:17 +03:00
|
|
|
import numpy
|
2014-09-10 22:41:37 +04: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
|
2016-03-10 15:01:34 +03:00
|
|
|
from .attrs cimport IS_BRACKET
|
|
|
|
from .attrs cimport IS_QUOTE
|
|
|
|
from .attrs cimport IS_LEFT_PUNCT
|
|
|
|
from .attrs cimport IS_RIGHT_PUNCT
|
2015-07-27 02:50:06 +03:00
|
|
|
from .attrs cimport IS_OOV
|
2015-07-26 17:37:16 +03:00
|
|
|
|
2014-10-09 12:53:30 +04:00
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
memset(&EMPTY_LEXEME, 0, sizeof(LexemeC))
|
2014-10-09 12:53:30 +04:00
|
|
|
|
|
|
|
|
2015-01-12 03:23:44 +03:00
|
|
|
cdef class Lexeme:
|
2015-01-24 12:48:34 +03:00
|
|
|
"""An entry in the vocabulary. A Lexeme has no string context --- it's a
|
|
|
|
word-type, as opposed to a word token. It therefore has no part-of-speech
|
|
|
|
tag, dependency parse, or lemma (lemmatization depends on the part-of-speech
|
|
|
|
tag).
|
|
|
|
"""
|
2015-08-22 23:04:34 +03:00
|
|
|
def __init__(self, Vocab vocab, int orth):
|
|
|
|
self.vocab = vocab
|
|
|
|
self.orth = orth
|
2015-09-06 20:45:15 +03:00
|
|
|
self.c = <LexemeC*><void*>vocab.get_by_orth(vocab.mem, orth)
|
2015-08-23 21:49:18 +03:00
|
|
|
assert self.c.orth == orth
|
2015-08-22 23:04:34 +03:00
|
|
|
|
2016-05-05 02:32:26 +03:00
|
|
|
def __richcmp__(self, other, int op):
|
|
|
|
if isinstance(other, Lexeme):
|
|
|
|
a = self.orth
|
|
|
|
b = other.orth
|
|
|
|
elif isinstance(other, int):
|
|
|
|
a = self.orth
|
|
|
|
b = other
|
|
|
|
elif isinstance(other, str):
|
2016-05-12 13:58:57 +03:00
|
|
|
a = self.orth_
|
2016-05-05 02:32:26 +03:00
|
|
|
b = other
|
|
|
|
else:
|
|
|
|
a = 0
|
|
|
|
b = 1
|
|
|
|
if op == 2: # ==
|
|
|
|
return a == b
|
|
|
|
elif op == 3: # !=
|
|
|
|
return a != b
|
|
|
|
elif op == 0: # <
|
|
|
|
return a < b
|
|
|
|
elif op == 1: # <=
|
|
|
|
return a <= b
|
|
|
|
elif op == 4: # >
|
|
|
|
return a > b
|
|
|
|
elif op == 5: # >=
|
|
|
|
return a >= b
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(op)
|
|
|
|
|
2015-09-15 06:06:18 +03:00
|
|
|
def set_flag(self, attr_id_t flag_id, bint value):
|
|
|
|
Lexeme.c_set_flag(self.c, flag_id, value)
|
2015-09-06 18:52:51 +03:00
|
|
|
|
2015-09-15 06:06:18 +03:00
|
|
|
def check_flag(self, attr_id_t flag_id):
|
|
|
|
return True if Lexeme.c_check_flag(self.c, flag_id) else False
|
2015-09-06 18:52:51 +03:00
|
|
|
|
2015-09-14 10:49:58 +03:00
|
|
|
def similarity(self, other):
|
2015-09-22 03:10:01 +03:00
|
|
|
if self.vector_norm == 0 or other.vector_norm == 0:
|
|
|
|
return 0.0
|
2015-09-14 10:49:58 +03:00
|
|
|
return numpy.dot(self.vector, other.vector) / (self.vector_norm * other.vector_norm)
|
|
|
|
|
2015-09-21 12:52:43 +03:00
|
|
|
property has_vector:
|
|
|
|
def __get__(self):
|
2015-09-22 02:43:16 +03:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.vocab.vectors_length):
|
2015-11-03 15:47:59 +03:00
|
|
|
if self.c.vector[i] != 0:
|
2015-09-22 02:43:16 +03:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2015-09-21 12:52:43 +03:00
|
|
|
|
2015-09-14 10:49:58 +03:00
|
|
|
property vector_norm:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.l2_norm
|
|
|
|
|
|
|
|
def __set__(self, float value):
|
|
|
|
self.c.l2_norm = value
|
|
|
|
|
|
|
|
property vector:
|
|
|
|
def __get__(self):
|
2015-09-15 12:05:11 +03:00
|
|
|
cdef int length = self.vocab.vectors_length
|
2015-09-21 11:35:40 +03:00
|
|
|
if length == 0:
|
|
|
|
raise ValueError(
|
|
|
|
"Word vectors set to length 0. This may be because the "
|
|
|
|
"data is not installed. If you haven't already, run"
|
2016-03-24 13:47:09 +03:00
|
|
|
"\npython -m spacy.%s.download all\n"
|
|
|
|
"to install the data." % self.vocab.lang
|
2015-09-21 11:35:40 +03:00
|
|
|
)
|
|
|
|
|
2015-11-03 15:47:59 +03:00
|
|
|
vector_view = <float[:length,]>self.c.vector
|
|
|
|
return numpy.asarray(vector_view)
|
2015-09-14 10:49:58 +03:00
|
|
|
|
2015-09-15 07:42:27 +03:00
|
|
|
def __set__(self, vector):
|
2015-09-15 12:05:11 +03:00
|
|
|
assert len(vector) == self.vocab.vectors_length
|
2015-09-15 07:42:27 +03:00
|
|
|
cdef float value
|
|
|
|
for i, value in enumerate(vector):
|
2015-11-03 15:47:59 +03:00
|
|
|
self.c.vector[i] = value
|
2015-09-15 07:42:27 +03:00
|
|
|
|
2015-11-08 18:18:25 +03:00
|
|
|
property rank:
|
|
|
|
def __get__(self):
|
|
|
|
return self.c.id
|
|
|
|
|
2015-09-14 10:49:58 +03:00
|
|
|
property repvec:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vector
|
|
|
|
|
2015-08-26 20:16:28 +03:00
|
|
|
property orth_:
|
|
|
|
def __get__(self):
|
|
|
|
return self.vocab.strings[self.c.orth]
|
2015-08-22 23:04:34 +03:00
|
|
|
|
|
|
|
property lower:
|
|
|
|
def __get__(self): return self.c.lower
|
|
|
|
def __set__(self, int x): self.c.lower = x
|
|
|
|
|
|
|
|
property norm:
|
|
|
|
def __get__(self): return self.c.norm
|
|
|
|
def __set__(self, int x): self.c.norm = x
|
|
|
|
|
|
|
|
property shape:
|
|
|
|
def __get__(self): return self.c.shape
|
|
|
|
def __set__(self, int x): self.c.shape = x
|
|
|
|
|
|
|
|
property prefix:
|
|
|
|
def __get__(self): return self.c.prefix
|
|
|
|
def __set__(self, int x): self.c.prefix = x
|
|
|
|
|
|
|
|
property suffix:
|
|
|
|
def __get__(self): return self.c.suffix
|
|
|
|
def __set__(self, int x): self.c.suffix = x
|
|
|
|
|
2015-08-26 20:16:28 +03:00
|
|
|
property cluster:
|
2015-09-09 15:29:22 +03:00
|
|
|
def __get__(self): return self.c.cluster
|
|
|
|
def __set__(self, int x): self.c.cluster = x
|
2015-08-26 20:16:28 +03:00
|
|
|
|
2016-03-10 15:01:34 +03:00
|
|
|
property lang:
|
|
|
|
def __get__(self): return self.c.lang
|
|
|
|
def __set__(self, int x): self.c.lang = x
|
|
|
|
|
2015-08-26 20:16:28 +03:00
|
|
|
property prob:
|
2015-09-09 15:29:22 +03:00
|
|
|
def __get__(self): return self.c.prob
|
|
|
|
def __set__(self, float x): self.c.prob = x
|
2015-08-22 23:04:34 +03:00
|
|
|
|
|
|
|
property lower_:
|
|
|
|
def __get__(self): return self.vocab.strings[self.c.lower]
|
|
|
|
def __set__(self, unicode x): self.c.lower = self.vocab.strings[x]
|
|
|
|
|
|
|
|
property norm_:
|
2015-09-09 15:29:22 +03:00
|
|
|
def __get__(self): return self.vocab.strings[self.c.norm]
|
2015-08-22 23:04:34 +03:00
|
|
|
def __set__(self, unicode x): self.c.norm = self.vocab.strings[x]
|
|
|
|
|
|
|
|
property shape_:
|
|
|
|
def __get__(self): return self.vocab.strings[self.c.shape]
|
|
|
|
def __set__(self, unicode x): self.c.shape = self.vocab.strings[x]
|
2015-02-07 16:42:44 +03:00
|
|
|
|
2015-08-22 23:04:34 +03:00
|
|
|
property prefix_:
|
2015-09-09 15:29:22 +03:00
|
|
|
def __get__(self): return self.vocab.strings[self.c.prefix]
|
2015-08-22 23:04:34 +03:00
|
|
|
def __set__(self, unicode x): self.c.prefix = self.vocab.strings[x]
|
2015-02-07 16:42:44 +03:00
|
|
|
|
2015-08-22 23:04:34 +03:00
|
|
|
property suffix_:
|
2015-09-09 15:29:22 +03:00
|
|
|
def __get__(self): return self.vocab.strings[self.c.suffix]
|
2015-08-22 23:04:34 +03:00
|
|
|
def __set__(self, unicode x): self.c.suffix = self.vocab.strings[x]
|
2015-07-26 17:37:16 +03:00
|
|
|
|
2016-03-10 15:01:34 +03:00
|
|
|
property lang_:
|
|
|
|
def __get__(self): return self.vocab.strings[self.c.lang]
|
|
|
|
def __set__(self, unicode x): self.c.lang = self.vocab.strings[x]
|
|
|
|
|
2015-08-26 20:16:28 +03:00
|
|
|
property flags:
|
|
|
|
def __get__(self): return self.c.flags
|
|
|
|
def __set__(self, flags_t x): self.c.flags = x
|
|
|
|
|
2015-07-27 02:50:06 +03:00
|
|
|
property is_oov:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_OOV)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_OOV, x)
|
2015-07-27 02:50:06 +03:00
|
|
|
|
2015-09-14 11:25:40 +03:00
|
|
|
property is_stop:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_STOP)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_STOP, x)
|
2015-09-14 11:25:40 +03:00
|
|
|
|
2015-07-26 17:37:16 +03:00
|
|
|
property is_alpha:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_ALPHA)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_ALPHA, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_ascii:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_ASCII)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_ASCII, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_digit:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_DIGIT)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_DIGIT, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_lower:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_LOWER)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_LOWER, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_title:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_TITLE)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_TITLE, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_punct:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_PUNCT)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_PUNCT, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property is_space:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_SPACE)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_SPACE, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
2016-02-04 15:04:16 +03:00
|
|
|
property is_bracket:
|
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_BRACKET)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_BRACKET, x)
|
|
|
|
|
|
|
|
property is_quote:
|
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_QUOTE)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_QUOTE, x)
|
|
|
|
|
|
|
|
property is_left_punct:
|
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_LEFT_PUNCT)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_LEFT_PUNCT, x)
|
|
|
|
|
|
|
|
property is_right_punct:
|
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, IS_RIGHT_PUNCT)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, IS_RIGHT_PUNCT, x)
|
|
|
|
|
|
|
|
|
2015-07-26 17:37:16 +03:00
|
|
|
property like_url:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, LIKE_URL)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, LIKE_URL, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property like_num:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, LIKE_NUM)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, LIKE_NUM, x)
|
2015-07-26 17:37:16 +03:00
|
|
|
|
|
|
|
property like_email:
|
2015-09-15 06:06:18 +03:00
|
|
|
def __get__(self): return Lexeme.c_check_flag(self.c, LIKE_EMAIL)
|
|
|
|
def __set__(self, bint x): Lexeme.c_set_flag(self.c, LIKE_EMAIL, x)
|