2017-04-15 10:05:47 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2016-10-09 10:24:24 +00:00
|
|
|
from contextlib import contextmanager
|
|
|
|
import shutil
|
2015-08-27 07:16:11 +00:00
|
|
|
|
2015-08-26 17:16:09 +00:00
|
|
|
from .tokenizer import Tokenizer
|
|
|
|
from .vocab import Vocab
|
|
|
|
from .tagger import Tagger
|
|
|
|
from .matcher import Matcher
|
2016-09-25 13:37:33 +00:00
|
|
|
from .lemmatizer import Lemmatizer
|
2016-10-09 10:24:24 +00:00
|
|
|
from .train import Trainer
|
2016-09-26 09:57:54 +00:00
|
|
|
from .syntax.parser import get_templates
|
2016-10-09 10:24:24 +00:00
|
|
|
from .syntax.nonproj import PseudoProjectivity
|
2016-10-16 15:58:12 +00:00
|
|
|
from .pipeline import DependencyParser, EntityRecognizer
|
2016-11-25 15:01:20 +00:00
|
|
|
from .syntax.arc_eager import ArcEager
|
|
|
|
from .syntax.ner import BiluoPushDown
|
2017-04-16 23:46:14 +00:00
|
|
|
from .compat import json_dumps
|
2017-04-15 10:05:47 +00:00
|
|
|
from .attrs import IS_STOP
|
2017-05-08 21:58:31 +00:00
|
|
|
from .lang.punctuation import TOKENIZER_PREFIXES, TOKENIZER_SUFFIXES, TOKENIZER_INFIXES
|
|
|
|
from .lang.tokenizer_exceptions import TOKEN_MATCH
|
|
|
|
from .lang.tag_map import TAG_MAP
|
2017-05-08 22:58:10 +00:00
|
|
|
from .lang.lex_attrs import LEX_ATTRS
|
2017-04-15 10:05:47 +00:00
|
|
|
from . import util
|
2016-10-09 10:24:24 +00:00
|
|
|
|
2015-08-27 07:16:11 +00:00
|
|
|
|
2016-09-24 18:26:17 +00:00
|
|
|
class BaseDefaults(object):
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
|
|
|
def create_lemmatizer(cls, nlp=None):
|
2017-03-15 09:52:50 +00:00
|
|
|
return Lemmatizer(cls.lemma_index, cls.lemma_exc, cls.lemma_rules)
|
2016-10-18 14:18:25 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_vocab(cls, nlp=None):
|
|
|
|
lemmatizer = cls.create_lemmatizer(nlp)
|
|
|
|
if nlp is None or nlp.path is None:
|
2016-11-23 23:13:55 +00:00
|
|
|
lex_attr_getters = dict(cls.lex_attr_getters)
|
|
|
|
# This is very messy, but it's the minimal working fix to Issue #639.
|
|
|
|
# This defaults stuff needs to be refactored (again)
|
|
|
|
lex_attr_getters[IS_STOP] = lambda string: string.lower() in cls.stop_words
|
2017-03-15 14:24:40 +00:00
|
|
|
vocab = Vocab(lex_attr_getters=lex_attr_getters, tag_map=cls.tag_map,
|
2016-10-18 14:18:25 +00:00
|
|
|
lemmatizer=lemmatizer)
|
2016-09-26 09:07:46 +00:00
|
|
|
else:
|
2017-03-15 14:24:40 +00:00
|
|
|
vocab = Vocab.load(nlp.path, lex_attr_getters=cls.lex_attr_getters,
|
2016-10-18 14:18:25 +00:00
|
|
|
tag_map=cls.tag_map, lemmatizer=lemmatizer)
|
2017-03-15 14:24:40 +00:00
|
|
|
for tag_str, exc in cls.morph_rules.items():
|
|
|
|
for orth_str, attrs in exc.items():
|
|
|
|
vocab.morphology.add_special_case(tag_str, orth_str, attrs)
|
|
|
|
return vocab
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
|
|
|
def add_vectors(cls, nlp=None):
|
2016-10-20 16:27:48 +00:00
|
|
|
if nlp is None or nlp.path is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
vec_path = nlp.path / 'vocab' / 'vec.bin'
|
2016-11-23 12:50:24 +00:00
|
|
|
if vec_path.exists():
|
|
|
|
return lambda vocab: vocab.load_vectors_from_bin_loc(vec_path)
|
2016-10-18 14:18:25 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_tokenizer(cls, nlp=None):
|
|
|
|
rules = cls.tokenizer_exceptions
|
2017-01-03 17:17:57 +00:00
|
|
|
if cls.token_match:
|
|
|
|
token_match = cls.token_match
|
2016-11-02 19:35:29 +00:00
|
|
|
if cls.prefixes:
|
|
|
|
prefix_search = util.compile_prefix_regex(cls.prefixes).search
|
|
|
|
else:
|
|
|
|
prefix_search = None
|
|
|
|
if cls.suffixes:
|
|
|
|
suffix_search = util.compile_suffix_regex(cls.suffixes).search
|
|
|
|
else:
|
|
|
|
suffix_search = None
|
|
|
|
if cls.infixes:
|
|
|
|
infix_finditer = util.compile_infix_regex(cls.infixes).finditer
|
|
|
|
else:
|
|
|
|
infix_finditer = None
|
2016-10-18 14:18:25 +00:00
|
|
|
vocab = nlp.vocab if nlp is not None else cls.create_vocab(nlp)
|
2016-11-26 11:36:04 +00:00
|
|
|
return Tokenizer(vocab, rules=rules,
|
2016-10-18 14:18:25 +00:00
|
|
|
prefix_search=prefix_search, suffix_search=suffix_search,
|
2017-01-03 17:17:57 +00:00
|
|
|
infix_finditer=infix_finditer, token_match=token_match)
|
2016-09-24 12:08:53 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
|
|
|
def create_tagger(cls, nlp=None):
|
|
|
|
if nlp is None:
|
|
|
|
return Tagger(cls.create_vocab(), features=cls.tagger_features)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is False:
|
2016-10-18 14:18:25 +00:00
|
|
|
return Tagger(nlp.vocab, features=cls.tagger_features)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is None or not (nlp.path / 'pos').exists():
|
2016-10-19 17:55:19 +00:00
|
|
|
return None
|
2016-09-24 12:08:53 +00:00
|
|
|
else:
|
2016-10-18 17:33:04 +00:00
|
|
|
return Tagger.load(nlp.path / 'pos', nlp.vocab)
|
2016-10-09 10:24:24 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
2016-11-25 15:01:20 +00:00
|
|
|
def create_parser(cls, nlp=None, **cfg):
|
2016-10-18 14:18:25 +00:00
|
|
|
if nlp is None:
|
2016-11-25 15:01:20 +00:00
|
|
|
return DependencyParser(cls.create_vocab(), features=cls.parser_features,
|
|
|
|
**cfg)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is False:
|
2016-11-25 15:01:20 +00:00
|
|
|
return DependencyParser(nlp.vocab, features=cls.parser_features, **cfg)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is None or not (nlp.path / 'deps').exists():
|
2016-10-19 17:55:19 +00:00
|
|
|
return None
|
2016-09-30 17:56:06 +00:00
|
|
|
else:
|
2016-11-25 15:01:20 +00:00
|
|
|
return DependencyParser.load(nlp.path / 'deps', nlp.vocab, **cfg)
|
2016-09-24 12:08:53 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
2016-11-25 15:01:20 +00:00
|
|
|
def create_entity(cls, nlp=None, **cfg):
|
2016-10-18 14:18:25 +00:00
|
|
|
if nlp is None:
|
2016-11-25 15:01:20 +00:00
|
|
|
return EntityRecognizer(cls.create_vocab(), features=cls.entity_features, **cfg)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is False:
|
2016-11-25 15:01:20 +00:00
|
|
|
return EntityRecognizer(nlp.vocab, features=cls.entity_features, **cfg)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is None or not (nlp.path / 'ner').exists():
|
2016-10-19 17:55:19 +00:00
|
|
|
return None
|
2016-09-26 09:07:46 +00:00
|
|
|
else:
|
2016-11-25 15:01:20 +00:00
|
|
|
return EntityRecognizer.load(nlp.path / 'ner', nlp.vocab, **cfg)
|
2016-09-24 12:08:53 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
|
|
|
def create_matcher(cls, nlp=None):
|
|
|
|
if nlp is None:
|
|
|
|
return Matcher(cls.create_vocab())
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is False:
|
2016-10-18 14:18:25 +00:00
|
|
|
return Matcher(nlp.vocab)
|
2016-10-23 18:19:01 +00:00
|
|
|
elif nlp.path is None or not (nlp.path / 'vocab').exists():
|
2016-10-19 17:55:19 +00:00
|
|
|
return None
|
2016-10-18 14:18:25 +00:00
|
|
|
else:
|
|
|
|
return Matcher.load(nlp.path / 'vocab', nlp.vocab)
|
2016-10-14 15:38:29 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
@classmethod
|
|
|
|
def create_pipeline(self, nlp=None):
|
2016-10-14 15:38:29 +00:00
|
|
|
pipeline = []
|
2016-10-18 14:18:25 +00:00
|
|
|
if nlp is None:
|
|
|
|
return []
|
2016-10-09 10:24:24 +00:00
|
|
|
if nlp.tagger:
|
|
|
|
pipeline.append(nlp.tagger)
|
|
|
|
if nlp.parser:
|
|
|
|
pipeline.append(nlp.parser)
|
2017-04-06 15:33:15 +00:00
|
|
|
pipeline.append(PseudoProjectivity.deprojectivize)
|
2016-10-09 10:24:24 +00:00
|
|
|
if nlp.entity:
|
|
|
|
pipeline.append(nlp.entity)
|
|
|
|
return pipeline
|
|
|
|
|
2017-05-08 21:58:31 +00:00
|
|
|
token_match = TOKEN_MATCH
|
|
|
|
prefixes = tuple(TOKENIZER_PREFIXES)
|
|
|
|
suffixes = tuple(TOKENIZER_SUFFIXES)
|
|
|
|
infixes = tuple(TOKENIZER_INFIXES)
|
|
|
|
tag_map = dict(TAG_MAP)
|
2016-10-09 10:24:24 +00:00
|
|
|
tokenizer_exceptions = {}
|
2016-09-26 09:57:54 +00:00
|
|
|
parser_features = get_templates('parser')
|
|
|
|
entity_features = get_templates('ner')
|
2016-10-18 14:18:25 +00:00
|
|
|
tagger_features = Tagger.feature_templates # TODO -- fix this
|
2016-09-24 18:26:17 +00:00
|
|
|
stop_words = set()
|
2016-12-18 14:50:09 +00:00
|
|
|
lemma_rules = {}
|
2017-03-15 09:52:50 +00:00
|
|
|
lemma_exc = {}
|
|
|
|
lemma_index = {}
|
2017-03-15 14:24:40 +00:00
|
|
|
morph_rules = {}
|
2017-05-08 22:58:10 +00:00
|
|
|
lex_attr_getters = LEX_ATTRS
|
2015-09-14 07:48:51 +00:00
|
|
|
|
2015-08-26 17:16:09 +00:00
|
|
|
|
2016-09-24 12:08:53 +00:00
|
|
|
class Language(object):
|
2017-04-15 09:59:21 +00:00
|
|
|
"""
|
|
|
|
A text-processing pipeline. Usually you'll load this once per process, and
|
2016-09-24 12:08:53 +00:00
|
|
|
pass the instance around your program.
|
2017-04-15 09:59:21 +00:00
|
|
|
"""
|
2016-09-24 18:26:17 +00:00
|
|
|
Defaults = BaseDefaults
|
2016-09-24 12:08:53 +00:00
|
|
|
lang = None
|
2015-08-25 13:37:17 +00:00
|
|
|
|
2016-10-09 10:24:24 +00:00
|
|
|
@classmethod
|
2017-04-14 21:51:24 +00:00
|
|
|
def setup_directory(cls, path, **configs):
|
2017-04-16 23:40:26 +00:00
|
|
|
"""
|
|
|
|
Initialise a model directory.
|
|
|
|
"""
|
2017-04-14 21:51:24 +00:00
|
|
|
for name, config in configs.items():
|
|
|
|
directory = path / name
|
|
|
|
if directory.exists():
|
|
|
|
shutil.rmtree(str(directory))
|
|
|
|
directory.mkdir()
|
2017-04-23 15:05:53 +00:00
|
|
|
with (directory / 'config.json').open('w') as file_:
|
2017-04-16 23:46:14 +00:00
|
|
|
data = json_dumps(config)
|
2017-04-14 21:51:24 +00:00
|
|
|
file_.write(data)
|
|
|
|
if not (path / 'vocab').exists():
|
|
|
|
(path / 'vocab').mkdir()
|
2016-10-09 10:24:24 +00:00
|
|
|
|
2017-04-14 21:51:24 +00:00
|
|
|
@classmethod
|
|
|
|
@contextmanager
|
|
|
|
def train(cls, path, gold_tuples, **configs):
|
2017-04-16 18:00:37 +00:00
|
|
|
parser_cfg = configs.get('deps', {})
|
|
|
|
if parser_cfg.get('pseudoprojective'):
|
2016-10-09 10:24:24 +00:00
|
|
|
# preprocess training data here before ArcEager.get_labels() is called
|
|
|
|
gold_tuples = PseudoProjectivity.preprocess_training_data(gold_tuples)
|
|
|
|
|
2017-04-14 21:51:24 +00:00
|
|
|
for subdir in ('deps', 'ner', 'pos'):
|
|
|
|
if subdir not in configs:
|
|
|
|
configs[subdir] = {}
|
2017-04-16 18:00:37 +00:00
|
|
|
if parser_cfg:
|
|
|
|
configs['deps']['actions'] = ArcEager.get_actions(gold_parses=gold_tuples)
|
|
|
|
if 'ner' in configs:
|
|
|
|
configs['ner']['actions'] = BiluoPushDown.get_actions(gold_parses=gold_tuples)
|
2017-04-14 21:51:24 +00:00
|
|
|
|
|
|
|
cls.setup_directory(path, **configs)
|
2016-10-09 10:24:24 +00:00
|
|
|
|
2016-10-12 11:45:58 +00:00
|
|
|
self = cls(
|
|
|
|
path=path,
|
|
|
|
vocab=False,
|
|
|
|
tokenizer=False,
|
|
|
|
tagger=False,
|
|
|
|
parser=False,
|
|
|
|
entity=False,
|
|
|
|
matcher=False,
|
|
|
|
vectors=False,
|
|
|
|
pipeline=False)
|
|
|
|
|
2016-11-25 15:01:20 +00:00
|
|
|
self.vocab = self.Defaults.create_vocab(self)
|
|
|
|
self.tokenizer = self.Defaults.create_tokenizer(self)
|
|
|
|
self.tagger = self.Defaults.create_tagger(self)
|
|
|
|
self.parser = self.Defaults.create_parser(self)
|
|
|
|
self.entity = self.Defaults.create_entity(self)
|
|
|
|
self.pipeline = self.Defaults.create_pipeline(self)
|
2016-10-09 10:24:24 +00:00
|
|
|
yield Trainer(self, gold_tuples)
|
2017-04-14 21:51:24 +00:00
|
|
|
self.end_training()
|
2017-04-16 20:28:27 +00:00
|
|
|
self.save_to_directory(path)
|
2016-10-09 10:24:24 +00:00
|
|
|
|
2016-11-23 12:26:34 +00:00
|
|
|
def __init__(self, **overrides):
|
2017-04-16 23:52:13 +00:00
|
|
|
"""
|
|
|
|
Create or load the pipeline.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
**overrides: Keyword arguments indicating which defaults to override.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Language: The newly constructed object.
|
|
|
|
"""
|
2016-11-23 15:12:45 +00:00
|
|
|
if 'data_dir' in overrides and 'path' not in overrides:
|
2016-10-18 14:18:25 +00:00
|
|
|
raise ValueError("The argument 'data_dir' has been renamed to 'path'")
|
2017-04-15 10:11:16 +00:00
|
|
|
path = util.ensure_path(overrides.get('path', True))
|
2016-10-15 12:12:54 +00:00
|
|
|
if path is True:
|
2017-03-18 11:58:45 +00:00
|
|
|
path = util.get_data_path() / self.lang
|
2017-03-26 14:46:00 +00:00
|
|
|
if not path.exists() and 'path' not in overrides:
|
|
|
|
path = None
|
2017-03-16 16:14:56 +00:00
|
|
|
self.meta = overrides.get('meta', {})
|
2016-09-24 13:42:01 +00:00
|
|
|
self.path = path
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2016-10-18 14:18:25 +00:00
|
|
|
self.vocab = self.Defaults.create_vocab(self) \
|
|
|
|
if 'vocab' not in overrides \
|
|
|
|
else overrides['vocab']
|
2016-10-20 16:27:48 +00:00
|
|
|
add_vectors = self.Defaults.add_vectors(self) \
|
|
|
|
if 'add_vectors' not in overrides \
|
|
|
|
else overrides['add_vectors']
|
2016-11-25 15:01:20 +00:00
|
|
|
if self.vocab and add_vectors:
|
2016-10-20 16:27:48 +00:00
|
|
|
add_vectors(self.vocab)
|
2016-10-18 14:18:25 +00:00
|
|
|
self.tokenizer = self.Defaults.create_tokenizer(self) \
|
|
|
|
if 'tokenizer' not in overrides \
|
|
|
|
else overrides['tokenizer']
|
|
|
|
self.tagger = self.Defaults.create_tagger(self) \
|
|
|
|
if 'tagger' not in overrides \
|
|
|
|
else overrides['tagger']
|
2016-10-18 17:36:44 +00:00
|
|
|
self.parser = self.Defaults.create_parser(self) \
|
2016-10-18 14:18:25 +00:00
|
|
|
if 'parser' not in overrides \
|
|
|
|
else overrides['parser']
|
|
|
|
self.entity = self.Defaults.create_entity(self) \
|
|
|
|
if 'entity' not in overrides \
|
|
|
|
else overrides['entity']
|
|
|
|
self.matcher = self.Defaults.create_matcher(self) \
|
|
|
|
if 'matcher' not in overrides \
|
|
|
|
else overrides['matcher']
|
|
|
|
|
|
|
|
if 'make_doc' in overrides:
|
|
|
|
self.make_doc = overrides['make_doc']
|
|
|
|
elif 'create_make_doc' in overrides:
|
2016-10-23 12:24:16 +00:00
|
|
|
self.make_doc = overrides['create_make_doc'](self)
|
2016-11-02 18:57:13 +00:00
|
|
|
elif not hasattr(self, 'make_doc'):
|
2016-10-18 14:18:25 +00:00
|
|
|
self.make_doc = lambda text: self.tokenizer(text)
|
|
|
|
if 'pipeline' in overrides:
|
|
|
|
self.pipeline = overrides['pipeline']
|
|
|
|
elif 'create_pipeline' in overrides:
|
2016-10-23 12:24:16 +00:00
|
|
|
self.pipeline = overrides['create_pipeline'](self)
|
2016-10-09 10:24:24 +00:00
|
|
|
else:
|
2016-10-18 14:18:25 +00:00
|
|
|
self.pipeline = [self.tagger, self.parser, self.matcher, self.entity]
|
2015-10-12 08:33:11 +00:00
|
|
|
|
2015-09-15 04:41:48 +00:00
|
|
|
def __call__(self, text, tag=True, parse=True, entity=True):
|
2017-04-15 10:05:47 +00:00
|
|
|
"""
|
|
|
|
Apply the pipeline to some text. The text can span multiple sentences,
|
2015-08-25 13:37:17 +00:00
|
|
|
and can contain arbtrary whitespace. Alignment into the original string
|
|
|
|
is preserved.
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2017-04-16 23:40:26 +00:00
|
|
|
Argsuments:
|
2015-08-25 13:37:17 +00:00
|
|
|
text (unicode): The text to be processed.
|
|
|
|
|
|
|
|
Returns:
|
2016-11-01 11:25:36 +00:00
|
|
|
doc (Doc): A container for accessing the annotations.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> from spacy.en import English
|
|
|
|
>>> nlp = English()
|
|
|
|
>>> tokens = nlp('An example sentence. Another example sentence.')
|
|
|
|
>>> tokens[0].orth_, tokens[0].head.tag_
|
|
|
|
('An', 'NN')
|
2015-08-25 13:37:17 +00:00
|
|
|
"""
|
2016-10-14 15:38:29 +00:00
|
|
|
doc = self.make_doc(text)
|
2015-08-25 13:37:17 +00:00
|
|
|
if self.entity and entity:
|
2016-01-19 19:09:26 +00:00
|
|
|
# Add any of the entity labels already set, in case we don't have them.
|
2016-05-17 14:55:42 +00:00
|
|
|
for token in doc:
|
|
|
|
if token.ent_type != 0:
|
|
|
|
self.entity.add_label(token.ent_type)
|
|
|
|
skip = {self.tagger: not tag, self.parser: not parse, self.entity: not entity}
|
2016-10-14 15:38:29 +00:00
|
|
|
for proc in self.pipeline:
|
2016-05-17 14:55:42 +00:00
|
|
|
if proc and not skip.get(proc):
|
|
|
|
proc(doc)
|
|
|
|
return doc
|
2015-08-25 13:37:17 +00:00
|
|
|
|
2016-10-14 15:38:29 +00:00
|
|
|
def pipe(self, texts, tag=True, parse=True, entity=True, n_threads=2, batch_size=1000):
|
2017-04-15 09:59:21 +00:00
|
|
|
"""
|
|
|
|
Process texts as a stream, and yield Doc objects in order.
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2016-11-01 11:25:36 +00:00
|
|
|
Supports GIL-free multi-threading.
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2016-11-01 11:25:36 +00:00
|
|
|
Arguments:
|
|
|
|
texts (iterator)
|
|
|
|
tag (bool)
|
|
|
|
parse (bool)
|
|
|
|
entity (bool)
|
2017-04-15 09:59:21 +00:00
|
|
|
"""
|
2016-05-17 14:55:42 +00:00
|
|
|
skip = {self.tagger: not tag, self.parser: not parse, self.entity: not entity}
|
2016-10-14 15:38:29 +00:00
|
|
|
stream = (self.make_doc(text) for text in texts)
|
|
|
|
for proc in self.pipeline:
|
2016-05-17 14:55:42 +00:00
|
|
|
if proc and not skip.get(proc):
|
|
|
|
if hasattr(proc, 'pipe'):
|
|
|
|
stream = proc.pipe(stream, n_threads=n_threads, batch_size=batch_size)
|
|
|
|
else:
|
|
|
|
stream = (proc(item) for item in stream)
|
2016-02-03 01:04:55 +00:00
|
|
|
for doc in stream:
|
|
|
|
yield doc
|
2016-02-01 08:01:13 +00:00
|
|
|
|
2017-04-14 21:51:24 +00:00
|
|
|
def save_to_directory(self, path):
|
2017-04-16 23:40:26 +00:00
|
|
|
"""
|
|
|
|
Save the Vocab, StringStore and pipeline to a directory.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
path (string or pathlib path): Path to save the model.
|
|
|
|
"""
|
2017-04-14 21:51:24 +00:00
|
|
|
configs = {
|
|
|
|
'pos': self.tagger.cfg if self.tagger else {},
|
|
|
|
'deps': self.parser.cfg if self.parser else {},
|
|
|
|
'ner': self.entity.cfg if self.entity else {},
|
|
|
|
}
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2017-04-16 23:40:14 +00:00
|
|
|
path = util.ensure_path(path)
|
2017-04-23 19:24:43 +00:00
|
|
|
if not path.exists():
|
|
|
|
path.mkdir()
|
2017-04-14 21:51:24 +00:00
|
|
|
self.setup_directory(path, **configs)
|
2017-04-15 10:05:47 +00:00
|
|
|
|
2017-04-14 21:51:24 +00:00
|
|
|
strings_loc = path / 'vocab' / 'strings.json'
|
|
|
|
with strings_loc.open('w', encoding='utf8') as file_:
|
|
|
|
self.vocab.strings.dump(file_)
|
|
|
|
self.vocab.dump(path / 'vocab' / 'lexemes.bin')
|
|
|
|
# TODO: Word vectors?
|
2016-10-09 10:24:24 +00:00
|
|
|
if self.tagger:
|
|
|
|
self.tagger.model.dump(str(path / 'pos' / 'model'))
|
2016-02-02 21:49:55 +00:00
|
|
|
if self.parser:
|
2016-10-12 18:26:38 +00:00
|
|
|
self.parser.model.dump(str(path / 'deps' / 'model'))
|
2016-02-02 21:49:55 +00:00
|
|
|
if self.entity:
|
2016-10-09 10:24:24 +00:00
|
|
|
self.entity.model.dump(str(path / 'ner' / 'model'))
|
2016-12-18 15:54:52 +00:00
|
|
|
|
2017-04-14 21:51:24 +00:00
|
|
|
def end_training(self, path=None):
|
2016-02-02 21:49:55 +00:00
|
|
|
if self.tagger:
|
2017-04-14 21:51:24 +00:00
|
|
|
self.tagger.model.end_training()
|
2016-02-02 21:49:55 +00:00
|
|
|
if self.parser:
|
2017-04-14 21:51:24 +00:00
|
|
|
self.parser.model.end_training()
|
2016-02-02 21:49:55 +00:00
|
|
|
if self.entity:
|
2017-04-14 21:51:24 +00:00
|
|
|
self.entity.model.end_training()
|
|
|
|
# NB: This is slightly different from before --- we no longer default
|
|
|
|
# to taking nlp.path
|
|
|
|
if path is not None:
|
|
|
|
self.save_to_directory(path)
|