本文整理汇总了Python中spacy.language方法的典型用法代码示例。如果您正苦于以下问题:Python spacy.language方法的具体用法?Python spacy.language怎么用?Python spacy.language使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spacy
的用法示例。
在下文中一共展示了spacy.language方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_installed
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [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: _load_lang_model
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def _load_lang_model(self) -> None:
"""Load spaCy language model.
If a model is not installed, download it before loading it.
Currenty supported spaCy languages
en English (50MB)
de German (645MB)
fr French (1.33GB)
es Spanish (377MB)
:return:
"""
if self.lang in self.languages:
if not SpacyParser.model_installed(self.lang):
download(self.lang)
model = spacy.load(self.lang)
elif self.lang in self.alpha_languages:
language_module = importlib.import_module(f"spacy.lang.{self.lang}")
language_method = getattr(language_module, self.alpha_languages[self.lang])
model = language_method()
self.model = model
示例3: load_nlp
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def load_nlp(vectors_loc, lang=None):
if lang is None:
nlp = Language()
else:
# create empty language class – this is required if you're planning to
# save the model to disk and load it back later (models always need a
# "lang" setting). Use 'xx' for blank multi-language class.
nlp = spacy.blank(lang)
with open(vectors_loc, 'rb') as file_:
header = file_.readline()
nr_row, nr_dim = header.split()
nlp.vocab.reset_vectors(width=int(nr_dim))
for line in file_:
line = line.rstrip().decode('utf8')
pieces = line.rsplit(' ', int(nr_dim))
word = pieces[0]
vector = numpy.asarray([float(v) for v in pieces[1:]], dtype='f')
nlp.vocab.set_vector(word, vector) # add the vectors to the vocab
return nlp
示例4: create
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def create(cls,
component_config: Dict[Text, Any],
config: RasaNLUModelConfig) -> 'SpacyNLP':
import spacy
component_config = override_defaults(cls.defaults, component_config)
spacy_model_name = component_config.get("model")
# if no model is specified, we fall back to the language string
if not spacy_model_name:
spacy_model_name = config.language
component_config["model"] = config.language
logger.info("Trying to load spacy model with "
"name '{}'".format(spacy_model_name))
nlp = spacy.load(spacy_model_name, disable=['parser'])
cls.ensure_proper_language_model(nlp)
return cls(component_config, nlp)
示例5: ensure_proper_language_model
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def ensure_proper_language_model(nlp: Optional['Language']) -> None:
"""Checks if the spacy language model is properly loaded.
Raises an exception if the model is invalid."""
if nlp is None:
raise Exception("Failed to load spacy language model. "
"Loading the model returned 'None'.")
if nlp.path is None:
# Spacy sets the path to `None` if
# it did not load the model from disk.
# In this case `nlp` is an unusable stub.
raise Exception("Failed to load spacy language model for "
"lang '{}'. Make sure you have downloaded the "
"correct model (https://spacy.io/docs/usage/)."
"".format(nlp.lang))
示例6: __call__
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def __call__(self, doc: Doc, language: Language = None, stop_on_first_profane_word: Optional[bool] = None) -> Doc:
self.register_extensions(exist_ok=True)
if language is None:
language = self._language
if stop_on_first_profane_word is None:
stop_on_first_profane_word = self._stop_on_first_profane_word
i = 0
while i < len(doc):
j = i + 1
while (j < len(doc)
and not doc[j - 1].whitespace_ and not doc[j - 1].is_space and not doc[j - 1].is_punct
and not doc[j].is_space and not doc[j].is_punct):
j += 1
span = self._censor_spaceless_span(doc[i:j], language=language)
if stop_on_first_profane_word and span._.is_profane:
break
i += len(span)
return doc
示例7: _censor
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def _censor(self, text: str, return_bool=False) -> Union[str, bool]:
""":return: text with any profane words censored or bool (True - text has profane words, False otherwise) if
return_bool=True"""
result = ''
text_parts = self._split_by_language(text=text)
for language, text_part in text_parts:
result_part = text_part
doc = self._parse(language=language, text=text_part)
for token in doc:
if token._.is_profane:
if return_bool:
return True
else:
result_part = self._replace_token(text=result_part, old=token, new=token._.censored)
result += result_part
if return_bool:
return False
else:
return result
示例8: create
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def create(
cls, component_config: Dict[Text, Any], config: RasaNLUModelConfig
) -> "SpacyNLP":
component_config = override_defaults(cls.defaults, component_config)
spacy_model_name = component_config.get("model")
# if no model is specified, we fall back to the language string
if not spacy_model_name:
spacy_model_name = config.language
component_config["model"] = config.language
logger.info(f"Trying to load spacy model with name '{spacy_model_name}'")
nlp = cls.load_model(spacy_model_name)
cls.ensure_proper_language_model(nlp)
return cls(component_config, nlp)
示例9: ensure_proper_language_model
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def ensure_proper_language_model(nlp: Optional["Language"]) -> None:
"""Checks if the spacy language model is properly loaded.
Raises an exception if the model is invalid."""
if nlp is None:
raise Exception(
"Failed to load spacy language model. "
"Loading the model returned 'None'."
)
if nlp.path is None:
# Spacy sets the path to `None` if
# it did not load the model from disk.
# In this case `nlp` is an unusable stub.
raise Exception(
"Failed to load spacy language model for "
"lang '{}'. Make sure you have downloaded the "
"correct model (https://spacy.io/docs/usage/)."
"".format(nlp.lang)
)
示例10: create
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def create(cls, cfg):
# type: (RasaNLUModelConfig) -> SpacyNLP
import spacy
component_conf = cfg.for_component(cls.name, cls.defaults)
spacy_model_name = component_conf.get("model")
# if no model is specified, we fall back to the language string
if not spacy_model_name:
spacy_model_name = cfg.language
component_conf["model"] = cfg.language
logger.info("Trying to load spacy model with "
"name '{}'".format(spacy_model_name))
nlp = spacy.load(spacy_model_name, parser=False)
cls.ensure_proper_language_model(nlp)
return SpacyNLP(component_conf, nlp)
示例11: ensure_proper_language_model
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def ensure_proper_language_model(nlp):
# type: (Optional[Language]) -> None
"""Checks if the spacy language model is properly loaded.
Raises an exception if the model is invalid."""
if nlp is None:
raise Exception("Failed to load spacy language model. "
"Loading the model returned 'None'.")
if nlp.path is None:
# Spacy sets the path to `None` if
# it did not load the model from disk.
# In this case `nlp` is an unusable stub.
raise Exception("Failed to load spacy language model for "
"lang '{}'. Make sure you have downloaded the "
"correct model (https://spacy.io/docs/usage/)."
"".format(nlp.lang))
示例12: test_neg_spacy_processor
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def test_neg_spacy_processor(self):
spacy = Pipeline[DataPack]()
spacy.set_reader(StringReader())
config = {
"processors": 'ner',
"lang": "xx_ent_wiki_sm",
# Language code for the language to build the Pipeline
"use_gpu": False
}
spacy.add(SpacyProcessor(), config=config)
spacy.initialize()
sentences = ["This tool is called Forte.",
"The goal of this project to help you build NLP "
"pipelines.",
"NLP has never been made this easy before."]
document = ' '.join(sentences)
with self.assertRaises(ProcessExecutionException):
_ = spacy.process(document)
示例13: __init__
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def __init__(
self, nlp: Language, input_id_col: str = "id", input_text_col: str = "text"
):
"""Initialize the SpacyExtractor pipeline.
nlp (spacy.language.Language): pre-loaded spacy language model
input_text_col (str): property on each document to run the model on
input_id_col (str): property on each document to correlate with request
RETURNS (EntityRecognizer): The newly constructed object.
"""
self.nlp = nlp
self.input_id_col = input_id_col
self.input_text_col = input_text_col
示例14: __init__
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def __init__(self, vocab: Vocab) -> None:
"""Initialize a custom tokenizer.
:param vocab: The vocab attribute of the respective spacy language object.
"""
self.vocab = vocab
示例15: cache_key
# 需要导入模块: import spacy [as 别名]
# 或者: from spacy import language [as 别名]
def cache_key(cls,
component_meta: Dict[Text, Any],
model_metadata: 'Metadata') -> Optional[Text]:
# Fallback, use the language name, e.g. "en",
# as the model name if no explicit name is defined
spacy_model_name = component_meta.get("model", model_metadata.language)
return cls.name + "-" + spacy_model_name