Source code for iobes.convert

from itertools import chain
from typing import List, Tuple, Callable, Sequence, Optional
from iobes import TokenFunction, Span, Error
from iobes.parse import (
    parse_spans_iob_with_errors,
    parse_spans_bio_with_errors,
    parse_spans_iobes_with_errors,
    parse_spans_bilou_with_errors,
    parse_spans_bmeow_with_errors,
    ParseWithErrorsCallable,
)
from iobes.write import (
    write_iob_tags,
    write_bio_tags,
    write_iobes_tags,
    write_bilou_tags,
    write_bmeow_tags,
    WriteCallable,
)


[docs] def convert_tags( tags: Sequence[str], parse_function: ParseWithErrorsCallable, write_function: WriteCallable ) -> List[str]: """Convert tags from one format to another. Args: tags: The tags that we are converting. parse_function: A function that parses tags into spans. write_function: A function the turns spans into a list of tags. Raises: ValueError: If there were errors in the tag formatting. Returns: The list of tags in the new format. """ spans, errors = parse_function(tags) if errors: error_string = "\n".join(str(e) for e in errors) raise ValueError(f"Found errors in the tag sequence, cannot be converted. Errors: {error_string}") return write_function(spans, length=len(tags))
[docs] def iob_to_bio(tags: Sequence[str]) -> List[str]: """Convert IOB tags to the BIO format. Args: tags: The IOB tags we are converting Raises: ValueError: If there were errors in the IOB formatting of the input. Returns: Tags that produce the same spans in the BIO format. """ return convert_tags(tags, parse_spans_iob_with_errors, write_bio_tags)
[docs] def iob_to_iobes(tags: Sequence[str]) -> List[str]: """Convert IOB tags to the IOBES format. Args: tags: The IOB tags we are converting Raises: ValueError: If there were errors in the IOB formatting of the input. Returns: Tags that produce the same spans in the IOBES format. """ return bio_to_iobes(iob_to_bio(tags))
[docs] def iob_to_bilou(tags: Sequence[str]) -> List[str]: """Convert IOB tags to the BILOU format. Args: tags: The IOB tags we are converting Raises: ValueError: If there were errors in the IOB formatting of the input. Returns: Tags that produce the same spans in the BILOU format. """ return iobes_to_bilou(iob_to_iobes(tags))
[docs] def iob_to_bmeow(tags: Sequence[str]) -> List[str]: """Convert IOB tags to the BMEOW format. Args: tags: The IOB tags we are converting Raises: ValueError: If there were errors in the IOB formatting of the input. Returns: Tags that produce the same spans in the BMEOW format. """ return iobes_to_bmeow(iob_to_iobes(tags))
[docs] def iob_to_bmewo(tags: Sequence[str]) -> List[str]: """Convert IOB tags to the BMEWO format. Note: Alias for :py:func:`~iobes.convert.iob_to_bmeow`. Args: tags: The IOB tags we are converting Raises: ValueError: If there were errors in the IOB formatting of the input. Returns: Tags that produce the same spans in the BMEOW format. """ return iob_to_bmeow(tags)
[docs] def bio_to_iob(tags: Sequence[str]) -> List[str]: """Convert BIO tags to the IOB format. Args: tags: The BIO tags we are converting Raises: ValueError: If there were errors in the BIO formatting of the input. Returns: Tags that produce the same spans in the IOB format. """ return convert_tags(tags, parse_spans_bio_with_errors, write_iob_tags)
[docs] def bio_to_iobes(tags: Sequence[str]) -> List[str]: """Convert BIO tags to the IOBES format. Args: tags: The BIO tags we are converting Raises: ValueError: If there were errors in the BIO formatting of the input. Returns: Tags that produce the same spans in the IOBES format. """ return convert_tags(tags, parse_spans_bio_with_errors, write_iobes_tags)
[docs] def bio_to_bilou(tags: Sequence[str]) -> List[str]: """Convert BIO tags to the BILOU format. Args: tags: The BIO tags we are converting Raises: ValueError: If there were errors in the BIO formatting of the input. Returns: Tags that produce the same spans in the BILOU format. """ return iobes_to_bilou(bio_to_iobes(tags))
[docs] def bio_to_bmeow(tags: Sequence[str]) -> List[str]: """Convert BIO tags to the BMEOW format. Args: tags: The BIO tags we are converting Raises: ValueError: If there were errors in the BIO formatting of the input. Returns: Tags that produce the same spans in the BMEOW format. """ return iobes_to_bmeow(bio_to_iobes(tags))
[docs] def bio_to_bmewo(tags: Sequence[str]) -> List[str]: """Convert BIO tags to the BMEWO format. Note: Alias for :py:func:`~iobes.convert.bio_to_bmeow` Args: tags: The BIO tags we are converting Raises: ValueError: If there were errors in the BIO formatting of the input. Returns: Tags that produce the same spans in the BMEWO format. """ return bio_to_bmeow(tags)
[docs] def iobes_to_iob(tags: Sequence[str]) -> List[str]: """Convert IOBES tags to the IOB format. Args: tags: The IOBES tags we are converting Raises: ValueError: If there were errors in the IOBES formatting of the input. Returns: Tags that produce the same spans in the IOB format. """ return bio_to_iob(iobes_to_bio(tags))
[docs] def iobes_to_bio(tags: Sequence[str]) -> List[str]: """Convert IOBES tags to the BIO format. Args: tags: The IOBES tags we are converting Raises: ValueError: If there were errors in the IOBES formatting of the input. Returns: Tags that produce the same spans in the BIO format. """ return convert_tags(tags, parse_spans_iobes_with_errors, write_bio_tags)
[docs] def iobes_to_bilou(tags: Sequence[str]) -> List[str]: """Convert IOBES tags to the BILOU format. Args: tags: The IOBES tags we are converting Raises: ValueError: If there were errors in the IOBES formatting of the input. Returns: Tags that produce the same spans in the BILOU format. """ return convert_tags(tags, parse_spans_iobes_with_errors, write_bilou_tags)
[docs] def iobes_to_bmeow(tags: Sequence[str]) -> List[str]: """Convert IOBES tags to the BMEOW format. Args: tags: The IOBES tags we are converting Raises: ValueError: If there were errors in the IOBES formatting of the input. Returns: Tags that produce the same spans in the BMEOW format. """ return convert_tags(tags, parse_spans_iobes_with_errors, write_bmeow_tags)
[docs] def iobes_to_bmewo(tags: Sequence[str]) -> List[str]: """Convert IOBES tags to the BMEWO format. Note: Alias for :py:func:`~iobes.convert.iobes_to_bmeow` Args: tags: The IOBES tags we are converting Raises: ValueError: If there were errors in the IOBES formatting of the input. Returns: Tags that produce the same spans in the BMEWO format. """ return iobes_to_bmeow(tags)
[docs] def bilou_to_iob(tags: Sequence[str]) -> List[str]: """Convert BILOU tags to the IOB format. Args: tags: The BILOU tags we are converting Raises: ValueError: If there were errors in the BILOU formatting of the input. Returns: Tags that produce the same spans in the IOB format. """ return convert_tags(tags, parse_spans_bilou_with_errors, write_iob_tags)
[docs] def bilou_to_bio(tags: Sequence[str]) -> List[str]: """Convert BILOU tags to the BIO format. Args: tags: The BILOU tags we are converting Raises: ValueError: If there were errors in the BILOU formatting of the input. Returns: Tags that produce the same spans in the BIO format. """ return convert_tags(tags, parse_spans_bilou_with_errors, write_bio_tags)
[docs] def bilou_to_iobes(tags: Sequence[str]) -> List[str]: """Convert BILOU tags to the IOBES format. Args: tags: The BILOU tags we are converting Raises: ValueError: If there were errors in the BILOU formatting of the input. Returns: Tags that produce the same spans in the IOBES format. """ return convert_tags(tags, parse_spans_bilou_with_errors, write_iobes_tags)
[docs] def bilou_to_bmeow(tags: Sequence[str]) -> List[str]: """Convert BILOU tags to the BMEOW format. Args: tags: The BILOU tags we are converting Raises: ValueError: If there were errors in the BILOU formatting of the input. Returns: Tags that produce the same spans in the BMEOW format. """ return convert_tags(tags, parse_spans_bilou_with_errors, write_bmeow_tags)
[docs] def bilou_to_bmewo(tags: Sequence[str]) -> List[str]: """Convert BILOU tags to the BMEWO format. Note: Alias for :py:func:`~iobes.convert.bilou_to_bmeow` Args: tags: The BILOU tags we are converting Raises: ValueError: If there were errors in the BILOU formatting of the input. Returns: Tags that produce the same spans in the BMEWO format. """ return bilou_to_bmeow(tags)
[docs] def bmeow_to_iob(tags: Sequence[str]) -> List[str]: """Convert BMEOW tags to the IOB format. Args: tags: The BMEOW tags we are converting Raises: ValueError: If there were errors in the BMEOW formatting of the input. Returns: Tags that produce the same spans in the IOB format. """ return convert_tags(tags, parse_spans_bmeow_with_errors, write_iob_tags)
[docs] def bmeow_to_bio(tags: Sequence[str]) -> List[str]: """Convert BMEOW tags to the BIO format. Args: tags: The BMEOW tags we are converting Raises: ValueError: If there were errors in the BMEOW formatting of the input. Returns: Tags that produce the same spans in the BIO format. """ return convert_tags(tags, parse_spans_bmeow_with_errors, write_bio_tags)
[docs] def bmeow_to_iobes(tags: Sequence[str]) -> List[str]: """Convert BMEOW tags to the IOBES format. Args: tags: The BMEOW tags we are converting Raises: ValueError: If there were errors in the BMEOW formatting of the input. Returns: Tags that produce the same spans in the IOBES format. """ return convert_tags(tags, parse_spans_bmeow_with_errors, write_iobes_tags)
[docs] def bmeow_to_bilou(tags: Sequence[str]) -> List[str]: """Convert BMEOW tags to the BILOU format. Args: tags: The BMEOW tags we are converting Raises: ValueError: If there were errors in the BMEOW formatting of the input. Returns: Tags that produce the same spans in the BILOU format. """ return convert_tags(tags, parse_spans_bmeow_with_errors, write_bilou_tags)
[docs] def bmewo_to_iob(tags: Sequence[str]) -> List[str]: """Convert BMEWO tags to the IOB format. Note: Alias for :py:func:`~iobes.convert.bmeow_to_iob` Args: tags: The BMEWO tags we are converting Raises: ValueError: If there were errors in the BMEWO formatting of the input. Returns: Tags that produce the same spans in the IOB format. """ return bmeow_to_iob(tags)
[docs] def bmewo_to_bio(tags: Sequence[str]) -> List[str]: """Convert BMEWO tags to the BIO format. Note: Alias for :py:func:`~iobes.convert.bmeow_to_bio` Args: tags: The BMEWO tags we are converting Raises: ValueError: If there were errors in the BMEWO formatting of the input. Returns: Tags that produce the same spans in the BIO format. """ return bmeow_to_bio(tags)
[docs] def bmewo_to_iobes(tags: Sequence[str]) -> List[str]: """Convert BMEWO tags to the IOBES format. Note: Alias for :py:func:`~iobes.convert.bmeow_to_iobes` Args: tags: The BMEWO tags we are converting Raises: ValueError: If there were errors in the BMEWO formatting of the input. Returns: Tags that produce the same spans in the IOBES format. """ return bmeow_to_iobes(tags)
[docs] def bmewo_to_bilou(tags: Sequence[str]) -> List[str]: """Convert BMEWO tags to the BILOU format. Note: Alias for :py:func:`~iobes.convert.bmeow_to_bilou` Args: tags: The BMEWO tags we are converting Raises: ValueError: If there were errors in the BMEWO formatting of the input. Returns: Tags that produce the same spans in the BILOU format. """ return bmeow_to_bilou(tags)