How to stop loading text into HashMap
in
Programming Questions
•
2 years ago
Dear interested reader,
I took an example code that loads a text file and seeks all words inside it. I changed the code a bit to what I need. What I need: I need to make the most common words in the text very small, and the rare text stays the same size.
Only now it's so, that the script is constantly scanning the text double. It never stops loading words.
How can I change the code so that it scans the text only once?
Kind regards,
Joshua
HashMap words;
String[] tokens;
int counter;
PFont f;
void setup() {
size(screen.width, screen.height);
words = new HashMap();
String[] lines = loadStrings("dnk_text_pdf.txt");
String allText = join(lines, " ");
tokens = splitTokens(allText, " ,.?!:;[]-");
f = createFont("Georgia", 50, true);
}
void draw() {
background(255);
fill(199,50,120);
textFont(f);
text("New Reading", 30, 50);
fill(0,0,0,210);
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
if (words.containsKey(s)) {
Word w = (Word) words.get(s);
w.count();
}
else {
Word w = new Word(s);
words.put(s, w);
}
Iterator i = words.values().iterator();
float x = 40;
float y = 120;
while (i.hasNext()) {
Word w = (Word) i.next();
if (w.count > 2) {
int fsize = constrain(int(w.count*1.7), 3, 100);
if (fsize > 38) {
fsize = 38;
}
fill(0,0,0,210);
textFont(f, 40-fsize);
text(w.word, x, y);
x += textWidth(w.word + " ");
}
if (w.count > 4) {
int fsize = constrain(int(w.count*3.7), 3, 100);
if (fsize > 38) {
fsize = 38;
}
fill(255,0,0,210);
textFont(f, 40-fsize);
text(w.word, x, y);
x += textWidth(w.word + " ");
}
if (w.count > 6) {
int fsize = constrain(int(w.count*3.7), 3, 100);
if (fsize > 38) {
fsize = 38;
}
fill(0,255,0,210);
textFont(f, 40-fsize);
text(w.word, x, y);
x += textWidth(w.word + " ");
}
if (x > width-150) {
x = 20;
y += 30;
if (y < 0) {
break;
}
if (y > height-50) {
noLoop();
}
}
}
}
class Word {
int count;
String word;
Word(String s) {
word = s;
count = 1;
}
void count() {
count++;
}
}
I took an example code that loads a text file and seeks all words inside it. I changed the code a bit to what I need. What I need: I need to make the most common words in the text very small, and the rare text stays the same size.
Only now it's so, that the script is constantly scanning the text double. It never stops loading words.
How can I change the code so that it scans the text only once?
Kind regards,
Joshua
HashMap words;
String[] tokens;
int counter;
PFont f;
void setup() {
size(screen.width, screen.height);
words = new HashMap();
String[] lines = loadStrings("dnk_text_pdf.txt");
String allText = join(lines, " ");
tokens = splitTokens(allText, " ,.?!:;[]-");
f = createFont("Georgia", 50, true);
}
void draw() {
background(255);
fill(199,50,120);
textFont(f);
text("New Reading", 30, 50);
fill(0,0,0,210);
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
if (words.containsKey(s)) {
Word w = (Word) words.get(s);
w.count();
}
else {
Word w = new Word(s);
words.put(s, w);
}
Iterator i = words.values().iterator();
float x = 40;
float y = 120;
while (i.hasNext()) {
Word w = (Word) i.next();
if (w.count > 2) {
int fsize = constrain(int(w.count*1.7), 3, 100);
if (fsize > 38) {
fsize = 38;
}
fill(0,0,0,210);
textFont(f, 40-fsize);
text(w.word, x, y);
x += textWidth(w.word + " ");
}
if (w.count > 4) {
int fsize = constrain(int(w.count*3.7), 3, 100);
if (fsize > 38) {
fsize = 38;
}
fill(255,0,0,210);
textFont(f, 40-fsize);
text(w.word, x, y);
x += textWidth(w.word + " ");
}
if (w.count > 6) {
int fsize = constrain(int(w.count*3.7), 3, 100);
if (fsize > 38) {
fsize = 38;
}
fill(0,255,0,210);
textFont(f, 40-fsize);
text(w.word, x, y);
x += textWidth(w.word + " ");
}
if (x > width-150) {
x = 20;
y += 30;
if (y < 0) {
break;
}
if (y > height-50) {
noLoop();
}
}
}
}
class Word {
int count;
String word;
Word(String s) {
word = s;
count = 1;
}
void count() {
count++;
}
}
1