Welcome to part two of "word counting coding challenge".
This time what I'm going to do is the
exact thing I just did in the previous video,
where I did a word counting application in Javascript.
I'm going to do the same exact thing in Processing.
This will be written in the Java programming language.
Now I explained a bunch of details in the previous video.
I'm going to gloss over those just to see, kind of work through this rather quickly.
But i'm going to use the same text file, and the same exact algorithm.
So, let's get started. Processing actually if you're not aware
has this lovely object built into it. called an "int dictionary". i-n-t-d-i-c-t.
So I'm going to call that "counts". And I can say counts equals a new
Int Dictionary. What I'm able to do with this is store right in there
strings with numbers. There's also a float dictionary if I want strings with floating
point numbers and a string dictionary if I want to pair strings with strings.
If i want to do any other sophisticated pairing in Processing, I need to use
something called a "hashmap". Which is a native Java class for doing hash tables,
dictionaries, that sort of thing. And I can demonstrate that in a future video
if people are interested. But the int dictionary is going to make this really
quick and easy. So first thing I want to do is load in the text file. So i already have that
Rainbow dot txt. The text from the rainbow wikipedia article. Loaded into
the data folder. Remember load strings just like in p5, loads that text file and
separates it into lines. So the first thing I want to do then, is say
Join that. So I could say join lines with a line break. So now i have
all the separate lines joined in one big text, Why? Because ultimately what i
want to do now is have a big array of all the tokens, all the words. So I'm going
to say split tokens, and actually I can use the native java split function
but split functions is in processing, it will be kind of easy for me to use it
I want to split all words by - anything I put in here now acts as a delimiter
So i'm just going to say period, question mark, exclamation, dollar sign...
Again, I can always refine what the delimiters are and there are ways
of being fancier with that using the Java split function, I can improve that later.
But, I just want to see print Array tokens. I just want to see that this is working.
So let me print out that array into the console. There it is, ooh that didn't
work. Ah, guess what I forgot, space! Space would be good, and line break,
that would be good too. Let's try that again, there we go. So this looks like
its working. We can see I have all of these tokens. Comma i should probably
include. Lets get a comma in there, semicolon. Thats a little bit better.
Lets see, ok great. So theres going to be straight junk in there, I could always
filter out that junk. But i got a big array of every single word in this array.
So now, the next thing I want to do, just as i did before is loop through that.
Loop through all of the tokens, and I want the same exact algorithm.
So i want to say, if the dictionary. So before I said if the word's value in the
dictionary is undefined, thats how I did that in Javascript. Here I want to use
the int dictionary function. Lets look at int dictionary reference page.
So we can see here, here is the reference page for dictionary and theres a bunch
of functions that I want to use. The function I'm looking for right now
is has Key. So has Key checks if a key already exists in the dictionary.
So thats the function I want to use. Come back over to my processing program
if counts has key, and lets make a variable here. String, word, equals
tokens index i. Has that word already as a key. if it exists, what do i want to do?
I want to add one to it's count and by the way Processing, knowing that this is
an int dictionary has functions that will just do stuff to the numbers in there.
So i can actually say, I need to add the key, sorry. First thing I need to do is
I could just use Set. I want to just set that word to counts dot set word to the
value one. Perfect, Oh no thats if it was a new one. So if its a new one I want to
set it to one. Right? if it doesn't exist. if it does exist what do i want to do?
I want to say counts dot increment that word. So the increment function
reaches into the value and adds one to it. So this now is the same exact algorithm.
In that dictionary either set a new word to one or increment the count of that word
by one. I'm not sure what will happen if I say print line counts. Lets see how it
decides to format it in the console. I have some sort of error. Lets look in the
errors tab, no errors. We can see this looks good. Quote definitely needs to be
a delimiter. Let me add quotes as a delimiter. Escape with a backslash.
Why do i see all these extra quotes in there. Maybe thats just the way its
spitting it out. Another thing I should do is do the same thing to change everything
to lower case. I'm assuming that this is working because I'm seeing a whole lot
of things with the number one. I'm getting all these words and their counts. Now
what I want to do is, lets draw something to the window. I want to iterate through
the entire list. How do I iterate over int dictionary in processing? I can just get
a copy of the keys because if I can get the keys in an array just like I did in
Javascript, then I can iterate over it. I could have my own array but here I know
what int dictionary is doing, so I'm going to ask for the array. I could say here a
String keys equals counts key Array. Then, and by the way I can use this sort
of enhanced loop. I can say for every key in keys I could say lets pick a random x
lets pick a random y, and lets draw the text to the window. I shouldn't use key,
because key is a built in variable in Processing that indicates the key that
was pressed. I'm going to draw the text at a random location. Lets just see what
happens when I do this. And Im going to say, and I already used the variable word
so thats a bad idea. W-D, I don't know, K for key. I don't like one letter variables
but i just did it anyway. And we can see here after a while this is going to run...
I think. Ooh this is terrible. Thats in the loop. Because I have this big font
i wasn't paying attention. Im doing this in the loop of looking at all the tokens.
Which is pretty much a disaster. By the way if you're ever doing an application
like this you should start with a really small text file. That would be much better
than what I'm doing. That worked. You can see that it drew all sorts of words
everywhere. Lets do a couple things One lets draw a black background
Lets actually make this full screen. So we
can quickly see full screen. There we go those are all the words, all the unique
words that are in that document. Now what I can do actually is I can say
lets get the count from the counts get from the key. Right lets get that count.
Lets say text size count. I'm going to assign it the font size to how many times
it appeared. There we go, so you can see very briefly most of the things are just
a font size of one. but you can see rainbow is there, a is there, the. They
are in random locations. I could be more thoughtful about how I position these,
placing these, I could use a different mapping, use some other sort of scale.
But you can see the basic idea I now have a word counting application that shows me
all the words appear with their size according to the frequency at which
they appear in that document in Processing
Ok, thanks very much and see you in the next video where I'm going to look at
something called T-F-I-D-F.
Subtitles by the Amara.org community