Browse Source

migrate outputGenerator from pelias/suggester-pipeline

pull/38/head
Peter Johnson 10 years ago
parent
commit
27e2c4e5b8
  1. 9
      helper/geojsonify.js
  2. 37
      helper/outputGenerator.js
  3. 14
      helper/outputSchema.json
  4. 6
      test/unit/controller/search.js
  5. 6
      test/unit/controller/suggest.js
  6. 2
      test/unit/helper/geojsonify.js
  7. 55
      test/unit/helper/outputSchema.js
  8. 251
      test/unit/mock/alpha3.json
  9. 3
      test/unit/run.js

9
helper/geojsonify.js

@ -1,5 +1,6 @@
var GeoJSON = require('geojson');
var GeoJSON = require('geojson'),
outputGenerator = require('./outputGenerator');
function search( docs ){
@ -37,10 +38,8 @@ function search( docs ){
if( doc.locality ){ output.locality = doc.locality; }
if( doc.neighborhood ){ output.neighborhood = doc.neighborhood; }
// map suggest output
if( doc.suggest && doc.suggest.output ){
output.text = doc.suggest.output;
}
// generate region-specific text string
output.text = outputGenerator( doc );
return output;

37
helper/outputGenerator.js

@ -0,0 +1,37 @@
var schemas = require('./outputSchema.json');
module.exports = function( record ){
var adminParts = [];
var schema = schemas.default;
if (record.alpha3 && record.alpha3.length && schemas[record.alpha3]) {
schema = schemas[record.alpha3];
}
var buildOutput = function(parts, schemaArr, record) {
for (var i=0; i<schemaArr.length; i++) {
var rec = record[schemaArr[i]];
if (rec && rec.length) {
parts.push( rec );
return parts;
}
}
return parts;
};
for (var key in schema) {
adminParts = buildOutput(adminParts, schema[key], record);
}
var outputs = [ record.name.default ].concat( adminParts );
// de-dupe outputs
outputs = outputs.filter( function( i, pos ) {
return outputs.indexOf( i ) == pos;
});
return outputs.join(', ').trim();
};

14
helper/outputSchema.json

@ -0,0 +1,14 @@
{
"USA": {
"local": ["local_admin", "locality", "neighborhood", "admin2"],
"regional": ["admin1_abbr", "admin1", "admin0"]
},
"GBR": {
"local": ["admin2","neighborhood", "local_admin", "locality"],
"regional": ["admin0","admin1"]
},
"default": {
"local": ["local_admin", "locality", "neighborhood", "admin2"],
"regional": ["admin1_abbr", "admin1", "admin0"]
}
}

6
test/unit/controller/search.js

@ -27,7 +27,8 @@ module.exports.tests.functional_success = function(test, common) {
name: 'test name1',
admin0: 'country1',
admin1: 'state1',
admin2: 'city1'
admin2: 'city1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
@ -39,7 +40,8 @@ module.exports.tests.functional_success = function(test, common) {
name: 'test name2',
admin0: 'country2',
admin1: 'state2',
admin2: 'city2'
admin2: 'city2',
text: 'test name2, city2, state2'
}
}];

6
test/unit/controller/suggest.js

@ -27,7 +27,8 @@ module.exports.tests.functional_success = function(test, common) {
name: 'test name1',
admin0: 'country1',
admin1: 'state1',
admin2: 'city1'
admin2: 'city1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
@ -39,7 +40,8 @@ module.exports.tests.functional_success = function(test, common) {
name: 'test name2',
admin0: 'country2',
admin1: 'state2',
admin2: 'city2'
admin2: 'city2',
text: 'test name2, city2, state2'
}
}];

2
test/unit/helper/geojsonify.js

@ -72,7 +72,7 @@ module.exports.tests.search = function(test, common) {
"id": "osmway/147495160",
"geo": "-0.101795,51.517806"
},
"output": "Blues Cafe, Smithfield, United Kingdom"
"output": "Blues Cafe, test3, COL"
}
}
];

55
test/unit/helper/outputSchema.js

