We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Word Count from List
Page Index Toggle Pages: 1
Word Count from List (Read 1433 times)
Word Count from List
Apr 30th, 2010, 2:18pm
 
I've found a processing-based wordcounter on this site.
This is very useful for me, but there's a way if I want to count only some words, such the name of persons from an "include" list (ie: name.txt)?

Help me pleaze
Re: Word Count from List
Reply #1 - Apr 30th, 2010, 2:19pm
 
here the code:

Code:
import java.util.*;
import java.util.List;

Map words;

void setup() {
words = new TreeMap();

addWord("rick");
addWord("rick");
addWord("hi");
addWord("hi");
addWord("hi");
addWord("hi");
addWord("haus");
addWord("hi");
addWord("test");
addWord("hi");
addWord("hi");
addWord("stefan");

println("--unsorted:");
print(words);

println("\n--sorted by value:");
print(createMapSortedByValue());
}

void addWord(String value) {
Integer n = (Integer) words.get(value);
int count = (n != null ? n.intValue() + 1 : 1);
words.put(value, new Integer(count));
}

Map createMapSortedByValue() {
//create a Comparator for words
SortByValueComparator byValueComp = new SortByValueComparator(words);
//create a sorted treemap, sorted with byValueComp
SortedMap sortedWords = new TreeMap(byValueComp);
//add all elements from words
sortedWords.putAll(words);
//println(sortedWords.size());
//println(sortedWords.get("auto"));
return sortedWords;
}

void print(Map map) {
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
System.out.println(name + " " + map.get(name));
}
}

/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
* http://www.manticmoo.com/articles/jeff/programming/java/sorting-maps-by-value.php
*/
class SortByValueComparator implements Comparator {
Map words;

SortByValueComparator(Map words) {
this.words = words;
}

int compare(Object arg0, Object arg1) {
int val0 = ((Integer) (words.get(arg0))).intValue();
int val1 = ((Integer) (words.get(arg1))).intValue();

if (val0 < val1) {
return 1;
}
else if (val0 == val1) {
// same counts so compare names
return ((String) arg0).compareTo((String) arg1);
}
else {
return -1;
}
}
}
Re: Word Count from List
Reply #2 - May 1st, 2010, 1:37am
 
Keep a list of names. I recommend using a HashSet for that: it allows a quick check if a string is in the set, using contains().
Each time you are about to add a string in your Map, first check if it is in your list. If not, just don't add it.
Re: Word Count from List
Reply #3 - May 3rd, 2010, 3:06am
 
sorry again...
if I want to check if an array contains item of another array, how can I do?
I've tried this, but it doesn't work:

Code:

import java.util.*;
import java.util.List;

String[] lines = loadStrings("original.txt");
for (int i = 0; i < lines.length; i++);

String[] include = loadStrings("list.txt");
for (int i = 0; i < include.length; i++);

if (include.contains(lines)) {
println (lines[i]);
}



console says:
Cannot invoke conains(String) on the array type String[]
Re: Word Count from List
Reply #4 - May 3rd, 2010, 3:11am
 
Your for loops aren't doing anything but wasting CPU cycles.

Try this:

import java.util.*;
import java.util.List;
String[] lines = loadStrings("original.txt");
String[] include = loadStrings("list.txt");
for( int i = 0; i < lines.length; i++){
 for( int j = 0; j < include.length; j++){
   if( lines[i].contains(include[j]) ){
     println( include[j] );
   }
 }
}
Re: Word Count from List
Reply #5 - May 3rd, 2010, 3:34am
 
now it works!!!!

thanks!!!!
Re: Word Count from List
Reply #6 - May 3rd, 2010, 4:34am
 
As I wrote, a hash set would be much more efficient... Smiley
Page Index Toggle Pages: 1