# Start again an InDict example

**URL:** <https://discourse.processing.org/t/start-again-an-indict-example/29676>\
**Category:** Coding Questions\
**Created:** [April 27, 2021, 10:08am UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676 "2021-04-27T10:08:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yves](https://avatars.discourse-cdn.com/v4/letter/y/9e8a1a/32.png) [@yves](https://discourse.processing.org/u/yves)\
**Post date:** [April 27, 2021, 10:08am UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/1 "2021-04-27T10:08:18Z")

</div>

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

```auto
/**
 * 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;
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 27, 2021, 1:41pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/2 "2021-04-27T13:41:18Z")

</div>

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

> <https://github.com/processing/processing-docs/blob/master/content/examples/Topics/Advanced%20Data/CountingStrings/CountingStrings.pde>

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?

---

<div class="post-metadata">

**Author:** ![yves](https://avatars.discourse-cdn.com/v4/letter/y/9e8a1a/32.png) [@yves](https://discourse.processing.org/u/yves)\
**Post date:** [April 27, 2021, 2:37pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/3 "2021-04-27T14:37:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 27, 2021, 2:44pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/4 "2021-04-27T14:44:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yves](https://avatars.discourse-cdn.com/v4/letter/y/9e8a1a/32.png) [@yves](https://discourse.processing.org/u/yves)\
**Post date:** [April 27, 2021, 2:51pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/5 "2021-04-27T14:51:18Z")

</div>

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

```auto
      if (y > height) {
        break;
      }

```

the point is to reset the variables

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 27, 2021, 3:26pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/6 "2021-04-27T15:26:14Z")

</div>

> [@yves](#):
>
> 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:

- Global variables [Variable Scope / Examples / Processing.org](https://processing.org/examples/variablescope.html)
- setup() [setup() / Reference / Processing.org](https://processing.org/reference/setup_.html)

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

- [IntDict / Reference / Processing.org](https://processing.org/reference/IntDict.html)

`:)`

---

<div class="post-metadata">

**Author:** ![yves](https://avatars.discourse-cdn.com/v4/letter/y/9e8a1a/32.png) [@yves](https://discourse.processing.org/u/yves)\
**Post date:** [April 27, 2021, 4:39pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/7 "2021-04-27T16:39:25Z")

</div>

Thank’s for the answer, I will check that

---

<div class="post-metadata">

**Author:** ![yves](https://avatars.discourse-cdn.com/v4/letter/y/9e8a1a/32.png) [@yves](https://discourse.processing.org/u/yves)\
**Post date:** [April 27, 2021, 5:29pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/8 "2021-04-27T17:29:12Z")

</div>

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

```auto
/**
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;
      }
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 27, 2021, 5:41pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/9 "2021-04-27T17:41:37Z")

</div>

> [@yves](#):
>
> it seems working, I don’t know if it’s the right way.

I did suggest:

- reset everything and then start commenting lines that do not seem to be necessary (reset).
- Take a look at what can be done with _concordance_ (an IntDict)  
[IntDict \ Language (API) \ Processing 3+](https://processing.org/reference/IntDict.html)

This is what I mean about commenting:

```auto
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.

`:)`

---

<div class="post-metadata">

**Author:** ![yves](https://avatars.discourse-cdn.com/v4/letter/y/9e8a1a/32.png) [@yves](https://discourse.processing.org/u/yves)\
**Post date:** [April 27, 2021, 8:03pm UTC](https://discourse.processing.org/t/start-again-an-indict-example/29676/10 "2021-04-27T20:03:56Z")

</div>

right !

```auto
  concordance.clear();

```

is enough  
Thank’s
