Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let PipeLine support copy() #1861

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions hanlp/components/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -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()