xxxxxxxxxx
168
let txt, jFont, graph;
let counting = {};
let keys = [];
let keysClean = [];
let toRemove = [];
let angle = 0;
let loopt = false;
let finalKey = [];
let dtxt;
function preload() {
txt = loadStrings("data/letras.txt");
jFont = loadFont("electroharmonix.ttf");
//console.log(txt);
}
// void mousePressed() {
// }
function resetSketch() {
background(0);
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
resetSketch();
//graph = createGraphics(600,600);
let alltextjoin = txt.join(" ");
//console.log(txt);
//console.log(alltextjoin);
//replace all spaecial characters like accents, from here: https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
let alltext = alltextjoin.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
//let alltext = alltextjoin.normalize("NFD").replace(/\p{Diacritic}/gu, "");
//console.log(alltext);
// createP(alltextjoin);
// createP(alltext);
let words = alltext.split(/\W+/);
//console.log(words);
//createP("WORD COUNTING");
for (let i = 0; i < words.length; i++) {
let word = words[i].toLowerCase();
//is the first time for word? else add
if (!counting[word]) {
counting[word] = 1;
//keys.push(word); Reemplazado despues por keys = Object.keys(counting);
} else {
counting[word]++;
}
}
keys = Object.keys(counting);
createP("WORD COUNTING:");
//console.log(counting);
//console.log(keys);
keys.sort(compare);
keysClean.sort(compare);
function compare(a, b) {
let countA = counting[a];
let countB = counting[b];
return countB - countA;
}
//array of prepositions, articles
toRemove = [
"a",
"ante",
"bajo",
"cabe",
"con",
"contra",
"de",
"desde",
"durante",
"en",
"entre",
"hacia",
"hasta",
"mediante",
"para",
"por",
"segun",
"sin",
"so",
"sobre",
"tras",
"versus",
"via",
"el",
"la",
"lo",
"los",
"las",
"un",
"una",
"unos",
"unas",
"que",
"",
"ese",
"esa",
"este",
"esta",
"del",
"al",
];
keysClean = keys.filter(function (el) {
return !toRemove.includes(el);
});
//removing one character words
for (let i = 0; i < keysClean.length; i++) {
if (keysClean[i].length >= 2) {
let key = keysClean[i];
createDiv(`${key} : ${counting[key]}`);
finalKey.push(key);
}
}
console.log(finalKey);
// for (let i = 0; i < keys.length; i++){
// let key = keys[i];
// createDiv(`${key} : ${counting[key]}`);
// }
}
function draw() {
frameRate(50);
//background(0);
//fill(random(150,255), random(200,255), 0);
textFont(jFont);
for (let i = 0; i < finalKey.length; i++) {
/*dtxt =*/ text(finalKey[i], random(width), random(height));
fill(random(255));
textSize(counting[finalKey[i]] * 10);
//ambientLight(1000);
//noStroke();
rotateX(angle);
rotateY(angle * 0.3);
rotateZ(angle * 10);
// graph.fill(random(255));
// graph.textAlign(CENTER);
// graph.textSize(random(counting[finalKey[i]]));
//graph.text(dtxt[i],random(10),random(10));
angle += 0.1;
}
}
function mousePressed() {
resetSketch();
// if (!loopt) {
// noLoop();
// } else {
// loop();
// }
// loopt = !loopt;
}