-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.js
96 lines (73 loc) · 3.3 KB
/
evaluator.js
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
var db = require('./config/sequelize'),
_ = require('lodash');
var i = 0,
results = [];
db.init(function(){
var evalData = require('./utilities/evaluator').getEvalData(),
cosineMatchingFunction = require('./utilities/cosine-similiarity'),
queryProcessor = require('./utilities/query-processor');
new Promise(function(resolve){
return executeQuery(evalData.queries[0], cosineMatchingFunction, queryProcessor, evalData);
})
.then(function(){
});
});
function executeQuery(query, cosineMatchingFunction, queryProcessor, evalData){
if(i == evalData.queries.length){
evalData.revelances.forEach(function(revelance){
console.log(" Query :" + revelance.queryNumber);
console.log(" TIME revelance : " + revelance.documents);
try{
console.log(" Our Revelance : " + results.scores[evalData.revelances.indexOf(revelance)].map(x => x.docId));
}catch(e) {}
});
i = 0;
results = [];
console.log("# Semantic Evaluation: \n#########################");
var matchingFunction = require('./utilities/semantic-simliarity');
return executeQuerySemantic(evalData.queries[0], matchingFunction, queryProcessor, evalData);
}
return queryProcessor
.process(query)
.then(function(tokens){
return cosineMatchingFunction
.match(tokens.queryEntries)
.then(function(result){
results.push(result);
executeQuery(evalData.queries[i++], cosineMatchingFunction, queryProcessor, evalData);
});
});
}
function executeQuerySemantic(query, matchingFunction, queryProcessor, evalData){
if(i == evalData.queries.length){
require('./utilities/wordnet').writeHyponymsHashMap();
for(let i = 0; i < results.length; i++){
console.log(" Query :" + evalData.revelances[i].queryNumber);
console.log(" TIME revelance : " + evalData.revelances[i].documents);
console.log(" Our Revelance : " + results[i].scores.map(x => x.docId));
}
return calculateMeasures(evalData); // calculate precision & recall
}
return matchingFunction.match(query).then(function(result){
results.push(result);
return executeQuerySemantic(evalData.queries[i++], matchingFunction, queryProcessor, evalData);
});
}
function calculateMeasures(evalData){
var measures = [];
for(let i = 0; i < results.length; i ++){
let revelance = evalData.revelances[i],
relevantItemsRetrieved = _.intersection(revelance.documents, results[i].scores.map(x => x.docId +'')).length,
retrievedItems = results[i].nRetrievedDocs,
relevantItems = evalData.revelances[i].documents.length;
measures.push({
relevantItemsRetrieved : relevantItemsRetrieved,
retrievedItems : retrievedItems,
relevantItems : relevantItems,
presision : relevantItemsRetrieved / retrievedItems,
recall : relevantItemsRetrieved / relevantItems,
query : evalData.revelances[i].queryNumber
});
}
require('fs').writeFile('./evals.json', JSON.stringify(measures));
}