mirror of
https://github.com/explosion/spaCy.git
synced 2025-08-02 19:30:19 +03:00
add functionality for 'last=False' when adding a pipeline component
This commit is contained in:
parent
8f058e39bd
commit
e55e4d6048
|
@ -743,6 +743,7 @@ class Language:
|
||||||
"""Add a component to the processing pipeline. Valid components are
|
"""Add a component to the processing pipeline. Valid components are
|
||||||
callables that take a `Doc` object, modify it and return it. Only one
|
callables that take a `Doc` object, modify it and return it. Only one
|
||||||
of before/after/first/last can be set. Default behaviour is "last".
|
of before/after/first/last can be set. Default behaviour is "last".
|
||||||
|
If "last" is set to False, then the default is "first".
|
||||||
|
|
||||||
factory_name (str): Name of the component factory.
|
factory_name (str): Name of the component factory.
|
||||||
name (str): Name of pipeline component. Overwrites existing
|
name (str): Name of pipeline component. Overwrites existing
|
||||||
|
@ -816,10 +817,10 @@ class Language:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
Errors.E006.format(args=all_args, opts=self.component_names)
|
Errors.E006.format(args=all_args, opts=self.component_names)
|
||||||
)
|
)
|
||||||
if last or not any(value is not None for value in [first, before, after]):
|
if first or last is False:
|
||||||
return len(self._components)
|
|
||||||
elif first:
|
|
||||||
return 0
|
return 0
|
||||||
|
elif last or not any(value is not None for value in [first, before, after]):
|
||||||
|
return len(self._components)
|
||||||
elif isinstance(before, str):
|
elif isinstance(before, str):
|
||||||
if before not in self.component_names:
|
if before not in self.component_names:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
|
@ -188,6 +188,14 @@ def test_add_pipe_last(nlp, name1, name2):
|
||||||
assert nlp.pipeline[-1][0] == name1
|
assert nlp.pipeline[-1][0] == name1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("name1,name2", [("parser", "lambda_pipe")])
|
||||||
|
def test_add_pipe_not_last(nlp, name1, name2):
|
||||||
|
Language.component("new_pipe2", func=lambda doc: doc)
|
||||||
|
nlp.add_pipe("new_pipe2", name=name2)
|
||||||
|
nlp.add_pipe("new_pipe", name=name1, last=False)
|
||||||
|
assert nlp.pipeline[-1][0] != name1
|
||||||
|
|
||||||
|
|
||||||
def test_cant_add_pipe_first_and_last(nlp):
|
def test_cant_add_pipe_first_and_last(nlp):
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
nlp.add_pipe("new_pipe", first=True, last=True)
|
nlp.add_pipe("new_pipe", first=True, last=True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user