-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.cpp
More file actions
96 lines (89 loc) · 2.56 KB
/
Copy pathevaluator.cpp
File metadata and controls
96 lines (89 loc) · 2.56 KB
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
#include "include/evaluator.h"
#include "include/util.h"
#include <fstream>
#include <sstream>
#include <iostream>
map<int, float> averageRecallPrecision(map<int, map<string, bool>> relevantDocuments, map<int, list<string>> evaluatingDocuments) {
map<int, float> recallPrecisionSum;
map<int, list<string>>::iterator iter = evaluatingDocuments.begin();
while(iter != evaluatingDocuments.end()) {
int queryNum = iter->first;
map<int, float> rp = recallPrecision(evaluatingDocuments[queryNum], relevantDocuments[queryNum]);
map<int, float>::iterator rpIter = rp.begin();
while(rpIter != rp.end()) {
recallPrecisionSum[rpIter->first] += rpIter->second;
rpIter++;
}
iter++;
}
map<int, float>::iterator rpIter = recallPrecisionSum.begin();
while(rpIter != recallPrecisionSum.end()) {
recallPrecisionSum[rpIter->first] /= evaluatingDocuments.size();
rpIter++;
}
return recallPrecisionSum;
}
map<int, float> recallPrecision(list<string> evaluatingDocuments, map<string, bool> relevantDocuments) {
float hit = 0;
float evaluateSize = 0;
float recall, precision;
int level = 1;
map<int, float> rp;
int relevantSize = relevantDocuments.size();
list<string>::iterator evalIter = evaluatingDocuments.begin();
while(evalIter != evaluatingDocuments.end()) {
//cout << *evalIter << endl;
if(relevantDocuments[*evalIter]) {
hit++;
recall = hit / relevantSize;
precision = hit / evaluateSize;
if(recall > ((float)level / 10.0)) {
rp[level++] = precision;
}
}
evalIter++;
evaluateSize += 1;
}
return rp;
}
map<int, map<string, bool>> getRelevantDocuments(string relevantFilePath) {
map<int, map<string, bool>> relevants;
ifstream file (relevantFilePath);
string line;
if(file.is_open()) {
while(getline(file, line)) {
istringstream iss(line);
string tokens [2];
string token;
for(int i = 0; getline(iss, token, '\t'); i++) {
tokens[i] = token;
}
util::trim(tokens[1]);
relevants[stoi(tokens[0])][tokens[1]] = true;
//cout << relevants[stoi(tokens[0])][tokens[1]] << endl;
}
file.close();
}
return relevants;
}
map<int, list<string>> getEvaluatingDocuments(string resultFilePath) {
map<int, list<string>> evaluates;
ifstream file (resultFilePath);
string line;
if(file.is_open()) {
while(getline(file, line)) {
int queryNum = stoi(line);
getline(file, line);
istringstream iss(line);
list<string> tokens;
string token;
for(int i = 0; getline(iss, token, '\t'); i++) {
util::trim(token);
tokens.push_back(token);
}
evaluates[queryNum] = tokens;
}
file.close();
}
return evaluates;
}