mtap.serialization

Serialization

MTAP input/output and serialization

class mtap.serialization.SerializationProcessor(serializer: Serializer, output_dir: str | bytes | PathLike, include_label_text: bool = False)[source]

An MTAP EventProcessor that serializes events to a specific directory.

serializer

The serializer to use.

Type:

Serializer

output_dir

The output directory.

Type:

str | bytes | os.PathLike

include_label_text

Whether to attach the covered text to labels.

Type:

bool

class mtap.serialization.Serializer[source]

Abstract base class for a serializer of MTAP events.

classmethod extension() str[source]

str: Filename extension, including period. Ex: '.json'.

abstract classmethod event_to_file(event: Event, f: str | bytes | PathLike | IO, *, include_label_text: bool = False)[source]

Writes the event to a file.

Parameters:
  • event – The event object to serialize.

  • f – A file or a path to a file to write the event to.

  • include_label_text – Whether, when serializing, to include the text

  • label. (that each label covers with the rest of the) –

abstract classmethod file_to_event(f: str | bytes | PathLike | IO, *, client: EventsClient | None = None) Event[source]

Loads an event from a serialized file.

Parameters:
  • f – The file to load from.

  • client – The events service to load the event into.

Returns:

The loaded event object.

Built-In Serializers

mtap.serialization.JsonSerializer[source]

Serializer implementation that performs serialization to JSON.

mtap.serialization.YamlSerializer[source]

Serializer implementation that performs serialization to YAML.

mtap.serialization.PickleSerializer[source]

Serializer implementation that performs serialization to .pickle.

Helpers

mtap.serialization.event_to_dict(event: Event, *, include_label_text: bool = False, include_binaries: bool = False) Dict[source]

A helper method that turns an event into a python dictionary.

Parameters:
  • event – The event object.

  • include_label_text – Whether to include the text labels cover with the labels.

  • include_binaries – Whether to serialize and include the event’s binaries.

Returns:

A dictionary object suitable for serialization.

Return type:

dict

mtap.serialization.dict_to_event(d: Dict, *, client: EventsClient | None = None) Event[source]

Turns a serialized dictionary into an Event.

Parameters:
  • d – A json-like (only str keys) dictionary representation of the event.

  • client – If specified, will upload the event to this events service. Otherwise, creates a local event.

Returns:

The deserialized event object.

mtap.serialization.document_to_dict(document: Document, *, include_label_text: bool = False) Dict[source]

A helper method that turns a document into a python dictionary.

Parameters:
  • document – The document object.

  • include_label_text – Whether to include the text labels cover with the labels.

Returns:

A dictionary object suitable for serialization.

mtap.serialization.dict_to_document(document_name: str, d: Dict, *, event: Event | None = None) Document[source]

Turns a serialized dictionary into a Document.

Parameters:
  • document_name – The name identifier of the document.

  • d – A json-like (only str keys) dictionary representation of the document.

  • event – If specified, the function will add the document to this event. Otherwise, it will create a stand-alone document.

Returns:

The deserialized Document object.