import re
import json


def transform_dstc8_to_tsv(path):
    """
    Transforms dstc8 json format to conversation response ranking tsv file.

    See https://github.com/dstc8-track2/NOESIS-II/ for more details of the input format.
    The output format is label \t utterance_1 \t utterance_2 \t ...... \t candidate_response.
    Since we do the negative sampling ourselves, we do not get the negative samples from the
    tsv files, and only read lines with label = 1.

    Args:
        path: str with the path for the json file.

    Returns: list with the tsv lines.
    """
    def remove_participant(utterance):
        no_participant = re.sub(r'participant_\d+ : ', '', utterance)
        no_participant = re.sub(r'participant_\d+', '', no_participant)
        return no_participant

    tsv_only_relevant = []
    with open(path) as json_file:
        data = json.load(json_file)
        for example in data:
            if len(example["options-for-correct-answers"])>0:
                tsv_instance = "1\t" + "\t".join([remove_participant(e["utterance"]) for e in example["messages-so-far"]])+"\t"+ \
                    remove_participant(example["options-for-correct-answers"][0]["utterance"]) + "\n"
                tsv_only_relevant.append(tsv_instance)
    return tsv_only_relevant