medkit.io._brat_utils
=====================

.. py:module:: medkit.io._brat_utils


Attributes
----------

.. autoapisummary::

   medkit.io._brat_utils.GROUPING_ENTITIES
   medkit.io._brat_utils.GROUPING_RELATIONS
   medkit.io._brat_utils.logger


Classes
-------

.. autoapisummary::

   medkit.io._brat_utils.BratEntity
   medkit.io._brat_utils.BratRelation
   medkit.io._brat_utils.BratAttribute
   medkit.io._brat_utils.BratNote
   medkit.io._brat_utils.Grouping
   medkit.io._brat_utils.BratAugmentedEntity
   medkit.io._brat_utils.BratDocument
   medkit.io._brat_utils.RelationConf
   medkit.io._brat_utils.AttributeConf
   medkit.io._brat_utils.BratAnnConfiguration


Functions
---------

.. autoapisummary::

   medkit.io._brat_utils.ensure_attr_value
   medkit.io._brat_utils.parse_file
   medkit.io._brat_utils.parse_string
   medkit.io._brat_utils._parse_entity
   medkit.io._brat_utils._parse_relation
   medkit.io._brat_utils._parse_attribute
   medkit.io._brat_utils._parse_note


Module Contents
---------------

.. py:data:: GROUPING_ENTITIES

.. py:data:: GROUPING_RELATIONS

.. py:data:: logger

.. py:class:: BratEntity

   
   A simple entity annotation data structure.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: uid
      :type:  str


   .. py:attribute:: type
      :type:  str


   .. py:attribute:: span
      :type:  list[tuple[int, int]]


   .. py:attribute:: text
      :type:  str


   .. py:property:: start
      :type: int



   .. py:property:: end
      :type: int



   .. py:method:: to_str() -> str


.. py:class:: BratRelation

   
   A simple relation data structure.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: uid
      :type:  str


   .. py:attribute:: type
      :type:  str


   .. py:attribute:: subj
      :type:  str


   .. py:attribute:: obj
      :type:  str


   .. py:method:: to_str() -> str


.. py:class:: BratAttribute

   
   A simple attribute data structure.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: uid
      :type:  str


   .. py:attribute:: type
      :type:  str


   .. py:attribute:: target
      :type:  str


   .. py:attribute:: value
      :type:  str
      :value: None



   .. py:method:: to_str() -> str


.. py:class:: BratNote

   
   A simple note data structure.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: uid
      :type:  str


   .. py:attribute:: target
      :type:  str


   .. py:attribute:: value
      :type:  str


   .. py:attribute:: type
      :type:  str
      :value: 'AnnotatorNotes'



   .. py:method:: to_str() -> str


.. py:function:: ensure_attr_value(attr_value: Any) -> str

   
   Ensure that the attribue value is a string.
















   ..
       !! processed by numpydoc !!

.. py:class:: Grouping

   
   A grouping data structure for entities of type And-Group and Or-Group.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: uid
      :type:  str


   .. py:attribute:: type
      :type:  str


   .. py:attribute:: items
      :type:  list[BratEntity]


   .. py:property:: text


.. py:class:: BratAugmentedEntity

   
   An augmented entity data structure with its relations and attributes.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: uid
      :type:  str


   .. py:attribute:: type
      :type:  str


   .. py:attribute:: span
      :type:  tuple[tuple[int, int], Ellipsis]


   .. py:attribute:: text
      :type:  str


   .. py:attribute:: relations_from_me
      :type:  tuple[BratRelation, Ellipsis]


   .. py:attribute:: relations_to_me
      :type:  tuple[BratRelation, Ellipsis]


   .. py:attribute:: attributes
      :type:  tuple[BratAttribute, Ellipsis]


   .. py:property:: start
      :type: int



   .. py:property:: end
      :type: int



.. py:class:: BratDocument

   .. py:attribute:: entities
      :type:  dict[str, BratEntity]


   .. py:attribute:: relations
      :type:  dict[str, BratRelation]


   .. py:attribute:: attributes
      :type:  dict[str, BratAttribute]


   .. py:attribute:: notes
      :type:  dict[str, BratNote]


   .. py:attribute:: groups
      :type:  dict[str, Grouping]
      :value: None



   .. py:method:: get_augmented_entities() -> dict[str, BratAugmentedEntity]


.. py:class:: RelationConf

   Bases: :py:obj:`NamedTuple`


   
   Configuration data structure of a BratRelation.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: type
      :type:  str


   .. py:attribute:: arg1
      :type:  str


   .. py:attribute:: arg2
      :type:  str


.. py:class:: AttributeConf

   Bases: :py:obj:`NamedTuple`


   
   Configuration data structure of a BratAttribure.
















   ..
       !! processed by numpydoc !!

   .. py:attribute:: from_entity
      :type:  bool


   .. py:attribute:: type
      :type:  str


   .. py:attribute:: value
      :type:  str


