mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.3 KiB
79 lines
2.3 KiB
9 years ago
|
|
||
|
var parser = require('addressit');
|
||
|
var extend = require('extend');
|
||
9 years ago
|
var type_mapping = require('../helper/type_mapping');
|
||
9 years ago
|
var delim = ',';
|
||
9 years ago
|
var check = require('check-types');
|
||
|
var logger = require('pelias-logger').get('api');
|
||
9 years ago
|
|
||
9 years ago
|
module.exports = {};
|
||
|
|
||
9 years ago
|
/*
|
||
|
* For performance, and to prefer POI and admin records, express a preference
|
||
|
* to only search coarse layers on very short text inputs.
|
||
|
*/
|
||
9 years ago
|
module.exports.get_layers = function get_layers(query) {
|
||
|
if (query.length <= 3 ) {
|
||
|
// no address parsing required
|
||
9 years ago
|
return type_mapping.layer_with_aliases_to_type.coarse;
|
||
9 years ago
|
}
|
||
|
};
|
||
|
|
||
|
module.exports.get_parsed_address = function get_parsed_address(query) {
|
||
9 years ago
|
|
||
9 years ago
|
var getAdminPartsBySplittingOnDelim = function(query) {
|
||
|
// naive approach - for admin matching during query time
|
||
|
// split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny'
|
||
|
var delimIndex = query.indexOf(delim);
|
||
|
var address = {};
|
||
|
if ( delimIndex !== -1 ) {
|
||
|
address.name = query.substring(0, delimIndex);
|
||
|
address.admin_parts = query.substring(delimIndex + 1).trim();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return address;
|
||
|
};
|
||
9 years ago
|
|
||
9 years ago
|
var getAddressParts = function(query) {
|
||
9 years ago
|
// perform full address parsing
|
||
|
// except on queries so short they obviously can't contain an address
|
||
9 years ago
|
if (query.length > 3) {
|
||
|
return parser( query );
|
||
|
}
|
||
9 years ago
|
};
|
||
|
|
||
|
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query);
|
||
9 years ago
|
var addressWithAddressParts= getAddressParts(query);
|
||
9 years ago
|
|
||
9 years ago
|
var parsedAddress = extend(addressWithAdminParts,
|
||
9 years ago
|
addressWithAddressParts);
|
||
9 years ago
|
|
||
|
var address_parts = [ 'name',
|
||
|
'number',
|
||
|
'street',
|
||
|
'city',
|
||
|
'state',
|
||
|
'country',
|
||
|
'postalcode',
|
||
|
'regions',
|
||
9 years ago
|
'admin_parts'
|
||
9 years ago
|
];
|
||
|
|
||
9 years ago
|
var parsed_text = {};
|
||
9 years ago
|
|
||
9 years ago
|
address_parts.forEach(function(part){
|
||
9 years ago
|
if (parsedAddress[part]) {
|
||
9 years ago
|
parsed_text[part] = parsedAddress[part];
|
||
9 years ago
|
}
|
||
|
});
|
||
|
|
||
9 years ago
|
// if all we found was regions, ignore it as it is not enough information to make smarter decisions
|
||
|
if (Object.keys(parsed_text).length === 1 && !check.undefined(parsed_text.regions))
|
||
|
{
|
||
|
logger.info('Ignoring address parser output, regions only');
|
||
|
return null;
|
||
|
}
|
||
|
|
||
9 years ago
|
return parsed_text;
|
||
9 years ago
|
};
|