From 69ddbaf3beebf73620000404ddabdcab693f3b2e Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Tue, 23 Oct 2018 14:37:12 +0200 Subject: [PATCH] feat(libpostal_patch): correctly parse australian-style unit numbers --- controller/libpostal.js | 13 +++ test/unit/controller/libpostal.js | 126 +++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/controller/libpostal.js b/controller/libpostal.js index 1cd12bab..2a608568 100644 --- a/controller/libpostal.js +++ b/controller/libpostal.js @@ -157,6 +157,19 @@ function patchBuggyResponses(response){ } } + // known bug where Australian unit numbers are incorrectly included in the house_number label + // note: in the case where a 'unit' label already exists, do nothing. + // https://github.com/pelias/pelias/issues/753 + let unit = _.get(idx, 'unit'); + let house_number = _.get(idx, 'house_number'); + if( _.isPlainObject(house_number) && !_.isPlainObject(unit) && _.isString(house_number.value) ){ + let split = _.trim(_.trim(house_number.value),'/').split('/'); + if( split.length === 2 ){ + response[house_number._pos].value = split[1]; // second part (house number) + response.push({ label: 'unit', value: split[0] }); // first part (unit number) + } + } + return response; } diff --git a/test/unit/controller/libpostal.js b/test/unit/controller/libpostal.js index c9569753..def911a8 100644 --- a/test/unit/controller/libpostal.js +++ b/test/unit/controller/libpostal.js @@ -359,7 +359,7 @@ module.exports.tests.bug_fixes = (test, common) => { test('bug fix: recast label for "zoo" from borough/city_district to house', t => { const service = (req, callback) => { - const response =[ + const response = [ { 'label': 'city_district', 'value': 'zoo' @@ -396,6 +396,130 @@ module.exports.tests.bug_fixes = (test, common) => { }); + test('bug fix: correctly parse australian-style unit numbers', t => { + const service = (req, callback) => { + const response = [ + { + 'label': 'house_number', + 'value': '11/1015' + }, + { + 'label': 'road', + 'value': 'nudgee road' + }, + { + 'label': 'suburb', + 'value': 'banyo' + }, + { + 'label': 'postcode', + 'value': '4014' + }, + { + 'label': 'state', + 'value': 'qld' + } + ]; + + 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: { + // unit: '11', + number: '1015', + street: 'nudgee road', + neighbourhood: 'banyo', + postalcode: '4014', + state: 'qld' + } + }, + errors: [] + }, 'req should not have been modified'); + + t.end(); + + }); + + }); + + test('bug fix: correctly parse australian-style unit numbers - no-op if "unit" already assigned', t => { + const service = (req, callback) => { + const response = [ + { + 'label': 'unit', + 'value': '99' + }, + { + 'label': 'house_number', + 'value': '11/1015' + }, + { + 'label': 'road', + 'value': 'nudgee road' + }, + { + 'label': 'suburb', + 'value': 'banyo' + }, + { + 'label': 'postcode', + 'value': '4014' + }, + { + 'label': 'state', + 'value': 'qld' + } + ]; + + 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: { + // unit: '999', + number: '11/1015', + street: 'nudgee road', + neighbourhood: 'banyo', + postalcode: '4014', + state: 'qld' + } + }, + errors: [] + }, 'req should not have been modified'); + + t.end(); + + }); + + }); + }; module.exports.all = (tape, common) => {