from typing import List, Tuple, Dict, Any from bitarray import bitarray from src.suppressors.interface import Suppressor class OrderBasedSuppressor(Suppressor): def __init__(self) -> None: super().__init__() def suppress(self, annotations: List[Tuple[int, int, Any]]) -> List[Tuple[int, int, Any]]: return suppress_order_based(annotations) def suppress_order_based(annotations: List[Tuple[int, int, Any]]) -> List[Tuple[int, int, Any]]: """If two annotations overlap, the first one int the list is kept. Args: annotations (List[Tuple[int, int, Any]]): List of annotations. Returns: List[Tuple[int, int, Any]]: List of annotations with overlapping annotations removed. """ if len(annotations) == 0: return annotations annotations = annotations bitarray_size = max([end for _, end, _ in annotations]) bitarray_ = bitarray(bitarray_size) bitarray_.setall(False) result = [] for start, end, entity_type in annotations: if not bitarray_[start:end].any(): bitarray_[start:end] = True result.append((start, end, entity_type)) return result