@ -0,0 +1,55 @@
var schemas = require('../../../helper/outputSchema.json');
var alpha3 = require('../mock/alpha3.json');
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('interface', function(t) {
t.equal(typeof schemas, 'object', 'valid object');
t.equal(schemas.hasOwnProperty('default'), true, 'has default defined');
t.end();
});
};
module.exports.tests.valid = function(test, common) {
var valid_keys = ['local_admin', 'locality', 'neighborhood', 'admin2', 'admin1_abbr', 'admin1', 'admin0'];
var default_schema = {
local: ['local_admin', 'locality', 'neighborhood', 'admin2'],
regional: ['admin1_abbr', 'admin1', 'admin0']
};
var isValid = function(keys, schema) {
test('valid key/object (' + keys + ')' , function(t) {
if (keys=="default") {
t.deepEqual(schema, default_schema, 'valid default schema');
} else {
t.equal(alpha3.hasOwnProperty(keys), true, 'valid key');
}
t.equal(typeof schema, 'object', 'valid object');
t.notEqual(Object.getOwnPropertyNames(schema).length, 0, 'object not empty');
for (levels in schema) {
t.equal(Object.prototype.toString.call(schema[levels]), '[object Array]', levels+' is an array');
for (var i=0;i<schema[levels].length;i++) {
var key = schema[levels][i];
t.notEqual(valid_keys.indexOf(key), -1, key + ' is valid');
}
}
t.end();
});
}
for (keys in schemas) {
isValid(keys, schemas[keys]);
}
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('schemas: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

251
test/unit/mock/alpha3.json

@ -0,0 +1,251 @@
{
"ABW": "Aruba",
"AFG": "Afghanistan",
"AGO": "Angola",
"AIA": "Anguilla",
"ALA": "Åland Islands",
"ALB": "Albania",
"AND": "Andorra",
"ARE": "United Arab Emirates",
"ARG": "Argentina",
"ARM": "Armenia",
"ASM": "American Samoa",
"ATA": "Antarctica",
"ATF": "French Southern Territories",
"ATG": "Antigua and Barbuda",
"AUS": "Australia",
"AUT": "Austria",
"AZE": "Azerbaijan",
"BDI": "Burundi",
"BEL": "Belgium",
"BEN": "Benin",
"BES": "Bonaire, Sint Eustatius and Saba",
"BFA": "Burkina Faso",
"BGD": "Bangladesh",
"BGR": "Bulgaria",
"BHR": "Bahrain",
"BHS": "Bahamas",
"BIH": "Bosnia and Herzegovina",
"BLM": "Saint Barthélemy",
"BLR": "Belarus",
"BLZ": "Belize",
"BMU": "Bermuda",
"BOL": "Bolivia, Plurinational State of",
"BRA": "Brazil",
"BRB": "Barbados",
"BRN": "Brunei Darussalam",
"BTN": "Bhutan",
"BVT": "Bouvet Island",
"BWA": "Botswana",
"CAF": "Central African Republic",
"CAN": "Canada",
"CCK": "Cocos (Keeling) Islands",
"CHE": "Switzerland",
"CHL": "Chile",
"CHN": "China",
"CIV": "Côte d'Ivoire",
"CMR": "Cameroon",
"COD": "Congo, the Democratic Republic of the",
"COG": "Congo",
"COK": "Cook Islands",
"COL": "Colombia",
"COM": "Comoros",
"CPV": "Cabo Verde",
"CRI": "Costa Rica",
"CUB": "Cuba",
"CUW": "Curaçao",
"CXR": "Christmas Island",
"CYM": "Cayman Islands",
"CYP": "Cyprus",
"CZE": "Czech Republic",
"DEU": "Germany",
"DJI": "Djibouti",
"DMA": "Dominica",
"DNK": "Denmark",
"DOM": "Dominican Republic",
"DZA": "Algeria",
"ECU": "Ecuador",
"EGY": "Egypt",
"ERI": "Eritrea",
"ESH": "Western Sahara",
"ESP": "Spain",
"EST": "Estonia",
"ETH": "Ethiopia",
"FIN": "Finland",
"FJI": "Fiji",
"FLK": "Falkland Islands (Malvinas)",
"FRA": "France",
"FRO": "Faroe Islands",
"FSM": "Micronesia, Federated States of",
"GAB": "Gabon",
"GBR": "United Kingdom",
"GEO": "Georgia",
"GGY": "Guernsey",
"GHA": "Ghana",
"GIB": "Gibraltar",
"GIN": "Guinea",
"GLP": "Guadeloupe",
"GMB": "Gambia",
"GNB": "Guinea-Bissau",
"GNQ": "Equatorial Guinea",
"GRC": "Greece",
"GRD": "Grenada",
"GRL": "Greenland",
"GTM": "Guatemala",
"GUF": "French Guiana",
"GUM": "Guam",
"GUY": "Guyana",
"HKG": "Hong Kong",
"HMD": "Heard Island and McDonald Islands",
"HND": "Honduras",
"HRV": "Croatia",
"HTI": "Haiti",
"HUN": "Hungary",
"IDN": "Indonesia",
"IMN": "Isle of Man",
"IND": "India",
"IOT": "British Indian Ocean Territory",
"IRL": "Ireland",
"IRN": "Iran, Islamic Republic of",
"IRQ": "Iraq",
"ISL": "Iceland",
"ISR": "Israel",
"ITA": "Italy",
"JAM": "Jamaica",
"JEY": "Jersey",
"JOR": "Jordan",
"JPN": "Japan",
"KAZ": "Kazakhstan",
"KEN": "Kenya",
"KGZ": "Kyrgyzstan",
"KHM": "Cambodia",
"KIR": "Kiribati",
"KNA": "Saint Kitts and Nevis",
"KOR": "Korea, Republic of",
"KWT": "Kuwait",
"LAO": "Lao People's Democratic Republic",
"LBN": "Lebanon",
"LBR": "Liberia",
"LBY": "Libya",
"LCA": "Saint Lucia",
"LIE": "Liechtenstein",
"LKA": "Sri Lanka",
"LSO": "Lesotho",
"LTU": "Lithuania",
"LUX": "Luxembourg",
"LVA": "Latvia",
"MAC": "Macao",
"MAF": "Saint Martin (French part)",
"MAR": "Morocco",
"MCO": "Monaco",
"MDA": "Moldova, Republic of",
"MDG": "Madagascar",
"MDV": "Maldives",
"MEX": "Mexico",
"MHL": "Marshall Islands",
"MKD": "Macedonia, the former Yugoslav Republic of",
"MLI": "Mali",
"MLT": "Malta",
"MMR": "Myanmar",
"MNE": "Montenegro",
"MNG": "Mongolia",
"MNP": "Northern Mariana Islands",
"MOZ": "Mozambique",
"MRT": "Mauritania",
"MSR": "Montserrat",
"MTQ": "Martinique",
"MUS": "Mauritius",
"MWI": "Malawi",
"MYS": "Malaysia",
"MYT": "Mayotte",
"NAM": "Namibia",
"NCL": "New Caledonia",
"NER": "Niger",
"NFK": "Norfolk Island",
"NGA": "Nigeria",
"NIC": "Nicaragua",
"NIU": "Niue",
"NLD": "Netherlands",
"NOR": "Norway",
"NPL": "Nepal",
"NRU": "Nauru",
"NZL": "New Zealand",
"OMN": "Oman",
"PAK": "Pakistan",
"PAN": "Panama",
"PCN": "Pitcairn",
"PER": "Peru",
"PHL": "Philippines",
"PLW": "Palau",
"PNG": "Papua New Guinea",
"POL": "Poland",
"PRI": "Puerto Rico",
"PRK": "Korea, Democratic People's Republic of",
"PRT": "Portugal",
"PRY": "Paraguay",
"PSE": "Palestine, State of",
"PYF": "French Polynesia",
"QAT": "Qatar",
"REU": "Réunion",
"ROU": "Romania",
"RUS": "Russian Federation",
"RWA": "Rwanda",
"SAU": "Saudi Arabia",
"SDN": "Sudan",
"SEN": "Senegal",
"SGP": "Singapore",
"SGS": "South Georgia and the South Sandwich Islands",
"SHN": "Saint Helena, Ascension and Tristan da Cunha",
"SJM": "Svalbard and Jan Mayen",
"SLB": "Solomon Islands",
"SLE": "Sierra Leone",
"SLV": "El Salvador",
"SMR": "San Marino",
"SOM": "Somalia",
"SPM": "Saint Pierre and Miquelon",
"SRB": "Serbia",
"SSD": "South Sudan",
"STP": "Sao Tome and Principe",
"SUR": "Suriname",
"SVK": "Slovakia",
"SVN": "Slovenia",
"SWE": "Sweden",
"SWZ": "Swaziland",
"SXM": "Sint Maarten (Dutch part)",
"SYC": "Seychelles",
"SYR": "Syrian Arab Republic",
"TCA": "Turks and Caicos Islands",
"TCD": "Chad",
"TGO": "Togo",
"THA": "Thailand",
"TJK": "Tajikistan",
"TKL": "Tokelau",
"TKM": "Turkmenistan",
"TLS": "Timor-Leste",
"TON": "Tonga",
"TTO": "Trinidad and Tobago",
"TUN": "Tunisia",
"TUR": "Turkey",
"TUV": "Tuvalu",
"TWN": "Taiwan, Province of China",
"TZA": "Tanzania, United Republic of",
"UGA": "Uganda",
"UKR": "Ukraine",
"UMI": "United States Minor Outlying Islands",
"URY": "Uruguay",
"USA": "United States",
"UZB": "Uzbekistan",
"VAT": "Holy See (Vatican City State)",
"VCT": "Saint Vincent and the Grenadines",
"VEN": "Venezuela, Bolivarian Republic of",
"VGB": "Virgin Islands, British",
"VIR": "Virgin Islands, U.S.",
"VNM": "Viet Nam",
"VUT": "Vanuatu",
"WLF": "Wallis and Futuna",
"WSM": "Samoa",
"YEM": "Yemen",
"ZAF": "South Africa",
"ZMB": "Zambia",
"ZWE": "Zimbabwe"
}

3
test/unit/run.js

@ -12,7 +12,8 @@ var tests = [
require('./query/suggest'),
require('./query/search'),
require('./query/reverse'),
require('./helper/geojsonify')
require('./helper/geojsonify'),
require('./helper/outputSchema')
];
tests.map(function(t) {

Loading…
Cancel
Save