[Código-Javascript] Palabras más utilizadas en un Sitio Web

Iniciado por JaAViEr, Noviembre 25, 2014, 05:56:26 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Noviembre 25, 2014, 05:56:26 AM Ultima modificación: Marzo 24, 2015, 03:42:06 PM por Expermicid
FUENTE ORIGINAL : You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login


Hola, muy buen día.

El código Javascript que publicaré más abajo se encarga de capturar todas las palabras dentro de algún elemento, clase o id. Nos creará un pequeño formulario "flotante", en el cuál nos pedirá "Longitud", "Veces", "Elemento" (Filtros).

You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login

¿ Que es eso de "Filtros"?
Los dos primeros son una especie de filtros, así mostramos resultados más específicos.

       
  • Longitud: Busca palabras con la longitud que tu deseas (>=)
  • Veces: Cantidad de veces que aparece en la web
  • Elemento: Acá ingresamos en que elemento de la web deseamos buscar Las palabras más utilizadas. El script al usar jQuery, nos permite insertar en este campo ".clases", "#ids" o simplemente elementos como <body>
Una vez rellenes los campos, el programa te dará una salida con una tabla como esta:

Ejemplo basado en el portal de noticias You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
Sin más, el código:
Código: javascript
var script = document.createElement('script');
script.src = 'http://code.jquery.com/jquery-latest.js';
document.getElementsByTagName('head')[0].appendChild(script);

var times_form;
var element_form;
var length_form;
var l_temp;
var dict = {};
var new_dict = {};
var splited = "";
var counter;
var get_data = function (text, length) {

splited = text.split(" ");

  for ( var a in splited ) {

if( dict[splited[a]] ) {

      splited[a] = splited[a].toLowerCase();
dict[splited[a]] = dict[splited[a]] + 1;
   
} else {
   
      if ( splited[a].length >= length ) {

      splited[a] = splited[a].toLowerCase();
        dict[splited[a]] = 1;
   
    }
   
    }
 
}
 
};

var extract = function (element, length, times) {

  new_dict = {};
  dict = {};
 
  $(element).each( function () {
 
  get_data($(this).text(), length);
 
});

  for ( var c in dict ) {
 
    if ( dict[c] >= times ) {
   
      new_dict[c] = dict[c];
   
    }
 
  } 

  var output = "<table border='1'>"+
      "<thead>"+
      "<th>Palabra</th>"+
      "<th>Longitud</th>"+
      "<th>Apariciones</th>"+
      "</thead>"+
      "<tbody>";
  counter = 0;
  for( var l in new_dict ) {

    counter++;
    l_temp = l.replace("<", "&lt;");
    l_temp = l_temp.replace(">", "&gt;");
    output = output + "<tr>"+
      "<td>" + l_temp + "</td><td>" + l.length + "</td><td>" + new_dict[l] + "</td>"+
      "</tr>";
   
  }
  output = output + "</tbody></table>";
 
  show_results(output);
  return new_dict;

};

var find = function (word) {

  if ( new_dict[word] ) {
 
  show_results(String(new_dict[word]));
 
  } else {
 
  show_results("Word Not found");
 
  }

};

var search = function () {

  length_form = $("input[name=length_craw]").val();
  times_form = $("input[name=times_craw]").val();
element_form = $("input[name=element_craw]").val();
  if( length_form.length > 0 && times_form.length > 0 && element_form.length > 0 ) {
 
    extract(element_form, length_form, times_form);
 
  }

};

var show_form = function () {

  $("<div class='show_words' style='border-radius:5px;box-shadow:0px 0px 10px #000;overflow:scroll;font-family:Trebuchet MS;width:auto;height:6cm;background-color:#fff;border:1px solid;position:fixed;top:12%;left:2%;padding:5px;z-index:30;'>"+
    "<center><h2>Palabras más utilizadas</h2></center><input style='padding: 2px;border: 1px solid;border-radius: 4px;margin: 4px;' name='length_craw' placeholder='Longitud'>&nbsp;<input style='padding: 2px;border: 1px solid;border-radius: 4px;margin: 4px;' name='times_craw' placeholder='Veces'><br/>"+
    "<input style='padding: 2px;border: 1px solid;border-radius: 4px;margin: 4px;' name='element_craw' placeholder='Elemento'>&nbsp;<button onclick='search()' style='padding: 2px;border: 1px solid;border-radius: 4px;margin: 4px;'>¡Buscar!</button>&nbsp;<button onclick='$(\".show_words\").fadeOut();' style='padding: 2px;border: 1px solid;border-radius: 4px;margin: 4px;'>Cerrar</button>"+
    "<div class='words_content'></div></div>").appendTo("body");

};

var show_results = function (content) {

  $(".words_content").html("Total:" + counter + "<br/>" + content);

};

show_form();


FUENTE ORIGINAL : You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login



Sin más que agregar... gracias por leer :-)

Saludos, Javier.
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login