Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 114 additions & 15 deletions middleware/confidenceScoreFallback.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/**
*
* Basic confidence score should be computed and returned for each item in the results.
Expand All @@ -11,6 +13,7 @@

var check = require('check-types');
var logger = require('pelias-logger').get('api');
const _ = require('lodash');

function setup() {
return computeScores;
Expand Down Expand Up @@ -67,9 +70,10 @@ function checkFallbackLevel(req, hit) {
// if we know a fallback occurred, deduct points based on layer granularity
switch (hit.layer) {
case 'venue':
case 'address':
logger.warn('Fallback scenarios should not result in address or venue records!', req.clean.parsed_text);
return 0.8;
case 'address':
return 0.8;
case 'street':
return 0.8;
case 'localadmin':
Expand All @@ -96,26 +100,121 @@ function checkFallbackLevel(req, hit) {
return 1.0;
}

/**
* In parsed_text we might find any of the following properties:
* query
* number
* street
* neighbourhood
* borough
* city
* county
* state
* postalcode
* country
*
* They do not map 1:1 to our layers so the following somewhat complicated
* mapping structure is needed to set clear rules for comparing what was requested
* by the query and what has been received as a result to determine if a fallback occurred.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yikes. complicated, but i think necessary and pretty readable.

*/
const fallbackRules = [
{
name: 'venue',
notSet: [],
set: ['query'],
expectedLayers: ['venue']
},
{
name: 'address',
notSet: ['query'],
set: ['number', 'street'],
expectedLayers: ['address']
},
{
name: 'street',
notSet: ['query', 'number'],
set: ['street'],
expectedLayers: ['street']
},
{
name: 'neighbourhood',
notSet: ['query', 'number', 'street'],
set: ['neighbourhood'],
expectedLayers: ['neighbourhood']
},
{
name: 'borough',
notSet: ['query', 'number', 'street', 'neighbourhood'],
set: ['borough'],
expectedLayers: ['borough']
},
{
name: 'city',
notSet: ['query', 'number', 'street', 'neighbourhood', 'borough'],
set: ['city'],
expectedLayers: ['borough', 'locality', 'localadmin']
},
{
name: 'county',
notSet: ['query', 'number', 'street', 'neighbourhood', 'borough', 'city'],
set: ['county'],
expectedLayers: ['county']
},
{
name: 'state',
notSet: ['query', 'number', 'street', 'neighbourhood', 'borough', 'city', 'county'],
set: ['state'],
expectedLayers: ['region']
},
{
name: 'country',
notSet: ['query', 'number', 'street', 'neighbourhood', 'borough', 'city', 'county', 'state'],
set: ['country'],
expectedLayers: ['country']
}
];

function checkFallbackOccurred(req, hit) {
return (requestedAddress(req) && hit.layer !== 'address') ||
(requestedStreet(req) && hit.layer !== 'street') ||
(requestedCity(req) && hit.layer !== 'locality' && hit.layer !== 'localadmin');
}

function requestedAddress(req) {
// house number and street name were specified
return req.clean.parsed_text.hasOwnProperty('number') &&
req.clean.parsed_text.hasOwnProperty('street');
// short-circuit after finding the first fallback scenario
const res = _.find(fallbackRules, (rule) => {

return (
// verify that more granular properties are not set
notSet(req.clean.parsed_text, rule.notSet) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part ends up really clean, quite nice

// verify that expected property is set
areSet(req.clean.parsed_text, rule.set) &&
// verify that expected layer(s) was not returned
rule.expectedLayers.indexOf(hit.layer) === -1
);
});

return !!res;
}

function requestedStreet(req) {
// only street name was specified
return !req.clean.parsed_text.hasOwnProperty('number') &&
req.clean.parsed_text.hasOwnProperty('street');
function notSet(parsed_text, notSet) {
if (notSet.length === 0) {
return true;
}

return (
_.every(notSet, (prop) => {
return !_.get(parsed_text, prop, false);
})
);
}

function requestedCity(req) {
return req.clean.parsed_text.hasOwnProperty('city');
function areSet(parsed_text, areSet) {
if (areSet.length === 0) {
logger.warn('Expected properties in fallbackRules should never be empty');
return true;
}

return (
_.every(areSet, (prop) => {
return _.get(parsed_text, prop, false);
})
);
}

module.exports = setup;
80 changes: 79 additions & 1 deletion test/unit/middleware/confidenceScoreFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 0.1, 'score was set');
t.equal(res.data[0].match_type, 'unknown', 'exact match indicated');
t.end();
});

Expand All @@ -105,6 +106,7 @@ module.exports.tests.confidenceScore = function(test, common) {
parsed_text: {
number: 123,
street: 'Main St',
city: 'City',
state: 'NM'
}
}
Expand All @@ -131,6 +133,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 1.0, 'max score was set');
t.equal(res.data[0].match_type, 'exact', 'exact match indicated');
t.end();
});

Expand Down Expand Up @@ -166,10 +169,78 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 1.0, 'max score was set');
t.equal(res.data[0].match_type, 'exact', 'exact match indicated');
t.end();
});

test('fallback to locality should have score deduction', function(t) {
test('no fallback state query should have max score', function(t) {
var req = {
clean: {
text: 'Region Name, Country',
parsed_text: {
state: 'Region Name',
country: 'Country'
}
}
};
var res = {
data: [{
_score: 10,
found: true,
value: 1,
layer: 'region',
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'Region Name' },
parent: {
country: ['country1']
}
}],
meta: {
scores: [10],
query_type: 'fallback'
}
};

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 1.0, 'max score was set');
t.equal(res.data[0].match_type, 'exact', 'exact match indicated');
t.end();
});

test('no fallback country query should have max score', function(t) {
var req = {
clean: {
text: 'Country Name',
parsed_text: {
country: 'Country Name'
}
}
};
var res = {
data: [{
_score: 10,
found: true,
value: 1,
layer: 'country',
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' },
parent: {
country: ['country1']
}
}],
meta: {
scores: [10],
query_type: 'fallback'
}
};

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 1.0, 'max score was set');
t.equal(res.data[0].match_type, 'exact', 'exact match indicated');
t.end();
});

test('fallback to locality when searching for address should have score deduction', function(t) {
var req = {
clean: {
text: '123 Main St, City, NM',
Expand Down Expand Up @@ -200,6 +271,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 0.6, 'score was set');
t.equal(res.data[0].match_type, 'fallback', 'fallback match indicated');
t.end();
});

Expand Down Expand Up @@ -234,6 +306,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 0.6, 'score was set');
t.equal(res.data[0].match_type, 'fallback', 'fallback match indicated');
t.end();
});

Expand Down Expand Up @@ -269,6 +342,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 0.1, 'score was set');
t.equal(res.data[0].match_type, 'fallback', 'fallback match indicated');
t.end();
});

Expand All @@ -292,6 +366,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 1.0, 'score was set');
t.equal(res.data[0].match_type, 'exact', 'exact match indicated');
t.end();
});

Expand All @@ -315,6 +390,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 1.0, 'score was set');
t.equal(res.data[0].match_type, 'exact', 'exact match indicated');
t.end();
});

Expand All @@ -338,6 +414,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 0.3, 'score was set');
t.equal(res.data[0].match_type, 'fallback', 'fallback match indicated');
t.end();
});

Expand All @@ -362,6 +439,7 @@ module.exports.tests.confidenceScore = function(test, common) {

confidenceScore(req, res, function() {});
t.equal(res.data[0].confidence, 0.1, 'score was set');
t.equal(res.data[0].match_type, 'fallback', 'fallback match indicated');
t.end();
});

Expand Down