diff --git a/controller/libpostal.js b/controller/libpostal.js index 70a2cc1e..3233fce6 100644 --- a/controller/libpostal.js +++ b/controller/libpostal.js @@ -114,12 +114,29 @@ function setup(libpostalService, should_execute) { return controller; } +const RECAST_LABELS = [{ value: 'zoo', label: { to: 'house' } }]; 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; } + // recast labels for certain values, currently only applied to parses which return a single label. + // the RECAST_LABELS array contains match/replace conditions which are applied in order. + // the 'value' and 'label.to' properties are mandatory, they define the value to match on and + // the replacement label to assign. you may optionally also provide 'label.from' which will restrict + // replacements to only records with BOTH a matching 'value' and a matching 'label.from'. + if( response.length === 1 ){ + let first = response[0]; + for( var ii=0; ii { }); + test('bug fix: recast label for "zoo" from borough/city_district to house', t => { + const service = (req, callback) => { + const response =[ + { + 'label': 'city_district', + 'value': 'zoo' + } + ]; + + 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: { + query: 'zoo' + } + }, + errors: [] + }, 'req should not have been modified'); + + t.end(); + + }); + + }); + }; module.exports.all = (tape, common) => {