Browse Source

Merge pull request #1216 from pelias/libpostal_aus_unit_numbers

libpostal aus unit numbers
libpostal_aus_unit_numbers
Julian Simioni 6 years ago committed by GitHub
parent
commit
42cfcd843a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      controller/libpostal.js
  2. 246
      test/unit/controller/libpostal.js

39
controller/libpostal.js

@ -17,7 +17,8 @@ var field_mapping = {
state_district: 'county',
state: 'state',
postcode: 'postalcode',
country: 'country'
country: 'country',
unit: 'unit',
};
// This controller calls the hosted libpostal service and converts the response
@ -136,19 +137,37 @@ function patchBuggyResponses(response){
});
}
// generate an index to avoid multiple iterations over the response array
let idx = {};
response.forEach((res, pos) => idx[res.label] = _.assign({ _pos: pos }, res));
// 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;
if( response.length > 1 ){
let road = _.get(idx, 'road');
if( _.isPlainObject(road) && _.isString(road.value) && road.value.length === 2 ){
if( DIAGONAL_DIRECTIONALS.includes( road.value.toLowerCase() ) ){
let subsequentElement = response[road._pos+1];
if( _.isString(subsequentElement.value) ){
response[road._pos].value += ' ' + subsequentElement.value; // merge elements
response.splice(road._pos+1, 1); // remove merged element
}
}
}
}
// 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 = _.trim(split[1]); // second part (house number)
response.push({ label: 'unit', value: _.trim(split[0]) }); // first part (unit number)
}
}

246
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,250 @@ 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 - with plus', t => {
const service = (req, callback) => {
const response = [
{
'label': 'house_number',
'value': '2+3/32'
},
{
'label': 'road',
'value': 'dixon street'
},
{
'label': 'suburb',
'value': 'strathpine'
},
{
'label': 'postcode',
'value': '4500'
},
{
'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: '2+3',
number: '32',
street: 'dixon street',
neighbourhood: 'strathpine',
postalcode: '4500',
state: 'qld'
}
},
errors: []
}, 'req should not have been modified');
t.end();
});
});
test('bug fix: correctly parse australian-style unit numbers - with unit spelled out', t => {
const service = (req, callback) => {
const response = [
{
'label': 'house_number',
'value': 'unit 3 /30'
},
{
'label': 'road',
'value': 'dan rees street'
},
{
'label': 'suburb',
'value': 'wallsend'
},
{
'label': 'postcode',
'value': '2287'
},
{
'label': 'state',
'value': 'nsw'
}
];
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: 'unit 3',
number: '30',
street: 'dan rees street',
neighbourhood: 'wallsend',
postalcode: '2287',
state: 'nsw'
}
},
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: '99',
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) => {

Loading…
Cancel
Save