Browse Source

Middleware: remove all use of alpha3/admin0/admin1

pull/426/head
Diana Shkolnikov 9 years ago committed by Julian Simioni
parent
commit
83de24d3c4
  1. 14
      middleware/confidenceScore.js
  2. 9
      middleware/dedupe.js
  3. 2
      middleware/localNamingConventions.js
  4. 65
      middleware/renamePlacenames.js

14
middleware/confidenceScore.js

@ -84,7 +84,7 @@ function computeConfidenceScore(req, mean, stdev, hit) {
/* /*
* Check for clearly mismatching properties in a result * Check for clearly mismatching properties in a result
* zip code and state (admin1) are currently checked if present * zip code and state (region) are currently checked if present
* *
* @param {object|undefined} text * @param {object|undefined} text
* @param {object} hit * @param {object} hit
@ -95,8 +95,8 @@ function checkForDealBreakers(req, hit) {
return false; return false;
} }
if (check.assigned(req.clean.parsed_text.state) && req.clean.parsed_text.state !== hit.admin1_abbr) { if (check.assigned(req.clean.parsed_text.state) && req.clean.parsed_text.state !== hit.parent.region_a) {
logger.debug('[confidence][deal-breaker]: state !== admin1_abbr'); logger.debug('[confidence][deal-breaker]: state !== region_a');
return true; return true;
} }
@ -210,8 +210,8 @@ function propMatch(textProp, hitProp, expectEnriched) {
* @param {string|number} [hit.address.number] * @param {string|number} [hit.address.number]
* @param {string} [hit.address.street] * @param {string} [hit.address.street]
* @param {string|number} [hit.address.zip] * @param {string|number} [hit.address.zip]
* @param {string} [hit.admin1_abbr] * @param {string} [hit.parent.region_a]
* @param {string} [hit.alpha3] * @param {string} [hit.parent.country_a]
* @returns {number} * @returns {number}
*/ */
function checkAddress(text, hit) { function checkAddress(text, hit) {
@ -222,8 +222,8 @@ function checkAddress(text, hit) {
res += propMatch(text.number, (hit.address ? hit.address.number : null), false); res += propMatch(text.number, (hit.address ? hit.address.number : null), false);
res += propMatch(text.street, (hit.address ? hit.address.street : null), false); res += propMatch(text.street, (hit.address ? hit.address.street : null), false);
res += propMatch(text.postalcode, (hit.address ? hit.address.zip: null), true); res += propMatch(text.postalcode, (hit.address ? hit.address.zip: null), true);
res += propMatch(text.state, hit.admin1_abbr, true); res += propMatch(text.state, hit.parent.region_a, true);
res += propMatch(text.country, hit.alpha3, true); res += propMatch(text.country, hit.parent.country_a, true);
res /= checkCount; res /= checkCount;
} }

9
middleware/dedupe.js

@ -39,8 +39,13 @@ function dedupeResults(req, res, next) {
*/ */
function isDifferent(item1, item2) { function isDifferent(item1, item2) {
try { try {
propMatch(item1, item2, 'admin1_abbr'); if (item1.hasOwnProperty('parent') && item2.hasOwnProperty('parent')) {
propMatch(item1, item2, 'alpha3'); propMatch(item1.parent, item2.parent, 'region_a');
propMatch(item1.parent, item2.parent, 'country');
}
else if (item1.parent !== item2.parent) {
throw new Error('different');
}
if (item1.hasOwnProperty('name') && item2.hasOwnProperty('name')) { if (item1.hasOwnProperty('name') && item2.hasOwnProperty('name')) {
propMatch(item1.name, item2.name, 'default'); propMatch(item1.name, item2.name, 'default');

2
middleware/localNamingConventions.js

@ -13,7 +13,7 @@ function applyLocalNamingConventions(req, res, next) {
// loop through data items and flip relevant number/street // loop through data items and flip relevant number/street
res.data.filter(function(place){ res.data.filter(function(place){
// only relevant for German addresses // only relevant for German addresses
if( 'DEU' !== place.alpha3 ){ return false; } if( 'DEU' !== place.parent.country_a ){ return false; }
if( !place.hasOwnProperty('address') ){ return false; } if( !place.hasOwnProperty('address') ){ return false; }
if( !place.address.hasOwnProperty('number') ){ return false; } if( !place.address.hasOwnProperty('number') ){ return false; }
if( !place.address.hasOwnProperty('street') ){ return false; } if( !place.address.hasOwnProperty('street') ){ return false; }

65
middleware/renamePlacenames.js

@ -1,4 +1,4 @@
var extend = require('extend'); var _ = require('lodash');
/** /**
- P is a preferred English name - P is a preferred English name
@ -11,19 +11,34 @@ var extend = require('extend');
- A is an abbreviation or code for the place (e.g. "NYC" for New - A is an abbreviation or code for the place (e.g. "NYC" for New
York) York)
*/ */
// config mapping of old names to new ones
var NAME_MAP = { var ADDRESS_PROPS = {
'number': 'housenumber', 'number': 'housenumber',
'zip': 'postalcode', 'zip': 'postalcode',
'alpha3': 'country_a', 'street': 'street'
'admin0': 'country',
'admin1': 'region',
'admin1_abbr': 'region_a',
'admin2': 'county',
'local_admin': 'localadmin',
'neighborhood': 'neighbourhood'
}; };
var PARENT_PROPS = [
'country',
'country_id',
'country_a',
'region',
'region_id',
'region_a',
'county',
'county_id',
'county_a',
'localadmin',
'localadmin_id',
'localadmin_a',
'locality',
'locality_id',
'locality_a',
'neighbourhood',
'neighbourhood_id'
];
function setup() { function setup() {
return renamePlacenames; return renamePlacenames;
@ -36,31 +51,27 @@ function renamePlacenames(req, res, next) {
return next(); return next();
} }
// loop through data items and remap placenames res.data = res.data.map(renameStuff);
res.data = res.data.map(renameProperties);
next(); next();
} }
function renameProperties(place) { function renameStuff(place) {
var newPlace = {};
Object.keys(place).forEach(function (property) { if (place.address) {
if (property === 'address') { Object.keys(ADDRESS_PROPS).forEach(function (prop) {
extend(newPlace, renameProperties(place[property])); place[ADDRESS_PROPS[prop]] = place.address[prop];
}
else {
renameProperty(place, newPlace, property);
}
}); });
return newPlace; }
}
function renameProperty(oldObj, newObj, property) { // merge the parent block into the top level object to flatten the structure
if (!oldObj.hasOwnProperty(property)) { if (place.parent) {
return; PARENT_PROPS.forEach(function (prop) {
place[prop] = place.parent[prop];
})
} }
newObj[(NAME_MAP[property] || property)] = oldObj[property]; return place;
} }
module.exports = setup; module.exports = setup;

Loading…
Cancel
Save