Browse Source

Merge pull request #1197 from pelias/libpostal_bugfixes

apply parser bugfix for incorrect parsing of diagonal directionals
pull/1198/head
Julian Simioni 6 years ago committed by GitHub
parent
commit
8751f1ae65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      controller/libpostal.js
  2. 56
      test/unit/controller/libpostal.js

30
controller/libpostal.js

@ -70,6 +70,7 @@ function setup(libpostalService, should_execute) {
const initialTime = debugLog.beginTimer(req); const initialTime = debugLog.beginTimer(req);
libpostalService(req, (err, response) => { libpostalService(req, (err, response) => {
if (err) { if (err) {
// push err.message or err onto req.errors // push err.message or err onto req.errors
req.errors.push( _.get(err, 'message', err) ); req.errors.push( _.get(err, 'message', err) );
@ -82,6 +83,10 @@ function setup(libpostalService, should_execute) {
return next(); return next();
} else { } else {
// apply fixes for known bugs in libpostal
response = patchBuggyResponses(response);
req.clean.parser = 'libpostal'; req.clean.parser = 'libpostal';
req.clean.parsed_text = response.reduce(function(o, f) { req.clean.parsed_text = response.reduce(function(o, f) {
if (field_mapping.hasOwnProperty(f.label)) { if (field_mapping.hasOwnProperty(f.label)) {
@ -109,4 +114,29 @@ function setup(libpostalService, should_execute) {
return controller; return controller;
} }
const DIAGONAL_DIRECTIONALS = ['ne','nw','se','sw'];
// apply fixes for known bugs in libpostal
function patchBuggyResponses(response){
if( !Array.isArray(response) || !response.length ){ return response; }
// known bug where the street name is only a directional, in this case we will merge it
// with the subsequent element.
// note: the bug only affects diagonals, not N,S,E,W
// https://github.com/OpenTransitTools/trimet-mod-pelias/issues/20#issuecomment-417732128
for( var i=0; i<response.length-1; i++ ){ // dont bother checking the last element
if( 'road' !== response[i].label ){ continue; }
if( 'string' !== typeof response[i].value ){ continue; }
if( 2 !== response[i].value.length ){ continue; }
if( DIAGONAL_DIRECTIONALS.includes( response[i].value.toLowerCase() ) ){
if( 'string' !== typeof response[i+1].value ){ continue; }
response[i].value += ' ' + response[i+1].value; // merge elements
response.splice(i+1, 1); // remove merged element
break;
}
}
return response;
}
module.exports = setup; module.exports = setup;

56
test/unit/controller/libpostal.js

@ -303,6 +303,62 @@ module.exports.tests.success_conditions = (test, common) => {
}; };
module.exports.tests.bug_fixes = (test, common) => {
test('bug fix: incorrect parsing of diagonal directionals', t => {
const service = (req, callback) => {
const response =[
{
'label': 'house_number',
'value': '4004'
},
{
'label': 'road',
'value': 'nw'
},
{
'label': 'suburb',
'value': 'beaverton-hillsdale'
},
{
'label': 'city',
'value': 'portland'
}
];
callback(null, response);
};
const controller = libpostal(service, () => true);
const req = {
clean: {
text: 'original query'
},
errors: []
};
controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
text: 'original query',
parser: 'libpostal',
parsed_text: {
number: '4004',
street: 'nw beaverton-hillsdale',
city: 'portland'
}
},
errors: []
}, 'req should not have been modified');
t.end();
});
});
};
module.exports.all = (tape, common) => { module.exports.all = (tape, common) => {
function test(name, testFunction) { function test(name, testFunction) {

Loading…
Cancel
Save