import json
import re

def create_cn_space(path):
    '''
    creates a dictionary that assigns index (dimension) to a CN
    :param path: path of the Country Map data
    :return:
    '''
    f = open(path, 'r')
    f.readline()
    space = {}
    i = 0
    for l in f:
        space[l[:2]] = i
        i += 1

    print(space)
    f.close()
    f = open('cn_space.txt', 'w')
    json.dump(space, f)
    f.close()
    return space

def upper_dict():
    dic = {}
    for i in range(26):
        dic[chr(i + 65)] = i
    return dic

def curated_inputs_vector_cn(path, space_cn, space_name):
    '''

    :param path: path to curated_inputs
    :param space_cn: dictionary of the cn_space as generated by create_cn_space
    :param space_name: dictionary of the chars being read
    :return: the vectorized inputs
    '''
    f = open(path, 'r')
    cur = json.load(f)
    f.close()
    cv = []
    for e in cur:

        cn = [0 for i in range(len(space_cn))]
        '''
        if e[2] in space_cn:
            cn[space_cn[e[1]]] = 1
        else:
            for k, va in space_cn.keys():
                if e[2][0] == k[0] or e[2][1] == k[1]:
                    cn[va] = 0.5

        '''
        cname = e[1].upper()
        for c in cname:
            if c in space_name.keys():
                cn[space_name[c]] += 1

        name = e[0].upper()
        nv = [0 for i in range(len(space_name))]
        for c in name:
            if c in space_name.keys():
                nv[space_name[c]] += 1

        cv.append((nv, cn))

    f = open('vectorized.txt', 'w')
    json.dump(cv, f)
    f.close()
    return cv

create_cn_space('Problem 3 Input Data - Country Map.txt')
dic = upper_dict()
curated_inputs_vector_cn('Problem 3 Input Datacurated_input.txt', dic, dic)