Add JSON-based serialization logic and deepcopy implementation for Sentence object#3669
Merged
Add JSON-based serialization logic and deepcopy implementation for Sentence object#3669
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a comprehensive solution for serializing and deep-copying Flair's Sentence objects, addressing a long-standing issue where standard methods like copy.deepcopy() and pickle would fail.
Closes #3245
This PR implements two key mechanisms to solve this problem:
JSON Serialization (
to_dict/from_dict):sentence.to_dict()method serializes the entireSentenceobject—including its text, tokenizer, and all annotations (spans, relations, and labels at all levels)—into a clean, JSON-compatible dictionary.Sentence.from_dict(), fully reconstructs theSentenceobject from this dictionary, correctly restoring all annotations and the original tokenizer._capture_annotations()and_reapply_annotations(), which use character offsets to robustly map annotations between different tokenizations. This also allowed for a significant simplification of the existingretokenize()logic.Custom Deep Copy (
__deepcopy__):__deepcopy__method has been implemented for theSentenceclass.SpanandRelation.Sentenceobjects fully compatible with Python'scopy.deepcopy(), resolving the originalTypeError.Key Changes
Sentence.to_dict(),Sentence.from_dict(), andSentence.__deepcopy__().Sentenceconstructor (__init__) to correctly handle initialization from raw text, a list of strings, and a list of pre-madeTokenobjects, which was critical for the deserialization logic.retokenize()implementation by reusing the new annotation helper methods._add_token()method more robust in calculating token start positions.tests/test_sentence_serialization.py, with comprehensive unit tests.mypytype-checking errors that arose during the refactoring to ensure the new code is robust and maintainable.