diff --git a/controller/libpostal.js b/controller/libpostal.js index dd85e9a2..70a2cc1e 100644 --- a/controller/libpostal.js +++ b/controller/libpostal.js @@ -70,6 +70,7 @@ function setup(libpostalService, should_execute) { const initialTime = debugLog.beginTimer(req); libpostalService(req, (err, response) => { + if (err) { // push err.message or err onto req.errors req.errors.push( _.get(err, 'message', err) ); @@ -82,6 +83,10 @@ function setup(libpostalService, should_execute) { return next(); } else { + + // apply fixes for known bugs in libpostal + response = patchBuggyResponses(response); + req.clean.parser = 'libpostal'; req.clean.parsed_text = response.reduce(function(o, f) { if (field_mapping.hasOwnProperty(f.label)) { @@ -109,4 +114,29 @@ function setup(libpostalService, should_execute) { 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 { }; +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) => { function test(name, testFunction) {