mirror of
https://github.com/explosion/spaCy.git
synced 2025-07-10 16:22:29 +03:00
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:
parent
efec28ce70
commit
cfdaf99b80
33
spacy/tests/regression/test_issue5137.py
Normal file
33
spacy/tests/regression/test_issue5137.py
Normal 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"
|
|
@ -208,6 +208,7 @@ def load_model_from_path(model_path, meta=False, **overrides):
|
||||||
for name in pipeline:
|
for name in pipeline:
|
||||||
if name not in disable:
|
if name not in disable:
|
||||||
config = meta.get("pipeline_args", {}).get(name, {})
|
config = meta.get("pipeline_args", {}).get(name, {})
|
||||||
|
config.update(overrides)
|
||||||
factory = factories.get(name, name)
|
factory = factories.get(name, name)
|
||||||
component = nlp.create_pipe(factory, config=config)
|
component = nlp.create_pipe(factory, config=config)
|
||||||
nlp.add_pipe(component, name=name)
|
nlp.add_pipe(component, name=name)
|
||||||
|
|
|
@ -216,7 +216,7 @@ class CustomComponent(object):
|
||||||
# Add something to the component's data
|
# Add something to the component's data
|
||||||
self.data.append(data)
|
self.data.append(data)
|
||||||
|
|
||||||
def to_disk(self, path):
|
def to_disk(self, path, **kwargs):
|
||||||
# This will receive the directory path + /my_component
|
# This will receive the directory path + /my_component
|
||||||
data_path = path / "data.json"
|
data_path = path / "data.json"
|
||||||
with data_path.open("w", encoding="utf8") as f:
|
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.
|
`to_disk` method, it will be called with the disk path.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def to_disk(self, path):
|
def to_disk(self, path, **kwargs):
|
||||||
snek_path = path / "snek.txt"
|
snek_path = path / "snek.txt"
|
||||||
with snek_path.open("w", encoding="utf8") as snek_file:
|
with snek_path.open("w", encoding="utf8") as snek_file:
|
||||||
snek_file.write(self.snek)
|
snek_file.write(self.snek)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user