Skip to content

collection

autocorpus.ac_bioc.collection ¤

This module defines the BioCCollection class.

BioCCollection extends BioCCollection to include a list of BioCDocument objects and provides a method to convert the collection to a dictionary representation.

Classes¤

BioCCollection(source=str(), date=str(), key=str(), documents=list(), infons=dict()) dataclass ¤

Bases: DataClassJsonMixin

A class representing a BioC collection.

Functions¤
from_xml(elem) classmethod ¤

Create a BioCCollection instance from an XML element.

Parameters:

Name Type Description Default
elem Element

The XML element representing a BioCCollection.

required

Returns:

Name Type Description
BioCCollection BioCCollection

An instance of BioCCollection created from the XML element.

Source code in autocorpus/ac_bioc/collection.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@classmethod
def from_xml(cls, elem: ET.Element) -> BioCCollection:
    """Create a BioCCollection instance from an XML element.

    Args:
        elem (ET.Element): The XML element representing a BioCCollection.

    Returns:
        BioCCollection: An instance of BioCCollection created from the XML element.
    """
    source = elem.findtext("source", default="")
    date = elem.findtext("date", default="")
    key = elem.findtext("key", default="")

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

    documents = [
        BioCDocument.from_xml(doc_elem) for doc_elem in elem.findall("document")
    ]

    return cls(
        source=source, date=date, key=key, infons=infons, documents=documents
    )
to_xml() ¤

Convert the BioCCollection instance to an XML element.

Returns:

Type Description
Element

ET.Element: An XML element representation of the BioCCollection instance.

Source code in autocorpus/ac_bioc/collection.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def to_xml(self) -> ET.Element:
    """Convert the BioCCollection instance to an XML element.

    Returns:
        ET.Element: An XML element representation of the BioCCollection instance.
    """
    collection_elem = ET.Element("collection")

    if self.source:
        source_elem = ET.SubElement(collection_elem, "source")
        source_elem.text = self.source

    if self.date:
        date_elem = ET.SubElement(collection_elem, "date")
        date_elem.text = self.date

    if self.key:
        key_elem = ET.SubElement(collection_elem, "key")
        key_elem.text = self.key

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

    for doc in self.documents:
        collection_elem.append(doc.to_xml())

    return collection_elem