Skip to content

Instantly share code, notes, and snippets.

@judsonbsilva
Last active July 18, 2017 00:00
Show Gist options
  • Save judsonbsilva/5ce96edccb193c9c7899ea9d3806a391 to your computer and use it in GitHub Desktop.
Save judsonbsilva/5ce96edccb193c9c7899ea9d3806a391 to your computer and use it in GitHub Desktop.
An NLP test in JS
const textao = `
SECRETARIA DE POLÍTICAS DE PROMOÇÃO
DA IGUALDADE RACIAL
DECRETOS DE 1o
- DE JANEIRO DE 2015
A PRESIDENTA DA REPÚBLICA, no uso da atribuição
que lhe confere o art. 84, caput, inciso I, da Constituição, resolve
EXONERAR
LUIZA HELENA DE BAIRROS do cargo de Ministra de Estado
Chefe da Secretaria de Políticas de Promoção da Igualdade Racial da
Presidência da República.
Brasília, 1º de janeiro de 2015; 194o da Independência e 127o
da República.
`;
const dslTest = "(@article|'de') @word";
class NLPJS {
static tags = {
article: {
/*
Matchs:
o, a, as, as,
do, da, dos, das,
no, na, nos, nas,
pelo, pela, pelos, pelas
*/
defined: /\b(d|n|pel)?[ao]s?\b/,
/*
Matchs:
um, uma, uns, umas
dum, duma, duns, dumas
*/
'undefined': /\b[dn]?u(m(as|a)?|ns)\b/,
/*
Matchs:
ao, aos, à, às
*/
compoused: /\b(ao|à)s?\b/
},
numeral: {
ordinal: /\b\d+(ª|º)\b/,
cardinal: /\b\d+([,.]\d+)?\b/
},
phoneNumber: /(\+?\d{2})?(\(\d{2}\))?9?\d{4}-?\d{4}\b/,
email: /[a-z0-9._%+-]+@[A-Z0-9.-]+\.[a-z]{2,4}\b/,
word: /\b[a-z]+\b/
};
static punctuation = {
point: /\./,
comma: /\,/,
colon: /\:/,
semicolon: /\;/,
openBraket: /\[/,
closeBraket: /\]/,
openParentheses: /\(/,
closeParentheses: /\)/,
exclamation: /\!/,
questionMark: /\?/
};
static preProcessing(text){
return text.replace(/\s+/g, ' ').replace(/\n+/g, '\n').split(' ');
}
static tokenize(text){
var words = NLPJS.preProcessing(text);
var lexeme = [];
console.log(words);
for(var index = 0; index < words.length; index++){
var word = words[index];
var tagList = [];
for(var tag in NLPJS.tags){
var tagRegexp = NLPJS.tags[tag];
let addtag = false;
if( tagRegexp.constructor == RegExp ){
if( tagRegexp.test(word[index]) )
addtag = true;
} else {
for(var subtag in tagRegexp){
let testTag = tagRegexp[subtag];
if( testTag.test(word) )
addtag = true;
}
}
if(addtag) tagList.push(tag);
}
lexeme[index] = [word, tagList];
}
return lexeme;
}
static generateAST(dslText){
var words = NLPJS.preProcessing(text);
}
};
console.log( NLPJS.tokenize('uma merda da powrran') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment