From 8be249b701b10a1d9fdc0a30ecd81173763cc451 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 16 Sep 2015 17:32:32 -0400 Subject: [PATCH] added helper variables for readability --- middleware/confidenceScoreReverse.js | 51 +++++++++++++++------------- routes/v1.js | 2 +- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/middleware/confidenceScoreReverse.js b/middleware/confidenceScoreReverse.js index c1c7da0b..aa29ad0c 100644 --- a/middleware/confidenceScoreReverse.js +++ b/middleware/confidenceScoreReverse.js @@ -3,16 +3,21 @@ var _ = require('lodash'); // these are subjective terms, but wanted to add shortcuts to denote something // about importance -var confidence = { - exact: 1.0, - excellent: 0.9, - good: 0.8, - okay: 0.7, - poor: 0.6, - none: 0.5 -}; - -function setup(peliasConfig) { +var EXACT = 1.0; +var EXCELLENT = 0.9; +var GOOD = 0.8; +var OKAY = 0.7; +var POOR = 0.6; +var NONE = 0.5; +var INVALID = 0.0; + +var _1_METER = 1; +var _10_METERS = 10; +var _100_METERS = 100; +var _250_METERS = 250; +var _1_KILOMETER = 1000; + +function setup() { return computeScores; } @@ -31,26 +36,26 @@ function computeScores(req, res, next) { function computeConfidenceScore(req, hit) { // non-number or invalid distance should be given confidence 0.0 if (typeof hit.distance !== 'number' || hit.distance < 0) { - hit.confidence = 0.0; + hit.confidence = INVALID; return hit; } - var meters = hit.distance * 1000; + var distance = hit.distance * 1000.0; // figure out which range the distance lies in and assign confidence accordingly // TODO: this could probably be made more node-y with a map of function->number - if (meters < 1.0) { - hit.confidence = confidence.exact; - } else if (_.inRange(meters, 1, 10)) { - hit.confidence = confidence.excellent; - } else if (_.inRange(meters, 10, 100)) { - hit.confidence = confidence.good; - } else if (_.inRange(meters, 100, 250)) { - hit.confidence = confidence.okay; - } else if (_.inRange(meters, 250, 1000)) { - hit.confidence = confidence.poor; + if (distance < _1_METER) { + hit.confidence = EXACT; + } else if (_.inRange(distance, _1_METER, _10_METERS)) { + hit.confidence = EXCELLENT; + } else if (_.inRange(distance, _10_METERS, _100_METERS)) { + hit.confidence = GOOD; + } else if (_.inRange(distance, _100_METERS, _250_METERS)) { + hit.confidence = OKAY; + } else if (_.inRange(distance, _250_METERS, _1_KILOMETER)) { + hit.confidence = POOR; } else { - hit.confidence = confidence.none; + hit.confidence = NONE; } return hit; diff --git a/routes/v1.js b/routes/v1.js index 6bc72183..3db635f0 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -77,7 +77,7 @@ function addRoutes(app, peliasConfig) { postProc.distances(), // reverse confidence scoring depends on distance from origin // so it must be calculated first - postProc.confidenceScoresReverse(peliasConfig), + postProc.confidenceScoresReverse(), postProc.renamePlacenames(), postProc.geocodeJSON(peliasConfig), postProc.sendJSON