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
|
|
|
|
|
2015-08-27 10:16:11 +03:00
|
|
|
from .attrs import TAG, DEP, ENT_IOB, ENT_TYPE, HEAD
|
2015-12-18 11:52:55 +03:00
|
|
|
from .util import get_package
|
2015-08-27 10:16:11 +03:00
|
|
|
|
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 shape(string):
|
|
|
|
return orth.word_shape(string)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def prefix(string):
|
|
|
|
return string[0]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def suffix(string):
|
|
|
|
return string[-3:]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def prob(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return -30
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cluster(string):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_alpha(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return orth.is_alpha(string)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_ascii(string):
|
|
|
|
return orth.is_ascii(string)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_digit(string):
|
|
|
|
return string.isdigit()
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_lower(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return orth.is_lower(string)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_punct(string):
|
|
|
|
return orth.is_punct(string)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_space(string):
|
|
|
|
return string.isspace()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_title(string):
|
|
|
|
return orth.is_title(string)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_upper(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return orth.is_upper(string)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def like_url(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return orth.like_url(string)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2015-11-04 14:01:44 +03:00
|
|
|
def like_num(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return orth.like_number(string)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def like_email(string):
|
2015-08-26 20:16:09 +03:00
|
|
|
return orth.like_email(string)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2015-09-14 10:48:51 +03:00
|
|
|
@staticmethod
|
|
|
|
def is_stop(string):
|
|
|
|
return 0
|
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
@classmethod
|
2015-12-07 08:01:28 +03:00
|
|
|
def default_lex_attrs(cls):
|
2015-08-25 16:37:17 +03:00
|
|
|
return {
|
|
|
|
attrs.LOWER: cls.lower,
|
|
|
|
attrs.NORM: cls.norm,
|
|
|
|
attrs.SHAPE: cls.shape,
|
|
|
|
attrs.PREFIX: cls.prefix,
|
|
|
|
attrs.SUFFIX: cls.suffix,
|
|
|
|
attrs.CLUSTER: cls.cluster,
|
2015-08-26 20:16:09 +03:00
|
|
|
attrs.PROB: lambda string: -10.0,
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
attrs.IS_ALPHA: cls.is_alpha,
|
|
|
|
attrs.IS_ASCII: cls.is_ascii,
|
|
|
|
attrs.IS_DIGIT: cls.is_digit,
|
|
|
|
attrs.IS_LOWER: cls.is_lower,
|
2015-08-26 20:16:09 +03:00
|
|
|
attrs.IS_PUNCT: cls.is_punct,
|
|
|
|
attrs.IS_SPACE: cls.is_space,
|
|
|
|
attrs.IS_TITLE: cls.is_title,
|
2015-08-25 16:37:17 +03:00
|
|
|
attrs.IS_UPPER: cls.is_upper,
|
|
|
|
attrs.LIKE_URL: cls.like_url,
|
2015-11-04 14:02:47 +03:00
|
|
|
attrs.LIKE_NUM: cls.like_num,
|
2015-08-25 16:37:17 +03:00
|
|
|
attrs.LIKE_EMAIL: cls.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
|
2015-12-07 08:01:28 +03:00
|
|
|
def default_vocab(cls, package=None, get_lex_attr=None):
|
|
|
|
if package is None:
|
2015-12-18 11:52:55 +03:00
|
|
|
package = get_package()
|
2015-08-25 16:37:17 +03:00
|
|
|
if get_lex_attr is None:
|
2015-12-07 08:01:28 +03:00
|
|
|
get_lex_attr = cls.default_lex_attrs()
|
|
|
|
return Vocab.from_package(package, get_lex_attr=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):
|
2015-12-18 11:52:55 +03:00
|
|
|
data_dir = package.dir_path('deps', require=False)
|
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)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
@classmethod
|
2015-12-07 08:01:28 +03:00
|
|
|
def default_entity(cls, package, vocab):
|
2015-12-18 11:52:55 +03:00
|
|
|
data_dir = package.dir_path('ner', require=False)
|
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)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2015-12-28 17:56:27 +03:00
|
|
|
def __init__(self,
|
|
|
|
data_dir=None,
|
|
|
|
model=None,
|
|
|
|
vocab=None,
|
|
|
|
tokenizer=None,
|
|
|
|
tagger=None,
|
|
|
|
parser=None,
|
|
|
|
entity=None,
|
|
|
|
matcher=None,
|
|
|
|
serializer=None,
|
|
|
|
load_vectors=True):
|
2015-12-18 11:52:55 +03:00
|
|
|
"""
|
|
|
|
a model can be specified:
|
|
|
|
|
|
|
|
1) by a path to the model directory (DEPRECATED)
|
|
|
|
- Language(data_dir='path/to/data')
|
|
|
|
|
|
|
|
2) by a language identifier (and optionally a package root dir)
|
|
|
|
- Language(lang='en')
|
|
|
|
- Language(lang='en', data_dir='spacy/data')
|
|
|
|
|
|
|
|
3) by a model name/version (and optionally a package root dir)
|
|
|
|
- Language(model='en_default')
|
2015-12-18 21:12:08 +03:00
|
|
|
- Language(model='en_default ==1.0.0')
|
|
|
|
- Language(model='en_default <1.1.0, data_dir='spacy/data')
|
2015-12-18 11:52:55 +03:00
|
|
|
"""
|
|
|
|
# support non-package data dirs
|
|
|
|
if data_dir and path.exists(path.join(data_dir, 'vocab')):
|
|
|
|
class Package(object):
|
|
|
|
def __init__(self, root):
|
|
|
|
self.root = root
|
|
|
|
|
|
|
|
def has_file(self, *path_parts):
|
|
|
|
return path.exists(path.join(self.root, *path_parts))
|
|
|
|
|
|
|
|
def file_path(self, *path_parts, **kwargs):
|
|
|
|
return path.join(self.root, *path_parts)
|
|
|
|
|
|
|
|
def dir_path(self, *path_parts, **kwargs):
|
|
|
|
return path.join(self.root, *path_parts)
|
|
|
|
|
|
|
|
def load_utf8(self, func, *path_parts, **kwargs):
|
|
|
|
with io.open(self.file_path(path.join(*path_parts)),
|
|
|
|
mode='r', encoding='utf8') as f:
|
|
|
|
return func(f)
|
|
|
|
|
|
|
|
warn("using non-package data_dir", DeprecationWarning)
|
|
|
|
package = Package(data_dir)
|
|
|
|
else:
|
2015-12-18 21:12:08 +03:00
|
|
|
package = get_package(name=model, data_path=data_dir)
|
2015-09-09 15:31:09 +03:00
|
|
|
if load_vectors is not True:
|
|
|
|
warn("load_vectors is deprecated", DeprecationWarning)
|
2015-09-29 13:54:12 +03:00
|
|
|
if vocab in (None, True):
|
2015-12-28 20:48:57 +03:00
|
|
|
vocab = self.default_vocab(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):
|
2015-12-28 20:48: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):
|
2015-12-28 18:54:03 +03:00
|
|
|
tagger = Tagger.from_package(package, self.vocab)
|
|
|
|
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):
|
2015-12-28 18:54:03 +03:00
|
|
|
matcher = Matcher.from_package(package, self.vocab)
|
|
|
|
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
|
2015-12-28 18:54:03 +03:00
|
|
|
None, # model
|
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')
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
self.entity(tokens)
|
|
|
|
return tokens
|
|
|
|
|
|
|
|
def end_training(self, data_dir=None):
|
|
|
|
if data_dir is None:
|
|
|
|
data_dir = self.data_dir
|
2015-11-06 19:24:30 +03:00
|
|
|
self.parser.model.end_training()
|
|
|
|
self.parser.model.dump(path.join(data_dir, 'deps', 'model'))
|
|
|
|
self.entity.model.end_training()
|
|
|
|
self.entity.model.dump(path.join(data_dir, 'ner', 'model'))
|
|
|
|
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_)
|
2015-08-25 16:37:17 +03:00
|
|
|
|
|
|
|
with open(path.join(data_dir, 'vocab', 'serializer.json'), 'w') as file_:
|
|
|
|
file_.write(
|
|
|
|
json.dumps([
|
|
|
|
(TAG, list(self.tagger.freqs[TAG].items())),
|
|
|
|
(DEP, list(self.parser.moves.freqs[DEP].items())),
|
|
|
|
(ENT_IOB, list(self.entity.moves.freqs[ENT_IOB].items())),
|
|
|
|
(ENT_TYPE, list(self.entity.moves.freqs[ENT_TYPE].items())),
|
|
|
|
(HEAD, list(self.parser.moves.freqs[HEAD].items()))]))
|