Reading a file and word counting.
in
Programming Questions
•
3 years ago
Hello,
Im new to processing but I really like it so spending a few hours everyday to improve myself.
What I am trying to achive now is, I am trying to read a txt file and count the different words inside it.
The next step will be visualising this data.
I am adding the code I am using:
This is the main sketch file.
strings$newWord@648016 and such.
can someone please direct me to the correct direction.
thanks
miki
Im new to processing but I really like it so spending a few hours everyday to improve myself.
What I am trying to achive now is, I am trying to read a txt file and count the different words inside it.
The next step will be visualising this data.
I am adding the code I am using:
This is the main sketch file.
- String[] lines; // Line Array
int[] wordCount = new int[100];
ArrayList wordsArray;
int wordCounter = 0;
int lineIndex = 0;
int charIndex = 0;
void setup() {
wordsArray = new ArrayList();
size(400, 400);
background(0);
noStroke();
frameRate(10);
lines = loadStrings("lipsum.txt");
noLoop();
smooth();
}
void draw() {
while(lineIndex < lines.length) {
String[] words = splitTokens(lines[lineIndex], " ");
for(int i=0; i < words.length; i++) {
wordsArray.add(new newWord(words[i],1));
//wordsArray[wordCounter] = words[i];
wordCount[wordCounter] = 0;
println(words[i]);
wordCounter++;
}
lineIndex++;
}
println("Word Count:"+wordsArray.size());
// for(int i = 0; i<wordsArray.length; i++) {
// if(wordsArray[i] != null) {
// ellipse(10+(25 * i), height/2, wordsArray[i].length()*2, wordsArray[i].length()*2);
// }
// }
for(int i=0; i < wordCounter; i++) {
for(int j=0; j < wordCounter; j++) {
if((wordsArray.get(j)).equals(wordsArray.get(i))) {
println(wordsArray.get(j));
wordCount[i]++;
}
//String[] word = match(wordsArray.get(j), wordsArray.get(i);
// if(word != null) {
//
//
// //word = null;
// } else {
// //print(" No match ");
// //word = null;
// }
//checkWord(wordsArray[i]);
}
}
for(int i = 0; i<wordCounter; i++) {
println(wordCount[i]);
}
} // Draw Son
- class newWord {
String word;
int count;
newWord(String wordTemp, int countTemp) {
word = trim(wordTemp);
count = countTemp;
}
}
strings$newWord@648016 and such.
can someone please direct me to the correct direction.
thanks
miki
1