Browse Source

Merge pull request #347 from pelias/finish-rename-to-text-parser

Finish Renaming address_parser to text_parser
pull/351/head
Julian Simioni 9 years ago
parent
commit
6fdc581a6c
  1. 4
      helper/text_parser.js
  2. 10
      helper/types.js
  3. 6
      sanitiser/_text.js
  4. 2
      test/unit/helper/text_parser.js
  5. 4
      test/unit/helper/types.js
  6. 2
      test/unit/query/autocomplete.js
  7. 2
      test/unit/query/search.js
  8. 3
      test/unit/run.js
  9. 32
      test/unit/sanitiser/_text.js
  10. 2
      test/unit/sanitiser/search.js

4
helper/query_parser.js → helper/text_parser.js

@ -8,6 +8,10 @@ var logger = require('pelias-logger').get('api');
module.exports = {};
/*
* For performance, and to prefer POI and admin records, express a preference
* to only search coarse layers on very short text inputs.
*/
module.exports.get_layers = function get_layers(query) {
if (query.length <= 3 ) {
// no address parsing required

10
helper/types.js

@ -2,13 +2,15 @@ var type_mapping = require( '../helper/type_mapping' );
var _ = require('lodash');
/**
* Combine all types and determine the unique subset
* Different parts of the code express "preferences" for which Elasticsearch types are going to be searched
* This method decides how to combine all the preferences.
*
* @param {Array} clean_types
* @returns {Array}
*/
module.exports = function calculate_types(clean_types) {
if (!clean_types || !(clean_types.from_layers || clean_types.from_sources || clean_types.from_address_parser)) {
//Check that at least one preference of types is defined
if (!clean_types || !(clean_types.from_layers || clean_types.from_sources || clean_types.from_text_parser)) {
throw new Error('clean_types should not be null or undefined');
}
@ -33,8 +35,8 @@ module.exports = function calculate_types(clean_types) {
* Type restrictions requested by the address parser should only be used
* if both the source and layers parameters are empty, so do this last
*/
if (clean_types.from_address_parser) {
return clean_types.from_address_parser;
if (clean_types.from_text_parser) {
return clean_types.from_text_parser;
}
throw new Error('no types specified');

6
sanitiser/_text.js

@ -1,5 +1,5 @@
var check = require('check-types'),
query_parser = require('../helper/query_parser');
text_parser = require('../helper/text_parser');
// validate texts, convert types and apply defaults
function sanitize( raw, clean ){
@ -19,14 +19,14 @@ function sanitize( raw, clean ){
clean.text = raw.text;
// parse text with query parser
var parsed_text = query_parser.get_parsed_address(clean.text);
var parsed_text = text_parser.get_parsed_address(clean.text);
if (check.assigned(parsed_text)) {
clean.parsed_text = parsed_text;
}
// try to set layers from query parser results
clean.types = clean.layers || {};
clean.types.from_address_parsing = query_parser.get_layers(clean.text);
clean.types.from_text_parser = text_parser.get_layers(clean.text);
}
return messages;

2
test/unit/helper/query_parser.js → test/unit/helper/text_parser.js

@ -1,4 +1,4 @@
var parser = require('../../../helper/query_parser');
var parser = require('../../../helper/text_parser');
var type_mapping = require('../../../helper/type_mapping');
var layers_map = type_mapping.layer_with_aliases_to_type;

4
test/unit/helper/types.js

@ -33,7 +33,7 @@ module.exports.tests.no_cleaned_types = function(test, common) {
module.exports.tests.address_parser = function(test, common) {
test('address parser specifies only admin layers', function(t) {
var cleaned_types = {
from_address_parser: ['admin0'] // simplified return value from address parser
from_text_parser: ['admin0'] // simplified return value from address parser
};
var actual = types(cleaned_types);
var expected = ['admin0']; // simplified expected value for all admin layers
@ -58,7 +58,7 @@ module.exports.tests.layers_parameter_and_address_parser = function(test, common
test('layers parameter and address parser present', function(t) {
var cleaned_types = {
from_layers: ['geoname'],
from_address_parser: ['admin0'] // simplified return value from address parse
from_text_parser: ['admin0'] // simplified return value from address parse
};
var actual = types(cleaned_types);
var expected = ['geoname'];

2
test/unit/query/autocomplete.js

@ -1,6 +1,6 @@
var generate = require('../../../query/autocomplete');
var parser = require('../../../helper/query_parser');
var parser = require('../../../helper/text_parser');
module.exports.tests = {};

2
test/unit/query/search.js

@ -1,5 +1,5 @@
var generate = require('../../../query/search');
var parser = require('../../../helper/query_parser');
var parser = require('../../../helper/text_parser');
module.exports.tests = {};

3
test/unit/run.js

@ -9,7 +9,7 @@ var tests = [
require('./helper/geojsonify'),
require('./helper/labelGenerator'),
require('./helper/labelSchema'),
require('./helper/query_parser'),
require('./helper/text_parser'),
require('./helper/type_mapping'),
require('./helper/types'),
require('./middleware/confidenceScore'),
@ -29,6 +29,7 @@ var tests = [
require('./sanitiser/_single_scalar_parameters'),
require('./sanitiser/_size'),
require('./sanitiser/_sources'),
require('./sanitiser/_text'),
require('./sanitiser/autocomplete'),
require('./sanitiser/place'),
require('./sanitiser/reverse'),

32
test/unit/sanitiser/_text.js

@ -0,0 +1,32 @@
var sanitiser = require('../../../sanitiser/_text');
var type_mapping = require('../../../helper/type_mapping');
module.exports.tests = {};
module.exports.tests.text_parser = function(test, common) {
test('short input text has admin layers set ', function(t) {
var raw = {
text: 'emp' //start of empire state building
};
var clean = {
};
var messages = sanitiser(raw, clean);
t.deepEquals(messages.errors, [], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.equal(clean.types.from_text_parser, type_mapping.layer_with_aliases_to_type.coarse, 'coarse layers preferred');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANITISER _text: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

2
test/unit/sanitiser/search.js

@ -1,6 +1,6 @@
var extend = require('extend'),
search = require('../../../sanitiser/search'),
parser = require('../../../helper/query_parser'),
parser = require('../../../helper/text_parser'),
sanitize = search.sanitize,
middleware = search.middleware,
defaultError = 'invalid param \'text\': text length, must be >0';

Loading…
Cancel
Save