Datasets:

Modalities:
Text
Formats:
json
Languages:
Slovak
DOI:
Libraries:
Datasets
pandas
License:
File size: 3,621 Bytes
a8a2b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from math import log2
import pandas as pd


def filtering_annotated_dataset_for_eval_ndcg(filtered_annotated_dataset):
    """
    Get from filtering annotated dataset only article id and category
    next step save in dict and all save dict save in list
    :param filtered_annotated_dataset: annotated dataset in json format
    :return: list of dict for prepare datas for evaluation ndcg
    """

    correct_answers = []

    for i in range(0, len(filtered_annotated_dataset)):
        correct_answer = {}
        line = filtered_annotated_dataset[i]
        for sublist in line["results"]:
            correct_answer[sublist['answer_id']] = sublist['category']

        correct_answers.append(correct_answer)

    return correct_answers


def filter_and_parsing_assignment_article_id_to_category(predicted_list, correct_answers):
    """
    Funtion to filter and parsing search_result id to category for evaluation ndcg
    :param predicted_list:  search results for evaluation ndcg
    :param correct_answers: correct answers for evaluation ndcg from annotated dataset
    :return: list with correct order with number of relevant category
    """

    predictions = []

    # for cycle in seared dataset
    for i in range(0, len(predicted_list)):
        prediction = []

        # parsing data to tmp
        line = predicted_list[i]
        correct_answer = correct_answers[i]

        # get on id and find same id in dict from annotated dataset
        for j in range(0, len(line)):

            # if article id in correct answers
            if line[j] in correct_answer:
                """
                acd correct order
                if category 1 in ndcg is 3
                if category 2 in ndcg is 2
                if category 3 in ndcg is 1
                else category in ndcg is 0
                """
                if correct_answer[line[j]] == 1:
                    prediction.append(3)
                elif correct_answer[line[j]] == 2:
                    prediction.append(2)
                else:
                    # correct_answer[line[j]] == 3:
                    prediction.append(1)
            else:
                prediction.append(0)

        # save get values
        predictions.append(prediction)

    return predictions


def count_ndcg(relevance, count_results):
    """
    Function count ndcg
    :param relevance: search results with value relevance
    :return: total ndcg value
    """

    df = pd.DataFrame()
    df['Count Results'] = range(1, count_results + 1)
    df['SUM NDCG'] = [0.00] * count_results

    K = count_results

    for line in relevance:

        # sort items in 'relevance' from most relevant to less relevant
        ideal_relevance = sorted(line, reverse=True)

        dcg = 0
        idcg = 0
        ndcg_list = []

        for k in range(1, K + 1):
            # calculate rel_k values
            rel_k = line[k - 1]
            ideal_rel_k = ideal_relevance[k - 1]

            # calculate dcg and idcg
            dcg += rel_k / log2(1 + k)
            idcg += ideal_rel_k / log2(1 + k)

            if dcg == 0.00 and idcg == 0.00:
                ndcg = 0

            else:
                # calculate ndcg
                ndcg = dcg / idcg

            df.at[k - 1, 'SUM NDCG'] += ndcg

    # Create new column "NDCG" in dataFrame df
    df['NDCG'] = round(df['SUM NDCG'] / len(relevance), 2)
    print_ndcg = round(df.at[count_results - 1, 'NDCG'], 2)
    print(f"NDCG Metric for {count_results} is: {print_ndcg} \n")
    print(df)

    sum_ndcg = df['NDCG'].sum()
    ndcg_mean = sum_ndcg / K

    return round(ndcg_mean, 2)