from pathlib import Path


def get_label(iemocap_dir):
    """Get labels for every utterance under the form "utt_name:label".

    Parameters
    ----------
    iemocap_dir : str
        Path to the `IEMOCAP_full_release` directory.

    Returns
    -------
    dict
        Dictionary of pairs utt_name:label
        (e.g., 'Ses01F_impro01_F000': 'neu').

    """
    emo_dict = dict()

    def get_info(line):
        line = line.split()
        name = line[3]
        label = line[4]
        # Convert "exc" (excited) to "hap" (happy)
        label = "hap" if label == "exc" else label
        return name, label

    for filepath in Path(iemocap_dir).rglob("EmoEvaluation/*.txt"):
        with open(filepath, "r") as fin:
            lines = fin.readlines()

        lines = filter(lambda x: x[0] == "[", lines)  # filter lines with label
        emo_dict.update(dict(map(get_info, lines)))
    return emo_dict