Python spacy.util() Examples

The following are 4 code examples of spacy.util(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module spacy , or try the search function .
Example #1
Source File: spacy_parser.py    From fonduer with MIT License 7 votes vote down vote up
def model_installed(name: str) -> bool:
        """Check if spaCy language model is installed.

        From https://github.com/explosion/spaCy/blob/master/spacy/util.py

        :param name:
        :return:
        """
        data_path = util.get_data_path()
        if not data_path or not data_path.exists():
            raise IOError(f"Can't find spaCy data path: {data_path}")
        if name in {d.name for d in data_path.iterdir()}:
            return True
        if is_package(name):  # installed as package
            return True
        if Path(name).exists():  # path to model data directory
            return True
        return False 
Example #2
Source File: spacy_annotator.py    From errudite with GNU General Public License v2.0 6 votes vote down vote up
def is_package(name: str):
        """Check if string maps to a package installed via pip.
        From https://github.com/explosion/spaCy/blob/master/spacy/util.py
        
        Arguments:
            name {str} -- Name of package
        
        Returns:
            [bool] -- True if installed package, False if not.
        """

        name = name.lower()  # compare package name against lowercase name
        packages = pkg_resources.working_set.by_key.keys()
        for package in packages:
            if package.lower().replace('-', '_') == name:
                return True
            return False 
Example #3
Source File: spacy_annotator.py    From errudite with GNU General Public License v2.0 6 votes vote down vote up
def model_installed(name: str):
        """Check if spaCy language model is installed
        From https://github.com/explosion/spaCy/blob/master/spacy/util.py
        
        Arguments:
            name {str} -- Name of package
        
        Returns:
            [bool] -- True if installed package, False if not.
        """
        data_path = util.get_data_path()
        if not data_path or not data_path.exists():
            raise IOError("Can't find spaCy data path: %s" % str(data_path))
        if name in set([d.name for d in data_path.iterdir()]):
            return True
        if SpacyAnnotator.is_package(name): # installed as package
            return True
        if Path(name).exists(): # path to model data directory
            return True
        return False 
Example #4
Source File: spacy_annotator.py    From errudite with GNU General Public License v2.0 5 votes vote down vote up
def load_lang_model(lang: str, disable: List[str]):
        """Load spaCy language model or download if
            model is available and not installed
        
        Arguments:
            lang {str} -- language
            disable {List[str]} -- If only using tokenizer, can disable ['parser', 'ner', 'textcat']
        
        Returns:
            [type] -- [description]
        """
        if 'coref' in lang:
            try:
                return spacy.load(lang, disable=disable) # 
            except Exception as e:
                return SpacyAnnotator.load_lang_model(lang.split('_')[0], disable=disable)
        try:
            return spacy.load(lang, disable=disable)
        except OSError:
            logger.warning(f"Spacy models '{lang}' not found.  Downloading and installing.")
            spacy_download(lang)
            # NOTE(mattg): The following four lines are a workaround suggested by Ines for spacy
            # 2.1.0, which removed the linking that was done in spacy 2.0.  importlib doesn't find
            # packages that were installed in the same python session, so the way `spacy_download`
            # works in 2.1.0 is broken for this use case.  These four lines can probably be removed
            # at some point in the future, once spacy has figured out a better way to handle this.
            # See https://github.com/explosion/spaCy/issues/3435.
            from spacy.cli import link
            from spacy.util import get_package_path
            package_path = get_package_path(lang)
            link(lang, lang, model_path=package_path)
            return spacy.load(lang, disable=disable)