Browse Source

libpostal changes

- updated for libpostal interface
- added tests for text_parser
pull/666/head
Stephen Hess 9 years ago
parent
commit
5821ae6e81
  1. 87
      query/text_parser.js
  2. 78
      test/unit/query/text_parser.js

87
query/text_parser.js

@ -1,42 +1,18 @@
var logger = require('pelias-logger').get('api');
var placeTypes = require('../helper/placeTypes');
/*
This list should only contain admin fields we are comfortable matching in the case
when we can't identify parts of an address. This shouldn't contain fields like country_a
or postalcode because we should only try to match those when we're sure that's what they are.
*/
var adminFields = placeTypes.concat([
'region_a'
]);
/**
@todo: refactor me
**/
// all the address parsing logic
function addParsedVariablesToQueryVariables( parsed_text, vs ){
// ==== add parsed matches [address components] ====
// is it a street address?
// var isStreetAddress = parsed_text.hasOwnProperty('number') && parsed_text.hasOwnProperty('street');
// if( isStreetAddress ){
// vs.var( 'input:name', parsed_text.number + ' ' + parsed_text.street );
// }
//
// // ?
// else if( parsed_text.admin_parts ) {
// vs.var( 'input:name', parsed_text.name );
// }
//
// // ?
// else {
// logger.warn( 'chaos monkey asks: what happens now?' );
// logger.warn( parsed_text );
// try{ throw new Error(); } catch(e){ logger.warn( e.stack ); } // print a stack trace
// }
// query - Mexitaly, Sunoco, Lowes
if (parsed_text.hasOwnProperty('query')) {
vs.var('input:query', parsed_text.query);
}
// ==== add parsed matches [address components] ====
// categories - restaurants, hotels, bars
if (parsed_text.hasOwnProperty('category')) {
vs.var('input:category', parsed_text.category);
}
// house number
if( parsed_text.hasOwnProperty('number') ){
@ -48,6 +24,16 @@ function addParsedVariablesToQueryVariables( parsed_text, vs ){
vs.var( 'input:street', parsed_text.street );
}
// neighbourhood
if (parsed_text.hasOwnProperty('neighbourhood')) {
vs.var( 'input:neighbourhood', parsed_text.neighbourhood);
}
// borough
if (parsed_text.hasOwnProperty('borough')) {
vs.var( 'input:borough', parsed_text.borough);
}
// postal code
if( parsed_text.hasOwnProperty('postalcode') ){
vs.var( 'input:postcode', parsed_text.postalcode );
@ -57,43 +43,24 @@ function addParsedVariablesToQueryVariables( parsed_text, vs ){
// city
if( parsed_text.hasOwnProperty('city') ){
vs.var( 'input:county', parsed_text.city );
vs.var( 'input:locality', parsed_text.city );
}
// county
if( parsed_text.hasOwnProperty('county') ){
vs.var( 'input:county', parsed_text.county );
}
// state
if( parsed_text.hasOwnProperty('state') ){
vs.var( 'input:region_a', parsed_text.state );
vs.var( 'input:region', parsed_text.state );
}
// country
if( parsed_text.hasOwnProperty('country') ){
vs.var( 'input:country_a', parsed_text.country );
vs.var( 'input:country', parsed_text.country );
}
// ==== deal with the 'leftover' components ====
// @todo: clean up this code
// a concept called 'leftovers' which is just 'admin_parts' /or 'regions'.
// var leftoversString = '';
// if( parsed_text.hasOwnProperty('admin_parts') ){
// leftoversString = parsed_text.admin_parts;
// }
// else if( parsed_text.hasOwnProperty('regions') ){
// leftoversString = parsed_text.regions.join(' ');
// }
//
// // if we have 'leftovers' then assign them to any fields which
// // currently don't have a value assigned.
// if( leftoversString.length ){
//
// // cycle through fields and set fields which
// // are still currently unset
// adminFields.forEach( function( key ){
// if( !vs.isset( 'input:' + key ) ){
// vs.var( 'input:' + key, leftoversString );
// }
// });
// }
}
module.exports = addParsedVariablesToQueryVariables;

78
test/unit/query/text_parser.js

@ -0,0 +1,78 @@
var VariableStore = require('pelias-query').Vars;
var text_parser = require('../../../query/text_parser');
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof text_parser, 'function', 'valid function');
t.end();
});
};
module.exports.tests.query = function(test, common) {
test('parsed_text without properties should leave vs properties unset', function(t) {
var parsed_text = {};
var vs = new VariableStore();
text_parser(parsed_text, vs);
t.false(vs.isset('input:query'));
t.false(vs.isset('input:category'));
t.false(vs.isset('input:housenumber'));
t.false(vs.isset('input:street'));
t.false(vs.isset('input:neighbourhood'));
t.false(vs.isset('input:borough'));
t.false(vs.isset('input:postcode'));
t.false(vs.isset('input:locality'));
t.false(vs.isset('input:county'));
t.false(vs.isset('input:region'));
t.false(vs.isset('input:country'));
t.end();
});
test('parsed_text without properties should leave vs properties unset', function(t) {
var parsed_text = {
query: 'query value',
category: 'category value',
number: 'number value',
street: 'street value',
neighbourhood: 'neighbourhood value',
borough: 'borough value',
postalcode: 'postalcode value',
city: 'city value',
county: 'county value',
state: 'state value',
country: 'country value'
};
var vs = new VariableStore();
text_parser(parsed_text, vs);
t.equals(vs.var('input:query').toString(), 'query value');
t.equals(vs.var('input:category').toString(), 'category value');
t.equals(vs.var('input:housenumber').toString(), 'number value');
t.equals(vs.var('input:street').toString(), 'street value');
t.equals(vs.var('input:neighbourhood').toString(), 'neighbourhood value');
t.equals(vs.var('input:borough').toString(), 'borough value');
t.equals(vs.var('input:postcode').toString(), 'postalcode value');
t.equals(vs.var('input:locality').toString(), 'city value');
t.equals(vs.var('input:county').toString(), 'county value');
t.equals(vs.var('input:region').toString(), 'state value');
t.equals(vs.var('input:country').toString(), 'country value');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('text_parser ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save