我目前正在构建一个带有自定义NER、实体链接器和Textcat组件的spacy管道。对于我的实体链接器组件,我已经修改了candidate_generator()以适应我的用例。我借鉴了爱默生的演示项目。下面是我的custom_functions代码。
import spacy
from functools import partial
from pathlib import Path
from typing import Iterable, Callable
from spacy.training import Example
from spacy.tokens import DocBin
from spacy.kb import Candidate, KnowledgeBase, get_candidates
@spacy.registry.misc("Custom_Candidate_Gen.v1")
def create_candidates():
return custom_get_candidates
def custom_get_candidates(kb, span):
return kb.get_alias_candidates(span.text.lower())
@spacy.registry.readers("MyCorpus.v1")
def create_docbin_reader(file: Path) -> Callable[["Language"], Iterable[Example]]:
return partial(read_files, file)
def read_files(file: Path, nlp: "Language") -> Iterable[Example]:
# we run the full pipeline and not just nlp.make_doc to ensure we have entities and sentences
# which are needed during training of the entity linker
with nlp.select_pipes(disable="entity_linker"):
doc_bin = DocBin().from_disk(file)
docs = doc_bin.get_docs(nlp.vocab)
for doc in docs:
yield Example(nlp(doc.text), doc)在培训了我的实体链接器并将我的textcat组件添加到管道之后,我得到了以下错误:
catalogue.RegistryError: [E893] Could not find function 'Custom_Candidate_Gen.v1' in function registry 'misc'. If you're using a custom function, make sure the code is available. If the function is provided by a third-party package, e.g. spacy-transformers, make sure the package is installed in your environment.
Available names: spacy.CandidateGenerator.v1, spacy.EmptyKB.v1, spacy.KBFromFile.v1, spacy.LookupsDataLoader.v1, spacy.ngram_range_suggester.v1, spacy.ngram_suggester.v1为什么我的自定义候选生成器没有注册?
发布于 2022-05-16 15:14:41
在加载模型时加载和注册自定义代码的选项:
spacy package --code将其打包到您的模型中,并从安装的包名(而不是目录)加载模型setup.cfg中的入口点来注册方法(这很好,但在这种情况下不是我的首选)请参见:
https://stackoverflow.com/questions/72256065
复制相似问题