2015-11-06 19:24:30 +03:00
|
|
|
from __future__ import absolute_import
|
2016-09-24 21:26:17 +03:00
|
|
|
from __future__ import unicode_literals
|
2015-09-09 15:31:09 +03:00
|
|
|
from warnings import warn
|
2016-09-24 15:08:53 +03:00
|
|
|
import pathlib
|
2016-10-09 13:24:24 +03:00
|
|
|
from contextlib import contextmanager
|
|
|
|
import shutil
|
2015-08-26 20:16:09 +03:00
|
|
|
|
2015-08-27 10:16:11 +03:00
|
|
|
try:
|
|
|
|
import ujson as json
|
|
|
|
except ImportError:
|
|
|
|
import json
|
|
|
|
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-09-24 23:16:43 +03:00
|
|
|
try:
|
|
|
|
basestring
|
|
|
|
except NameError:
|
|
|
|
basestring = str
|
|
|
|
|
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
from .tokenizer import Tokenizer
|
|
|
|
from .vocab import Vocab
|
|
|
|
from .syntax.parser import Parser
|
|
|
|
from .tagger import Tagger
|
|
|
|
from .matcher import Matcher
|
|
|
|
from . import attrs
|
|
|
|
from . import orth
|
|
|
|
from .syntax.ner import BiluoPushDown
|
|
|
|
from .syntax.arc_eager import ArcEager
|
2016-09-24 21:26:17 +03:00
|
|
|
from . import util
|
2016-09-25 16:37:33 +03:00
|
|
|
from .lemmatizer import Lemmatizer
|
2016-10-09 13:24:24 +03:00
|
|
|
from .train import Trainer
|
2015-08-26 20:16:09 +03:00
|
|
|
|
2016-09-24 21:26:17 +03:00
|
|
|
from .attrs import TAG, DEP, ENT_IOB, ENT_TYPE, HEAD, PROB, LANG, IS_STOP
|
2016-09-26 12:57:54 +03:00
|
|
|
from .syntax.parser import get_templates
|
2016-10-09 13:24:24 +03:00
|
|
|
from .syntax.nonproj import PseudoProjectivity
|
|
|
|
|
2015-08-27 10:16:11 +03:00
|
|
|
|
2016-09-24 21:26:17 +03:00
|
|
|
class BaseDefaults(object):
|
2016-09-24 15:08:53 +03:00
|
|
|
def __init__(self, lang, path):
|
|
|
|
self.path = path
|
2016-09-24 21:26:17 +03:00
|
|
|
self.lang = lang
|
2016-09-24 16:42:01 +03:00
|
|
|
self.lex_attr_getters = dict(self.__class__.lex_attr_getters)
|
2016-09-26 12:07:46 +03:00
|
|
|
if self.path and (self.path / 'vocab' / 'oov_prob').exists():
|
2016-09-24 16:42:01 +03:00
|
|
|
with (self.path / 'vocab' / 'oov_prob').open() as file_:
|
|
|
|
oov_prob = file_.read().strip()
|
2016-09-24 21:26:17 +03:00
|
|
|
self.lex_attr_getters[PROB] = lambda string: oov_prob
|
|
|
|
self.lex_attr_getters[LANG] = lambda string: lang
|
|
|
|
self.lex_attr_getters[IS_STOP] = lambda string: string in self.stop_words
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-09-25 16:37:33 +03:00
|
|
|
def Lemmatizer(self):
|
2016-09-26 12:07:46 +03:00
|
|
|
return Lemmatizer.load(self.path) if self.path else Lemmatizer({}, {}, {})
|
2016-09-25 16:37:33 +03:00
|
|
|
|
2016-09-24 15:08:53 +03:00
|
|
|
def Vectors(self):
|
2016-09-24 21:26:17 +03:00
|
|
|
return True
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-09-25 16:37:33 +03:00
|
|
|
def Vocab(self, lex_attr_getters=True, tag_map=True,
|
|
|
|
lemmatizer=True, serializer_freqs=True, vectors=True):
|
|
|
|
if lex_attr_getters is True:
|
|
|
|
lex_attr_getters = self.lex_attr_getters
|
|
|
|
if tag_map is True:
|
|
|
|
tag_map = self.tag_map
|
|
|
|
if lemmatizer is True:
|
|
|
|
lemmatizer = self.Lemmatizer()
|
|
|
|
if vectors is True:
|
|
|
|
vectors = self.Vectors()
|
2016-09-26 12:07:46 +03:00
|
|
|
if self.path:
|
|
|
|
return Vocab.load(self.path, lex_attr_getters=lex_attr_getters,
|
|
|
|
tag_map=tag_map, lemmatizer=lemmatizer,
|
|
|
|
serializer_freqs=serializer_freqs)
|
|
|
|
else:
|
|
|
|
return Vocab(lex_attr_getters=lex_attr_getters, tag_map=tag_map,
|
|
|
|
lemmatizer=lemmatizer, serializer_freqs=serializer_freqs)
|
2016-09-25 16:37:33 +03:00
|
|
|
|
|
|
|
def Tokenizer(self, vocab, rules=None, prefix_search=None, suffix_search=None,
|
|
|
|
infix_finditer=None):
|
|
|
|
if rules is None:
|
|
|
|
rules = self.tokenizer_exceptions
|
|
|
|
if prefix_search is None:
|
|
|
|
prefix_search = util.compile_prefix_regex(self.prefixes).search
|
|
|
|
if suffix_search is None:
|
|
|
|
suffix_search = util.compile_suffix_regex(self.suffixes).search
|
|
|
|
if infix_finditer is None:
|
|
|
|
infix_finditer = util.compile_infix_regex(self.infixes).finditer
|
2016-09-26 12:07:46 +03:00
|
|
|
if self.path:
|
|
|
|
return Tokenizer.load(self.path, vocab, rules=rules,
|
|
|
|
prefix_search=prefix_search,
|
|
|
|
suffix_search=suffix_search,
|
|
|
|
infix_finditer=infix_finditer)
|
|
|
|
else:
|
2016-10-09 13:24:24 +03:00
|
|
|
tokenizer = Tokenizer(vocab, rules=rules,
|
2016-09-26 12:07:46 +03:00
|
|
|
prefix_search=prefix_search, suffix_search=suffix_search,
|
|
|
|
infix_finditer=infix_finditer)
|
2016-10-09 13:24:24 +03:00
|
|
|
return tokenizer
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
def Tagger(self, vocab, **cfg):
|
2016-09-26 12:07:46 +03:00
|
|
|
if self.path:
|
|
|
|
return Tagger.load(self.path / 'pos', vocab)
|
|
|
|
else:
|
2016-09-27 14:21:28 +03:00
|
|
|
return Tagger.blank(vocab, Tagger.default_templates())
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
def Parser(self, vocab, **cfg):
|
2016-10-12 21:26:38 +03:00
|
|
|
if self.path and (self.path / 'deps').exists():
|
|
|
|
return Parser.load(self.path / 'deps', vocab, ArcEager)
|
2016-09-24 15:08:53 +03:00
|
|
|
else:
|
2016-10-09 13:24:24 +03:00
|
|
|
if 'features' not in cfg:
|
|
|
|
cfg['features'] = self.parser_features
|
|
|
|
if 'labels' not in cfg:
|
|
|
|
cfg['labels'] = self.parser_labels
|
|
|
|
return Parser.blank(vocab, ArcEager, **cfg)
|
|
|
|
|
|
|
|
def Entity(self, vocab, **cfg):
|
|
|
|
if self.path and (self.path / 'ner').exists():
|
2016-09-30 20:56:06 +03:00
|
|
|
return Parser.load(self.path / 'ner', vocab, BiluoPushDown)
|
|
|
|
else:
|
2016-10-09 13:24:24 +03:00
|
|
|
if 'features' not in cfg:
|
|
|
|
cfg['features'] = self.entity_features
|
|
|
|
if 'labels' not in cfg:
|
|
|
|
cfg['labels'] = self.entity_labels
|
|
|
|
return Parser.blank(vocab, BiluoPushDown, **cfg)
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
def Matcher(self, vocab, **cfg):
|
2016-09-26 12:07:46 +03:00
|
|
|
if self.path:
|
|
|
|
return Matcher.load(self.path, vocab)
|
|
|
|
else:
|
|
|
|
return Matcher(vocab)
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-10-14 18:38:29 +03:00
|
|
|
def MakeDoc(self, nlp, **cfg):
|
|
|
|
return nlp.tokenizer.__call__
|
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
def Pipeline(self, nlp, **cfg):
|
2016-10-14 18:38:29 +03:00
|
|
|
pipeline = []
|
2016-10-09 13:24:24 +03:00
|
|
|
if nlp.tagger:
|
|
|
|
pipeline.append(nlp.tagger)
|
|
|
|
if nlp.parser:
|
|
|
|
pipeline.append(nlp.parser)
|
|
|
|
if nlp.entity:
|
|
|
|
pipeline.append(nlp.entity)
|
|
|
|
return pipeline
|
|
|
|
|
|
|
|
prefixes = tuple()
|
|
|
|
|
|
|
|
suffixes = tuple()
|
2016-09-24 15:08:53 +03:00
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
infixes = tuple()
|
|
|
|
|
|
|
|
tag_map = {}
|
|
|
|
|
|
|
|
tokenizer_exceptions = {}
|
|
|
|
|
2016-10-12 20:12:40 +03:00
|
|
|
parser_labels = {0: {'': True}, 1: {'': True}, 2: {'ROOT': True, 'nmod': True},
|
|
|
|
3: {'ROOT': True, 'nmod': True}, 4: {'ROOT': True}}
|
|
|
|
|
2016-09-24 16:42:01 +03:00
|
|
|
|
2016-10-12 20:12:40 +03:00
|
|
|
entity_labels = {
|
|
|
|
0: {'': True},
|
|
|
|
1: {'PER': True, 'LOC': True, 'ORG': True, 'MISC': True},
|
|
|
|
2: {'PER': True, 'LOC': True, 'ORG': True, 'MISC': True},
|
|
|
|
3: {'PER': True, 'LOC': True, 'ORG': True, 'MISC': True},
|
|
|
|
4: {'PER': True, 'LOC': True, 'ORG': True, 'MISC': True},
|
|
|
|
5: {'': True}
|
|
|
|
}
|
2016-09-26 12:57:54 +03:00
|
|
|
|
|
|
|
parser_features = get_templates('parser')
|
|
|
|
|
|
|
|
entity_features = get_templates('ner')
|
2016-09-24 16:42:01 +03:00
|
|
|
|
2016-09-24 21:26:17 +03:00
|
|
|
stop_words = set()
|
|
|
|
|
2016-09-24 16:42:01 +03:00
|
|
|
lex_attr_getters = {
|
|
|
|
attrs.LOWER: lambda string: string.lower(),
|
|
|
|
attrs.NORM: lambda string: string,
|
|
|
|
attrs.SHAPE: orth.word_shape,
|
|
|
|
attrs.PREFIX: lambda string: string[0],
|
|
|
|
attrs.SUFFIX: lambda string: string[-3:],
|
|
|
|
attrs.CLUSTER: lambda string: 0,
|
|
|
|
attrs.IS_ALPHA: orth.is_alpha,
|
|
|
|
attrs.IS_ASCII: orth.is_ascii,
|
|
|
|
attrs.IS_DIGIT: lambda string: string.isdigit(),
|
|
|
|
attrs.IS_LOWER: orth.is_lower,
|
|
|
|
attrs.IS_PUNCT: orth.is_punct,
|
|
|
|
attrs.IS_SPACE: lambda string: string.isspace(),
|
|
|
|
attrs.IS_TITLE: orth.is_title,
|
|
|
|
attrs.IS_UPPER: orth.is_upper,
|
|
|
|
attrs.IS_BRACKET: orth.is_bracket,
|
|
|
|
attrs.IS_QUOTE: orth.is_quote,
|
|
|
|
attrs.IS_LEFT_PUNCT: orth.is_left_punct,
|
|
|
|
attrs.IS_RIGHT_PUNCT: orth.is_right_punct,
|
|
|
|
attrs.LIKE_URL: orth.like_url,
|
|
|
|
attrs.LIKE_NUM: orth.like_number,
|
|
|
|
attrs.LIKE_EMAIL: orth.like_email,
|
|
|
|
attrs.IS_STOP: lambda string: False,
|
|
|
|
attrs.IS_OOV: lambda string: True
|
|
|
|
}
|
2015-09-14 10:48:51 +03:00
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
|
2016-09-24 15:08:53 +03:00
|
|
|
class Language(object):
|
|
|
|
'''A text-processing pipeline. Usually you'll load this once per process, and
|
|
|
|
pass the instance around your program.
|
|
|
|
'''
|
2016-09-24 21:26:17 +03:00
|
|
|
Defaults = BaseDefaults
|
2016-09-24 15:08:53 +03:00
|
|
|
lang = None
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
@classmethod
|
|
|
|
@contextmanager
|
|
|
|
def train(cls, path, gold_tuples, *configs):
|
|
|
|
if isinstance(path, basestring):
|
|
|
|
path = pathlib.Path(path)
|
|
|
|
tagger_cfg, parser_cfg, entity_cfg = configs
|
2016-10-12 21:26:38 +03:00
|
|
|
dep_model_dir = path / 'deps'
|
2016-10-09 13:24:24 +03:00
|
|
|
ner_model_dir = path / 'ner'
|
|
|
|
pos_model_dir = path / 'pos'
|
|
|
|
if dep_model_dir.exists():
|
|
|
|
shutil.rmtree(str(dep_model_dir))
|
|
|
|
if ner_model_dir.exists():
|
|
|
|
shutil.rmtree(str(ner_model_dir))
|
|
|
|
if pos_model_dir.exists():
|
|
|
|
shutil.rmtree(str(pos_model_dir))
|
|
|
|
dep_model_dir.mkdir()
|
|
|
|
ner_model_dir.mkdir()
|
|
|
|
pos_model_dir.mkdir()
|
|
|
|
|
|
|
|
if parser_cfg['pseudoprojective']:
|
|
|
|
# preprocess training data here before ArcEager.get_labels() is called
|
|
|
|
gold_tuples = PseudoProjectivity.preprocess_training_data(gold_tuples)
|
|
|
|
|
|
|
|
parser_cfg['labels'] = ArcEager.get_labels(gold_tuples)
|
|
|
|
entity_cfg['labels'] = BiluoPushDown.get_labels(gold_tuples)
|
|
|
|
|
|
|
|
with (dep_model_dir / 'config.json').open('wb') as file_:
|
|
|
|
json.dump(parser_cfg, file_)
|
|
|
|
with (ner_model_dir / 'config.json').open('wb') as file_:
|
|
|
|
json.dump(entity_cfg, file_)
|
|
|
|
with (pos_model_dir / 'config.json').open('wb') as file_:
|
|
|
|
json.dump(tagger_cfg, file_)
|
|
|
|
|
2016-10-12 14:45:58 +03:00
|
|
|
self = cls(
|
|
|
|
path=path,
|
|
|
|
vocab=False,
|
|
|
|
tokenizer=False,
|
|
|
|
tagger=False,
|
|
|
|
parser=False,
|
|
|
|
entity=False,
|
|
|
|
matcher=False,
|
|
|
|
serializer=False,
|
|
|
|
vectors=False,
|
|
|
|
pipeline=False)
|
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
self.defaults.parser_labels = parser_cfg['labels']
|
|
|
|
self.defaults.entity_labels = entity_cfg['labels']
|
2016-10-12 14:45:58 +03:00
|
|
|
|
|
|
|
self.vocab = self.defaults.Vocab()
|
2016-10-09 13:24:24 +03:00
|
|
|
self.tokenizer = self.defaults.Tokenizer(self.vocab)
|
|
|
|
self.tagger = self.defaults.Tagger(self.vocab, **tagger_cfg)
|
|
|
|
self.parser = self.defaults.Parser(self.vocab, **parser_cfg)
|
|
|
|
self.entity = self.defaults.Entity(self.vocab, **entity_cfg)
|
|
|
|
self.pipeline = self.defaults.Pipeline(self)
|
|
|
|
yield Trainer(self, gold_tuples)
|
|
|
|
self.end_training()
|
|
|
|
|
2015-12-28 17:56:27 +03:00
|
|
|
def __init__(self,
|
2016-09-24 15:08:53 +03:00
|
|
|
path=None,
|
|
|
|
vocab=True,
|
|
|
|
tokenizer=True,
|
|
|
|
tagger=True,
|
|
|
|
parser=True,
|
|
|
|
entity=True,
|
|
|
|
matcher=True,
|
|
|
|
serializer=True,
|
|
|
|
vectors=True,
|
2016-10-14 18:38:29 +03:00
|
|
|
make_doc=True,
|
2016-09-24 15:08:53 +03:00
|
|
|
pipeline=True,
|
|
|
|
defaults=True,
|
|
|
|
data_dir=None):
|
2015-12-18 11:52:55 +03:00
|
|
|
"""
|
2016-04-14 11:36:57 +03:00
|
|
|
A model can be specified:
|
2015-12-18 11:52:55 +03:00
|
|
|
|
2016-04-14 11:36:57 +03:00
|
|
|
1) by calling a Language subclass
|
|
|
|
- spacy.en.English()
|
2016-01-15 20:01:02 +03:00
|
|
|
|
2016-04-14 11:36:57 +03:00
|
|
|
2) by calling a Language subclass with data_dir
|
|
|
|
- spacy.en.English('my/model/root')
|
|
|
|
- spacy.en.English(data_dir='my/model/root')
|
2016-01-15 20:01:02 +03:00
|
|
|
|
2016-04-14 11:36:57 +03:00
|
|
|
3) by package name
|
|
|
|
- spacy.load('en_default')
|
|
|
|
- spacy.load('en_default==1.0.0')
|
2015-12-18 11:52:55 +03:00
|
|
|
|
2016-04-14 11:36:57 +03:00
|
|
|
4) by package name with a relocated package base
|
|
|
|
- spacy.load('en_default', via='/my/package/root')
|
|
|
|
- spacy.load('en_default==1.0.0', via='/my/package/root')
|
2015-12-18 11:52:55 +03:00
|
|
|
"""
|
2016-09-24 15:08:53 +03:00
|
|
|
if data_dir is not None and path is None:
|
|
|
|
warn("'data_dir' argument now named 'path'. Doing what you mean.")
|
|
|
|
path = data_dir
|
|
|
|
if isinstance(path, basestring):
|
|
|
|
path = pathlib.Path(path)
|
2016-09-24 21:26:17 +03:00
|
|
|
if path is None:
|
|
|
|
path = util.match_best_version(self.lang, '', util.get_data_path())
|
2016-09-24 16:42:01 +03:00
|
|
|
self.path = path
|
2016-09-24 15:08:53 +03:00
|
|
|
defaults = defaults if defaults is not True else self.get_defaults(self.path)
|
|
|
|
|
2016-10-09 13:24:24 +03:00
|
|
|
self.defaults = defaults
|
2016-09-24 15:08:53 +03:00
|
|
|
self.vocab = vocab if vocab is not True else defaults.Vocab(vectors=vectors)
|
|
|
|
self.tokenizer = tokenizer if tokenizer is not True else defaults.Tokenizer(self.vocab)
|
|
|
|
self.tagger = tagger if tagger is not True else defaults.Tagger(self.vocab)
|
|
|
|
self.entity = entity if entity is not True else defaults.Entity(self.vocab)
|
|
|
|
self.parser = parser if parser is not True else defaults.Parser(self.vocab)
|
|
|
|
self.matcher = matcher if matcher is not True else defaults.Matcher(self.vocab)
|
2016-10-14 18:38:29 +03:00
|
|
|
|
|
|
|
if make_doc in (None, True, False):
|
|
|
|
self.make_doc = defaults.MakeDoc(self)
|
|
|
|
else:
|
|
|
|
self.make_doc = make_doc
|
2016-10-09 13:24:24 +03:00
|
|
|
if pipeline in (None, False):
|
|
|
|
self.pipeline = []
|
|
|
|
elif pipeline is True:
|
|
|
|
self.pipeline = defaults.Pipeline(self)
|
|
|
|
else:
|
|
|
|
self.pipeline = pipeline(self)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2015-10-12 11:33:11 +03:00
|
|
|
def __reduce__(self):
|
2015-12-28 17:56:27 +03:00
|
|
|
args = (
|
2016-09-24 15:08:53 +03:00
|
|
|
self.path,
|
2015-12-28 17:56:27 +03:00
|
|
|
self.vocab,
|
|
|
|
self.tokenizer,
|
|
|
|
self.tagger,
|
|
|
|
self.parser,
|
|
|
|
self.entity,
|
2015-12-28 18:54:03 +03:00
|
|
|
self.matcher
|
2015-12-28 17:56:27 +03:00
|
|
|
)
|
|
|
|
return (self.__class__, args, None, None)
|
2015-10-12 11:33:11 +03:00
|
|
|
|
2015-09-15 07:41:48 +03:00
|
|
|
def __call__(self, text, tag=True, parse=True, entity=True):
|
2015-08-25 16:37:17 +03:00
|
|
|
"""Apply the pipeline to some text. The text can span multiple sentences,
|
|
|
|
and can contain arbtrary whitespace. Alignment into the original string
|
|
|
|
is preserved.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text (unicode): The text to be processed.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
tokens (spacy.tokens.Doc):
|
|
|
|
|
|
|
|
>>> from spacy.en import English
|
|
|
|
>>> nlp = English()
|
|
|
|
>>> tokens = nlp('An example sentence. Another example sentence.')
|
|
|
|
>>> tokens[0].orth_, tokens[0].head.tag_
|
|
|
|
('An', 'NN')
|
|
|
|
"""
|
2016-10-14 18:38:29 +03:00
|
|
|
doc = self.make_doc(text)
|
2015-08-25 16:37:17 +03:00
|
|
|
if self.entity and entity:
|
2016-01-19 22:09:26 +03:00
|
|
|
# Add any of the entity labels already set, in case we don't have them.
|
2016-05-17 17:55:42 +03: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 18:38:29 +03:00
|
|
|
for proc in self.pipeline:
|
2016-05-17 17:55:42 +03:00
|
|
|
if proc and not skip.get(proc):
|
|
|
|
proc(doc)
|
|
|
|
return doc
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2016-10-14 18:38:29 +03:00
|
|
|
def pipe(self, texts, tag=True, parse=True, entity=True, n_threads=2, batch_size=1000):
|
2016-05-17 17:55:42 +03:00
|
|
|
skip = {self.tagger: not tag, self.parser: not parse, self.entity: not entity}
|
2016-10-14 18:38:29 +03:00
|
|
|
stream = (self.make_doc(text) for text in texts)
|
|
|
|
for proc in self.pipeline:
|
2016-05-17 17:55:42 +03: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 04:04:55 +03:00
|
|
|
for doc in stream:
|
|
|
|
yield doc
|
2016-02-01 11:01:13 +03:00
|
|
|
|
2016-09-24 15:08:53 +03:00
|
|
|
def end_training(self, path=None):
|
|
|
|
if path is None:
|
|
|
|
path = self.path
|
2016-10-09 13:24:24 +03:00
|
|
|
elif isinstance(path, basestring):
|
|
|
|
path = pathlib.Path(path)
|
|
|
|
|
|
|
|
if self.tagger:
|
|
|
|
self.tagger.model.end_training()
|
|
|
|
self.tagger.model.dump(str(path / 'pos' / 'model'))
|
2016-02-03 00:49:55 +03:00
|
|
|
if self.parser:
|
|
|
|
self.parser.model.end_training()
|
2016-10-12 21:26:38 +03:00
|
|
|
self.parser.model.dump(str(path / 'deps' / 'model'))
|
2016-02-03 00:49:55 +03:00
|
|
|
if self.entity:
|
|
|
|
self.entity.model.end_training()
|
2016-10-09 13:24:24 +03:00
|
|
|
self.entity.model.dump(str(path / 'ner' / 'model'))
|
|
|
|
|
2016-09-24 15:08:53 +03:00
|
|
|
strings_loc = path / 'vocab' / 'strings.json'
|
|
|
|
with strings_loc.open('w', encoding='utf8') as file_:
|
2015-10-22 13:13:03 +03:00
|
|
|
self.vocab.strings.dump(file_)
|
2016-09-24 15:08:53 +03:00
|
|
|
self.vocab.dump(path / 'vocab' / 'lexemes.bin')
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2016-02-03 00:49:55 +03:00
|
|
|
if self.tagger:
|
|
|
|
tagger_freqs = list(self.tagger.freqs[TAG].items())
|
|
|
|
else:
|
|
|
|
tagger_freqs = []
|
|
|
|
if self.parser:
|
|
|
|
dep_freqs = list(self.parser.moves.freqs[DEP].items())
|
|
|
|
head_freqs = list(self.parser.moves.freqs[HEAD].items())
|
|
|
|
else:
|
|
|
|
dep_freqs = []
|
|
|
|
head_freqs = []
|
|
|
|
if self.entity:
|
|
|
|
entity_iob_freqs = list(self.entity.moves.freqs[ENT_IOB].items())
|
|
|
|
entity_type_freqs = list(self.entity.moves.freqs[ENT_TYPE].items())
|
|
|
|
else:
|
|
|
|
entity_iob_freqs = []
|
|
|
|
entity_type_freqs = []
|
2016-10-09 13:24:24 +03:00
|
|
|
with (path / 'vocab' / 'serializer.json').open('wb') as file_:
|
2015-08-25 16:37:17 +03:00
|
|
|
file_.write(
|
|
|
|
json.dumps([
|
2016-02-03 00:49:55 +03:00
|
|
|
(TAG, tagger_freqs),
|
|
|
|
(DEP, dep_freqs),
|
|
|
|
(ENT_IOB, entity_iob_freqs),
|
|
|
|
(ENT_TYPE, entity_type_freqs),
|
|
|
|
(HEAD, head_freqs)
|
|
|
|
]))
|
2016-09-24 15:08:53 +03:00
|
|
|
|
|
|
|
def get_defaults(self, path):
|
2016-09-24 21:26:17 +03:00
|
|
|
return self.Defaults(self.lang, path)
|