本文整理匯總了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)