Word Counter Question

edited April 2016 in Questions about Code

Hi, I've got a sketch calculates the frequency of a word in a given text. This is the visual output. I want to add a line to the code like "if the frequency is greater than 15, start drawing 40 pixels down" to show the most frequent words at the same time on lines and columns. How can I achieve this? Thanks.

//PFont font;
String[] yazi;    //text'in bagli oldugu array
int kelimeno = 0;   // text'in baslangic noktasi


String sonrakinegec = " ,.?!;:";

void setup() {
  size(500,500);

  // fontu yukle
  //font = loadFont( "CourierNewPSMT-24.vlw" );

  // text'i string array'ine yukle
  String[] rawtext = loadStrings("ham.txt");

  // butun array'i tek bir stringe cevir
  String everything = join(rawtext, "" );


  yazi = splitTokens(everything,sonrakinegec);
  frameRate(2);
}

void draw() {
  background(255);

  // kelimeyi yaz
  String theword = yazi[kelimeno];

// kelimeyi say
int total = 0;
  for (int i = 0; i < yazi.length; i ++ ) {
    if (theword.equals(yazi[i])) {      
      total ++;
    }
  }

  // Display the text and total times the word appears
  //textFont(font);
  fill(0);
  float y=0;
  if (total>=15) {
    y=y+40;
    print(theword+"-" +total+" ");
  }

  text(theword,10,90+y);
  text(total,10,110+y);
  stroke(0);
  fill(175);
  rect(10,50+y,total/4,20);

  // Move onto the next word
  kelimeno = (kelimeno + 1);


}

Answers

  • ??

    in line 43 you want 10 instead of 15 !!!!!!!!!!!!!!!!!!!!!!!!!

    in line 44 you want 50

    homework?

    why line 52?

  • edited April 2016

    I'm trying to do, "if the number is greater than 15, move the text and the bar 40 pixels down"

    not a homework

    edit: aaah, Now I get it, you're saying my first message is different right? Ignore it, I just made them up when writing. fixing now, sorry.

  • edited April 2016

    ??

    well you wrote

    "if the frequency is greater than 10, start drawing 50 pixels down"

    so ??????????

    maybe you have to decide what you want first 10 or 15 ; 40 or 50....

    but the code works for me (with my little test data on cats)

  • it works

  • //PFont font;
    String[] yazi;    //text'in bagli oldugu array
    int kelimeno = 0;   // text'in baslangic noktasi
    
    
    String sonrakinegec = " ,.?!;:";
    
    
    String[] rawtext  = {"dfkglfdsgklj  cat cat cat cat cdsflgjslkdfjg sdfgkj word cat cat cat cat cat cat hello cat dog dog"}
      ;
    
    void setup() {
      size(500, 500);
    
      // fontu yukle
      //font = loadFont( "CourierNewPSMT-24.vlw" );
    
      // text'i string array'ine yukle
      //  String[] rawtext  = loadStrings("ham.txt");
    
      // butun array'i tek bir stringe cevir
      String everything = join(rawtext, "" );
    
    
      yazi = splitTokens(everything, sonrakinegec);
      frameRate(2);
    }
    
    void draw() {
      background(255);
    
      // kelimeyi yaz
      String theword = yazi[kelimeno];
    
      // kelimeyi say
      int total = 0;
      for (int i = 0; i < yazi.length; i ++ ) {
        if (theword.equals(yazi[i])) {      
          total ++;
        }
      }
    
      // Display the text and total times the word appears
      //textFont(font);
      fill(0);
    
      float y=0;
      if (total>10) {
        y=y+40;
        print(theword+"-" +total+" ");
      }
    
      text(theword, 10, 90+y);
      text(total, 10, 110+y);
      stroke(0);
      fill(175);
      rect(10, 50+y, total, 20);
    
      // Move onto the next word
      kelimeno = (kelimeno + 1);
    }
    
  • edited April 2016

    Yes it does. But not in the way I want. I want to move it, but keep the stuff upside, like pressing enter and going to next line on word, notepad etc.

  • remove line 30 to avoid clear screen?

    place line 47 (in my code) before setup

  • Yeah, setting the float before setup worked. But I still need to clear screen and to go to the next line. http://i.imgur.com/zZPcaD6.jpg

  • Answer ✓
    //PFont font;
    String[] yazi;    //text'in bagli oldugu array
    int kelimeno = 0;   // text'in baslangic noktasi
    
    float y=0;
    
    String sonrakinegec = " ,.?!;:";
    
    
    String[] rawtext  = {"dfkglfdsgklj  cat cat cat cat cdsflgjslkdfjg sdfgkj word cat cat cat cat cat cat hello cat dog dog"}
      ;
    
    String alreadyShownWords="#"; 
    
    void setup() {
      size(500, 1200);
    
      // fontu yukle
      //font = loadFont( "CourierNewPSMT-24.vlw" );
    
      // text'i string array'ine yukle
      //  String[] rawtext  = loadStrings("ham.txt");
    
      // butun array'i tek bir stringe cevir
      String everything = join(rawtext, "" );
    
    
      yazi = splitTokens(everything, sonrakinegec);
      //frameRate(2);
    }
    
    void draw() {
      //  background(255);
    
      if (kelimeno>=yazi.length)
        kelimeno-=1;
    
      // kelimeyi yaz
      String theword = yazi[kelimeno];
    
      if (alreadyShownWords.indexOf(theword)<=0) {
    
        alreadyShownWords+=theword + "#" ; 
    
        // kelimeyi say
        int total = 0;
        for (int i = 0; i < yazi.length; i ++ ) {
          if (theword.equals(yazi[i])) {      
            total ++;
          }
        }
    
        // Display the text and total times the word appears
        //textFont(font);
        fill(0);
    
    
        if (total>10) {
          y=y+72;
          print(theword+"-" +total+" ");
        } else y+=72; 
    
        text(theword, 10, 90+y);
        text(total, 10, 110+y);
        stroke(0);
        fill(175, 2, 2);
        rect(10, 50+y, total/4, 20);
      }
    
      // Move onto the next word
      kelimeno = (kelimeno + 1);
    }
    
  • That's perfect! Thanks a lot

  • this clears the screen:

    void draw() {
      background(255);
    

    I have to leave

    sorry

  • I made a trick to check if we have shown the word already.

    there are faster ways to do it of course

    bye bye

  • Look at hashMap to see how to store words / count them

    Make a result list in setup and display it in draw

    atm draw () is slow since we go over each word even when we have displayed it already...

    Why did you want +50 only when it's> 15 ? You need +70 for y every time.

Sign In or Register to comment.