#!/usr/bin/env python3 import fileinput from math import log if __name__ == '__main__': scores = dict() for line in fileinput.input(): query, dbpediaid, relevance, field, nvalues, nmatches = line.split('\t') if field not in scores: scores[field] = 0 scores[field] += float(relevance) * log(1 + int(nmatches)/int(nvalues)) for field, score in scores.items(): print('{}\t{}'.format(field, score))