Browse Source

feat(libpostal_patch): add a libpostal patch which allows recasting labels

libpostal_aus_unit_numbers
Peter Johnson 6 years ago committed by Peter Johnson
parent
commit
19eb0b57d1
  1. 19
      controller/libpostal.js
  2. 39
      test/unit/controller/libpostal.js

19
controller/libpostal.js

@ -114,12 +114,29 @@ function setup(libpostalService, should_execute) {
return controller; return controller;
} }
const RECAST_LABELS = [{ value: 'zoo', label: { to: 'house' } }];
const DIAGONAL_DIRECTIONALS = ['ne','nw','se','sw']; const DIAGONAL_DIRECTIONALS = ['ne','nw','se','sw'];
// apply fixes for known bugs in libpostal // apply fixes for known bugs in libpostal
function patchBuggyResponses(response){ function patchBuggyResponses(response){
if( !Array.isArray(response) || !response.length ){ return 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<RECAST_LABELS.length; ii++ ){
let recast = RECAST_LABELS[ii];
if( !recast.hasOwnProperty('label') || !recast.label.hasOwnProperty('to') ){ continue; }
if( recast.value !== first.value ){ continue; }
if( recast.label.hasOwnProperty('from') && recast.label.from !== first.label ){ continue; }
response[0].label = recast.label.to;
}
}
// known bug where the street name is only a directional, in this case we will merge it // known bug where the street name is only a directional, in this case we will merge it
// with the subsequent element. // with the subsequent element.
// note: the bug only affects diagonals, not N,S,E,W // note: the bug only affects diagonals, not N,S,E,W
@ -129,7 +146,7 @@ function patchBuggyResponses(response){
if( 'string' !== typeof response[i].value ){ continue; } if( 'string' !== typeof response[i].value ){ continue; }
if( 2 !== response[i].value.length ){ continue; } if( 2 !== response[i].value.length ){ continue; }
if( DIAGONAL_DIRECTIONALS.includes( response[i].value.toLowerCase() ) ){ if( DIAGONAL_DIRECTIONALS.includes( response[i].value.toLowerCase() ) ){
if( 'string' !== typeof response[i+1].value ){ continue; } if( 'string' !== typeof response[i+1].value ){ continue; }
response[i].value += ' ' + response[i+1].value; // merge elements response[i].value += ' ' + response[i+1].value; // merge elements
response.splice(i+1, 1); // remove merged element response.splice(i+1, 1); // remove merged element
break; break;

39
test/unit/controller/libpostal.js

@ -357,6 +357,45 @@ 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 =[
{
'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) => { module.exports.all = (tape, common) => {

Loading…
Cancel
Save