import json
import os


def json_to_lists(xs):
    return [[int(i) for i in x] for x in xs]

def remove_labels(xs, blacklist):
    return [[int(i) for i in x if i not in blacklist] for x in xs]

def get_labels(xs, labels):
    mp = dict(zip(labels, range(len(labels))))
    def tr(x): 
        i = next((y for y in labels if y in x), None)
        return 0 if i == None else i

    return [ tr(x) for x in xs]

def write_dat(name, xss):
    print("\n".join([" ".join([str(i) for i in xs])
                     for xs in xss]), file=open(name, 'w+'))

def write_labels(name, labels):
    print("\n".join([" ".join([str(i) for i in labels])]), file=open(name, 'w+'))
                     

json_path = '../datasets'
outdir = './datasets/'

for name in os.listdir(json_path):
    print(name)
    fn = json_path + "" + name
    if not os.path.isfile(fn) or not fn.endswith(".json"):
        continue
    with open(fn, 'r') as f:
        array = json.load(f)
        dat = []
        if "label" in array:
            y = array["label"]
            x = json_to_lists(array["data"])
            x = remove_labels(x, y)
            write_dat(outdir + name.replace(".json", ".dat"), x)
            write_labels(outdir + name.replace(".json", ".labels"), y)
        elif "included_class_labels" in array:
            labels = array["included_class_labels"]
            dat = json_to_lists(array["data"])
            y = get_labels(dat, labels)
            x = remove_labels(dat, labels)
            write_dat(outdir + name.replace(".json", ".dat"), x)
            write_labels(outdir + name.replace(".json", ".labels"), y)
        else:
            x = json_to_lists(array["data"])
            write_dat(outdir + name.replace(".json", ".dat"), x)
