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.
Page Index Toggle Pages: 1
StopWords (Read 628 times)
StopWords
Dec 14th, 2009, 7:05pm
 
OK, so I'm doing this project for a class and I'm using a HashMap class but i want to modify it. I want to use a function stop common words ('the', 'a', 'there', 'is', etc.) from being counted in the HashMap. I have written some code for it, but obviously I'm not very good with processing so I don't know how to do this.

thus far I'm getting an 'unexpected token: stopwords' error.
does anyone know how to fix my code?!  


import ddf.minim.*;

Minim minim;
AudioPlayer player;

HashMap words;  

String[] tokens;  
int counter;

 String[] stopwords = {"a", "the", "is"};

PFont f;
PImage crow;

void setup() {
 size(550, 500);
 minim = new Minim(this);
   player = minim.loadFile("ravenReading.wav", 2048);
   player.loop();
 crow = loadImage("crow2.png");
 words = new HashMap();

 
 String[] lines = loadStrings("theRaven.txt");
 String allText = join(lines, " ");
 tokens = splitTokens(allText, " ,.?!:;[]-_");
 f = createFont("Herculanum", 36, true);  
}

void draw() {
 background(255);
 pushMatrix();
 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);
 if ( isStopWord(s) == false ) words.put(s, w);  
 }
 

 Iterator i = words.values().iterator();


 float x = 10;
 float y = height-65;

 while (i.hasNext()) {
   
   Word w = (Word) i.next();
   
   if (w.count > 3) {
     int fsize = constrain(w.count, 10, 100);  
     textFont(f, fsize);
     text(w.word, x, y);
     x += textWidth(w.word + " ");
   }

   if (x > width) {    
     x = 0;
     y -= 10;
     if (y < 0) {    
       break;
     }
   }
 }
 popMatrix();
 image(crow, 0, 0);
 fill(0);
}

void stopwords {
   for (int i=0; i < stopwords.length; i++) {
     String stopword = stopwords[i];
     if ( stopwords.equals(word) ) return true;
   }
 else{
  return false;
 }
 }

class Word {
 
 int count;
 String word;
 
 Word(String s) {
   word = s;
   count = 1;
 }
 
 void count() {
   count++;
 }

}  

Re: StopWords
Reply #1 - Dec 14th, 2009, 11:21pm
 
Replace your void stopwords { ..  } method (you forgot () after stopwords) with:

Code:

boolean isStopWord(String word) {
for (int i=0; i < stopwords.length; i++) {
String stopword = stopwords[i];
if ( stopwords.equals(word) )
{
return true;
}
}
return false;
}


That should work.
Also, in your case, I would replace .equals with .equalsIgnoreCase
Re: StopWords
Reply #2 - Dec 15th, 2009, 9:22am
 
Thank you!!
No errors now, but when I run the code the words that I don't want to be counted are still showing up.
?
and I get and error with .equalsIgnoreCase
"cannot invoke equalsIgnoreCase(String) on the array type String[]"
Re: StopWords
Reply #3 - Dec 16th, 2009, 4:28am
 
[quote author=580B0A330 link=1260846344/2#2 date=1260897775]when I run the code the words that I don't want to be counted are still showing up.  and I get and error with .equalsIgnoreCase
"cannot invoke equalsIgnoreCase(String) on the array type String[]"/quote]
Related errors, I suppose... You wrote:
String stopword = stopwords[i];
if ( stopwords.equals(word) ) return true;

stopword vs. stopwords...
Page Index Toggle Pages: 1