Skip to content

Commit

Permalink
feat(libpostal): discard parse containing single "suburb" label
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Jun 21, 2022
1 parent 10d56c5 commit ea065bb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions controller/libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ function patchBuggyResponses(response){
if( response.length === 1 ){
let first = response[0];

// libpostal classifies some airports as 'suburb'
// when we see a single 'suburb' label, discard it.
// examples: 'john f kennedy international airport', 'soho'
if (first.label === 'suburb') { return []; }

// given only a number, libpostal will attempt to classify it.
// if we find a single label which is entirely numeric then it's recast to the
// libpostal label 'house', which is mapped to 'query' in our schema.
Expand Down
66 changes: 65 additions & 1 deletion test/unit/controller/libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ module.exports.tests.bug_fixes = (test, common) => {
test('bug fix: recast entirely numeric input - 99', t => {
const service = (req, callback) => {
callback(null, [{
'label': 'suburb',
'label': 'house_number',
'value': '99'
}]);
};
Expand Down Expand Up @@ -831,6 +831,70 @@ module.exports.tests.bug_fixes = (test, common) => {
});
});

test('bug fix: discard single label of type "suburb"', t => {
const service = (req, callback) => {
callback(null, [{
'label': 'suburb',
'value': 'example'
}]);
};
const controller = libpostal(service, () => true);
const req = {
clean: {
text: 'example'
},
errors: []
};
controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
text: 'example',
parser: 'libpostal',
parsed_text: {
// discarded
}
},
errors: []
});

t.end();
});
});

test('bug fix: do not discard "suburb" when accompanied by another label', t => {
const service = (req, callback) => {
callback(null, [{
'label': 'road',
'value': 'avenue'
},{
'label': 'suburb',
'value': 'example'
}]);
};
const controller = libpostal(service, () => true);
const req = {
clean: {
text: 'avenue example'
},
errors: []
};
controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
text: 'avenue example',
parser: 'libpostal',
parsed_text: {
street: 'avenue',
neighbourhood: 'example'
}
},
errors: []
});

t.end();
});
});

};

module.exports.all = (tape, common) => {
Expand Down

0 comments on commit ea065bb

Please sign in to comment.