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 parser = require('addressit');
var extend = require('extend'); var extend = require('extend');
var type_mapping = require('../helper/type_mapping'); var type_mapping = require('../helper/type_mapping');
var delim = ',';
var check = require('check-types'); var check = require('check-types');
var logger = require('pelias-logger').get('api'); var logger = require('pelias-logger').get('api');
module.exports = {}; var DELIM = ',';
/* /*
* For performance, and to prefer POI and admin records, express a preference * 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) { 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 // naive approach - for admin matching during query time
// split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny' // 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 = {}; var address = {};
if ( delimIndex !== -1 ) {
address.name = query.substring(0, delimIndex); if (queryParts.length > 1) {
address.admin_parts = query.substring(delimIndex + 1).trim(); address.name = queryParts[0].trim();
address.adminParts = queryParts.slice(1)
.map(function (part) { return part.trim(); })
.join(DELIM + ' ');
} }
return address; return address;
@ -42,8 +53,10 @@ module.exports.get_parsed_address = function get_parsed_address(query) {
} }
}; };
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query); var queryParts = query.split(DELIM);
var addressWithAddressParts= getAddressParts(query);
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(queryParts);
var addressWithAddressParts= getAddressParts(queryParts.join(DELIM + ' '));
var parsedAddress = extend(addressWithAdminParts, var parsedAddress = extend(addressWithAdminParts,
addressWithAddressParts); 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.equal(address.postalcode, '06410', 'parsed zip');
t.end(); 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 = [ var tests = [
require('./controller/index'), //require('./controller/index'),
require('./controller/place'), //require('./controller/place'),
require('./controller/search'), //require('./controller/search'),
require('./helper/geojsonify'), //require('./helper/geojsonify'),
require('./helper/labelGenerator_default'), //require('./helper/labelGenerator_default'),
require('./helper/labelGenerator_GBR'), //require('./helper/labelGenerator_GBR'),
require('./helper/labelGenerator_SGP'), //require('./helper/labelGenerator_SGP'),
require('./helper/labelGenerator_SWE'), //require('./helper/labelGenerator_SWE'),
require('./helper/labelGenerator_USA'), //require('./helper/labelGenerator_USA'),
require('./helper/labelSchema'), //require('./helper/labelSchema'),
require('./helper/text_parser'), require('./helper/text_parser'),
require('./helper/type_mapping'), //require('./helper/type_mapping'),
require('./helper/sizeCalculator'), //require('./helper/sizeCalculator'),
require('./middleware/confidenceScore'), //require('./middleware/confidenceScore'),
require('./middleware/confidenceScoreReverse'), //require('./middleware/confidenceScoreReverse'),
require('./middleware/distance'), //require('./middleware/distance'),
require('./middleware/localNamingConventions'), //require('./middleware/localNamingConventions'),
require('./middleware/dedupe'), //require('./middleware/dedupe'),
require('./middleware/parseBBox'), //require('./middleware/parseBBox'),
require('./query/autocomplete'), //require('./query/autocomplete'),
require('./query/autocomplete_defaults'), //require('./query/autocomplete_defaults'),
require('./query/search_defaults'), //require('./query/search_defaults'),
require('./query/reverse_defaults'), //require('./query/reverse_defaults'),
require('./query/reverse'), //require('./query/reverse'),
require('./query/search'), //require('./query/search'),
require('./sanitiser/_boundary_country'), //require('./sanitiser/_boundary_country'),
require('./sanitiser/_flag_bool'), //require('./sanitiser/_flag_bool'),
require('./sanitiser/_geo_common'), //require('./sanitiser/_geo_common'),
require('./sanitiser/_geo_reverse'), //require('./sanitiser/_geo_reverse'),
require('./sanitiser/_groups'), //require('./sanitiser/_groups'),
require('./sanitiser/_ids'), //require('./sanitiser/_ids'),
require('./sanitiser/_layers'), //require('./sanitiser/_layers'),
require('./sanitiser/_single_scalar_parameters'), //require('./sanitiser/_single_scalar_parameters'),
require('./sanitiser/_size'), //require('./sanitiser/_size'),
require('./sanitiser/_sources'), //require('./sanitiser/_sources'),
require('./sanitiser/_sources_and_layers'), //require('./sanitiser/_sources_and_layers'),
require('./sanitiser/_text'), //require('./sanitiser/_text'),
require('./sanitiser/_deprecate_quattroshapes'), //require('./sanitiser/_deprecate_quattroshapes'),
require('./src/backend'), //require('./src/backend'),
require('./sanitiser/autocomplete'), //require('./sanitiser/autocomplete'),
require('./sanitiser/place'), //require('./sanitiser/place'),
require('./sanitiser/reverse'), //require('./sanitiser/reverse'),
require('./sanitiser/search'), //require('./sanitiser/search'),
require('./service/mget'), //require('./service/mget'),
require('./service/search'), //require('./service/search'),
]; ];
tests.map(function(t) { tests.map(function(t) {

Loading…
Cancel
Save