diff --git a/hanlp/components/pipeline.py b/hanlp/components/pipeline.py index 7f700b1e7..c6156f497 100644 --- a/hanlp/components/pipeline.py +++ b/hanlp/components/pipeline.py @@ -145,15 +145,21 @@ def __call__(self, doc: Union[Document, Any] = None, **kwargs) -> Document: for component in self: doc = component(doc) return doc - + + def copy(self): + return self.__copy__() + + def __copy__(self): + config = self.meta + return Pipeline.from_config(config) + @property def meta(self): return { 'classpath': classpath_of(self), 'hanlp_version': hanlp.version.__version__, 'pipes': [pipe.config for pipe in self] - } - + } @meta.setter def meta(self, value): pass diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py new file mode 100644 index 000000000..5c1f5f86a --- /dev/null +++ b/tests/test_pipeline.py @@ -0,0 +1,15 @@ +import unittest +import hanlp + + +class TestPipeLine(unittest.TestCase): + def test_copy(self): + pipe = hanlp.pipeline().append(hanlp.utils.rules.split_sentence) + copied_pipe = pipe.copy() + test_text = "今天天气真好。我要去散步。" + assert pipe is not copied_pipe + copied_pipe.append(lambda sent: "".join(sent)) + assert pipe(test_text) != copied_pipe(test_text) + +if __name__ == '__main__': + unittest.main()