Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libpostal: discard parse containing single "suburb" label #1627

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions controller/libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ function setup(libpostalService, should_execute) {
// push err.message or err onto req.errors
req.errors.push( _.get(err, 'message', err) );

} else if (!Array.isArray(response) || !response.length) {
return next();

} else if (_.some(_.countBy(response, o => o.label), count => count > 1)) {
logger.warn(`discarding libpostal parse of '${req.clean.text}' due to duplicate field assignments`);
return next();

} else if (_.isEmpty(response)) {
// libpostal classifies some airports as 'suburb'
// when we see a single 'suburb' label, discard it.
// examples: 'john f kennedy international airport', 'soho'
} else if (response.length === 1 && response[0].label === 'suburb') {
logger.warn(`discarding libpostal parse of '${req.clean.text}' due to solo 'suburb' label`);
return next();

} else {
Expand Down Expand Up @@ -121,7 +128,6 @@ const IS_NUMERIC_REGEXP = /^\d+$/;

// apply fixes for known bugs in libpostal
function patchBuggyResponses(response){
if( !Array.isArray(response) || !response.length ){ return response; }

// patches which are only applied when a single label is generated
if( response.length === 1 ){
Expand Down
63 changes: 62 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,67 @@ 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',
// parse 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