代码是-
ruler = EntityRuler(nlp, overwrite_ents=True,attr='lemma')
print("Adding pattern")
ruler.add_patterns(patterns)
if not nlp.has_pipe("skills_ruler"):
nlp.add_pipe(ruler, name="skills_ruler")
print("added pattern")
将模型另存为
output_dir=Path("path_name")
if output_dir is not None:
output_dir = Path(output_dir)
if not output_dir.exists():
output_dir.mkdir()
nlp.to_disk(output_dir)
print("Saved model to", output_dir)
将模型加载为
nlp = spacy.load(path_name)
出现以下错误-
KeyError: "[E002] Can't find factory for 'skills_ruler'. This usually happens when spaCy calls `nlp.create_pipe` with a component name that's not built in - for example, when constructing the pipeline from a model's meta.json. If you're using a custom component, you can write to `Language.factories['skills_ruler']` or remove it from the model meta and add it via `nlp.add_pipe` instead."
我尝试过这个:-
from spacy.language import Language
from spacy.pipeline import EntityRuler
Language.factories["skills_ruler"] = lambda nlp, **cfg: EntityRuler(nlp, **cfg)
pipe = nlp.create_pipe("skills_ruler")
但它也会给出错误-:
ImportError: cannot import name 'EntityRuler'