spaCy/spacy/word.pyx

81 lines
3.0 KiB
Cython
Raw Normal View History

2014-08-24 16:14:08 +00:00
# cython: profile=True
# cython: embedsignature=True
from .lexeme cimport lexeme_get_string
from .lexeme cimport lexeme_check_orth_flag, lexeme_check_dist_flag
from .lexeme cimport *
2014-09-10 18:41:47 +00:00
2014-08-25 14:42:22 +00:00
cdef class Lexeme:
2014-08-29 01:01:40 +00:00
"""A lexical type --- a word, punctuation symbol, whitespace sequence, etc
keyed by a case-sensitive unicode string. All tokens with the same string,
e.g. all instances of "dog", ",", "NASA" etc should be mapped to the same
Lexeme.
2014-08-24 16:14:08 +00:00
2014-08-29 01:01:40 +00:00
You should avoid instantiating Lexemes directly, and instead use the
:py:meth:`space.lang.Language.tokenize` and :py:meth:`spacy.lang.Language.lookup`
methods on the global object exposed by the language you're working with,
e.g. :py:data:`spacy.en.EN`.
2014-08-24 16:14:08 +00:00
2014-08-25 14:42:22 +00:00
Attributes:
string (unicode):
The unicode string.
Implemented as a property; relatively expensive.
2014-08-24 16:14:08 +00:00
length (size_t):
2014-08-25 14:42:22 +00:00
The number of unicode code-points in the string.
2014-08-24 16:14:08 +00:00
prob (double):
An estimate of the word's unigram log probability.
Probabilities are calculated from a large text corpus, and smoothed using
simple Good-Turing. Estimates are read from data/en/probabilities, and
can be replaced using spacy.en.load_probabilities.
2014-08-29 01:01:40 +00:00
cluster (size_t):
2014-08-24 16:14:08 +00:00
An integer representation of the word's Brown cluster.
A Brown cluster is an address into a binary tree, which gives some (noisy)
information about the word's distributional context.
>>> strings = (u'pineapple', u'apple', u'dapple', u'scalable')
>>> print ["{0:b"} % lookup(s).cluster for s in strings]
["100111110110", "100111100100", "01010111011001", "100111110110"]
The clusterings are unideal, but often slightly useful.
"pineapple" and "apple" share a long prefix, indicating a similar meaning,
while "dapple" is totally different. On the other hand, "scalable" receives
the same cluster ID as "pineapple", which is not what we'd like.
"""
def __cinit__(self, size_t lexeme_addr):
self._c = <LexemeC*>lexeme_addr
2014-09-10 18:41:47 +00:00
property string:
def __get__(self):
2014-10-10 08:17:22 +00:00
cdef bytes utf8_string = self._c.strings[<int>LexStr_orig]
2014-09-10 18:41:47 +00:00
cdef unicode string = utf8_string.decode('utf8')
return string
property prob:
def __get__(self):
return self._c.floats[<int>LexFloat_prob]
2014-09-10 18:41:47 +00:00
property cluster:
def __get__(self):
return self._c.ints[<int>LexInt_cluster]
2014-08-24 16:14:08 +00:00
property length:
def __get__(self):
return self._c.ints[<int>LexInt_length]
2014-08-29 01:01:40 +00:00
cpdef bint check_orth_flag(self, size_t flag_id) except *:
return lexeme_check_orth_flag(self._c, flag_id)
2014-08-29 01:01:40 +00:00
cpdef bint check_dist_flag(self, size_t flag_id) except *:
return lexeme_check_dist_flag(self._c, flag_id)
2014-08-25 14:42:22 +00:00
2014-08-29 01:01:40 +00:00
cpdef unicode string_view(self, size_t view_id):
return lexeme_get_string(self._c, view_id)