Skip to content

location

autocorpus.ac_bioc.location ¤

This module defines the BioCLocation class.

It provides methods for converting between dictionary, XML, and object representations.

Classes¤

BioCLocation(offset=int(), length=int()) dataclass ¤

Represents a location in BioC format.

Functions¤
from_dict(data) classmethod ¤

Create a BioCLocation instance from a dictionary.

Parameters:

Name Type Description Default
cls type

The class to instantiate.

required
data dict[str, int]

A dictionary containing the 'offset' and 'length' keys. If the keys are not present, default values of 0 will be used.

required

Returns:

Name Type Description
BioCLocation BioCLocation

An instance of BioCLocation created from the dictionary.

Source code in autocorpus/ac_bioc/location.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@classmethod
def from_dict(cls, data: dict[str, int]) -> BioCLocation:
    """Create a BioCLocation instance from a dictionary.

    Args:
        cls (type): The class to instantiate.
        data (dict[str, int]): A dictionary containing the 'offset' and 'length' keys.
            If the keys are not present, default values of 0 will be used.

    Returns:
        BioCLocation: An instance of BioCLocation created from the dictionary.
    """
    return cls(
        offset=data.get("offset", 0),
        length=data.get("length", 0),
    )
from_xml(elem) classmethod ¤

Create a BioCLocation instance from an XML element.

Parameters:

Name Type Description Default
elem Element

An XML element with 'offset' and 'length' attributes. The attributes will be converted to integers.

required

Returns:

Type Description
BioCLocation

BioCLocation An instance of BioCLocation created from the XML element. If the attributes are not present, default values of 0 will be used.

Source code in autocorpus/ac_bioc/location.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@classmethod
def from_xml(cls, elem: ET.Element) -> BioCLocation:
    """Create a BioCLocation instance from an XML element.

    Args:
        elem (ET.Element): An XML element with 'offset' and 'length' attributes.
            The attributes will be converted to integers.

    Returns:
        BioCLocation
            An instance of BioCLocation created from the XML element.
            If the attributes are not present, default values of 0 will be used.
    """
    offset = int(elem.attrib.get("offset", 0))
    length = int(elem.attrib.get("length", 0))
    return cls(offset=offset, length=length)
to_xml() ¤

Convert the BioCLocation instance to an XML element.

Returns:

Type Description
Element

ET.Element An XML element representation of the BioCLocation instance. The element will have 'offset' and 'length' attributes.

Source code in autocorpus/ac_bioc/location.py
44
45
46
47
48
49
50
51
52
53
54
55
def to_xml(self) -> ET.Element:
    """Convert the BioCLocation instance to an XML element.

    Returns:
        ET.Element
            An XML element representation of the BioCLocation instance.
            The element will have 'offset' and 'length' attributes.
    """
    elem = ET.Element("location")
    elem.set("offset", str(self.offset))
    elem.set("length", str(self.length))
    return elem