<commit_before>from __future__ import absolute_import
from os import path
from warnings import warn
import io

try:
    import ujson as json
except ImportError:
    import json

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

from . import util
from .attrs import TAG, DEP, ENT_IOB, ENT_TYPE, HEAD


class Language(object):
    lang = None

    @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

    @staticmethod
    def is_digit(string):
        return string.isdigit()

    @staticmethod
    def is_space(string):
        return string.isspace()

    @staticmethod
    def is_stop(string):
        return 0

    @classmethod
    def default_lex_attrs(cls, *args, **kwargs):
        oov_prob = kwargs.get('oov_prob', -20)
        return {
            attrs.LOWER: cls.lower,
            attrs.NORM: cls.norm,
            attrs.SHAPE: orth.word_shape,
            attrs.PREFIX: cls.prefix,
            attrs.SUFFIX: cls.suffix,
            attrs.CLUSTER: cls.cluster,
            attrs.PROB: lambda string: oov_prob,
            attrs.LANG: lambda string: cls.lang,
            attrs.IS_ALPHA: orth.is_alpha,
            attrs.IS_ASCII: orth.is_ascii,
            attrs.IS_DIGIT: cls.is_digit,
            attrs.IS_LOWER: orth.is_lower,
            attrs.IS_PUNCT: orth.is_punct,
            attrs.IS_SPACE: cls.is_space,
            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: cls.is_stop,
            attrs.IS_OOV: lambda string: True
        }

    @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}}

    @classmethod
    def default_vocab(cls, package, get_lex_attr=None, vectors_package=None):
        if get_lex_attr is None:
            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()
        if hasattr(package, 'dir_path'):
            return Vocab.from_package(package, get_lex_attr=get_lex_attr,
                vectors_package=vectors_package)
        else:
            return Vocab.load(package, get_lex_attr)

    @classmethod
    def default_parser(cls, package, vocab):
        if hasattr(package, 'dir_path'):
            data_dir = package.dir_path('deps')
        else:
            data_dir = package
        if data_dir and path.exists(data_dir):
            return Parser.from_dir(data_dir, vocab.strings, ArcEager)
        else:
            return None

    @classmethod
    def default_entity(cls, package, vocab):
        if hasattr(package, 'dir_path'):
            data_dir = package.dir_path('ner')
        else:
            data_dir = package
        if data_dir and path.exists(data_dir):
            return Parser.from_dir(data_dir, vocab.strings, BiluoPushDown)
        else:
            return None

    def __init__(self,
        data_dir=None,
        vocab=None,
        tokenizer=None,
        tagger=None,
        parser=None,
        entity=None,
        matcher=None,
        serializer=None,
        load_vectors=True,
        package=None,
        vectors_package=None):
        """
           a model can be specified:

           1) by calling a Language subclass
             - spacy.en.English()

           2) by calling a Language subclass with data_dir
             - spacy.en.English('my/model/root')
             - spacy.en.English(data_dir='my/model/root')

           3) by package name
             - spacy.load('en_default')
             - spacy.load('en_default==1.0.0')

           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')
        """

        if package is None:
            if data_dir is None:
                package = util.get_package_by_name()
            else:<commit_msg>* Fix language.py docstring<commit_after>from __future__ import absolute_import
from os import path
from warnings import warn
import io

try:
    import ujson as json
except ImportError:
    import json

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

from . import util
from .attrs import TAG, DEP, ENT_IOB, ENT_TYPE, HEAD


class Language(object):
    lang = None

    @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

    @staticmethod
    def is_digit(string):
        return string.isdigit()

    @staticmethod
    def is_space(string):
        return string.isspace()

    @staticmethod
    def is_stop(string):
        return 0

    @classmethod
    def default_lex_attrs(cls, *args, **kwargs):
        oov_prob = kwargs.get('oov_prob', -20)
        return {
            attrs.LOWER: cls.lower,
            attrs.NORM: cls.norm,
            attrs.SHAPE: orth.word_shape,
            attrs.PREFIX: cls.prefix,
            attrs.SUFFIX: cls.suffix,
            attrs.CLUSTER: cls.cluster,
            attrs.PROB: lambda string: oov_prob,
            attrs.LANG: lambda string: cls.lang,
            attrs.IS_ALPHA: orth.is_alpha,
            attrs.IS_ASCII: orth.is_ascii,
            attrs.IS_DIGIT: cls.is_digit,
            attrs.IS_LOWER: orth.is_lower,
            attrs.IS_PUNCT: orth.is_punct,
            attrs.IS_SPACE: cls.is_space,
            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: cls.is_stop,
            attrs.IS_OOV: lambda string: True
        }

    @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}}

    @classmethod
    def default_vocab(cls, package, get_lex_attr=None, vectors_package=None):
        if get_lex_attr is None:
            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()
        if hasattr(package, 'dir_path'):
            return Vocab.from_package(package, get_lex_attr=get_lex_attr,
                vectors_package=vectors_package)
        else:
            return Vocab.load(package, get_lex_attr)

    @classmethod
    def default_parser(cls, package, vocab):
        if hasattr(package, 'dir_path'):
            data_dir = package.dir_path('deps')
        else:
            data_dir = package
        if data_dir and path.exists(data_dir):
            return Parser.from_dir(data_dir, vocab.strings, ArcEager)
        else:
            return None

    @classmethod
    def default_entity(cls, package, vocab):
        if hasattr(package, 'dir_path'):
            data_dir = package.dir_path('ner')
        else:
            data_dir = package
        if data_dir and path.exists(data_dir):
            return Parser.from_dir(data_dir, vocab.strings, BiluoPushDown)
        else:
            return None

    def __init__(self,
        data_dir=None,
        vocab=None,
        tokenizer=None,
        tagger=None,
        parser=None,
        entity=None,
        matcher=None,
        serializer=None,
        load_vectors=True,
        package=None,
        vectors_package=None):
        """
        A model can be specified:

        1) by calling a Language subclass
            - spacy.en.English()

        2) by calling a Language subclass with data_dir
            - spacy.en.English('my/model/root')
            - spacy.en.English(data_dir='my/model/root')

        3) by package name
            - spacy.load('en_default')
            - spacy.load('en_default==1.0.0')

        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')
        """
        if package is None:
            if data_dir is None:
                package = util.get_package_by_name()
            else:
{'fraction_non_alphanumeric': 0.09445053302779716, 'fraction_numerical': 0.0035867291023214108, 'mean_word_length': 2.0362976406533577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2530321', 'n_tokens_mistral': 3174, 'n_tokens_neox': 3098, 'n_words': 702}