2015-11-06 19:24:30 +03:00
|
|
|
from __future__ import absolute_import
|
2015-08-26 20:16:09 +03:00
|
|
|
from os import path
|
2015-09-09 15:31:09 +03:00
|
|
|
from warnings import warn
|
2015-10-22 19:48:12 +03:00
|
|
|
import io
|
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
|
|
|
|
|
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 .serialize.packer import Packer
|
|
|
|
from . import attrs
|
|
|
|
from . import orth
|
|
|
|
from .syntax.ner import BiluoPushDown
|
|
|
|
from .syntax.arc_eager import ArcEager
|
|
|
|
|
2016-01-15 20:01:02 +03:00
|
|
|
from . import util
|
2016-04-12 17:00:56 +03:00
|
|
|
from . import about
|
2015-08-27 10:16:11 +03:00
|
|
|
from .attrs import TAG, DEP, ENT_IOB, ENT_TYPE, HEAD
|
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
|
2015-08-25 16:37:17 +03:00
|
|
|
class Language(object):
|
2015-12-28 18:54:03 +03:00
|
|
|
lang = None
|
|
|
|
|
2015-08-25 16:37:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def lower(string):
|
|
|
|
return string.lower()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def norm(string):
|
|
|
|
return string
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def prefix(string):
|
|
|
|
return string[0]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def suffix(string):
|
|
|
|
return string[-3:]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cluster(string):
|
|
|
|
return 0
|
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
@staticmethod
|
|
|
|
def is_digit(string):
|
|
|
|
return string.isdigit()
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
@staticmethod
|
|
|
|
def is_space(string):
|
|
|
|
return string.isspace()
|
|
|
|
|
2015-09-14 10:48:51 +03:00
|
|
|
@staticmethod
|
|
|
|
def is_stop(string):
|
|
|
|
return 0
|
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
@classmethod
|
2016-02-06 17:13:55 +03:00
|
|
|
def default_lex_attrs(cls, *args, **kwargs):
|
|
|
|
oov_prob = kwargs.get('oov_prob', -20)
|
2015-08-25 16:37:17 +03:00
|
|
|
return {
|
|
|
|
attrs.LOWER: cls.lower,
|
|
|
|
attrs.NORM: cls.norm,
|
2016-03-09 20:07:37 +03:00
|
|
|
attrs.SHAPE: orth.word_shape,
|
2015-08-25 16:37:17 +03:00
|
|
|
attrs.PREFIX: cls.prefix,
|
|
|
|
attrs.SUFFIX: cls.suffix,
|
|
|
|
attrs.CLUSTER: cls.cluster,
|
2016-02-06 17:13:55 +03:00
|
|
|
attrs.PROB: lambda string: oov_prob,
|
2016-03-10 15:01:34 +03:00
|
|
|
attrs.LANG: lambda string: cls.lang,
|
2016-03-09 20:07:37 +03:00
|
|
|
attrs.IS_ALPHA: orth.is_alpha,
|
|
|
|
attrs.IS_ASCII: orth.is_ascii,
|
2015-08-25 16:37:17 +03:00
|
|
|
attrs.IS_DIGIT: cls.is_digit,
|
2016-03-09 20:07:37 +03:00
|
|
|
attrs.IS_LOWER: orth.is_lower,
|
|
|
|
attrs.IS_PUNCT: orth.is_punct,
|
2015-08-26 20:16:09 +03:00
|
|
|
attrs.IS_SPACE: cls.is_space,
|
2016-03-09 20:07:37 +03:00
|
|
|
attrs.IS_TITLE: orth.is_title,
|
|
|
|
attrs.IS_UPPER: orth.is_upper,
|
2016-03-10 15:01:34 +03:00
|
|
|
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,
|
2016-03-09 20:07:37 +03:00
|
|
|
attrs.LIKE_URL: orth.like_url,
|
|
|
|
attrs.LIKE_NUM: orth.like_number,
|
|
|
|
attrs.LIKE_EMAIL: orth.like_email,
|
2015-09-14 10:48:51 +03:00
|
|
|
attrs.IS_STOP: cls.is_stop,
|
2015-08-25 16:37:17 +03:00
|
|
|
attrs.IS_OOV: lambda string: True
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
@classmethod
|
|
|
|
def default_dep_labels(cls):
|
|
|
|
return {0: {'ROOT': True}}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default_ner_labels(cls):
|
|
|
|
return {0: {'PER': True, 'LOC': True, 'ORG': True, 'MISC': True}}
|
|
|
|
|
2015-08-25 16:37:17 +03:00
|
|
|
@classmethod
|
2016-03-04 13:09:06 +03:00
|
|
|
def default_vocab(cls, package, get_lex_attr=None, vectors_package=None):
|
2015-08-25 16:37:17 +03:00
|
|
|
if get_lex_attr is None:
|
2016-02-06 17:13:55 +03:00
|
|
|
if package.has_file('vocab', 'oov_prob'):
|
|
|
|
with package.open(('vocab', 'oov_prob')) as file_:
|
|
|
|
oov_prob = float(file_.read().strip())
|
|
|
|
get_lex_attr = cls.default_lex_attrs(oov_prob=oov_prob)
|
|
|
|
else:
|
|
|
|
get_lex_attr = cls.default_lex_attrs()
|
2016-01-19 00:37:24 +03:00
|
|
|
if hasattr(package, 'dir_path'):
|
2016-03-04 13:09:06 +03:00
|
|
|
return Vocab.from_package(package, get_lex_attr=get_lex_attr,
|
|
|
|
vectors_package=vectors_package)
|
2016-01-19 00:37:24 +03:00
|
|
|
else:
|
|
|
|
return Vocab.load(package, get_lex_attr)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@classmethod
|
2015-12-07 08:01:28 +03:00
|
|
|
def default_parser(cls, package, vocab):
|
2016-01-19 00:37:24 +03:00
|
|
|
if hasattr(package, 'dir_path'):
|
|
|
|
data_dir = package.dir_path('deps')
|
|
|
|
else:
|
|
|
|
data_dir = package
|
2015-12-07 08:50:26 +03:00
|
|
|
if data_dir and path.exists(data_dir):
|
|
|
|
return Parser.from_dir(data_dir, vocab.strings, ArcEager)
|
2016-01-19 00:37:24 +03:00
|
|
|
else:
|
|
|
|
return None
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@classmethod
|
2015-12-07 08:01:28 +03:00
|
|
|
def default_entity(cls, package, vocab):
|
2016-01-19 00:37:24 +03:00
|
|
|
if hasattr(package, 'dir_path'):
|
|
|
|
data_dir = package.dir_path('ner')
|
|
|
|
else:
|
|
|
|
data_dir = package
|
2015-12-07 08:50:26 +03:00
|
|
|
if data_dir and path.exists(data_dir):
|
|
|
|
return Parser.from_dir(data_dir, vocab.strings, BiluoPushDown)
|
2016-01-19 00:37:24 +03:00
|
|
|
else:
|
|
|
|
return None
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2015-12-28 17:56:27 +03:00
|
|
|
def __init__(self,
|
|
|
|
data_dir=None,
|
|
|
|
vocab=None,
|
|
|
|
tokenizer=None,
|
|
|
|
tagger=None,
|
|
|
|
parser=None,
|
|
|
|
entity=None,
|
|
|
|
matcher=None,
|
|
|
|
serializer=None,
|
2016-01-16 12:00:57 +03:00
|
|
|
load_vectors=True,
|
2016-03-04 13:09:06 +03:00
|
|
|
package=None,
|
|
|
|
vectors_package=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-01-16 12:00:57 +03:00
|
|
|
if package is None:
|
2016-01-16 14:23:45 +03:00
|
|
|
if data_dir is None:
|
2016-04-12 17:00:56 +03:00
|
|
|
package = util.get_package_by_name(about.__models__[self.lang])
|
2016-01-16 12:00:57 +03:00
|
|
|
else:
|
2016-01-16 14:23:45 +03:00
|
|
|
package = util.get_package(data_dir)
|
2016-01-15 20:01:02 +03:00
|
|
|
|
2015-09-09 15:31:09 +03:00
|
|
|
if load_vectors is not True:
|
|
|
|
warn("load_vectors is deprecated", DeprecationWarning)
|
2016-01-16 12:00:57 +03:00
|
|
|
|
2015-09-29 13:54:12 +03:00
|
|
|
if vocab in (None, True):
|
2016-03-04 13:09:06 +03:00
|
|
|
vocab = self.default_vocab(package, vectors_package=vectors_package)
|
2015-12-28 18:54:03 +03:00
|
|
|
self.vocab = vocab
|
2015-09-29 13:54:12 +03:00
|
|
|
if tokenizer in (None, True):
|
2016-01-16 12:00:57 +03:00
|
|
|
tokenizer = Tokenizer.from_package(package, self.vocab)
|
2015-12-28 18:54:03 +03:00
|
|
|
self.tokenizer = tokenizer
|
2015-09-29 13:54:12 +03:00
|
|
|
if tagger in (None, True):
|
2016-01-16 12:00:57 +03:00
|
|
|
tagger = Tagger.from_package(package, self.vocab)
|
2015-12-28 18:54:03 +03:00
|
|
|
self.tagger = tagger
|
2015-09-29 13:54:12 +03:00
|
|
|
if entity in (None, True):
|
2015-12-28 18:54:03 +03:00
|
|
|
entity = self.default_entity(package, self.vocab)
|
|
|
|
self.entity = entity
|
2015-09-29 13:54:12 +03:00
|
|
|
if parser in (None, True):
|
2015-12-28 18:54:03 +03:00
|
|
|
parser = self.default_parser(package, self.vocab)
|
|
|
|
self.parser = parser
|
2015-09-29 13:54:12 +03:00
|
|
|
if matcher in (None, True):
|
2016-01-16 12:00:57 +03:00
|
|
|
matcher = Matcher.from_package(package, self.vocab)
|
2015-12-28 18:54:03 +03:00
|
|
|
self.matcher = matcher
|
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 = (
|
|
|
|
None, # data_dir
|
|
|
|
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')
|
|
|
|
"""
|
|
|
|
tokens = self.tokenizer(text)
|
|
|
|
if self.tagger and tag:
|
|
|
|
self.tagger(tokens)
|
|
|
|
if self.matcher and entity:
|
|
|
|
self.matcher(tokens)
|
|
|
|
if self.parser and parse:
|
|
|
|
self.parser(tokens)
|
|
|
|
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.
|
|
|
|
for tok in tokens:
|
|
|
|
if tok.ent_type != 0:
|
|
|
|
self.entity.add_label(tok.ent_type)
|
2015-08-25 16:37:17 +03:00
|
|
|
self.entity(tokens)
|
|
|
|
return tokens
|
|
|
|
|
2016-02-03 04:04:55 +03:00
|
|
|
def pipe(self, texts, tag=True, parse=True, entity=True, n_threads=2,
|
|
|
|
batch_size=1000):
|
2016-02-05 14:20:29 +03:00
|
|
|
stream = self.tokenizer.pipe(texts,
|
2016-02-03 04:04:55 +03:00
|
|
|
n_threads=n_threads, batch_size=batch_size)
|
|
|
|
if self.tagger and tag:
|
2016-02-05 14:20:29 +03:00
|
|
|
stream = self.tagger.pipe(stream,
|
2016-02-03 04:04:55 +03:00
|
|
|
n_threads=n_threads, batch_size=batch_size)
|
|
|
|
if self.matcher and entity:
|
2016-02-05 14:20:29 +03:00
|
|
|
stream = self.matcher.pipe(stream,
|
2016-02-03 04:04:55 +03:00
|
|
|
n_threads=n_threads, batch_size=batch_size)
|
2016-02-01 11:01:13 +03:00
|
|
|
if self.parser and parse:
|
2016-02-05 14:20:29 +03:00
|
|
|
stream = self.parser.pipe(stream,
|
2016-02-03 04:04:55 +03:00
|
|
|
n_threads=n_threads, batch_size=batch_size)
|
|
|
|
if self.entity and entity:
|
2016-02-05 14:20:29 +03:00
|
|
|
stream = self.entity.pipe(stream,
|
2016-02-05 14:47:57 +03:00
|
|
|
n_threads=1, batch_size=batch_size)
|
2016-02-03 04:04:55 +03:00
|
|
|
for doc in stream:
|
|
|
|
yield doc
|
2016-02-01 11:01:13 +03:00
|
|
|
|
2015-08-25 16:37:17 +03:00
|
|
|
def end_training(self, data_dir=None):
|
|
|
|
if data_dir is None:
|
|
|
|
data_dir = self.data_dir
|
2016-02-03 00:49:55 +03:00
|
|
|
if self.parser:
|
|
|
|
self.parser.model.end_training()
|
|
|
|
self.parser.model.dump(path.join(data_dir, 'deps', 'model'))
|
|
|
|
if self.entity:
|
|
|
|
self.entity.model.end_training()
|
|
|
|
self.entity.model.dump(path.join(data_dir, 'ner', 'model'))
|
|
|
|
if self.tagger:
|
|
|
|
self.tagger.model.end_training()
|
|
|
|
self.tagger.model.dump(path.join(data_dir, 'pos', 'model'))
|
2015-10-22 13:13:03 +03:00
|
|
|
|
2015-10-22 19:48:12 +03:00
|
|
|
strings_loc = path.join(data_dir, 'vocab', 'strings.json')
|
|
|
|
with io.open(strings_loc, 'w', encoding='utf8') as file_:
|
2015-10-22 13:13:03 +03:00
|
|
|
self.vocab.strings.dump(file_)
|
2016-02-03 00:49:55 +03:00
|
|
|
self.vocab.dump(path.join(data_dir, '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 = []
|
2015-08-25 16:37:17 +03:00
|
|
|
with open(path.join(data_dir, 'vocab', 'serializer.json'), 'w') as file_:
|
|
|
|
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)
|
|
|
|
]))
|