xxxxxxxxxx
118
//English Song Data Visualization In Korean
//Soojin Lee
//Programming from A-Z
//Week4 Assignment Project
let concordance = {};
// The raw array of words
let lines;
let tokens;
let counter = 0;
function preload() {
lines = loadStrings("data/meanit.txt");
}
function setup() {
createCanvas(300, 700);
concordance = {};
// Load file and chop it up
let allText = join(lines, " ").toLowerCase();
tokens = allText.split(/[^a-z0-9']+/gi);
}
function draw() {
background("white");
// Look at words one at a time
if (counter < tokens.length) {
let s = tokens[counter];
if (!concordance[s]) {
concordance[s] = {
count: 1,
korean: korean(s),
length: s.length
};
} else {
concordance[s].count++;
}
counter++;
}
// console.log ( s, concordance[s], counter
// s is the list of words word in token
//concordance [s] is counting how many time has the word s appeared in token.
//counter is counting nth number of word in token
write();
// korean();
}
function write() {
let x = 0;
let y = 48;
// x and y will be used to locate each word
let keys = Object.keys(concordance);
keys.sort((a, b) => concordance[b] - concordance[a]);
//console.log(keys);
//concordance[b] - concordance[a] => most repeated word come first
// Look at each word
for (let word of keys) {
let count = concordance[word];
// Display all the words that appear more than 3 times
if (count > 3) {
// The size is the count +1
let fsize = count + 5;
// text size vaires based on the count of the word
textSize(fsize);
//rectangle color changes based on the count
fill(fsize * fsize * 0.1, fsize * fsize * 0.1, fsize * fsize * 0.1);
//draw rectangle first. Then the text.
//x value = fill the canvas / y value = fits the word size and the padding value
rect(0, y - 48, 300, fsize + 48);
noStroke();
//write text
fill(fsize * fsize + 30, fsize + 100, fsize * 4 + 80);
text(word, x, y);
let translated = korean(word);
text(translated, x + 200, y);
// Move along the x-axis
x += 300 + textWidth(word);
}
// If x gets to the end, move y
if (x > width) {
x = 0;
y += 48;
}
// If y gets to the end, we're done
if (y > height) {
break;
}
}
}
//convert English to Korean
function korean(word) {
translated = word.replace("a", "ㅇ");
translated = translated.replace("b", "ㅂ");
return translated;
}
//end