.. py:class:: BratAnnConfiguration(top_values_by_attr: int = 50)

   
   A data structure to represent 'annotation.conf' in brat documents.

   This is necessary to generate a valid annotation project in brat.
   An 'annotation.conf' has four sections. The section 'events' is not
   supported in medkit, so the section is empty.















   ..
       !! processed by numpydoc !!

   .. py:attribute:: _entity_types
      :type:  set[str]


   .. py:attribute:: _rel_types_arg_1
      :type:  dict[str, set[str]]


   .. py:attribute:: _rel_types_arg_2
      :type:  dict[str, set[str]]


   .. py:attribute:: _attr_entity_values
      :type:  dict[str, list[str]]


   .. py:attribute:: _attr_relation_values
      :type:  dict[str, list[str]]


   .. py:attribute:: top_values_by_attr
      :value: 50



   .. py:property:: entity_types
      :type: list[str]



   .. py:property:: rel_types_arg_1
      :type: dict[str, list[str]]



   .. py:property:: rel_types_arg_2
      :type: dict[str, list[str]]



   .. py:property:: attr_relation_values
      :type: dict[str, list[str]]



   .. py:property:: attr_entity_values
      :type: dict[str, list[str]]



   .. py:method:: add_entity_type(type: str)


   .. py:method:: add_relation_type(relation_conf: RelationConf)


   .. py:method:: add_attribute_type(attr_conf: AttributeConf)


   .. py:method:: to_str() -> str


   .. py:method:: _attribute_to_str(type: str, values: list[str], from_entity: bool) -> str
      :staticmethod:



   .. py:method:: _relation_to_str(type: str, arg_1_types: list[str], arg_2_types: list[str]) -> str
      :staticmethod:



.. py:function:: parse_file(ann_path: str | pathlib.Path, detect_groups: bool = False) -> BratDocument

   
   Read an annotation file to get the Entities, Relations and Attributes in it.

   All other lines are ignored.

   :Parameters:

       **ann_path** : str or Path
           The path to the annotation file to be processed.

       **detect_groups** : bool, default=False
           If set to True, the function will also parse the group of entities according
           to some specific keywords.
           By default, it is set to False.



   :Returns:

       Document
           The dataclass object containing entities, relations and attributes











   ..
       !! processed by numpydoc !!

.. py:function:: parse_string(ann_string: str, detect_groups: bool = False) -> BratDocument

   
   Read a string containing all annotations and extract Entities, Relations and Attributes.

   All other lines are ignored.

   :Parameters:

       **ann_string** : str
           The string containing all brat annotations

       **detect_groups** : bool, default=False
           If set to True, the function will also parse the group of entities according
           to some specific keywords.
           By default, it is set to False.



   :Returns:

       Document
           The dataclass object containing entities, relations and attributes











   ..
       !! processed by numpydoc !!

.. py:function:: _parse_entity(entity_id: str, entity_content: str) -> BratEntity

   
   Parse the brat entity string into an Entity structure.


   :Parameters:

       **entity_id** : str
           The ID defined in the brat annotation (e.g., 'T12')

       **entity_content** : str
           The string content for this ID to parse
            (e.g., 'Temporal-Modifier 116 126\thistory of')



   :Returns:

       BratEntity
           The dataclass object representing the entity




   :Raises:

       ValueError
           Raises when the entity can't be parsed







   ..
       !! processed by numpydoc !!

.. py:function:: _parse_relation(relation_id: str, relation_content: str) -> BratRelation

   
   Parse the annotation string into a Relation structure.


   :Parameters:

       **relation_id** : str
           The ID defined in the brat annotation (e.g., 'R12')

       **relation_content** : str
           The relation text content. (e.g., 'Modified-By Arg1:T8 Arg2:T6\t')



   :Returns:

       BratRelation
           The dataclass object representing the relation




   :Raises:

       ValueError
           Raises when the relation can't be parsed







   ..
       !! processed by numpydoc !!

.. py:function:: _parse_attribute(attribute_id: str, attribute_content: str) -> BratAttribute

   
   Parse the annotation string into an Attribute structure.


   :Parameters:

       **attribute_id** : str
           The attribute ID defined in the annotation. (e.g., 'A1')

       **attribute_content** : str
           The attribute text content. (e.g., 'Tense T19 Past-Ended')



   :Returns:

       BratAttribute
           The dataclass object representing the attribute




   :Raises:

       ValueError
           Raises when the attribute can't be parsed







   ..
       !! processed by numpydoc !!

.. py:function:: _parse_note(note_id: str, note_content: str) -> BratNote

   
   Parse the annotation string into an Note structure.


   :Parameters:

       **note_id** : str
           The note ID defined in the annotation. (e.g., '#1')

       **note_content** : str
           The note text content. (e.g., 'AnnotatorNotes T10       C0011849')



   :Returns:

       BratNote
           The dataclass object representing the note




   :Raises:

       ValueError
           Raises when the note can't be parsed







   ..
       !! processed by numpydoc !!

