Skip to content

Commit

Permalink
Merge pull request pelias#1454 from pelias/libpostal_bugfixes
Browse files Browse the repository at this point in the history
patch libpostal output for post-directionals
  • Loading branch information
orangejulius authored Jun 26, 2020
2 parents a3fc0b6 + f503118 commit 56b6bdb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
37 changes: 27 additions & 10 deletions controller/libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,35 @@ function patchBuggyResponses(response){
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.
// known bug where the road name directional is parsed into an adjacent label, in
// this case we will merge it with the road label.
// note: the bug only affects diagonals, not N,S,E,W
// https://github.com/OpenTransitTools/trimet-mod-pelias/issues/20#issuecomment-417732128
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( subsequentElement && _.isString(subsequentElement.value) ){
response[road._pos].value += ' ' + subsequentElement.value; // merge elements
response.splice(road._pos+1, 1); // remove merged element
if (_.size(response) > 1) {

// find the road label
const road = _.get(idx, 'road');
if (_.isPlainObject(road) && _.isString(road.value)) {

// find the label which directly follows the road label
const next = _.nth(response, road._pos + 1);
if (_.isPlainObject(next) && _.isString(next.value)) {

// detect the two different occurrences of the bug
if (
// where the road label contains the directional
// eg. { 'label':'road', 'value':'nw' }, { 'label':'suburb', 'value':'foo st' }
(_.size(road.value) === 2 && DIAGONAL_DIRECTIONALS.includes(road.value.toLowerCase())) ||

// or where the following label contains the directional
// eg. { 'label':'road', 'value':'foo st' }, { 'label':'suburb', 'value':'nw' }
(_.size(next.value) === 2 && DIAGONAL_DIRECTIONALS.includes(next.value.toLowerCase()))
) {
// concatenate the two labels
response[road._pos].value += ` ${next.value}`;

// remove the obsolete label
_.pullAt(response, road._pos + 1);
}
}
}
Expand Down
56 changes: 55 additions & 1 deletion test/unit/controller/libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ module.exports.tests.success_conditions = (test, common) => {
};

module.exports.tests.bug_fixes = (test, common) => {
test('bug fix: incorrect parsing of diagonal directionals', t => {
test('bug fix: incorrect parsing of diagonal directionals - pre-directional', t => {
const service = (req, callback) => {
const response =[
{
Expand Down Expand Up @@ -357,6 +357,60 @@ module.exports.tests.bug_fixes = (test, common) => {

});

test('bug fix: incorrect parsing of diagonal directionals - post-directional', t => {
const service = (req, callback) => {
// 1125 Couch St NW, Portland
const response = [
{
'label': 'house_number',
'value': '1125'
},
{
'label': 'road',
'value': 'couch st'
},
{
'label': 'suburb',
'value': 'nw'
},
{
'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: {
housenumber: '1125',
street: 'couch st nw',
city: 'portland'
}
},
errors: []
}, 'req should not have been modified');

t.end();

});

});

test('bug fix: incorrect parsing of diagonal directionals - no subsequent element', t => {
const service = (req, callback) => {
const response = [
Expand Down

0 comments on commit 56b6bdb

Please sign in to comment.