#!/usr/bin/env python3

from argparse import ArgumentParser, RawTextHelpFormatter as RawText

def run(models):
    results = score_model_files(models)
    for (model, (score, count)) in results.items():
        print("{}\t{}\t{}".format(model, count, "\t".join(str(s) for s in score)))

def score_model_files(models):
    return {model: score_model_file(model)
            for model in models}

def score_model_file(model):
    with open(model) as file_:
        read_model = [line.lower().strip().split() for line in file_]
    return score_model(read_model)

def score_model(model):
    length = len(model[0]) - 3
    indices = [score_analogy(analogy) for analogy in model]
    score = [sum(0 < index <= i for index in indices) / len(indices)
             for i in range(1, length)]
    return (score, len(model))

def score_analogy(analogy):
    target = analogy[3]
    answers = analogy[4:]
    if target not in answers:
        return 0
    return answers.index(target) + 1

def _main():
    args = _get_args()
    run(args.model)

def _get_args():
    parser = ArgumentParser(description=__doc__, formatter_class=RawText)
    parser.add_argument("model", nargs="+",
                        help="File with results from test_analogy.py")
    return parser.parse_args()

if __name__ == "__main__":
    _main()

