forked from pelias/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfidenceScoreReverse.js
68 lines (56 loc) · 1.52 KB
/
confidenceScoreReverse.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
var logger = require('pelias-logger').get('api');
var _ = require('lodash');
// these are subjective terms, but wanted to add shortcuts to denote something
// about importance
var SCORES = {
EXACT: 1.0,
EXCELLENT: 0.9,
GOOD: 0.8,
OKAY: 0.7,
POOR: 0.6,
NONE: 0.5,
INVALID: 0.0
};
var BUCKETS = {
_1_METER: 1,
_10_METERS: 10,
_100_METERS: 100,
_250_METERS: 250,
_1_KILOMETER: 1000
};
function setup() {
return computeScores;
}
function computeScores(req, res, next) {
// do nothing if no result data set
if (!res.data || !res.data) {
return next();
}
// loop through data items and determine confidence scores
res.data = res.data.map(computeConfidenceScore);
next();
}
function computeConfidenceScore(hit) {
// non-number or invalid distance should be given confidence 0.0
if (!_.isFinite(hit.distance) || hit.distance < 0) {
hit.confidence = SCORES.INVALID;
return hit;
}
var distance = hit.distance * 1000.0;
// figure out which range the distance lies in and assign confidence accordingly
if (distance < BUCKETS._1_METER) {
hit.confidence = SCORES.EXACT;
} else if (distance < BUCKETS._10_METERS) {
hit.confidence = SCORES.EXCELLENT;
} else if (distance < BUCKETS._100_METERS) {
hit.confidence = SCORES.GOOD;
} else if (distance < BUCKETS._250_METERS) {
hit.confidence = SCORES.OKAY;
} else if (distance < BUCKETS._1_KILOMETER) {
hit.confidence = SCORES.POOR;
} else {
hit.confidence = SCORES.NONE;
}
return hit;
}
module.exports = setup;