Skip to content

Commit

Permalink
Merge pull request #9 from ntkog/feature/improvement_regex_regexgen
Browse files Browse the repository at this point in the history
Recovered old randomize, because randomPosition work worse. Other adjustments
  • Loading branch information
ntkog authored May 26, 2019
2 parents 1b4288a + 5d6fa30 commit 731a1f0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
9 changes: 6 additions & 3 deletions config/elections.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,39 @@
"words" : {
"pp": [
"PP",
"@populares",
"pablocasado_",
"Casado",
"ppopular",
"populares"
],
"psoe": [
"PSOE",
"@PSOE",
"sanchezcastejon",
"Sanchez",
"socialistas"
],
"podemos": [
"Podemos",
"@ahorapodemos",
"pablo_iglesias_",
"ahorapodemos",
"unidaspodemos",
"unidas podemos"
],
"ciudadanos": [
"Ciudadanos",
"@CiudadanosCs",
"Cs",
"Albert_Rivera",
"Rivera",
"ciudadanoscs"
],
"vox": [
"VOX",
"@vox_es",
"santi_abascal",
"Abascal",
"vox_es"
"Abascal"
]
},
"ws" : {
Expand Down
19 changes: 8 additions & 11 deletions lib/helpers/elections.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const WORDS = require('../../config/elections.json').words;
const WORD_SEPARATOR = ",";
const PARTIES = Object.keys(WORDS);
const regexgen = require('regexgen');

let rePartiesMap = PARTIES.reduce((old,cur,i,arr) => {
old[cur] = new RegExp(`\\b(${WORDS[cur].map(el => el.toLowerCase()).join("|")})\\b`, "ig")
return old;
const rePartiesMap = PARTIES.reduce((old,cur,i,arr) => {
old[cur] = regexgen(WORDS[cur], "ig");
return old;
}, {})

let PARTIES_EXTENDED = [
Expand All @@ -14,9 +15,8 @@ let PARTIES_EXTENDED = [
old.push(...cur);
return old; },
[]);
console.log(PARTIES_EXTENDED);

const reParties = new RegExp(`\\b(${Object.keys(PARTIES_EXTENDED).join("|")})\\b`, "ig");
const reParties = regexgen(PARTIES_EXTENDED, "ig");
var reTrackingWords;

function mapping_parties (t) {
Expand All @@ -25,9 +25,6 @@ function mapping_parties (t) {
let obj = PARTIES.reduce((old,cur,i,arr) => {
let test = rePartiesMap[cur].test(t.text)
old[cur] = test;
// if (test) {
// markWords.add(cur.toLowerCase());
// }
return old;
}, {});
obj.markWords = [...markWords];
Expand All @@ -36,10 +33,10 @@ function mapping_parties (t) {

function _setRegexWords (wordsStr){
let trackingWords = wordsStr.split(WORD_SEPARATOR).filter(el => !reParties.test(el));
reTrackingWords = new RegExp(`\\b(${trackingWords.map(el => el.toLowerCase()).join("|")})\\b`, "ig");
reTrackingWords = regexgen(trackingWords, "ig");
}

module.exports = function(words2track) {
_setRegexWords(words2track);
return mapping_parties;
_setRegexWords(words2track);
return mapping_parties;
}
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
"jsonpath": "^1.0.1",
"lowdb": "^1.0.0",
"node-fetch": "^2.4.0",
"polygon-lookup": "ntkog/polygon-lookup",
"regexgen": "^1.3.0",
"twitter-stream-api": "^0.5.2",
"websocket-stream": "^5.5.0",
"ws": "^6.2.1",
"polygon-lookup": "ntkog/polygon-lookup"
"ws": "^6.2.1"
}
}
39 changes: 34 additions & 5 deletions src/locationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,45 @@ const spain = require('../data/spain-boundaries.json')
const ccaa = require('../data/spanish-ccaa-boundaries.json')
const LOOKUP_CCAA = new PolygonLookup(ccaa);
const LOOKUP_SPAIN = new PolygonLookup(spain);
const SPAIN_EXTENT = [-18.1607999245551,27.6377369098232,4.32788540952535,43.7899967308538];

function randomize(location){
let {xmin,ymin,xmax,ymax} = location.boundingbox;
let [lon,lat] = randomPosition([xmin,ymin,xmax,ymax]);
let lat, lon;
try{
let i = 1,
ymin = parseFloat(location.boundingbox.ymin),
ymax = parseFloat(location.boundingbox.ymax),
xmin = parseFloat(location.boundingbox.xmin),
xmax = parseFloat(location.boundingbox.xmax);
yDiff = ymax - ymin,
xDiff = xmax - xmin;
do {
lat = getRandomArbitrary(ymin, ymax);
lon = getRandomArbitrary(xmin, xmax);
ymin += yDiff;
ymax -= yDiff;
xmin += xDiff;
xmax -= xDiff;
i++;
} while(!_isInsideSpain(lon, lat) && i < 3);
if(i === 3){
throw "Geolocation attempts exceeded";
}
} catch(err) {
console.log(`${err}\nRandomzing location: ${JSON.stringify(location)}`.red);
lon = lat = 0;
}

return {
lat : parseFloat(lat),
lon : parseFloat(lon)
};
lat: lat,
lon: lon
}
}

function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
};

function _isInsideSpain(lon, lat){
let candidate = LOOKUP_SPAIN.search(lon,lat);
return candidate !== undefined;
Expand Down

0 comments on commit 731a1f0

Please sign in to comment.