Skip to content

annotation

autocorpus.ac_bioc.annotation ¤

This module defines the BioCAnnotation class.

Classes¤

BioCAnnotation(id=str(), text=str(), offset=int(), length=int(), infons=dict(), locations=list()) dataclass ¤

Represents an annotation in a BioC document.

Functions¤
from_dict(data) classmethod ¤

Create a BioCAnnotation instance from a dictionary.

Parameters:

Name Type Description Default
data dict[str, Any]

A dictionary containing annotation data.

required

Returns:

Name Type Description
BioCAnnotation BioCAnnotation

An instance of BioCAnnotation created from the dictionary.

Source code in autocorpus/ac_bioc/annotation.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_dict(cls, data: dict[str, Any]) -> BioCAnnotation:
    """Create a BioCAnnotation instance from a dictionary.

    Args:
        data (dict[str, Any]): A dictionary containing annotation data.

    Returns:
        BioCAnnotation: An instance of BioCAnnotation created from the dictionary.
    """
    from .location import BioCLocation  # Prevent circular imports

    locations = [BioCLocation.from_dict(loc) for loc in data.get("locations", [])]

    return cls(
        id=data["id"],
        text=data.get("text", ""),
        offset=data.get("offset", 0),
        length=data.get("length", 0),
        infons=data.get("infons", {}),
        locations=locations,
    )
from_xml(elem) classmethod ¤

Create a BioCAnnotation instance from an XML element.

Parameters:

Name Type Description Default
elem Element

An XML element representing the annotation.

required

Returns:

Name Type Description
BioCAnnotation BioCAnnotation

An instance of BioCAnnotation created from the XML element.

Source code in autocorpus/ac_bioc/annotation.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@classmethod
def from_xml(cls, elem: ET.Element) -> BioCAnnotation:
    """Create a BioCAnnotation instance from an XML element.

    Args:
        elem (ET.Element): An XML element representing the annotation.

    Returns:
        BioCAnnotation: An instance of BioCAnnotation created from the XML element.
    """
    from .location import BioCLocation  # Again, avoid circular imports

    id_ = elem.attrib.get("id", "")
    text = elem.findtext("text", default="")

    infons = {
        e.attrib["key"]: e.text for e in elem.findall("infon") if e.text is not None
    }

    locations = [
        BioCLocation.from_xml(loc_elem) for loc_elem in elem.findall("location")
    ]

    # Offset and length can be derived if needed from locations
    offset = int(locations[0].offset) if locations else 0
    length = int(locations[0].length) if locations else 0

    return cls(
        id=id_,
        text=text,
        offset=offset,
        length=length,
        infons=infons,
        locations=locations,
    )
to_json() ¤

Convert the annotation to a JSON-serializable dictionary.

Returns:

Type Description
dict[str, Any]

dict[str, Any]: A dictionary containing the annotation's id, text, offset, length, infons, and locations.

Source code in autocorpus/ac_bioc/annotation.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def to_json(self) -> dict[str, Any]:
    """Convert the annotation to a JSON-serializable dictionary.

    Returns:
        dict[str, Any]: A dictionary containing the annotation's id, text, offset, length, infons, and locations.
    """
    return {
        "id": self.id,
        "text": self.text,
        "offset": self.offset,
        "length": self.length,
        "infons": self.infons,
        "locations": [loc.to_dict() for loc in self.locations],
    }
to_xml() ¤

Convert the annotation to an XML element.

Returns:

Type Description
Element

ET.Element: An XML element representing the annotation.

Source code in autocorpus/ac_bioc/annotation.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def to_xml(self) -> ET.Element:
    """Convert the annotation to an XML element.

    Returns:
        ET.Element: An XML element representing the annotation.
    """
    annotation_elem = ET.Element("annotation")
    annotation_elem.set("id", self.id)

    for k, v in self.infons.items():
        infon = ET.SubElement(annotation_elem, "infon", {"key": k})
        infon.text = v

    for loc in self.locations:
        annotation_elem.append(loc.to_xml())

    text_elem = ET.SubElement(annotation_elem, "text")
    text_elem.text = self.text

    return annotation_elem