def ensemble(folder, ratio, clf_list, runtag, output):
    ensemble_dict = {}
    for clf in clf_list:
        with open('{}/{}/rerank_{}.txt'.format(folder, clf, ratio), 'r') as f:
            for line in f:
                data = line.split()
                (topic, docid, score) = (data[0], data[2], float(data[4]))
                if (topic not in ensemble_dict):
                    ensemble_dict[topic] = {}
                if (docid not in ensemble_dict[topic]):
                    ensemble_dict[topic][docid] = 0
                ensemble_dict[topic][docid] += score
    with open(output, 'w') as f:
        for topic in ensemble_dict:
            for (rank, (docid, score)) in enumerate(sorted(ensemble_dict[topic].items(), key=(lambda x: (- x[1])))):
                f.write('{} Q0 {} {} {} {}\n'.format(topic, docid, (rank + 1), score, runtag))
def ensemble(folder, ratio, clf_list, runtag, output):
    ensemble_dict = {}
    for clf in clf_list:
        with open('{}/{}/rerank_{}.txt'.format(folder, clf, ratio), 'r') as f:
            for line in f:
                data = line.split()
                (topic, docid, score) = (data[0], data[2], float(data[4]))
                if (topic not in ensemble_dict):
                    ensemble_dict[topic] = {}
                if (docid not in ensemble_dict[topic]):
                    ensemble_dict[topic][docid] = 0
                ensemble_dict[topic][docid] += score
    with open(output, 'w') as f:
        for topic in ensemble_dict:
            for (rank, (docid, score)) in enumerate(sorted(ensemble_dict[topic].items(), key=(lambda x: (- x[1])))):
                f.write('{} Q0 {} {} {} {}\n'.format(topic, docid, (rank + 1), score, runtag))