# Path: kraken/lib/exceptions.py
# class KrakenEncodeException(Exception):
# 
#     def __init__(self, message=None):
#         Exception.__init__(self, message)
# 
# class KrakenCodecException(Exception):
# 
#     def __init__(self, message=None):
#         Exception.__init__(self, message)


import logging
import numpy as np
from collections import Counter
from typing import List, Tuple, Set, Union, Dict, Sequence
from torch import IntTensor
from kraken.lib.exceptions import KrakenEncodeException, KrakenCodecException
        cuts and confidences aggregated across label-code point
        correspondences. When decoding multilabels to code points the resulting
        cuts are min/max, confidences are averaged.

        Args:
            labels: Input containing tuples (label, start, end,
                           confidence).

        Returns:
            A list of tuples (code point, start, end, confidence)
        """
        start = [x for _, x, _, _ in labels]
        end = [x for _, _, x, _ in labels]
        con = [x for _, _, _, x in labels]
        labels = tuple(x for x, _, _, _ in labels)
        decoded = []
        idx = 0
        while idx < len(labels):
            decodable_suffix = False
            for code in self.l2c.keys():
                if code == labels[idx:idx+len(code)]:
                    decoded.extend([(c, s, e, u) for c, s, e, u in zip(self.l2c[code],
                                                                       len(self.l2c[code]) * [start[idx]],
                                                                       len(self.l2c[code]) * [end[idx + len(code) - 1]],
                                                                       len(self.l2c[code]) * [np.mean(con[idx:idx + len(code)])])])
                    idx += len(code)
                    decodable_suffix = True
                    break
            if not decodable_suffix:
                if self.strict:

# Path: kraken/lib/exceptions.py
# class KrakenEncodeException(Exception):
# 
#     def __init__(self, message=None):
#         Exception.__init__(self, message)
# 
# class KrakenCodecException(Exception):
# 
#     def __init__(self, message=None):
#         Exception.__init__(self, message)


# Path: kraken/lib/codec.py
import logging
import numpy as np
from collections import Counter
from typing import List, Tuple, Set, Union, Dict, Sequence
from torch import IntTensor
from kraken.lib.exceptions import KrakenEncodeException, KrakenCodecException

        cuts and confidences aggregated across label-code point
        correspondences. When decoding multilabels to code points the resulting
        cuts are min/max, confidences are averaged.

        Args:
            labels: Input containing tuples (label, start, end,
                           confidence).

        Returns:
            A list of tuples (code point, start, end, confidence)
        """
        start = [x for _, x, _, _ in labels]
        end = [x for _, _, x, _ in labels]
        con = [x for _, _, _, x in labels]
        labels = tuple(x for x, _, _, _ in labels)
        decoded = []
        idx = 0
        while idx < len(labels):
            decodable_suffix = False
            for code in self.l2c.keys():
                if code == labels[idx:idx+len(code)]:
                    decoded.extend([(c, s, e, u) for c, s, e, u in zip(self.l2c[code],
                                                                       len(self.l2c[code]) * [start[idx]],
                                                                       len(self.l2c[code]) * [end[idx + len(code) - 1]],
                                                                       len(self.l2c[code]) * [np.mean(con[idx:idx + len(code)])])])
                    idx += len(code)
                    decodable_suffix = True
                    break
            if not decodable_suffix:
                if self.strict:

                    raise KrakenEncodeException(f'Non-decodable sequence {labels[idx:idx+5]}... encountered.')