Browse Source

Fixed the cosmetic concerns for the PR

pull/1058/head
Alex Loyko 7 years ago
parent
commit
5dd3cdb9aa
  1. 2
      controller/search.js
  2. 92
      helper/intersectionsParsing.js
  3. 3
      query/search_intersections.js
  4. 2
      routes/v1.js
  5. 2
      test/unit/run.js
  6. 75
      util/arrayHelper.js

2
controller/search.js

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const util = require('util');
const searchService = require('../service/search'); const searchService = require('../service/search');
const logger = require('pelias-logger').get('api'); const logger = require('pelias-logger').get('api');
@ -22,7 +21,6 @@ function setup( apiConfig, esclient, query, should_execute ){
const debugLog = new Debug('controller:search'); const debugLog = new Debug('controller:search');
let cleanOutput = _.cloneDeep(req.clean); let cleanOutput = _.cloneDeep(req.clean);
if (logging.isDNT(req)) { if (logging.isDNT(req)) {
cleanOutput = logging.removeFields(cleanOutput); cleanOutput = logging.removeFields(cleanOutput);
} }

92
helper/intersectionsParsing.js

@ -9,59 +9,43 @@ this function returns an object that denotes an intersection of form:
module.exports = function parseIntersections(text) { module.exports = function parseIntersections(text) {
var str1 = '', str2 = ''; var str1 = '', str2 = '';
if(text.trim().length > 1) { if(text.trim().length > 1) {
// var words = text.toLowerCase().split(' ');
var words = _.words(text.toLowerCase(), /[^ ]+/g); var words = _.words(text.toLowerCase(), /[^ ]+/g);
// changes 'e15' to 'East 15', etc. // changes 'e15' to 'East 15', etc.
words = directionalSanitizer(words); words = words.map(directionalSanitizer)
.reduce(function(a, b) { return a.concat(b); }, []);
// changes '6' to '6th', etc // changes '6' to '6th', etc
words = addOrdinality(words); words = words.map(addOrdinality);
// only treat input as intersection if contains '&' or 'and' // only treat input as intersection if contains '&' or 'and'
const delimiter = _.includes(text, '&') ? '&' : 'and'; const delimiter = _.includes(text, '&') ? '&' : 'and';
const delimiterIndex = words.indexOf(delimiter); const delimiterIndex = words.indexOf(delimiter);
str1 = wordsToSentence(words, 0, delimiterIndex); str1 = words.slice(0,delimiterIndex).join(' ');
str2 = wordsToSentence(words, delimiterIndex+1, words.length); str2 = words.slice(delimiterIndex+1, words.length).join(' ');
} else {
throw 'Missing streets in the intersection';
} }
return { street1: str1, street2: str2 }; return { street1: str1, street2: str2 };
}; };
// intended for intersections only // intended for intersections only
// this function turns '77' into '77th', '3' into '3rd', etc // this function turns '77' into '77th', '3' into '3rd', etc
function addOrdinality(arr) { function addOrdinality(elmnt) {
arr.forEach( function (elmnt, index) {
// is it only numbers
let isNum = /^\d+$/.test(elmnt); let isNum = /^\d+$/.test(elmnt);
if (isNum) { if (isNum) {
switch(elmnt[elmnt.length-1]){ var cent = elmnt % 100;
case '1': if (cent >= 10 && cent <= 20) { return `${elmnt}th`; }
elmnt += 'st'; var dec = elmnt % 10;
arr[index] = elmnt; if (dec === 1) { return `${elmnt}st`; }
break; if (dec === 2) { return `${elmnt}nd`; }
case '2': if (dec === 3) { return `${elmnt}rd`; }
elmnt += 'nd'; return `${elmnt}th`;
arr[index] = elmnt;
break;
case '3':
elmnt += 'rd';
arr[index] = elmnt;
break;
default :
elmnt += 'th';
arr[index] = elmnt;
} }
} return elmnt;
});
return arr;
} }
// intended to do the conversions like: // intended to do the conversions like:
// 'w28' -> 'West 28' // 'w28' -> 'West 28'
// 'e17' -> 'East 17' // 'e17' -> 'East 17'
function directionalSanitizer(arr){
const mapping = { const mapping = {
e : 'East', e : 'East',
w : 'West', w : 'West',
@ -73,31 +57,19 @@ function directionalSanitizer(arr){
sw: 'Southwest' sw: 'Southwest'
}; };
for (let i = 0; i < arr.length; i++) { function directionalSanitizer(word){
let streetNum; let streetNum;
switch (stringStartsWithDirAcronym(arr[i])) { switch (stringStartsWithDirAcronym(word)) {
case 1: case 1:
streetNum = arr[i].substring(1); streetNum = word.substring(1);
arr[i] = mapping[arr[i][0]]; word = mapping[word[0]];
if (i+1 === arr.length) { return [word, streetNum];
arr.push(streetNum);
} else {
arr.splice(i+1,0,streetNum);
}
break;
case 2: case 2:
streetNum = arr[i].substring(2); streetNum = word.substring(2);
arr[i] = mapping[arr[i].substring(0,2)]; word = mapping[word.substring(0,2)];
if(i+1 === arr.length) { return [word, streetNum];
arr.push(streetNum);
} else {
arr.splice(i+1,0,streetNum);
} }
break; return word;
}
}
return arr;
} }
/* /*
@ -110,25 +82,11 @@ function stringStartsWithDirAcronym (text) {
/^\d$/.test(text[2])) { /^\d$/.test(text[2])) {
return 2; return 2;
} }
} } else if (text.length > 1) {
if (text.length > 1) {
if ((text[0].toLowerCase() === 'e' || text[0].toLowerCase() === 'w' || if ((text[0].toLowerCase() === 'e' || text[0].toLowerCase() === 'w' ||
text[0].toLowerCase() === 'n' || text[0].toLowerCase() === 's') && /^\d$/.test(text[1])) { text[0].toLowerCase() === 'n' || text[0].toLowerCase() === 's') && /^\d$/.test(text[1])) {
return 1; return 1;
} }
} }
return 0; return 0;
} }
function wordsToSentence(arr, start, end) {
var sentence = '';
for (let i = start; i < end; i++) {
sentence += arr[i];
if (i < (end - 1)) {
sentence += ' ';
}
}
return sentence;
}

3
query/search_intersections.js

@ -1,6 +1,3 @@
/* eslint-disable */
'use strict'; 'use strict';
const peliasQuery = require('pelias-query'); const peliasQuery = require('pelias-query');

2
routes/v1.js

@ -143,7 +143,6 @@ function addRoutes(app, peliasConfig) {
); );
const libpostalShouldExecute = all( const libpostalShouldExecute = all(
//not(isIntersectionLayer),
not(hasResponseData), not(hasResponseData),
not(hasRequestErrors), not(hasRequestErrors),
not(isRequestSourcesOnlyWhosOnFirst) not(isRequestSourcesOnlyWhosOnFirst)
@ -221,7 +220,6 @@ function addRoutes(app, peliasConfig) {
// call very old prod query if addressit was the parser // call very old prod query if addressit was the parser
const oldProdQueryShouldExecute = all( const oldProdQueryShouldExecute = all(
not(hasRequestErrors), not(hasRequestErrors),
//not(isIntersectionLayer),
isAddressItParse isAddressItParse
); );

2
test/unit/run.js

@ -27,12 +27,14 @@ var tests = [
require('./controller/predicates/is_addressit_parse'), require('./controller/predicates/is_addressit_parse'),
require('./controller/predicates/is_admin_only_analysis'), require('./controller/predicates/is_admin_only_analysis'),
require('./controller/predicates/is_coarse_reverse'), require('./controller/predicates/is_coarse_reverse'),
require('./controller/predicates/is_intersection_layer'),
require('./controller/predicates/is_only_non_admin_layers'), require('./controller/predicates/is_only_non_admin_layers'),
require('./controller/predicates/is_request_sources_only_whosonfirst'), require('./controller/predicates/is_request_sources_only_whosonfirst'),
require('./helper/debug'), require('./helper/debug'),
require('./helper/diffPlaces'), require('./helper/diffPlaces'),
require('./helper/geojsonify_place_details'), require('./helper/geojsonify_place_details'),
require('./helper/geojsonify'), require('./helper/geojsonify'),
require('./helper/intersectionsParsing'),
require('./helper/logging'), require('./helper/logging'),
require('./helper/type_mapping'), require('./helper/type_mapping'),
require('./helper/sizeCalculator'), require('./helper/sizeCalculator'),

75
util/arrayHelper.js

@ -1,75 +0,0 @@
module.exports.removeWhitespaceElements = function (arr) {
for(let i = 0; i < arr.length; i++) {
if(arr[i] === '') {
arr.splice(i, 1);
i--;
}
}
return arr;
};
// intended for intersections only
// this function turns '77' into '77th', '3' into '3rd', etc
module.exports.addOrdinality = function (arr) {
arr.forEach( function (elmnt, index) {
// is it only numbers
let isNum = /^\d+$/.test(elmnt);
if(isNum) {
switch(elmnt[elmnt.length-1]){
case '1':
elmnt += 'st';
arr[index] = elmnt;
break;
case '2':
elmnt += 'nd';
arr[index] = elmnt;
break;
case '3':
elmnt += 'rd';
arr[index] = elmnt;
break;
default :
elmnt += 'th';
arr[index] = elmnt;
}
}
});
return arr;
};
// intended to do the conversions like:
// 'w28' -> 'West 28'
// 'e17' -> 'East 17'
module.exports.EWStreetsSanitizer = function(arr){
const mapping = {
e : 'East',
w : 'West'
};
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > 1) {
if((arr[i][0].toLowerCase() === 'e' || arr[i][0].toLowerCase() === 'w') && /^\d$/.test(arr[i][1])) {
let streetNum = arr[i].substring(1);
arr[i] = mapping[arr[i][0]];
if(i+1 === arr.length) {
arr.push(streetNum);
} else {
arr.splice(i+1,0,streetNum);
}
}
}
}
return arr;
};
module.exports.wordsToSentence = function (arr, start, end) {
var sentence = '';
for (let i = start; i < end; i++) {
sentence += arr[i];
if (i < (end - 1)) {
sentence += ' ';
}
}
return sentence;
};
Loading…
Cancel
Save