def count_mrr(actual_list, predicted_list, count_results): """ Function for calculate MRR metric :param actual_list: actual results from annotated datatest :param predicted_list: predicted results from searched engine :return: None """ # number of queries Q = len(actual_list) # calculate the reciprocal rank for each query cumulative_reciprocal = 0 # initialize dictionary to store count of relevant results for each query relevant_results_count = {i: 0 for i in range(1, count_results + 1)} for i in range(Q): actual = actual_list[i] pred = predicted_list[i][:count_results] reciprocal_rank = 0 for j, result in enumerate(pred, 1): if result in actual: reciprocal_rank = 1 / j relevant_results_count[j] += 1 # increment count of relevant results for this query break cumulative_reciprocal += reciprocal_rank # print(f"query #{i+1} = {1}/{j} = {reciprocal_rank}") # calculate MRR mrr = cumulative_reciprocal / Q # generate result print(f"MRR Metric for {count_results} is: {round(mrr, 2)}") # generate table of relevant results count print("Table of Relevant Results Count:") print("Position | Count") for position, count in relevant_results_count.items(): print(f"{position:^9} | {count:^5}")