Fix vectors for reserved words. Closes #2871

This commit is contained in:
Matthew Honnibal 2018-12-10 16:09:49 +01:00
parent 16fd8dce1d
commit 90aec6d2f6

View File

@ -10,7 +10,8 @@ cimport numpy as np
from thinc.neural.util import get_array_module from thinc.neural.util import get_array_module
from thinc.neural._classes.model import Model from thinc.neural._classes.model import Model
from .strings cimport StringStore, hash_string from .strings cimport StringStore
from .strings import get_string_id
from .compat import basestring_, path2str from .compat import basestring_, path2str
from .errors import Errors from .errors import Errors
from . import util from . import util
@ -218,12 +219,10 @@ cdef class Vectors:
raise ValueError(Errors.E059.format(kwargs=bad_kwargs)) raise ValueError(Errors.E059.format(kwargs=bad_kwargs))
xp = get_array_module(self.data) xp = get_array_module(self.data)
if key is not None: if key is not None:
if isinstance(key, basestring_): key = get_string_id(key)
key = hash_string(key)
return self.key2row.get(key, -1) return self.key2row.get(key, -1)
elif keys is not None: elif keys is not None:
keys = [hash_string(key) if isinstance(key, basestring_) else key keys = [get_string_id(key) for key in keys]
for key in keys]
rows = [self.key2row.get(key, -1.) for key in keys] rows = [self.key2row.get(key, -1.) for key in keys]
return xp.asarray(rows, dtype='i') return xp.asarray(rows, dtype='i')
else: else:
@ -248,8 +247,7 @@ cdef class Vectors:
row (int / None): The row number of a vector to map the key to. row (int / None): The row number of a vector to map the key to.
RETURNS (int): The row the vector was added to. RETURNS (int): The row the vector was added to.
""" """
if isinstance(key, basestring): key = get_string_id(key)
key = hash_string(key)
if row is None and key in self.key2row: if row is None and key in self.key2row:
row = self.key2row[key] row = self.key2row[key]
elif row is None: elif row is None: