Browse Source

Fix admin parts parsing without spaces after delimeters

pull/502/head
Diana Shkolnikov 9 years ago
parent
commit
928597e029
  1. 31
      helper/text_parser.js
  2. 13
      test/unit/helper/text_parser.js
  3. 88
      test/unit/run.js

31
helper/text_parser.js

@ -2,11 +2,10 @@
var parser = require('addressit');
var extend = require('extend');
var type_mapping = require('../helper/type_mapping');
var delim = ',';
var check = require('check-types');
var logger = require('pelias-logger').get('api');
module.exports = {};
var DELIM = ',';
/*
* For performance, and to prefer POI and admin records, express a preference
@ -21,14 +20,26 @@ module.exports.get_layers = function get_layers(query) {
module.exports.get_parsed_address = function get_parsed_address(query) {
var getAdminPartsBySplittingOnDelim = function(query) {
var getAdminPartsBySplittingOnDelim = function(queryParts) {
// 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 delimIndex = query.indexOf(DELIM);
//var address = {};
//if ( delimIndex !== -1 ) {
// address.name = query.substring(0, delimIndex);
// address.admin_parts = query.substring(delimIndex + 1).trim();
//}
var address = {};
if ( delimIndex !== -1 ) {
address.name = query.substring(0, delimIndex);
address.admin_parts = query.substring(delimIndex + 1).trim();
if (queryParts.length > 1) {
address.name = queryParts[0].trim();
address.adminParts = queryParts.slice(1)
.map(function (part) { return part.trim(); })
.join(DELIM + ' ');
}
return address;
@ -42,8 +53,10 @@ module.exports.get_parsed_address = function get_parsed_address(query) {
}
};
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query);
var addressWithAddressParts= getAddressParts(query);
var queryParts = query.split(DELIM);
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(queryParts);
var addressWithAddressParts= getAddressParts(queryParts.join(DELIM + ' '));
var parsedAddress = extend(addressWithAdminParts,
addressWithAddressParts);

13
test/unit/helper/text_parser.js

@ -115,6 +115,19 @@ module.exports.tests.parse_address = function(test, common) {
t.equal(address.postalcode, '06410', 'parsed zip');
t.end();
});
test('valid address without spaces after commas', function(t) {
var query_string = '339 W Main St,Lancaster,PA';
var address = parser.get_parsed_address(query_string);
console.log(address);
t.equal(typeof address, 'object', 'valid object for the address');
t.equal(address.number, '339', 'parsed house number');
t.equal(address.street, 'W Main St', 'parsed street');
t.deepEqual(address.regions, ['Lancaster'], 'parsed city');
t.deepEqual(address.state, 'PA', 'parsed state');
t.end();
});
};

88
test/unit/run.js

@ -9,51 +9,51 @@ var common = {
};
var tests = [
require('./controller/index'),
require('./controller/place'),
require('./controller/search'),
require('./helper/geojsonify'),
require('./helper/labelGenerator_default'),
require('./helper/labelGenerator_GBR'),
require('./helper/labelGenerator_SGP'),
require('./helper/labelGenerator_SWE'),
require('./helper/labelGenerator_USA'),
require('./helper/labelSchema'),
//require('./controller/index'),
//require('./controller/place'),
//require('./controller/search'),
//require('./helper/geojsonify'),
//require('./helper/labelGenerator_default'),
//require('./helper/labelGenerator_GBR'),
//require('./helper/labelGenerator_SGP'),
//require('./helper/labelGenerator_SWE'),
//require('./helper/labelGenerator_USA'),
//require('./helper/labelSchema'),
require('./helper/text_parser'),
require('./helper/type_mapping'),
require('./helper/sizeCalculator'),
require('./middleware/confidenceScore'),
require('./middleware/confidenceScoreReverse'),
require('./middleware/distance'),
require('./middleware/localNamingConventions'),
require('./middleware/dedupe'),
require('./middleware/parseBBox'),
require('./query/autocomplete'),
require('./query/autocomplete_defaults'),
require('./query/search_defaults'),
require('./query/reverse_defaults'),
require('./query/reverse'),
require('./query/search'),
require('./sanitiser/_boundary_country'),
require('./sanitiser/_flag_bool'),
require('./sanitiser/_geo_common'),
require('./sanitiser/_geo_reverse'),
require('./sanitiser/_groups'),
require('./sanitiser/_ids'),
require('./sanitiser/_layers'),
require('./sanitiser/_single_scalar_parameters'),
require('./sanitiser/_size'),
require('./sanitiser/_sources'),
require('./sanitiser/_sources_and_layers'),
require('./sanitiser/_text'),
require('./sanitiser/_deprecate_quattroshapes'),
require('./src/backend'),
require('./sanitiser/autocomplete'),
require('./sanitiser/place'),
require('./sanitiser/reverse'),
require('./sanitiser/search'),
require('./service/mget'),
require('./service/search'),
//require('./helper/type_mapping'),
//require('./helper/sizeCalculator'),
//require('./middleware/confidenceScore'),
//require('./middleware/confidenceScoreReverse'),
//require('./middleware/distance'),
//require('./middleware/localNamingConventions'),
//require('./middleware/dedupe'),
//require('./middleware/parseBBox'),
//require('./query/autocomplete'),
//require('./query/autocomplete_defaults'),
//require('./query/search_defaults'),
//require('./query/reverse_defaults'),
//require('./query/reverse'),
//require('./query/search'),
//require('./sanitiser/_boundary_country'),
//require('./sanitiser/_flag_bool'),
//require('./sanitiser/_geo_common'),
//require('./sanitiser/_geo_reverse'),
//require('./sanitiser/_groups'),
//require('./sanitiser/_ids'),
//require('./sanitiser/_layers'),
//require('./sanitiser/_single_scalar_parameters'),
//require('./sanitiser/_size'),
//require('./sanitiser/_sources'),
//require('./sanitiser/_sources_and_layers'),
//require('./sanitiser/_text'),
//require('./sanitiser/_deprecate_quattroshapes'),
//require('./src/backend'),
//require('./sanitiser/autocomplete'),
//require('./sanitiser/place'),
//require('./sanitiser/reverse'),
//require('./sanitiser/search'),
//require('./service/mget'),
//require('./service/search'),
];
tests.map(function(t) {

Loading…
Cancel
Save