How can I create a dictionary of feminine and masculine words?

0

I have a question, I have defined a function to restrict offensive words in my input but I need a code so that regardless of the word that is entered, the code restricts the words in both female and male, example: dog - bitch

the following is my code in javascript:

var malasPalabras = ['perro'];

const checkMalasPalabras = (palabra) => {

var rgx = new RegExp(malasPalabras.join("|")+"|" + "/gi");
return (rgx.test(palabra));
}

$('#boton-guardar').click(() => {        
      
  var nombre = $("#nombretxt").val().toLowerCase();
  
  if(checkMalasPalabras(nombre) == true){
  	swal("Ups! algo ha ocurrido", "Ingresaste una palabra indebida, intenta de nuevo", "error");
    document.getElementById("nombretxt").value = "";	  
  }
    
asked by Anderson Rodríguez 26.09.2018 в 16:43
source

1 answer

0

A simple option is to replace the last letter / s if it is feminine ending by masculine, so it is not necessary to modify the dictionary.

Using this approach you can manage a simpler dictionary in which only the masculine singular appears and you create approximations in code to detect the feminine and / or plural terminations. Its usefulness will depend on the complexity of your system.

I'll give you an example. Hope this can help you.

var malasPalabras = ['perro'];
const checkMalasPalabras = (palabra) => {

var rgx = new RegExp(malasPalabras.join("|")+"|" + "/gi");
return (rgx.test(palabra));
}

$('#boton-guardar').click(() => {        
      
  var nombre = $("#nombretxt").val().toLowerCase(); 
  var lastletter = nombre.substr(nombre.length - 1);  
  if (lastletter == "a") {
  	nombre = nombre.slice(0, -1)+"o"; //alert(nombre);
  }
  
  if(checkMalasPalabras(nombre)){
  	
    document.getElementById("nombretxt").value = "";	
    alert("no permitido");
  } else {
    alert("continua");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="nombretxt" id="nombretxt">
<input type="submit" id="boton-guardar">
    
answered by 27.09.2018 / 17:11
source