from typing import Dict
from typing import Any


def release2prediction(example: Dict[str, Any], load_gold: bool = False):
    """
    Convert the SLURP release format into prediction format.

    :param example: the example in release format\
            (as they come with the dataset release)
    :param load_gold: When evaluating gold hypotheses upper bound
    :return: a list of examples in prediction format: List[Dict[str, Union[str, List]]]
    """
    result = {}
    res = {
        "text": " ".join([t["surface"] for t in example["tokens"]]),
        "scenario": example["scenario"],
        "action": example["action"],
        "entities": [
            {
                "type": entity["type"],
                "filler": " ".join(
                    [example["tokens"][i]["surface"].lower() for i in entity["span"]]
                ),
            }
            for entity in example["entities"]
        ],
    }
    if load_gold:
        result[str(example["slurp_id"])] = res
    else:
        for file in example["recordings"]:
            result[file["file"]] = res
    return result