Týden 10 - 29. 11. 2019
V závěrečném cvičení v Processingu budeme vizualizovat texty se zaměřením na poezii (periodicitu v textu).
Vytvořte program, který:
1) načte text ze souboru text1 (viz níže))
2) zjistí frekvenci slov a vytvoří vizualizaci "word cloud" do dolní části obrazovky ?[https://wordcram.org/]
3) zjistí periodicitu krátkých fragmentů a kombinací slov (1-10 bez interpunkce a dle chuti i bez mezer)
4) vykreslí frekvenční diagram (kolikrát se opakuje libovolný fragment na nějakou vzdálenost v závislosti od vzdálenosti, čili periody) v horní části obrazovky ?[https://funprogramming.org/45-Convert-distances-into-colors-or-widths.html]
5) pokud stihnete, nechte vypsat ke každé frekvenci četné fragmenty a obarvit slova, ve kterých se vyskytují stejnou barvou, nejvíce zastoupenou periodu a podobně
text1
Jack and Jill went up the hill
To fetch a pail of water.
Jack fell down and broke his crown,
And Jill came tumbling after.
Up Jack got, and home did trot,
As fast as he could caper,
To old Dame Dob,
Who patched his nob
With vinegar and brown paper.
When Jill came in
How she did grin
To see Jack's paper plaster;
Mother vexed
Did whip her next
For causing Jack's disaster.
Jack laughed a lot and Jill did not
but her tears did soon abate
then Jill did say that they should play
at see-saw across the gate.
text2
What are little boys made of?
Frogs and snails and puppy-dogs' tails,
And that are little boys made of.
What are little girls made of?
Sugar and spice and all things nice,
And that are little girls made of.
What are young men made of?
Sighs and leers, and crocodile tears,
And that are young men made of.
What are young women made of?
Ribbons and laces, and sweet pretty faces,
And that are young women made of.
KOD
/**
* Loads a text file that contains words separated by spaces (' ').
*
*/
// import wordcram.*;
// import java.util.Map;
IntDict hash;
String[] lines;
String str1;
// String str2;
int w = 3;
int width = 0;
void setup() {
size(800, 600);
background(255);
colorMode(HSB);
stroke(255);
frameRate(5);
hash = new IntDict();
// Read in poem; must be all on one line
lines = loadStrings("poem_clean.txt");
// Split it into words
String[] words = split(lines[0], ' ');
// Make a hash of word counts
for(int k = 0; k<words.length; k++) {
if(hash.hasKey(words[k])) {
hash.increment(words[k]);
} else {
hash.set(words[k], 1);
}
}
// Sort the hash by count
hash.sortValuesReverse();
// Print basic characteristics
println("Poem has " + words.length + " words.");
println("Poem has " + lines[0].length() + " characters.");
println();
// Print the top 5 words
String[] keys = hash.keyArray();
int[] values = hash.valueArray();
for (int i = 0; i<5; i++) {
println(values[i] + " " + keys[i]);
}
println();
// new WordCram(this)
// .fromTextFile("./poem_clean.txt")
// .withFont(createFont("/usr/share/fonts/liberation/LiberationSerif-Regular.ttf", 1))
// .sizedByWeight(10, 90)
// .withColors(color(0, 250, 200), color(30), color(170, 230, 200))
// .drawAll();
int[] num = new int[lines[0].length()-w];
for(int pos=0; pos<lines[0].length()-w; pos++) {
num[pos] = 0;
}
// For each position in the text
for(int pos=0; pos<lines[0].length()-w; pos++) {
// get a fragment of length w
str1 = lines[0].substring(pos, pos+w);
// if it is periodic
if(lines[0].indexOf(str1, pos+1) > pos) {
// calculate the period
width = lines[0].indexOf(str1, pos+1) - pos;
// print the fragment, its periodicity and position
println(str1 + " " + width + " (pos = " + pos + ")");
num[width]++;
}
}
// Print frequency spectrum
println();
for(int pos=0; pos<lines[0].length()-w; pos++) {
println(pos + " " + num[pos]);
}
}
void draw() {
}
jack and jill went up the hill to fetch a pail of water jack fell down and broke his crown and jill came tumbling after up jack got and home did trot as fast as he could caper to old dame dob who patched his nob with vinegar and brown paper when jill came in how she did grin to see jacks paper plaster mother vexed did whip her next for causing jacks disaster jack laughed a lot and jill did not but her tears did soon abate then jill did say that they should play at see saw across the gate
what are little boys made of frogs and snails and puppy dogs tails and that are little boys made of what are little girls made of sugar and spice and all things nice and that are little girls made of what are young men made of sighs and leers, and crocodile tears and that are young men made of what are young women made of ribbons and laces and sweet pretty faces and that are young women made o