0

spacy 3.0我已经像这样注册了我的自定义语言:

import spacy
from spacy.lang.en import English

class CustomEnglishDefaults(English.Defaults):
    stop_words = set(["custom", "stop"])

@spacy.registry.languages("custom_en")
class CustomEnglish(English):
    lang = "custom_en"
    Defaults = CustomEnglishDefaults

我已经尝试了代码行nlp = spacy.blank("custom_en"),它工作正常。但是当我lang="custom-en"config.cfg文件中使用并使用命令时:

python -m spacy train config.cfg --paths.train ./train.spacy --paths.dev ./dev.spacy

我收到以下错误:

Traceback (most recent call last):
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 244, in get_lang_class
    module = importlib.import_module(f".lang.{lang}", "spacy")
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'spacy.lang.custom_en'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/__main__.py", line 4, in <module>
    setup_cli()
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/_util.py", line 69, in setup_cli
    command(prog_name=COMMAND)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/typer/main.py", line 497, in wrapper
    return callback(**use_params)  # type: ignore
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/train.py", line 56, in train_cli
    nlp = init_nlp(config, use_gpu=use_gpu)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/training/initialize.py", line 39, in init_nlp
    nlp = load_model_from_config(raw_config, auto_fill=True)
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 425, in load_model_from_config
    lang_cls = get_lang_class(nlp_config["lang"])
  File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 246, in get_lang_class
    raise ImportError(Errors.E048.format(lang=lang, err=err)) from err
ImportError: [E048] Can't import language custom_en from spacy.lang: No module named 'spacy.lang.custom_en'

我该如何解决这个问题?

4

1 回答 1

1

请参阅本节右侧的侧边栏:https ://spacy.io/usage/linguistic-features#language-subclass

python -m spacy train config.cfg --code code.py
于 2021-04-19T11:26:55.130 回答