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("<", "<");
l_temp = l_temp.replace(">", ">");
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'> <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'> <button onclick='search()' style='padding: 2px;border: 1px solid;border-radius: 4px;margin: 4px;'>¡Buscar!</button> <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();