spaCy/spacy/word.pyx

104 lines
3.9 KiB
Cython
Raw Normal View History

2014-08-24 16:14:08 +00:00
# cython: profile=True
# cython: embedsignature=True
from libc.stdlib cimport calloc, free, realloc
2014-08-24 16:14:08 +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.
"""
2014-08-28 17:45:09 +00:00
def __cinit__(self, unicode string, double prob, int cluster, dict case_stats,
dict tag_stats, list string_features, list flag_features):
self.prob = prob
self.cluster = cluster
self.length = len(string)
2014-08-28 17:45:09 +00:00
self.string = string
2014-08-30 17:01:15 +00:00
self.views = []
cdef unicode view
2014-08-28 17:45:09 +00:00
for string_feature in string_features:
view = string_feature(string, prob, cluster, case_stats, tag_stats)
self.views.append(view)
2014-08-28 17:45:09 +00:00
for i, flag_feature in enumerate(flag_features):
if flag_feature(string, prob, case_stats, tag_stats):
2014-08-29 01:01:40 +00:00
self.flags |= (1 << i)
2014-08-24 16:14:08 +00:00
def __dealloc__(self):
pass
2014-08-24 16:14:08 +00:00
2014-08-25 14:42:22 +00:00
cpdef bint check_flag(self, size_t flag_id) except *:
2014-08-29 01:01:40 +00:00
"""Lexemes may store language-specific boolean features in a bit-field,
with values accessed by providing an ID constant to this function.
The ID constants are exposed as global variables in the language module,
e.g.
>>> from spacy.en import EN
>>> lexeme = EN.lookup(u'Nasa')
>>> lexeme.check_flag(EN.IS_UPPER)
False
>>> lexeme.check_flag(EN.OFT_UPPER)
True
2014-08-24 16:14:08 +00:00
"""
2014-08-25 14:42:22 +00:00
return self.flags & (1 << flag_id)
2014-08-29 01:01:40 +00:00
cpdef unicode string_view(self, size_t view_id):
"""Lexemes may store language-specific string-view features, obtained
by transforming the string, possibly in light of distributional information.
The string-view features are accessed by providing an ID constant to this
function.
The ID constants are exposed as global variables in the language module,
e.g.
>>> from spacy.en import EN
>>> lexeme = EN.lookup(u'Nasa')
>>> lexeme.string_view(EN.CANON_CASED)
u'NASA'
>>> lexeme.string_view(EN.SHAPE)
u'Xxxx'
>>> lexeme.string_view(EN.NON_SPARSE)
u'Xxxx'
"""
return self.views[view_id]