-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ntkog/feature/improve_regex
Feature/improve regex
- Loading branch information
Showing
3 changed files
with
63 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
const PARTIDOS = require('../../config/elections.json').words; | ||
const WORDS = require('../../config/elections.json').words; | ||
const WORD_SEPARATOR = ","; | ||
const PARTIES = Object.keys(WORDS); | ||
|
||
let rePartiesMap = PARTIES.reduce((old,cur,i,arr) => { | ||
old[cur] = new RegExp(`\\b(${WORDS[cur].map(el => el.toLowerCase()).join("|")})\\b`, "ig") | ||
return old; | ||
}, {}) | ||
|
||
let PARTIES_EXTENDED = [ | ||
...PARTIES.map(k => [...WORDS[k]]) | ||
] | ||
.reduce((old,cur,i,arr) => { | ||
old.push(...cur); | ||
return old; }, | ||
[]); | ||
console.log(PARTIES_EXTENDED); | ||
|
||
const reParties = new RegExp(`\\b(${Object.keys(PARTIES_EXTENDED).join("|")})\\b`, "ig"); | ||
var reTrackingWords; | ||
|
||
function mapping_parties (t) { | ||
let obj = Object.keys(PARTIDOS).reduce((old,cur,i,arr) => { | ||
reParties = new RegExp(`(${PARTIDOS[cur].join("|").toLowerCase()})`, "ig"); | ||
old[cur] = reParties.test(t.text); | ||
let matchWords = t.text.match(reTrackingWords); | ||
var markWords = new Set(matchWords); | ||
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]; | ||
return obj; | ||
} | ||
|
||
module.exports = { | ||
mapping_parties : mapping_parties | ||
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"); | ||
} | ||
|
||
module.exports = function(words2track) { | ||
_setRegexWords(words2track); | ||
return mapping_parties; | ||
} |