Fix passing of component configuration (#5374)

* add kwargs to to_disk methods in docs - otherwise crashes on 'exclude' argument

* add fix and test for Issue 5137
This commit is contained in:
Sofie Van Landeghem 2020-04-29 12:56:17 +02:00 committed by GitHub
parent efec28ce70
commit cfdaf99b80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View File

@ -0,0 +1,33 @@
import spacy
from spacy.language import Language
from spacy.lang.en import English
from spacy.tests.util import make_tempdir
def test_issue5137():
class MyComponent(object):
name = "my_component"
def __init__(self, nlp, **cfg):
self.nlp = nlp
self.categories = cfg.get("categories", "all_categories")
def __call__(self, doc):
pass
def to_disk(self, path, **kwargs):
pass
def from_disk(self, path, **cfg):
pass
Language.factories["my_component"] = lambda nlp, **cfg: MyComponent(nlp, **cfg)
nlp = English()
nlp.add_pipe(nlp.create_pipe("my_component"))
assert nlp.get_pipe("my_component").categories == "all_categories"
with make_tempdir() as tmpdir:
nlp.to_disk(tmpdir)
nlp2 = spacy.load(tmpdir, categories="my_categories")
assert nlp2.get_pipe("my_component").categories == "my_categories"

View File

@ -208,6 +208,7 @@ def load_model_from_path(model_path, meta=False, **overrides):
for name in pipeline:
if name not in disable:
config = meta.get("pipeline_args", {}).get(name, {})
config.update(overrides)
factory = factories.get(name, name)
component = nlp.create_pipe(factory, config=config)
nlp.add_pipe(component, name=name)

View File

@ -216,7 +216,7 @@ class CustomComponent(object):
# Add something to the component's data
self.data.append(data)
def to_disk(self, path):
def to_disk(self, path, **kwargs):
# This will receive the directory path + /my_component
data_path = path / "data.json"
with data_path.open("w", encoding="utf8") as f:
@ -461,7 +461,7 @@ model. When you save out a model using `nlp.to_disk` and the component exposes a
`to_disk` method, it will be called with the disk path.
```python
def to_disk(self, path):
def to_disk(self, path, **kwargs):
snek_path = path / "snek.txt"
with snek_path.open("w", encoding="utf8") as snek_file:
snek_file.write(self.snek)