import csv


def gete2wlandw2el(datafile):
    """Get dict inputs for the EM algorithm.

    Parameters
    ----------
    datafile: str
        path to datafile. The data file itself must be a csv file with
        3 columns: question, worker, answer
        and the first row is the column names.

    Returns
    -------
    dict
        Indexed by question
        Each value is a list of workers and labels assigned by them.
        For example, e2wl['1'][3] -> ['4', '3']
        means that questoin '1', was answered as '3' by worker '4'
    dict
        Indexed by worker name
        Each value is a list of questions and labels assigned by the worker.
        For example, w2el['4'][0] -> ['1', '3']
        means that worker '4', when asked question '1', assigned the label '3'.
    list
        list of unique labels in the dataset

    """
    e2wl = {}
    w2el = {}
    label_set = []

    f = open(datafile, 'r')
    reader = csv.reader(f)
    next(reader)

    for line in reader:
        example, worker, label = line
        if example not in e2wl:
            e2wl[example] = []
        e2wl[example].append([worker, label])

        if worker not in w2el:
            w2el[worker] = []
        w2el[worker].append([example, label])

        if label not in label_set:
            label_set.append(label)

    return e2wl, w2el, label_set