Browse Source

Merge pull request #670 from pelias/accuracy

Add accuracy property to results
pull/625/head
Stephen K Hess 8 years ago committed by GitHub
parent
commit
5b95d2129c
  1. 1
      helper/geojsonify_place_details.js
  2. 57
      middleware/accuracy.js
  3. 6
      routes/v1.js
  4. 1
      test/ciao/search/address_parsing.coffee
  5. 90
      test/unit/middleware/accuracy.js
  6. 1
      test/unit/run.js

1
helper/geojsonify_place_details.js

@ -10,6 +10,7 @@ var DETAILS_PROPS = [
{ name: 'confidence', type: 'default' }, { name: 'confidence', type: 'default' },
{ name: 'match_type', type: 'string' }, { name: 'match_type', type: 'string' },
{ name: 'distance', type: 'default' }, { name: 'distance', type: 'default' },
{ name: 'accuracy', type: 'string' },
{ name: 'country', type: 'string' }, { name: 'country', type: 'string' },
{ name: 'country_gid', type: 'string' }, { name: 'country_gid', type: 'string' },
{ name: 'country_a', type: 'string' }, { name: 'country_a', type: 'string' },

57
middleware/accuracy.js

@ -0,0 +1,57 @@
/**
*
* Accuracy level should be set for each item in the results.
* The level can be any of the following:
* - point
* - interpolated
* - centroid
*/
var check = require('check-types');
var accuracyLevelPoint = 'point';
var accuracyLevelInterpolated = 'interpolated';
var accuracyLevelCentroid = 'centroid';
function setup() {
return computeAccuracy;
}
function computeAccuracy(req, res, next) {
// do nothing if no result data set
if (check.undefined(res) || check.undefined(res.data)) {
return next();
}
// loop through data items and determine accuracy levels
res.data = res.data.map(computeAccuracyLevelForResult);
next();
}
/**
* Determine accuracy level based on the type of result being returned.
*
* @param {object} hit
* @returns {object}
*/
function computeAccuracyLevelForResult(hit) {
// TODO: add a check for interpolated addresses when that feature lands
switch (hit.layer) {
case 'venue':
case 'address':
hit.accuracy = accuracyLevelPoint;
break;
// this means it's a street or admin area
default:
hit.accuracy = accuracyLevelCentroid;
break;
}
return hit;
}
module.exports = setup;

6
routes/v1.js

@ -39,6 +39,7 @@ var postProc = {
confidenceScores: require('../middleware/confidenceScore'), confidenceScores: require('../middleware/confidenceScore'),
confidenceScoresFallback: require('../middleware/confidenceScoreFallback'), confidenceScoresFallback: require('../middleware/confidenceScoreFallback'),
confidenceScoresReverse: require('../middleware/confidenceScoreReverse'), confidenceScoresReverse: require('../middleware/confidenceScoreReverse'),
accuracy: require('../middleware/accuracy'),
dedupe: require('../middleware/dedupe'), dedupe: require('../middleware/dedupe'),
localNamingConventions: require('../middleware/localNamingConventions'), localNamingConventions: require('../middleware/localNamingConventions'),
renamePlacenames: require('../middleware/renamePlacenames'), renamePlacenames: require('../middleware/renamePlacenames'),
@ -81,6 +82,7 @@ function addRoutes(app, peliasConfig) {
postProc.confidenceScores(peliasConfig), postProc.confidenceScores(peliasConfig),
postProc.confidenceScoresFallback(), postProc.confidenceScoresFallback(),
postProc.dedupe(), postProc.dedupe(),
postProc.accuracy(),
postProc.localNamingConventions(), postProc.localNamingConventions(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.parseBoundingBox(), postProc.parseBoundingBox(),
@ -94,6 +96,7 @@ function addRoutes(app, peliasConfig) {
postProc.distances('focus.point.'), postProc.distances('focus.point.'),
postProc.confidenceScores(peliasConfig), postProc.confidenceScores(peliasConfig),
postProc.dedupe(), postProc.dedupe(),
postProc.accuracy(),
postProc.localNamingConventions(), postProc.localNamingConventions(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.parseBoundingBox(), postProc.parseBoundingBox(),
@ -110,6 +113,7 @@ function addRoutes(app, peliasConfig) {
// so it must be calculated first // so it must be calculated first
postProc.confidenceScoresReverse(), postProc.confidenceScoresReverse(),
postProc.dedupe(), postProc.dedupe(),
postProc.accuracy(),
postProc.localNamingConventions(), postProc.localNamingConventions(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.parseBoundingBox(), postProc.parseBoundingBox(),
@ -126,6 +130,7 @@ function addRoutes(app, peliasConfig) {
// so it must be calculated first // so it must be calculated first
postProc.confidenceScoresReverse(), postProc.confidenceScoresReverse(),
postProc.dedupe(), postProc.dedupe(),
postProc.accuracy(),
postProc.localNamingConventions(), postProc.localNamingConventions(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.parseBoundingBox(), postProc.parseBoundingBox(),
@ -136,6 +141,7 @@ function addRoutes(app, peliasConfig) {
place: createRouter([ place: createRouter([
sanitisers.place.middleware, sanitisers.place.middleware,
controllers.place(peliasConfig), controllers.place(peliasConfig),
postProc.accuracy(),
postProc.localNamingConventions(), postProc.localNamingConventions(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.parseBoundingBox(), postProc.parseBoundingBox(),

1
test/ciao/search/address_parsing.coffee

@ -42,3 +42,4 @@ json.geocoding.query.parsed_text['admin_parts'].should.eql "ny"
json.features[0].properties.confidence.should.eql 1 json.features[0].properties.confidence.should.eql 1
json.features[0].properties.match_type.should.eql "exact" json.features[0].properties.match_type.should.eql "exact"
json.features[0].properties.accuracy.should.eql "point"

90
test/unit/middleware/accuracy.js

@ -0,0 +1,90 @@
var accuracy = require('../../../middleware/accuracy')();
module.exports.tests = {};
module.exports.tests.accuracy = function(test, common) {
test('empty res and req should not throw exception', function(t) {
function testIt() {
accuracy({}, {}, function() {});
}
t.doesNotThrow(testIt, 'an exception should not have been thrown');
t.end();
});
test('res.results without parsed_text should not throw exception', function(t) {
var res = {
data: [{
layer: 'venue'
}]
};
accuracy({}, res, function() {
t.equal(res.data[0].accuracy, 'point', 'accuracy was set');
t.end();
});
});
test('venue should have accuracy set to point', function(t) {
var res = {
data: [{
layer: 'venue'
}]
};
accuracy({}, res, function() {
t.equal(res.data[0].accuracy, 'point', 'accuracy was set');
t.end();
});
});
test('address should have accuracy set to point', function(t) {
var res = {
data: [{
layer: 'address'
}]
};
accuracy({}, res, function() {
t.equal(res.data[0].accuracy, 'point', 'accuracy was set');
t.end();
});
});
test('region should have accuracy set to centroid', function(t) {
var res = {
data: [{
layer: 'region'
}]
};
accuracy({}, res, function() {
t.equal(res.data[0].accuracy, 'centroid', 'accuracy was set');
t.end();
});
});
test('street should have accuracy set to centroid', function(t) {
var res = {
data: [{
layer: 'street'
}]
};
accuracy({}, res, function() {
t.equal(res.data[0].accuracy, 'centroid', 'accuracy was set');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] confidenceScore: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -25,6 +25,7 @@ var tests = [
require('./helper/type_mapping'), require('./helper/type_mapping'),
require('./helper/sizeCalculator'), require('./helper/sizeCalculator'),
require('./middleware/access_log'), require('./middleware/access_log'),
require('./middleware/accuracy'),
require('./middleware/confidenceScore'), require('./middleware/confidenceScore'),
require('./middleware/confidenceScoreFallback'), require('./middleware/confidenceScoreFallback'),
require('./middleware/confidenceScoreReverse'), require('./middleware/confidenceScoreReverse'),

Loading…
Cancel
Save