Skip to content

Commit 293ef5c

Browse files
committed
feat(libpostal): discard parse containing single "suburb" label
1 parent 10d56c5 commit 293ef5c

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

controller/libpostal.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,18 @@ function setup(libpostalService, should_execute) {
7676
// push err.message or err onto req.errors
7777
req.errors.push( _.get(err, 'message', err) );
7878

79+
} else if (!Array.isArray(response) || !response.length) {
80+
return next();
81+
7982
} else if (_.some(_.countBy(response, o => o.label), count => count > 1)) {
8083
logger.warn(`discarding libpostal parse of '${req.clean.text}' due to duplicate field assignments`);
8184
return next();
8285

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

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

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

126132
// patches which are only applied when a single label is generated
127133
if( response.length === 1 ){

test/unit/controller/libpostal.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ module.exports.tests.bug_fixes = (test, common) => {
770770
test('bug fix: recast entirely numeric input - 99', t => {
771771
const service = (req, callback) => {
772772
callback(null, [{
773-
'label': 'suburb',
773+
'label': 'house_number',
774774
'value': '99'
775775
}]);
776776
};
@@ -831,6 +831,67 @@ module.exports.tests.bug_fixes = (test, common) => {
831831
});
832832
});
833833

834+
test('bug fix: discard single label of type "suburb"', t => {
835+
const service = (req, callback) => {
836+
callback(null, [{
837+
'label': 'suburb',
838+
'value': 'example'
839+
}]);
840+
};
841+
const controller = libpostal(service, () => true);
842+
const req = {
843+
clean: {
844+
text: 'example'
845+
},
846+
errors: []
847+
};
848+
controller(req, undefined, () => {
849+
t.deepEquals(req, {
850+
clean: {
851+
text: 'example',
852+
// parse discarded
853+
},
854+
errors: []
855+
});
856+
857+
t.end();
858+
});
859+
});
860+
861+
test('bug fix: do not discard "suburb" when accompanied by another label', t => {
862+
const service = (req, callback) => {
863+
callback(null, [{
864+
'label': 'road',
865+
'value': 'avenue'
866+
},{
867+
'label': 'suburb',
868+
'value': 'example'
869+
}]);
870+
};
871+
const controller = libpostal(service, () => true);
872+
const req = {
873+
clean: {
874+
text: 'avenue example'
875+
},
876+
errors: []
877+
};
878+
controller(req, undefined, () => {
879+
t.deepEquals(req, {
880+
clean: {
881+
text: 'avenue example',
882+
parser: 'libpostal',
883+
parsed_text: {
884+
street: 'avenue',
885+
neighbourhood: 'example'
886+
}
887+
},
888+
errors: []
889+
});
890+
891+
t.end();
892+
});
893+
});
894+
834895
};
835896

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

0 commit comments

Comments
 (0)