2017-04-15 13:05:47 +03:00
|
|
|
# coding: utf8
|
2015-07-23 14:24:20 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-01-17 08:21:17 +03:00
|
|
|
import bz2
|
2017-04-15 13:13:34 +03:00
|
|
|
import ujson
|
2017-02-17 01:26:21 +03:00
|
|
|
import re
|
2014-12-19 22:54:03 +03:00
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
from libc.string cimport memset, memcpy
|
2017-04-15 13:05:47 +03:00
|
|
|
from libc.stdint cimport int32_t
|
|
|
|
from libc.math cimport sqrt
|
|
|
|
from cymem.cymem cimport Address
|
2014-12-19 22:54:03 +03:00
|
|
|
from .lexeme cimport EMPTY_LEXEME
|
2015-01-17 08:21:17 +03:00
|
|
|
from .lexeme cimport Lexeme
|
2015-01-12 02:26:22 +03:00
|
|
|
from .strings cimport hash_string
|
2015-01-31 08:38:58 +03:00
|
|
|
from .typedefs cimport attr_t
|
2017-05-17 13:04:50 +03:00
|
|
|
from .cfile cimport CFile
|
2016-11-25 14:43:24 +03:00
|
|
|
from .tokens.token cimport Token
|
2016-03-16 17:53:35 +03:00
|
|
|
from .attrs cimport PROB, LANG
|
2017-05-17 13:04:50 +03:00
|
|
|
from .structs cimport SerializedLexemeC
|
2016-09-24 16:42:01 +03:00
|
|
|
|
2017-04-15 13:11:16 +03:00
|
|
|
from .compat import copy_reg, pickle
|
2017-04-15 13:05:47 +03:00
|
|
|
from .lemmatizer import Lemmatizer
|
|
|
|
from .attrs import intify_attrs
|
|
|
|
from . import util
|
|
|
|
from . import attrs
|
|
|
|
from . import symbols
|
2015-10-13 12:04:40 +03:00
|
|
|
|
2014-12-24 09:42:00 +03:00
|
|
|
|
2014-12-19 22:54:03 +03:00
|
|
|
cdef class Vocab:
|
2017-05-20 14:59:31 +03:00
|
|
|
"""A look-up table that allows you to access `Lexeme` objects. The `Vocab`
|
|
|
|
instance also provides access to the `StringStore`, and owns underlying
|
|
|
|
C-data that is shared between `Doc` objects.
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2016-09-25 15:49:53 +03:00
|
|
|
def __init__(self, lex_attr_getters=None, tag_map=None, lemmatizer=None,
|
2017-05-09 18:28:50 +03:00
|
|
|
strings=tuple(), **deprecated_kwargs):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Create the vocabulary.
|
|
|
|
|
|
|
|
lex_attr_getters (dict): A dictionary mapping attribute IDs to functions
|
|
|
|
to compute them. Defaults to `None`.
|
|
|
|
tag_map (dict): A dictionary mapping fine-grained tags to coarse-grained
|
|
|
|
parts-of-speech, and optionally morphological attributes.
|
|
|
|
lemmatizer (object): A lemmatizer. Defaults to `None`.
|
|
|
|
strings (StringStore): StringStore that maps strings to integers, and
|
|
|
|
vice versa.
|
|
|
|
RETURNS (Vocab): The newly constructed vocab object.
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2016-09-25 15:49:53 +03:00
|
|
|
lex_attr_getters = lex_attr_getters if lex_attr_getters is not None else {}
|
|
|
|
tag_map = tag_map if tag_map is not None else {}
|
|
|
|
if lemmatizer in (None, True, False):
|
2015-09-10 15:49:10 +03:00
|
|
|
lemmatizer = Lemmatizer({}, {}, {})
|
2016-09-25 15:49:53 +03:00
|
|
|
|
2015-09-10 15:49:10 +03:00
|
|
|
self.mem = Pool()
|
|
|
|
self._by_hash = PreshMap()
|
|
|
|
self._by_orth = PreshMap()
|
|
|
|
self.strings = StringStore()
|
2017-03-17 20:29:04 +03:00
|
|
|
if strings:
|
|
|
|
for string in strings:
|
2017-05-28 16:10:22 +03:00
|
|
|
self.strings.add(string)
|
2015-10-06 16:39:50 +03:00
|
|
|
# Load strings in a special order, so that we have an onset number for
|
|
|
|
# the vocabulary. This way, when words are added in order, the orth ID
|
|
|
|
# is the frequency rank of the word, plus a certain offset. The structural
|
|
|
|
# strings are loaded first, because the vocab is open-class, and these
|
|
|
|
# symbols are closed class.
|
2016-09-24 16:42:01 +03:00
|
|
|
# TODO: Actually this has turned out to be a pain in the ass...
|
|
|
|
# It means the data is invalidated when we add a symbol :(
|
|
|
|
# Need to rethink this.
|
2015-10-10 14:12:06 +03:00
|
|
|
for name in symbols.NAMES + list(sorted(tag_map.keys())):
|
2015-10-10 10:27:03 +03:00
|
|
|
if name:
|
2017-05-28 14:03:16 +03:00
|
|
|
self.strings.add(name)
|
2016-09-25 15:49:53 +03:00
|
|
|
self.lex_attr_getters = lex_attr_getters
|
2015-09-10 15:49:10 +03:00
|
|
|
self.morphology = Morphology(self.strings, tag_map, lemmatizer)
|
2016-12-21 20:04:41 +03:00
|
|
|
|
2015-09-10 15:49:10 +03:00
|
|
|
self.length = 1
|
2015-07-19 16:18:17 +03:00
|
|
|
|
2016-03-16 17:53:35 +03:00
|
|
|
property lang:
|
|
|
|
def __get__(self):
|
|
|
|
langfunc = None
|
2016-09-25 15:49:53 +03:00
|
|
|
if self.lex_attr_getters:
|
|
|
|
langfunc = self.lex_attr_getters.get(LANG, None)
|
2016-03-16 17:53:35 +03:00
|
|
|
return langfunc('_') if langfunc else ''
|
|
|
|
|
2014-12-19 22:54:03 +03:00
|
|
|
def __len__(self):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""The current number of lexemes stored.
|
|
|
|
|
|
|
|
RETURNS (int): The current number of lexemes stored.
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2015-07-18 23:42:15 +03:00
|
|
|
return self.length
|
2017-05-20 14:59:31 +03:00
|
|
|
|
2016-10-14 13:15:38 +03:00
|
|
|
def add_flag(self, flag_getter, int flag_id=-1):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Set a new boolean flag to words in the vocabulary.
|
2016-12-21 20:04:41 +03:00
|
|
|
|
2017-05-20 14:59:31 +03:00
|
|
|
The flag_getter function will be called over the words currently in the
|
2016-11-01 14:25:36 +03:00
|
|
|
vocab, and then applied to new words as they occur. You'll then be able
|
|
|
|
to access the flag value on each token, using token.check_flag(flag_id).
|
2017-05-20 14:59:31 +03:00
|
|
|
See also: `Lexeme.set_flag`, `Lexeme.check_flag`, `Token.set_flag`,
|
|
|
|
`Token.check_flag`.
|
|
|
|
|
2017-05-21 14:17:40 +03:00
|
|
|
flag_getter (callable): A function `f(unicode) -> bool`, to get the flag
|
2017-05-20 14:59:31 +03:00
|
|
|
value.
|
|
|
|
flag_id (int): An integer between 1 and 63 (inclusive), specifying
|
|
|
|
the bit at which the flag will be stored. If -1, the lowest
|
|
|
|
available bit will be chosen.
|
|
|
|
RETURNS (int): The integer ID by which the flag value can be checked.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
>>> MY_PRODUCT = nlp.vocab.add_flag(lambda text: text in ['spaCy', 'dislaCy'])
|
|
|
|
>>> doc = nlp(u'I like spaCy')
|
|
|
|
>>> assert doc[2].check_flag(MY_PRODUCT) == True
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2016-10-14 13:15:38 +03:00
|
|
|
if flag_id == -1:
|
|
|
|
for bit in range(1, 64):
|
|
|
|
if bit not in self.lex_attr_getters:
|
|
|
|
flag_id = bit
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"Cannot find empty bit for new lexical flag. All bits between "
|
|
|
|
"0 and 63 are occupied. You can replace one by specifying the "
|
|
|
|
"flag_id explicitly, e.g. nlp.vocab.add_flag(your_func, flag_id=IS_ALPHA")
|
|
|
|
elif flag_id >= 64 or flag_id < 1:
|
|
|
|
raise ValueError(
|
|
|
|
"Invalid value for flag_id: %d. Flag IDs must be between "
|
|
|
|
"1 and 63 (inclusive)" % flag_id)
|
|
|
|
for lex in self:
|
|
|
|
lex.set_flag(flag_id, flag_getter(lex.orth_))
|
|
|
|
self.lex_attr_getters[flag_id] = flag_getter
|
|
|
|
return flag_id
|
|
|
|
|
2015-07-22 05:49:39 +03:00
|
|
|
cdef const LexemeC* get(self, Pool mem, unicode string) except NULL:
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Get a pointer to a `LexemeC` from the lexicon, creating a new `Lexeme`
|
|
|
|
if necessary, using memory acquired from the given pool. If the pool
|
2017-04-15 12:59:21 +03:00
|
|
|
is the lexicon's own memory, the lexeme is saved in the lexicon.
|
|
|
|
"""
|
2015-07-26 01:18:30 +03:00
|
|
|
if string == u'':
|
|
|
|
return &EMPTY_LEXEME
|
2015-01-12 02:26:22 +03:00
|
|
|
cdef LexemeC* lex
|
2015-07-22 05:49:39 +03:00
|
|
|
cdef hash_t key = hash_string(string)
|
|
|
|
lex = <LexemeC*>self._by_hash.get(key)
|
2015-08-23 21:49:18 +03:00
|
|
|
cdef size_t addr
|
2014-12-19 22:54:03 +03:00
|
|
|
if lex != NULL:
|
2016-09-30 21:20:22 +03:00
|
|
|
if lex.orth != self.strings[string]:
|
2015-10-06 02:34:59 +03:00
|
|
|
raise LookupError.mismatched_strings(
|
2017-05-17 13:04:50 +03:00
|
|
|
lex.orth, self.strings[string], string)
|
2014-12-19 22:54:03 +03:00
|
|
|
return lex
|
2015-07-20 02:37:34 +03:00
|
|
|
else:
|
2015-08-22 23:04:34 +03:00
|
|
|
return self._new_lexeme(mem, string)
|
2014-12-19 22:54:03 +03:00
|
|
|
|
2015-07-23 02:18:19 +03:00
|
|
|
cdef const LexemeC* get_by_orth(self, Pool mem, attr_t orth) except NULL:
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Get a pointer to a `LexemeC` from the lexicon, creating a new `Lexeme`
|
|
|
|
if necessary, using memory acquired from the given pool. If the pool
|
2017-04-15 12:59:21 +03:00
|
|
|
is the lexicon's own memory, the lexeme is saved in the lexicon.
|
|
|
|
"""
|
2015-07-26 20:26:41 +03:00
|
|
|
if orth == 0:
|
2015-07-26 19:39:27 +03:00
|
|
|
return &EMPTY_LEXEME
|
2015-07-23 02:18:19 +03:00
|
|
|
cdef LexemeC* lex
|
|
|
|
lex = <LexemeC*>self._by_orth.get(orth)
|
|
|
|
if lex != NULL:
|
|
|
|
return lex
|
2015-08-22 23:04:34 +03:00
|
|
|
else:
|
2016-09-30 21:10:30 +03:00
|
|
|
return self._new_lexeme(mem, self.strings[orth])
|
2015-08-22 23:04:34 +03:00
|
|
|
|
|
|
|
cdef const LexemeC* _new_lexeme(self, Pool mem, unicode string) except NULL:
|
2015-08-23 21:49:18 +03:00
|
|
|
cdef hash_t key
|
2016-12-27 23:03:45 +03:00
|
|
|
if len(string) < 3 or self.length < 10000:
|
2016-09-30 21:10:30 +03:00
|
|
|
mem = self.mem
|
2016-12-27 23:03:45 +03:00
|
|
|
cdef bint is_oov = mem is not self.mem
|
2016-09-30 21:10:30 +03:00
|
|
|
lex = <LexemeC*>mem.alloc(sizeof(LexemeC), 1)
|
2017-05-28 16:10:22 +03:00
|
|
|
lex.orth = self.strings.add(string)
|
2015-08-26 20:21:46 +03:00
|
|
|
lex.length = len(string)
|
2015-08-23 21:49:18 +03:00
|
|
|
lex.id = self.length
|
2016-09-25 15:49:53 +03:00
|
|
|
if self.lex_attr_getters is not None:
|
|
|
|
for attr, func in self.lex_attr_getters.items():
|
2015-08-23 21:49:18 +03:00
|
|
|
value = func(string)
|
|
|
|
if isinstance(value, unicode):
|
2017-05-28 13:36:27 +03:00
|
|
|
value = self.strings.add(value)
|
2015-08-26 20:21:46 +03:00
|
|
|
if attr == PROB:
|
|
|
|
lex.prob = value
|
2016-10-09 13:24:24 +03:00
|
|
|
elif value is not None:
|
2015-08-26 20:21:46 +03:00
|
|
|
Lexeme.set_struct_attr(lex, attr, value)
|
2016-09-30 21:10:30 +03:00
|
|
|
if is_oov:
|
2015-07-23 02:18:19 +03:00
|
|
|
lex.id = 0
|
|
|
|
else:
|
2015-08-23 21:49:18 +03:00
|
|
|
key = hash_string(string)
|
2015-08-22 23:04:34 +03:00
|
|
|
self._add_lex_to_vocab(key, lex)
|
|
|
|
assert lex != NULL, string
|
2015-07-23 02:18:19 +03:00
|
|
|
return lex
|
|
|
|
|
2015-01-13 16:03:48 +03:00
|
|
|
cdef int _add_lex_to_vocab(self, hash_t key, const LexemeC* lex) except -1:
|
2015-07-18 23:42:15 +03:00
|
|
|
self._by_hash.set(key, <void*>lex)
|
|
|
|
self._by_orth.set(lex.orth, <void*>lex)
|
|
|
|
self.length += 1
|
|
|
|
|
2016-03-08 18:49:10 +03:00
|
|
|
def __contains__(self, unicode string):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Check whether the string has an entry in the vocabulary.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
2017-05-20 14:59:31 +03:00
|
|
|
string (unicode): The ID string.
|
|
|
|
RETURNS (bool) Whether the string has an entry in the vocabulary.
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2016-03-08 18:49:10 +03:00
|
|
|
key = hash_string(string)
|
|
|
|
lex = self._by_hash.get(key)
|
2017-01-11 12:18:22 +03:00
|
|
|
return lex is not NULL
|
2016-03-08 18:49:10 +03:00
|
|
|
|
2015-07-18 23:42:15 +03:00
|
|
|
def __iter__(self):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Iterate over the lexemes in the vocabulary.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
2017-05-20 14:59:31 +03:00
|
|
|
YIELDS (Lexeme): An entry in the vocabulary.
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2015-07-18 23:42:15 +03:00
|
|
|
cdef attr_t orth
|
|
|
|
cdef size_t addr
|
|
|
|
for orth, addr in self._by_orth.items():
|
2015-08-23 21:49:18 +03:00
|
|
|
yield Lexeme(self, orth)
|
2015-01-13 16:03:48 +03:00
|
|
|
|
2014-12-19 22:54:03 +03:00
|
|
|
def __getitem__(self, id_or_string):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Retrieve a lexeme, given an int ID or a unicode string. If a
|
|
|
|
previously unseen unicode string is given, a new lexeme is created and
|
|
|
|
stored.
|
|
|
|
|
|
|
|
id_or_string (int or unicode): The integer ID of a word, or its unicode
|
|
|
|
string. If `int >= Lexicon.size`, `IndexError` is raised. If
|
|
|
|
`id_or_string` is neither an int nor a unicode string, `ValueError`
|
|
|
|
is raised.
|
|
|
|
RETURNS (Lexeme): The lexeme indicated by the given ID.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
>>> apple = nlp.vocab.strings['apple']
|
|
|
|
>>> assert nlp.vocab[apple] == nlp.vocab[u'apple']
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2015-07-18 23:42:15 +03:00
|
|
|
cdef attr_t orth
|
2016-09-30 21:10:30 +03:00
|
|
|
if type(id_or_string) == unicode:
|
2017-05-28 13:36:27 +03:00
|
|
|
orth = self.strings.add(id_or_string)
|
2015-01-14 16:33:16 +03:00
|
|
|
else:
|
2015-08-23 21:49:18 +03:00
|
|
|
orth = id_or_string
|
|
|
|
return Lexeme(self, orth)
|
2014-12-19 22:54:03 +03:00
|
|
|
|
2015-08-28 03:02:33 +03:00
|
|
|
cdef const TokenC* make_fused_token(self, substrings) except NULL:
|
|
|
|
cdef int i
|
|
|
|
tokens = <TokenC*>self.mem.alloc(len(substrings) + 1, sizeof(TokenC))
|
|
|
|
for i, props in enumerate(substrings):
|
2016-11-25 14:43:24 +03:00
|
|
|
props = intify_attrs(props, strings_map=self.strings, _do_deprecated=True)
|
2015-08-28 03:02:33 +03:00
|
|
|
token = &tokens[i]
|
2016-11-25 14:43:24 +03:00
|
|
|
# Set the special tokens up to have arbitrary attributes
|
|
|
|
token.lex = <LexemeC*>self.get_by_orth(self.mem, props[attrs.ORTH])
|
|
|
|
if attrs.TAG in props:
|
|
|
|
self.morphology.assign_tag(token, props[attrs.TAG])
|
|
|
|
for attr_id, value in props.items():
|
|
|
|
Token.set_struct_attr(token, attr_id, value)
|
2015-08-28 03:02:33 +03:00
|
|
|
return tokens
|
2016-12-21 20:04:41 +03:00
|
|
|
|
2017-05-28 12:45:32 +03:00
|
|
|
def get_vector(self, orth):
|
|
|
|
"""Retrieve a vector for a word in the vocabulary.
|
|
|
|
|
|
|
|
Words can be looked up by string or int ID.
|
|
|
|
|
|
|
|
RETURNS:
|
|
|
|
A word vector. Size and shape determed by the
|
|
|
|
vocab.vectors instance. Usually, a numpy ndarray
|
|
|
|
of shape (300,) and dtype float32.
|
|
|
|
|
|
|
|
RAISES: If no vectors data is loaded, ValueError is raised.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def has_vector(self, orth):
|
|
|
|
"""Check whether a word has a vector. Returns False if no
|
|
|
|
vectors have been loaded. Words can be looked up by string
|
|
|
|
or int ID."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
def to_disk(self, path):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Save the current state to a directory.
|
|
|
|
|
|
|
|
path (unicode or Path): A path to a directory, which will be created if
|
|
|
|
it doesn't exist. Paths may be either strings or `Path`-like objects.
|
|
|
|
"""
|
2017-05-17 13:04:50 +03:00
|
|
|
path = util.ensure_path(path)
|
|
|
|
if not path.exists():
|
|
|
|
path.mkdir()
|
|
|
|
strings_loc = path / 'strings.json'
|
|
|
|
with strings_loc.open('w', encoding='utf8') as file_:
|
|
|
|
self.strings.dump(file_)
|
2017-05-20 14:59:31 +03:00
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
def from_disk(self, path):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Loads state from a directory. Modifies the object in place and
|
|
|
|
returns it.
|
|
|
|
|
|
|
|
path (unicode or Path): A path to a directory. Paths may be either
|
|
|
|
strings or `Path`-like objects.
|
|
|
|
RETURNS (Vocab): The modified `Vocab` object.
|
|
|
|
"""
|
2017-05-17 13:04:50 +03:00
|
|
|
path = util.ensure_path(path)
|
|
|
|
with (path / 'vocab' / 'strings.json').open('r', encoding='utf8') as file_:
|
|
|
|
strings_list = ujson.load(file_)
|
|
|
|
for string in strings_list:
|
2017-05-28 13:36:27 +03:00
|
|
|
self.strings.add(string)
|
2017-05-17 13:04:50 +03:00
|
|
|
self.load_lexemes(path / 'lexemes.bin')
|
2016-11-01 14:25:36 +03:00
|
|
|
|
2017-05-20 14:59:31 +03:00
|
|
|
def to_bytes(self, **exclude):
|
|
|
|
"""Serialize the current state to a binary string.
|
|
|
|
|
|
|
|
**exclude: Named attributes to prevent from being serialized.
|
|
|
|
RETURNS (bytes): The serialized form of the `Vocab` object.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2017-05-21 15:18:46 +03:00
|
|
|
def from_bytes(self, bytes_data, **exclude):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Load state from a binary string.
|
|
|
|
|
|
|
|
bytes_data (bytes): The data to load from.
|
|
|
|
**exclude: Named attributes to prevent from being loaded.
|
|
|
|
RETURNS (Vocab): The `Vocab` object.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
def lexemes_to_bytes(self, **exclude):
|
2014-12-19 22:54:03 +03:00
|
|
|
cdef hash_t key
|
2017-05-17 13:04:50 +03:00
|
|
|
cdef size_t addr
|
2017-03-11 21:43:09 +03:00
|
|
|
cdef LexemeC* lexeme = NULL
|
2017-05-17 13:04:50 +03:00
|
|
|
cdef SerializedLexemeC lex_data
|
|
|
|
cdef int size = 0
|
2015-07-27 11:58:15 +03:00
|
|
|
for key, addr in self._by_hash.items():
|
2017-05-17 13:04:50 +03:00
|
|
|
if addr == 0:
|
|
|
|
continue
|
|
|
|
size += sizeof(lex_data.data)
|
|
|
|
byte_string = b'\0' * size
|
|
|
|
byte_ptr = <unsigned char*>byte_string
|
|
|
|
cdef int j
|
|
|
|
cdef int i = 0
|
|
|
|
for key, addr in self._by_hash.items():
|
|
|
|
if addr == 0:
|
|
|
|
continue
|
2015-07-18 23:42:15 +03:00
|
|
|
lexeme = <LexemeC*>addr
|
2017-05-17 13:04:50 +03:00
|
|
|
lex_data = Lexeme.c_to_bytes(lexeme)
|
|
|
|
for j in range(sizeof(lex_data.data)):
|
|
|
|
byte_ptr[i] = lex_data.data[j]
|
|
|
|
i += 1
|
|
|
|
return byte_string
|
2016-11-01 14:25:36 +03:00
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
def lexemes_from_bytes(self, bytes bytes_data):
|
2017-05-20 14:59:31 +03:00
|
|
|
"""Load the binary vocabulary data from the given string."""
|
2017-05-17 13:04:50 +03:00
|
|
|
cdef LexemeC* lexeme
|
2017-03-07 22:25:12 +03:00
|
|
|
cdef hash_t key
|
|
|
|
cdef unicode py_str
|
2017-05-17 13:04:50 +03:00
|
|
|
cdef int i = 0
|
|
|
|
cdef int j = 0
|
|
|
|
cdef SerializedLexemeC lex_data
|
|
|
|
chunk_size = sizeof(lex_data.data)
|
|
|
|
cdef unsigned char* bytes_ptr = bytes_data
|
|
|
|
for i in range(0, len(bytes_data), chunk_size):
|
|
|
|
lexeme = <LexemeC*>self.mem.alloc(1, sizeof(LexemeC))
|
|
|
|
for j in range(sizeof(lex_data.data)):
|
|
|
|
lex_data.data[j] = bytes_ptr[i+j]
|
|
|
|
Lexeme.c_from_bytes(lexeme, lex_data)
|
2017-03-07 22:25:12 +03:00
|
|
|
|
|
|
|
py_str = self.strings[lexeme.orth]
|
2017-05-17 13:04:50 +03:00
|
|
|
assert self.strings[py_str] == lexeme.orth, (py_str, lexeme.orth)
|
2017-03-07 22:25:12 +03:00
|
|
|
key = hash_string(py_str)
|
|
|
|
self._by_hash.set(key, lexeme)
|
|
|
|
self._by_orth.set(lexeme.orth, lexeme)
|
|
|
|
self.length += 1
|
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
|
2017-03-07 22:25:12 +03:00
|
|
|
def pickle_vocab(vocab):
|
|
|
|
sstore = vocab.strings
|
|
|
|
morph = vocab.morphology
|
|
|
|
length = vocab.length
|
|
|
|
data_dir = vocab.data_dir
|
|
|
|
lex_attr_getters = vocab.lex_attr_getters
|
|
|
|
|
2017-05-17 13:04:50 +03:00
|
|
|
lexemes_data = vocab.lexemes_to_bytes()
|
2017-03-07 22:25:12 +03:00
|
|
|
vectors_length = vocab.vectors_length
|
|
|
|
|
|
|
|
return (unpickle_vocab,
|
2017-05-09 18:28:50 +03:00
|
|
|
(sstore, morph, data_dir, lex_attr_getters,
|
2017-03-07 22:25:12 +03:00
|
|
|
lexemes_data, length, vectors_length))
|
|
|
|
|
|
|
|
|
2017-05-09 18:28:50 +03:00
|
|
|
def unpickle_vocab(sstore, morphology, data_dir,
|
2017-03-07 22:25:12 +03:00
|
|
|
lex_attr_getters, bytes lexemes_data, int length, int vectors_length):
|
|
|
|
cdef Vocab vocab = Vocab()
|
|
|
|
vocab.length = length
|
|
|
|
vocab.vectors_length = vectors_length
|
|
|
|
vocab.strings = sstore
|
|
|
|
vocab.morphology = morphology
|
|
|
|
vocab.data_dir = data_dir
|
|
|
|
vocab.lex_attr_getters = lex_attr_getters
|
2017-05-17 13:04:50 +03:00
|
|
|
vocab.lexemes_from_bytes(lexemes_data)
|
2017-03-07 22:25:12 +03:00
|
|
|
vocab.length = length
|
|
|
|
vocab.vectors_length = vectors_length
|
|
|
|
return vocab
|
|
|
|
|
|
|
|
|
|
|
|
copy_reg.pickle(Vocab, pickle_vocab, unpickle_vocab)
|
|
|
|
|
|
|
|
|
2015-10-06 02:34:59 +03:00
|
|
|
class LookupError(Exception):
|
|
|
|
@classmethod
|
|
|
|
def mismatched_strings(cls, id_, id_string, original_string):
|
|
|
|
return cls(
|
|
|
|
"Error fetching a Lexeme from the Vocab. When looking up a string, "
|
|
|
|
"the lexeme returned had an orth ID that did not match the query string. "
|
|
|
|
"This means that the cached lexeme structs are mismatched to the "
|
|
|
|
"string encoding table. The mismatched:\n"
|
|
|
|
"Query string: {query}\n"
|
|
|
|
"Orth cached: {orth_str}\n"
|
|
|
|
"ID of orth: {orth_id}".format(
|
2015-11-05 16:48:08 +03:00
|
|
|
query=repr(original_string), orth_str=repr(id_string), orth_id=id_)
|
2015-10-06 02:34:59 +03:00
|
|
|
)
|