mirror of
https://github.com/processing/processing4.git
synced 2026-02-04 06:09:17 +01:00
reworking of IntHash and HashMap examples
This commit is contained in:
@@ -8,8 +8,11 @@
|
||||
* If you are familiar with associative arrays from other languages,
|
||||
* this is the same idea.
|
||||
*
|
||||
* This example uses the HashMap to perform a simple concordance
|
||||
* http://en.wikipedia.org/wiki/Concordance_(publishing)
|
||||
* A simpler example is CountingStrings which uses IntHash instead of
|
||||
* HashMap. The Processing classes IntHash, FloatHash, and StringHash
|
||||
* offer a simpler way of pairing Strings with numbers or other Strings.
|
||||
* Here we use a HashMap because we want to pair a String with a custom
|
||||
* object, in this case a "Word" object that stores two numbers.
|
||||
*/
|
||||
|
||||
// The next line is needed if running in JavaScript Mode with Processing.js
|
||||
@@ -17,71 +20,64 @@
|
||||
|
||||
HashMap<String, Word> words; // HashMap object
|
||||
|
||||
String[] tokens; // Array of all words from input file
|
||||
int counter;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
|
||||
words = new HashMap<String,Word>();
|
||||
// Create the HashMap
|
||||
words = new HashMap<String, Word>();
|
||||
|
||||
// Load two files
|
||||
loadFile("dracula.txt");
|
||||
loadFile("frankenstein.txt");
|
||||
|
||||
// Load file and chop it up
|
||||
String[] lines = loadStrings("dracula.txt");
|
||||
String allText = join(lines, " ");
|
||||
tokens = splitTokens(allText, " ,.?!:;[]-");
|
||||
|
||||
// Create the font
|
||||
textFont(createFont("Georgia", 24));
|
||||
textFont(createFont("Georgia", 24));
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(51);
|
||||
fill(255);
|
||||
background(0);
|
||||
|
||||
// Look at words one at a time
|
||||
String s = tokens[counter];
|
||||
counter = (counter + 1) % tokens.length;
|
||||
|
||||
// Is the word in the HashMap
|
||||
if (words.containsKey(s)) {
|
||||
// Get the word object and increase the count
|
||||
// We access objects from a HashMap via its key, the String
|
||||
Word w = words.get(s);
|
||||
w.count();
|
||||
} else {
|
||||
// Otherwise make a new word
|
||||
Word w = new Word(s);
|
||||
// And add to the HashMap put() takes two arguments, "key" and "value"
|
||||
// The key for us is the String and the value is the Word object
|
||||
words.put(s, w);
|
||||
}
|
||||
|
||||
// x and y will be used to locate each word
|
||||
float x = 0;
|
||||
float y = height-10;
|
||||
|
||||
// Look at each word
|
||||
// Show words
|
||||
for (Word w : words.values()) {
|
||||
|
||||
// Only display words that appear 3 times
|
||||
if (w.count > 3) {
|
||||
// The size is the count
|
||||
int fsize = constrain(w.count, 0, 100);
|
||||
textSize(fsize);
|
||||
text(w.word, x, y);
|
||||
// Move along the x-axis
|
||||
x += textWidth(w.word + " ");
|
||||
if (w.qualify()) {
|
||||
w.display();
|
||||
w.move();
|
||||
}
|
||||
|
||||
// If x gets to the end, move y
|
||||
if (x > width) {
|
||||
x = 0;
|
||||
y -= 100;
|
||||
// If y gets to the end, we're done
|
||||
if (y < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load a file
|
||||
void loadFile(String filename) {
|
||||
String[] lines = loadStrings(filename);
|
||||
String allText = join(lines, " ").toLowerCase();
|
||||
String[] tokens = splitTokens(allText, " ,.?!:;[]-\"'");
|
||||
|
||||
for (String s : tokens) {
|
||||
// Is the word in the HashMap
|
||||
if (words.containsKey(s)) {
|
||||
// Get the word object and increase the count
|
||||
// We access objects from a HashMap via its key, the String
|
||||
Word w = words.get(s);
|
||||
// Which book am I loading?
|
||||
if (filename.contains("dracula")) {
|
||||
w.incrementDracula();
|
||||
}
|
||||
else if (filename.contains("frankenstein")) {
|
||||
w.incrementFranken();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Otherwise make a new word
|
||||
Word w = new Word(s);
|
||||
// And add to the HashMap put() takes two arguments, "key" and "value"
|
||||
// The key for us is the String and the value is the Word object
|
||||
words.put(s, w);
|
||||
if (filename.contains("dracula")) {
|
||||
w.incrementDracula();
|
||||
} else if (filename.contains("frankenstein")) {
|
||||
w.incrementFranken();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,72 @@
|
||||
class Word {
|
||||
|
||||
int count;
|
||||
// Store a count for occurences in two different books
|
||||
int countDracula;
|
||||
int countFranken;
|
||||
// Also the total count
|
||||
int totalCount;
|
||||
|
||||
// What is the String
|
||||
String word;
|
||||
|
||||
// Where is it on the screen
|
||||
PVector position;
|
||||
|
||||
Word(String s) {
|
||||
position = new PVector(random(width), random(-height, height*2));
|
||||
word = s;
|
||||
count = 1;
|
||||
}
|
||||
|
||||
void count() {
|
||||
count++;
|
||||
// We will display a word if it appears at least 5 times
|
||||
// and only in one of the books
|
||||
boolean qualify() {
|
||||
if ((countDracula == totalCount || countFranken == totalCount) && totalCount > 5) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the count for Dracula
|
||||
void incrementDracula() {
|
||||
countDracula++;
|
||||
totalCount++;
|
||||
}
|
||||
|
||||
|
||||
// Increment the count for Frankenstein
|
||||
void incrementFranken() {
|
||||
countFranken++;
|
||||
totalCount++;
|
||||
}
|
||||
|
||||
// The more often it appears, the faster it falls
|
||||
void move() {
|
||||
float speed = map(totalCount,5,25,0.1,3);
|
||||
speed = constrain(speed,0,10);
|
||||
position.y += speed;
|
||||
|
||||
if (position.y > height*2) {
|
||||
position.y = -height;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Depending on which book it gets a color
|
||||
void display() {
|
||||
if (countDracula > 0) {
|
||||
fill(121, 0, 31);
|
||||
}
|
||||
else if (countFranken > 0) {
|
||||
fill(33, 72, 122);
|
||||
}
|
||||
// Its size is also tied to number of occurences
|
||||
float fs = map(totalCount,5,25,2,24);
|
||||
fs = constrain(fs,2,48);
|
||||
textSize(fs);
|
||||
textAlign(CENTER);
|
||||
text(word, position.x, position.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
||||
mode.id=processing.mode.java.JavaMode
|
||||
mode=Java
|
||||
Reference in New Issue
Block a user