Right, I experimented with the example. Rather than a list of addWord I am trying to use something like:
String addWord = loadStrings("ft.txt");
Where ft in this case is a txt file of terms each followed by a carriage return. So this produces an array of terms when read with println...but how do I get that array to be counted in the same manner as Companje's example?
Ok, many thanks for pointing me to that example. I have a feeling I am asking a rather amateurish question, but I am still making my way through the basics of programming. Please, if you can, explain to me rather bluntly how one might get an array of terms to be frequency counted with hashmap.
I suggest to search a bit the forum (this one and the older one) as this question is coming up regularly and so there are some solutions to reap. One idea is to put the words as keys of the hashmap, and counts as values.
Yep, I've been looking around everywhere I could today. I have managed to come up with a work-around using the count() function in a similar manner to the way it is deployed in the hashmap example included with processing. I think the example was created by Daniel Shiffman.
Using Shiffman's function I am able to reveal the frequency of the words. However, they still remain unsorted. It is easy enough to get processing's printwriter to make a tsv file and have one column for the terms and another for the frequencies. I can then sort the output in a spreadsheet. This is not a great solution (since it is basically counting up all the terms as it goes...i.e., chimpanzee 199, chimpanzee 200, rhino 89, chimpanzee 201, duck 8...and so on).
Ok, I will try to experiment with your idea. Though that may still have a lack-of-sorting problem to deal with, no?
What I would like to find is a way to establish the final count for each term and ignore the rest of the counting that happens along the way. Any ideas in that vein would be greatly received.
Nice, thanks for that link phi.lho. I will have to try it out this evening.
Since I am not too familiar with arraylist I took a look at the arraylist and the interface map documentation; it will take some tinkering for me to adapt my code. Though perhaps what I am after is not as exacting as the code you produced. Referring to your earlier idea of words as K and count as V (which it appears the Shiffman example is doing), is there not a simple was to reveal the max V for the keys associated with a particular word? According to the Map documentation "the Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings."
I know the hashmap is an unsorted map. Though, it would seem if I have 200 keys (eg words) all called "chimpanzee" and each of these received a value (eg a count) between 1 and 200 there ought to be an easy way to interrogate this subset of keys for the max value among them (i.e., viewing via key-value mappings)?
Ok, please do tell me if I am missing something. Perhaps the keys in a hashmap, despite being linked, in this case to words, are actually each unique expressions as far as java is concerned...?
Your approach to have 200 "chimpanzee" words in a collection (might be an ArrayList indeed) is simple, but a bit wasteful of memory... HashMap, by definition, ensures each key is unique, ie. if you put a new value with an existing key, the new value will erase the old one. That's why we get the previous value, if there is one, create it otherwise, then increment the value (from 0 if new) and update the entry in the hash map. That way, you have a nice collection of words with their associated frequency. But, you have then to put these values in a sortable collection (here an ArrayList) and sort it by frequency (using a special comparison class) to get something ordered.
Yes, that description was helpful. I think I have more or less covered all my needs by adapting your work with arraylists. Many thanks phi.lho. I am glad you took the time to clarify concepts which might otherwise baffle novices like myself.
Best wishes, Dan
Leave a comment on PhiLho's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic