import random
import codecs
import operator

unique_names = False


def main(config):
    global unique_names    
    path = config['path']
    corpus = config['corpus']

    use_atf = config['flags']['use_atf']
    
    train_key = path + 'target_train.KEY'
    train_target = path + 'target_train.RT'
    dev_key = path + 'target_dev.KEY'
    dev_target = path + 'target_dev.RT'
    dev_pred = path + 'dev_prediction.RT'

    atf_key = path + 'target_atf.KEY'
    atf_pred = path + 'atf_prediction.RT'

    flags = config['flags']
    if flags['disjoint_names'] == True:
        uniqueNames = True
        print("Using disjoint name sets for train / dev.")
    else:
        uniqueNames = False

    

    train_names = set()
    dev_names = set()
    pred_names = set()
    dev_output = []
    
    atf_names = set()
    atf_output = []
    
    
    total_train_names = addNames(train_key, train_target, train_names)
    total_dev_names = add_names_and_unique(dev_key, dev_target, dev_names, train_names, dev_output)
    total_pred_names = addNames(dev_key, dev_pred, pred_names)

    total_atf_names = None
    if use_atf:
        total_atf_names = add_names_and_unique(atf_key, atf_pred,
                                               atf_names, train_names, atf_output, 2)

    dev_unique_names = dev_names - train_names
    new_names = pred_names - train_names    
    atf_unique_names = None
    if use_atf:
         atf_unique_names = atf_names - train_names

    print ("Training found %d names." % len(train_names))

    if not use_atf:
        print("dev found %d names." % len(dev_names))
        print("Unique names in dev: %d" % (len(new_names)))
        print("dev name occurrences: %d" % total_dev_names)
        print("dev unique name occurrences: %d - %.3f%%" % (len(dev_output), 100 * len(dev_output) / total_dev_names))
        
    # Exit early when looking at ATF names.             
    if use_atf:
        print("Names found in atf: %d" % len(atf_names))
        print("Unique names found in atf: %d" % len(atf_unique_names))
        print("Total name occurrences in ATF: %d" % total_atf_names)
        print("Unique name occurrences in ATF: %d - %.3f%%" % (len(atf_output), 100 * len(atf_output) /
                                                               total_atf_names ))
        outputATF(config, atf_output)
        return


        
    dev_key = open(dev_key, "r")
    dev_target = open(dev_target, "r")
    dev_pred = open(dev_pred, "r")

    correct = 0
    missed = 0
    mislabeled = 0
    correct_names = set()

    verbose = config['flags']['verbose']

    for x, y, z in zip(dev_key, dev_target, dev_pred):
        x = x.split(',')[3].strip()
        y = y.strip()
        z = z.strip()
        
        if int(y) == 1:
            if int(z) == 1:
                if x in new_names:
                    if verbose:
                        print("Found correct new name: %s" % x)
                    correct_names.add(x)
                    correct += 1
            else :
                if verbose:
                    print("Missed the correct name: %s" % x)
                missed += 1

        else:
            if int(z) == 1:
                if verbose:
                    print("Mislabeled name: %s" % x)
                mislabeled += 1


    
    print ()