本文整理汇总了Python中spacy.util方法的典型用法代码示例。如果您正苦于以下问题:Python spacy.util方法的具体用法?Python spacy.util怎么用?Python spacy.util使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spacy
的用法示例。
在下文中一共展示了spacy.util方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_installed
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import util [as 别名]
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
示例2: is_package
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import util [as 别名]
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
示例3: model_installed
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import util [as 别名]
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
示例4: load_lang_model
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import util [as 别名]
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)