2019-08-22 15:21:32 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
import srsly
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from .errors import Errors
|
|
|
|
from .util import SimpleFrozenDict, ensure_path
|
2019-08-22 15:21:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Lookups(object):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Container for large lookup tables and dictionaries, e.g. lemmatization
|
|
|
|
data or tokenizer exception lists. Lookups are available via vocab.lookups,
|
|
|
|
so they can be accessed before the pipeline components are applied (e.g.
|
|
|
|
in the tokenizer and lemmatizer), as well as within the pipeline components
|
|
|
|
via doc.vocab.lookups.
|
|
|
|
|
|
|
|
Important note: At the moment, this class only performs a very basic
|
|
|
|
dictionary lookup. We're planning to replace this with a more efficient
|
|
|
|
implementation. See #3971 for details.
|
|
|
|
"""
|
|
|
|
|
2019-08-22 15:21:32 +03:00
|
|
|
def __init__(self):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Initialize the Lookups object.
|
|
|
|
|
|
|
|
RETURNS (Lookups): The newly created object.
|
|
|
|
"""
|
|
|
|
self._tables = OrderedDict()
|
2019-08-22 15:21:32 +03:00
|
|
|
|
|
|
|
def __contains__(self, name):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Check if the lookups contain a table of a given name. Delegates to
|
|
|
|
Lookups.has_table.
|
|
|
|
|
|
|
|
name (unicode): Name of the table.
|
2019-09-12 15:00:14 +03:00
|
|
|
RETURNS (bool): Whether a table of that name is in the lookups.
|
2019-09-09 20:17:55 +03:00
|
|
|
"""
|
2019-08-22 15:21:32 +03:00
|
|
|
return self.has_table(name)
|
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
def __len__(self):
|
|
|
|
"""RETURNS (int): The number of tables in the lookups."""
|
|
|
|
return len(self._tables)
|
|
|
|
|
2019-08-22 15:21:32 +03:00
|
|
|
@property
|
|
|
|
def tables(self):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""RETURNS (list): Names of all tables in the lookups."""
|
2019-08-22 15:21:32 +03:00
|
|
|
return list(self._tables.keys())
|
|
|
|
|
|
|
|
def add_table(self, name, data=SimpleFrozenDict()):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Add a new table to the lookups. Raises an error if the table exists.
|
|
|
|
|
|
|
|
name (unicode): Unique name of table.
|
|
|
|
data (dict): Optional data to add to the table.
|
|
|
|
RETURNS (Table): The newly added table.
|
|
|
|
"""
|
2019-08-22 15:21:32 +03:00
|
|
|
if name in self.tables:
|
2019-09-09 20:17:55 +03:00
|
|
|
raise ValueError(Errors.E158.format(name=name))
|
2019-08-22 15:21:32 +03:00
|
|
|
table = Table(name=name)
|
|
|
|
table.update(data)
|
|
|
|
self._tables[name] = table
|
|
|
|
return table
|
|
|
|
|
|
|
|
def get_table(self, name):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Get a table. Raises an error if the table doesn't exist.
|
|
|
|
|
|
|
|
name (unicode): Name of the table.
|
|
|
|
RETURNS (Table): The table.
|
|
|
|
"""
|
2019-08-22 15:21:32 +03:00
|
|
|
if name not in self._tables:
|
2019-09-09 20:17:55 +03:00
|
|
|
raise KeyError(Errors.E159.format(name=name, tables=self.tables))
|
2019-08-22 15:21:32 +03:00
|
|
|
return self._tables[name]
|
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
def remove_table(self, name):
|
|
|
|
"""Remove a table. Raises an error if the table doesn't exist.
|
|
|
|
|
2019-09-12 15:00:14 +03:00
|
|
|
name (unicode): Name of the table to remove.
|
2019-09-09 20:17:55 +03:00
|
|
|
RETURNS (Table): The removed table.
|
|
|
|
"""
|
|
|
|
if name not in self._tables:
|
|
|
|
raise KeyError(Errors.E159.format(name=name, tables=self.tables))
|
|
|
|
return self._tables.pop(name)
|
|
|
|
|
2019-08-22 15:21:32 +03:00
|
|
|
def has_table(self, name):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Check if the lookups contain a table of a given name.
|
|
|
|
|
|
|
|
name (unicode): Name of the table.
|
|
|
|
RETURNS (bool): Whether a table of that name exists.
|
|
|
|
"""
|
2019-08-22 15:21:32 +03:00
|
|
|
return name in self._tables
|
|
|
|
|
2019-09-12 15:00:14 +03:00
|
|
|
def to_bytes(self, **kwargs):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Serialize the lookups to a bytestring.
|
|
|
|
|
|
|
|
RETURNS (bytes): The serialized Lookups.
|
|
|
|
"""
|
|
|
|
return srsly.msgpack_dumps(self._tables)
|
2019-08-22 15:21:32 +03:00
|
|
|
|
2019-09-12 15:00:14 +03:00
|
|
|
def from_bytes(self, bytes_data, **kwargs):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Load the lookups from a bytestring.
|
|
|
|
|
2019-09-12 15:00:14 +03:00
|
|
|
bytes_data (bytes): The data to load.
|
|
|
|
RETURNS (Lookups): The loaded Lookups.
|
2019-09-09 20:17:55 +03:00
|
|
|
"""
|
|
|
|
self._tables = OrderedDict()
|
|
|
|
msg = srsly.msgpack_loads(bytes_data)
|
|
|
|
for key, value in msg.items():
|
|
|
|
self._tables[key] = Table.from_dict(value)
|
|
|
|
return self
|
2019-08-22 15:21:32 +03:00
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
def to_disk(self, path, **kwargs):
|
2019-09-12 15:00:14 +03:00
|
|
|
"""Save the lookups to a directory as lookups.bin. Expects a path to a
|
|
|
|
directory, which will be created if it doesn't exist.
|
2019-08-22 15:21:32 +03:00
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
path (unicode / Path): The file path.
|
|
|
|
"""
|
|
|
|
if len(self._tables):
|
|
|
|
path = ensure_path(path)
|
2019-09-12 15:00:01 +03:00
|
|
|
if not path.exists():
|
|
|
|
path.mkdir()
|
2019-09-09 20:17:55 +03:00
|
|
|
filepath = path / "lookups.bin"
|
|
|
|
with filepath.open("wb") as file_:
|
|
|
|
file_.write(self.to_bytes())
|
2019-08-22 15:21:32 +03:00
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
def from_disk(self, path, **kwargs):
|
2019-09-12 15:00:14 +03:00
|
|
|
"""Load lookups from a directory containing a lookups.bin. Will skip
|
|
|
|
loading if the file doesn't exist.
|
2019-09-09 20:17:55 +03:00
|
|
|
|
2019-09-12 15:00:14 +03:00
|
|
|
path (unicode / Path): The directory path.
|
2019-09-09 20:17:55 +03:00
|
|
|
RETURNS (Lookups): The loaded lookups.
|
|
|
|
"""
|
|
|
|
path = ensure_path(path)
|
|
|
|
filepath = path / "lookups.bin"
|
|
|
|
if filepath.exists():
|
|
|
|
with filepath.open("rb") as file_:
|
|
|
|
data = file_.read()
|
|
|
|
return self.from_bytes(data)
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class Table(OrderedDict):
|
2019-09-12 15:00:14 +03:00
|
|
|
"""A table in the lookups. Subclass of OrderedDict that implements a
|
2019-09-09 20:17:55 +03:00
|
|
|
slightly more consistent and unified API.
|
|
|
|
"""
|
2019-09-11 15:00:36 +03:00
|
|
|
|
2019-09-09 20:17:55 +03:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, data, name=None):
|
2019-09-12 15:00:14 +03:00
|
|
|
"""Initialize a new table from a dict.
|
|
|
|
|
|
|
|
data (dict): The dictionary.
|
|
|
|
name (unicode): Optional table name for reference.
|
|
|
|
RETURNS (Table): The newly created object.
|
|
|
|
"""
|
2019-09-09 20:17:55 +03:00
|
|
|
self = cls(name=name)
|
|
|
|
self.update(data)
|
|
|
|
return self
|
2019-08-22 15:21:32 +03:00
|
|
|
|
|
|
|
def __init__(self, name=None):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Initialize a new table.
|
|
|
|
|
|
|
|
name (unicode): Optional table name for reference.
|
|
|
|
RETURNS (Table): The newly created object.
|
|
|
|
"""
|
|
|
|
OrderedDict.__init__(self)
|
2019-08-22 15:21:32 +03:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def set(self, key, value):
|
2019-09-09 20:17:55 +03:00
|
|
|
"""Set new key/value pair. Same as table[key] = value."""
|
2019-08-22 15:21:32 +03:00
|
|
|
self[key] = value
|