Start again an InDict example

Hi every one,
I got this InDict example, I would like to restart the all process, when the text is on the bottom of the screen.
If you have any idea …
Thank’s
Yves

/**
 * CountingString example
 * by Daniel Shiffman.  
 * 
 * This example demonstrates how to use a IntDict to store 
 * a number associated with a String.  Java HashMaps can also
 * be used for this, however, this example uses the IntDict
 * class offered by Processing's data package for simplicity
 * and added functionality. 
 *
 * This example uses the IntDict to perform a simple concordance
 * http://en.wikipedia.org/wiki/Concordance_(publishing)
 *
 */

// The next line is needed if running in JavaScript Mode with Processing.js
/* @pjs font="Georgia.ttf"; */

// An IntDict pairs Strings with integers
IntDict concordance;

// The raw array of words in 
String[] tokens;
int counter = 0;

void setup() {
  size(1900, 1000, P3D);

  concordance = new IntDict();

  // Load file and chop it up
  String[] lines = loadStrings("dracula.txt");
  String allText = join(lines, " ");
  tokens = splitTokens(allText, " ,.?!:;[]-\"");
  // Create the font
  textFont(createFont("Courier", 40, true));
  smooth(8);
}

void draw() {

  background(0);
  noStroke();



  // Look at words one at a time
  if (counter < tokens.length) {
    String s = tokens[counter];
    counter++;
    concordance.increment(s);
  }

  // x and y will be used to locate each word
  float x = 0;
  float y = 18;

  concordance.sortValues();

  String[] keys = concordance.keyArray();

  // Look at each word
  for (String word : keys) {
    int count = concordance.get(word);

    // Only display words that appear 3 times
    if (count > 3) {
      // The size is the count
      float fsize = constrain(count, 0, 30);
      textSize(fsize);
      fill(255);
      text(word, x, y);
      // Move along the x-axis
      x += textWidth(word + " ")*2;
    }
    
// If x gets to the end, move y
    if (x > width*2) {
      x = 0;
      y += 18;
      // If y gets to the end, we're done
      if (y > height) {
       // y = 0;
        break;
      }
    }
  }
}

Hi! The code is just with some parameter change from this example, right?

It’s nothing wrong with starting from an example, but it’s important to credit it - also it gives others an idea of what the process / progress you had to reach to this code.

You need to elaborate a bit more on what you want to do. Do you mean that when the text fills the screen and reached the bottom, you want to erase what’s displayed and start over scanning the text?

1 Like

Thank’s for the answer,
sorry for the credit, the Daniel Shiffman’s work is very usefull, and this forum too !
I start developing in Processing few weeks ago and I couldn’t learn without your help.
Yes, that’s what I want to do, starting the text scan again, when the text reached the bottom.
Yves

no worries! And do you need a general guidance or a complete answer? I guess the latter doesn’t help if you are learning, and that’s why it’s forbidden by the homework policy.

I would split the task into two: 1) detect if the text reached the bottom and 2) reset the process. Which is easier? In fact 1) is already somewhere in the code. 2) may be a bit more difficult because you have to reset a few variables, but hints are in setup function.

Thank’s
a general guidance will be fine !
to detect the bottom, no problem :

      if (y > height) {
        break;
      }

the point is to reset the variables

What do you need to reset?
Give this a lot of thought!
Trial and error is one approach that you can learn from; reset everything and then start commenting lines that do not seem to be necessary.

A “reset” can be clear, initialize, etc.

Look at the:

Take a look at what can be done with concordance (an IntDict):

:)

Thank’s for the answer, I will check that

I tried to recall the txt when the text is at the bootom of the screen, it seems working, I don’t know if it’s the right way.
Thank’s for your answers, and the links.
Very usefull
Yves

/**
https://github.com/processing/processing-docs/blob/master/content/examples/Topics/Advanced%20Data/CountingStrings/CountingStrings.pde
 * CountingString example
 * by Daniel Shiffman.  
 * 
 * This example demonstrates how to use a IntDict to store 
 * a number associated with a String.  Java HashMaps can also
 * be used for this, however, this example uses the IntDict
 * class offered by Processing's data package for simplicity
 * and added functionality. 
 *
 * This example uses the IntDict to perform a simple concordance
 * http://en.wikipedia.org/wiki/Concordance_(publishing)
 *
 */

// The next line is needed if running in JavaScript Mode with Processing.js
/* @pjs font="Georgia.ttf"; */

// An IntDict pairs Strings with integers
IntDict concordance;

// The raw array of words in 
String[] tokens;
int counter = 0;


void setup() {
 size(640, 360);
  concordance = new IntDict();
  // Load file and chop it up
  String[] lines = loadStrings("dracula.txt");  
      String allText = join(lines, " ");
  tokens = splitTokens(allText, " ,.?!:;[]-\"");
  // Create the font
  textFont(createFont("Courier", 40, true));
  smooth(8);
}

void draw() {
  background(0);
fill(255);


  // Look at words one at a time
  if (counter < tokens.length) {
    String s = tokens[counter];
    counter++;
    concordance.increment(s);
  }

  // x and y will be used to locate each word
  float x = 0;
  float y = 18;

  concordance.sortValues();

  String[] keys = concordance.keyArray();

  // Look at each word
  for (String word : keys) {
    int count = concordance.get(word);

    // Only display words that appear 3 times
    if (count > 3) {
      // The size is the count
      float fsize = constrain(count, 0, 30);
      textSize(fsize);
      text(word, x, y);
      // Move along the x-axis
      x += textWidth(word + " ");
    }

// If x gets to the end, move y
    if (x > width) {
      x = 0;
      y += 18;
      // If y gets to the end, we're done
     
    }
     if (y > height) {  
         concordance = new IntDict();

  // Load file and chop it up
  String[] lines = loadStrings("dracula.txt");  
      String allText = join(lines, " ");
  tokens = splitTokens(allText, " ,.?!:;[]-\"");
  // Create the font
  textFont(createFont("Courier", 40, true));

        y = 0;
        break;
      }
  }
}

I did suggest:

This is what I mean about commenting:

if (y > height) 
  {  
  //concordance = new IntDict();

  // Load file and chop it up
  //String[] lines = loadStrings("dracula.txt");  
  //String allText = join(lines, " ");
  //tokens = splitTokens(allText, " ,.?!:;[]-\"");
  // Create the font
  //textFont(createFont("Courier", 40, true));

  //y = 0;
  break;
  }

Try uncommenting only what is necessary.

I did what you did initially and then did some more exploration and was able to do this by just clearing concordance.

:)

1 Like

right !

  concordance.clear();

is enough
Thank’s