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

128
helper/intersectionsParsing.js

@ -9,95 +9,67 @@ this function returns an object that denotes an intersection of form:
module.exports = function parseIntersections(text) {
var str1 = '', str2 = '';
if(text.trim().length > 1) {
// var words = text.toLowerCase().split(' ');
var words = _.words(text.toLowerCase(), /[^ ]+/g);
// 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
words = addOrdinality(words);
words = words.map(addOrdinality);
// only treat input as intersection if contains '&' or 'and'
const delimiter = _.includes(text, '&') ? '&' : 'and';
const delimiterIndex = words.indexOf(delimiter);
str1 = wordsToSentence(words, 0, delimiterIndex);
str2 = wordsToSentence(words, delimiterIndex+1, words.length);
} else {
throw 'Missing streets in the intersection';
str1 = words.slice(0,delimiterIndex).join(' ');
str2 = words.slice(delimiterIndex+1, words.length).join(' ');
}
return { street1: str1, street2: str2 };
};
// intended for intersections only
// this function turns '77' into '77th', '3' into '3rd', etc
function addOrdinality(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;
function addOrdinality(elmnt) {
let isNum = /^\d+$/.test(elmnt);
if (isNum) {
var cent = elmnt % 100;
if (cent >= 10 && cent <= 20) { return `${elmnt}th`; }
var dec = elmnt % 10;
if (dec === 1) { return `${elmnt}st`; }
if (dec === 2) { return `${elmnt}nd`; }
if (dec === 3) { return `${elmnt}rd`; }
return `${elmnt}th`;
}
return elmnt;
}
// intended to do the conversions like:
// 'w28' -> 'West 28'
// 'e17' -> 'East 17'
function directionalSanitizer(arr){
const mapping = {
e : 'East',
w : 'West',
s : 'South',
n : 'North',
ne: 'Northeast',
nw: 'Northwest',
se: 'Southeast',
sw: 'Southwest'
};
const mapping = {
e : 'East',
w : 'West',
s : 'South',
n : 'North',
ne: 'Northeast',
nw: 'Northwest',
se: 'Southeast',
sw: 'Southwest'
};
for (let i = 0; i < arr.length; i++) {
let streetNum;
switch (stringStartsWithDirAcronym(arr[i])) {
case 1:
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);
}
break;
case 2:
streetNum = arr[i].substring(2);
arr[i] = mapping[arr[i].substring(0,2)];
if(i+1 === arr.length) {
arr.push(streetNum);
} else {
arr.splice(i+1,0,streetNum);
}
break;
}
function directionalSanitizer(word){
let streetNum;
switch (stringStartsWithDirAcronym(word)) {
case 1:
streetNum = word.substring(1);
word = mapping[word[0]];
return [word, streetNum];
case 2:
streetNum = word.substring(2);
word = mapping[word.substring(0,2)];
return [word, streetNum];
}
return arr;
return word;
}
/*
@ -105,30 +77,16 @@ Checks if an acronym for any direction exists and returns the number of characte
*/
function stringStartsWithDirAcronym (text) {
if (text.length > 2) {
if ((text.substring(0,2).toLowerCase() === 'ne' || text.substring(0,2).toLowerCase() === 'nw' ||
if ((text.substring(0,2).toLowerCase() === 'ne' || text.substring(0,2).toLowerCase() === 'nw' ||
text.substring(0,2).toLowerCase() === 'se' || text.substring(0,2).toLowerCase() === 'sw') &&
/^\d$/.test(text[2])) {
return 2;
return 2;
}
}
if (text.length > 1) {
} else if (text.length > 1) {
if ((text[0].toLowerCase() === 'e' || text[0].toLowerCase() === 'w' ||
text[0].toLowerCase() === 'n' || text[0].toLowerCase() === 's') && /^\d$/.test(text[1])) {
return 1;
}
}
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';
const peliasQuery = require('pelias-query');

2
routes/v1.js

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

2
test/unit/run.js

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