@@ -714,8 +714,13 @@ def add_all_annotations_from_other(
714
714
process_predictions : bool = True ,
715
715
strict : bool = True ,
716
716
verbose : bool = True ,
717
- ) -> None :
718
- """Adds all annotations from another document to this document.
717
+ ) -> Dict [str , List [Annotation ]]:
718
+ """Adds all annotations from another document to this document. It allows to blacklist annotations
719
+ and also to override annotations. It returns the original annotations for which a new annotation was
720
+ added to the current document.
721
+
722
+ The method is useful if e.g. a text-based document is converted to a token-based document and the
723
+ annotations should be added to the token-based document.
719
724
720
725
Args:
721
726
other: The document to add annotations from.
@@ -744,6 +749,11 @@ def add_all_annotations_from_other(
744
749
verbose: Whether to print a warning if the other document contains annotations that reference
745
750
annotations that are not present in the current document (see parameter removed_annotations).
746
751
752
+ Returns:
753
+ A mapping from annotation field names to the set of annotations from the original document for which
754
+ a new annotation was added to the current document. This can be useful to check if all original
755
+ annotations were added (possibly to multiple target documents).
756
+
747
757
Example:
748
758
```
749
759
@dataclasses.dataclass(frozen=True)
@@ -787,6 +797,7 @@ class TokenBasedDocumentWithEntitiesRelationsAndRelationAttributes(TokenBasedDoc
787
797
```
788
798
"""
789
799
removed_annotations = defaultdict (set , removed_annotations or dict ())
800
+ added_annotations = defaultdict (list )
790
801
791
802
annotation_store : Dict [str , Dict [int , Annotation ]] = defaultdict (dict )
792
803
named_annotation_fields = {field .name : field for field in self .annotation_fields ()}
@@ -829,6 +840,7 @@ class TokenBasedDocumentWithEntitiesRelationsAndRelationAttributes(TokenBasedDoc
829
840
if ann ._id != new_ann ._id :
830
841
annotation_store [field_name ][ann ._id ] = new_ann
831
842
self [field_name ].append (new_ann )
843
+ added_annotations [field_name ].append (ann )
832
844
else :
833
845
if strict :
834
846
raise ValueError (
@@ -853,6 +865,7 @@ class TokenBasedDocumentWithEntitiesRelationsAndRelationAttributes(TokenBasedDoc
853
865
if ann ._id != new_ann ._id :
854
866
annotation_store [field_name ][ann ._id ] = new_ann
855
867
self [field_name ].predictions .append (new_ann )
868
+ added_annotations [field_name ].append (ann )
856
869
else :
857
870
if strict :
858
871
raise ValueError (
@@ -868,6 +881,8 @@ class TokenBasedDocumentWithEntitiesRelationsAndRelationAttributes(TokenBasedDoc
868
881
# The annotation was removed, so we need to make sure that it is not referenced by any other
869
882
removed_annotations [field_name ].add (ann ._id )
870
883
884
+ return dict (added_annotations )
885
+
871
886
872
887
def resolve_annotation (
873
888
id_or_annotation : Union [int , Annotation ],
0 commit comments