|
|
@ -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; |
|
|
|