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. 69
      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
* 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} hit
@ -95,8 +95,8 @@ function checkForDealBreakers(req, hit) {
return false;
}
if (check.assigned(req.clean.parsed_text.state) && req.clean.parsed_text.state !== hit.admin1_abbr) {
logger.debug('[confidence][deal-breaker]: state !== 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 !== region_a');
return true;
}
@ -210,8 +210,8 @@ function propMatch(textProp, hitProp, expectEnriched) {
* @param {string|number} [hit.address.number]
* @param {string} [hit.address.street]
* @param {string|number} [hit.address.zip]
* @param {string} [hit.admin1_abbr]
* @param {string} [hit.alpha3]
* @param {string} [hit.parent.region_a]
* @param {string} [hit.parent.country_a]
* @returns {number}
*/
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.street, (hit.address ? hit.address.street : null), false);
res += propMatch(text.postalcode, (hit.address ? hit.address.zip: null), true);
res += propMatch(text.state, hit.admin1_abbr, true);
res += propMatch(text.country, hit.alpha3, true);
res += propMatch(text.state, hit.parent.region_a, true);
res += propMatch(text.country, hit.parent.country_a, true);
res /= checkCount;
}

9
middleware/dedupe.js

@ -39,8 +39,13 @@ function dedupeResults(req, res, next) {
*/
function isDifferent(item1, item2) {
try {
propMatch(item1, item2, 'admin1_abbr');
propMatch(item1, item2, 'alpha3');
if (item1.hasOwnProperty('parent') && item2.hasOwnProperty('parent')) {
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')) {
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
res.data.filter(function(place){
// 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.address.hasOwnProperty('number') ){ return false; }
if( !place.address.hasOwnProperty('street') ){ return false; }

69
middleware/renamePlacenames.js

@ -1,4 +1,4 @@
var extend = require('extend');
var _ = require('lodash');
/**
- 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
York)
*/
// config mapping of old names to new ones
var NAME_MAP = {
var ADDRESS_PROPS = {
'number': 'housenumber',
'zip': 'postalcode',
'alpha3': 'country_a',
'admin0': 'country',
'admin1': 'region',
'admin1_abbr': 'region_a',
'admin2': 'county',
'local_admin': 'localadmin',
'neighborhood': 'neighbourhood'
'street': 'street'
};
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() {
return renamePlacenames;
@ -36,31 +51,27 @@ function renamePlacenames(req, res, next) {
return next();
}
// loop through data items and remap placenames
res.data = res.data.map(renameProperties);
res.data = res.data.map(renameStuff);
next();
}
function renameProperties(place) {
var newPlace = {};
Object.keys(place).forEach(function (property) {
if (property === 'address') {
extend(newPlace, renameProperties(place[property]));
}
else {
renameProperty(place, newPlace, property);
}
});
return newPlace;
}
function renameStuff(place) {
if (place.address) {
Object.keys(ADDRESS_PROPS).forEach(function (prop) {
place[ADDRESS_PROPS[prop]] = place.address[prop];
});
}
function renameProperty(oldObj, newObj, property) {
if (!oldObj.hasOwnProperty(property)) {
return;
// merge the parent block into the top level object to flatten the structure
if (place.parent) {
PARENT_PROPS.forEach(function (prop) {
place[prop] = place.parent[prop];
})
}
newObj[(NAME_MAP[property] || property)] = oldObj[property];
return place;
}
module.exports = setup;

Loading…
Cancel
Save