Multiple words from a text file

99 views
Skip to first unread message

dennis rothfuss

unread,
Jan 13, 2016, 7:24:28 PM1/13/16
to WordCram

Hi,

 I was hoping that someone could help me fine tune my sketch. What I'm wanting to do is fill a shape with words from a txt file and output it as SVG.
I figured out how to fill a shape with words supplied with a text file and output to SVG, the problem was, the words were not repeating, so it felt sparse.

 In the gallery, there's an example of words repeating in a yin-yang, but I couldn't figure out how to mimic that behavior with supplying a text file.
If I could somehow edit the below line to except multiple string arguments, that would suffice, but I can't figure out how to do that

    fromWords(repeatWord("flexible", 500)).

The best I could figure out was to create patches of color for each time I want the words to repeat, this isn't a great solution and it still leaves the image sparse; I really want it to be filled in. I also outputs to 3 different SVG files that I stitch together manually.

Any help on this would be greatly appreciated!






import wordcram.*;
import java.awt.*;

void setup() {
  size(600, 600);
  background(255);

  PImage image = loadImage("img/eagle3c.png");
  image.resize(width, height);

  Shape imageShape = new ImageShaper().shape(image, #ff0000);
  ShapeBasedPlacer placer = new ShapeBasedPlacer(imageShape);

  try {
    new WordCram(this).
      fromTextFile("txt/sampletext.txt").
      withFont(createFont("fonts/bedrock.ttf", 1)).
      toSvg("letters.svg", width, height).
      withPlacer(placer).
      withNudger(placer).
      sizedByWeight(8, 40).
      angledAt(2, 3, 5, 6, 7).
      withColor(#F5B502).
      drawAll();
  }
  catch (java.io.FileNotFoundException x) {
    println(x.getMessage());
  }

  imageShape = new ImageShaper().shape(image, #000000);
  placer = new ShapeBasedPlacer(imageShape);

  try {
    new WordCram(this).
      fromTextFile("txt/sampletext.txt").
      withFont(createFont("fonts/bedrock.ttf", 1)).
      toSvg("letters2.svg", width, height).
      withPlacer(placer).
      withNudger(placer).
      sizedByWeight(4, 12).
      angledAt(2, 3, 5, 6, 7).
      withColor(#F5B502).
      drawAll();
  }
  catch (java.io.FileNotFoundException x) {
    println(x.getMessage());
  }

  imageShape = new ImageShaper().shape(image, #0000ff);
  placer = new ShapeBasedPlacer(imageShape);

  try {
    new WordCram(this).
      fromTextFile("txt/sampletext.txt").
      withFont(createFont("fonts/bedrock.ttf", 1)).
      toSvg("letters3.svg", width, height).
      withPlacer(placer).
      withNudger(placer).
      sizedByWeight(2, 12).
      angledAt(2, 3, 5, 6, 7).
      withColor(#F5B502).
      drawAll();
  }
  catch (java.io.FileNotFoundException x) {
    println(x.getMessage());
  }


  println("done rendering the SVG");

  println("loading the svg...");
  PShape sh = loadShape("letters.svg");

  background(255);
  shape(sh, 0, 0);
}

dennis rothfuss

unread,
Jan 13, 2016, 7:31:26 PM1/13/16
to WordCram
Side note:
I used an image that is white, black, red, and blue. I mistakenly attached the wrong image above; for some reason couldn't get green to work.

Dan Bernier

unread,
Jan 13, 2016, 8:10:42 PM1/13/16
to dennis rothfuss, WordCram
Yeah, if you give WordCram a text file, it'll remove duplicates, and weight the words based on how often they appear in the text file. If you want to have words duplicated, you have to make your own array of Words. If you look again at that repeatWord method, you should get a sense of how to do it. If you want multiple words repeated, I could see doing something like this:

void repeatWords(int num, String... wordStrings) {
  Word[] words = new Word[num];
  for (int i = 0; i < num; i++) {
    float weight = random(1);
    words[i] = new Word(wordStrings[i % wordStrings.length], weight);
  }
  return words;
}
// use it like this:
fromWords(repeatWords(500, "hello", "goodbye", "apples", "bananas"))...

That'll give you an even mix of the words. (If you're unfamiliar with the "%" in the code above, it's called the modulo operator (https://en.wikipedia.org/wiki/Modulo_operation).)

You could also dump your words into a text file, one per line, and read it in with Processing's loadStrings method (https://processing.org/reference/loadStrings_.html), and give each a random weight. If you wanted a word to repeat, just add it on multiple lines. (You'd use WordCram's fromWords method in this case, too, rather than fromTextFile.)

Also: if you only want to render to the screen, you don't need to make one SVG per color - you can just use drawAll() normally. But if you ultimately want a SVG, then yeah, you're doing it the right way. Sorry!

On Wed, Jan 13, 2016 at 7:31 PM, dennis rothfuss <drot...@gmail.com> wrote:
Side note:
I used an image that is white, black, red, and blue. I mistakenly attached the wrong image above; for some reason couldn't get green to work.

--
You received this message because you are subscribed to the Google Groups "WordCram" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wordcram+u...@googlegroups.com.
To post to this group, send email to word...@googlegroups.com.



--

Dan Bernier

unread,
Jan 13, 2016, 8:11:06 PM1/13/16
to dennis rothfuss, WordCram
BTW, that's a great image, and a great font to pair with it. :)

dennis rothfuss

unread,
Jan 14, 2016, 8:18:02 PM1/14/16
to WordCram, drot...@gmail.com
Thanks so much for the reply. I haven't had a chance to revisit this, but I'll let you know how it goes when I do.

I'm a little familiar with program concepts do to studying Python a little bit. The Processing syntax is a bit different, But I'm figuring it out. Thanks for making this tool available; it's pretty awesome.

Roberto Weigand

unread,
Jun 3, 2018, 11:09:07 AM6/3/18
to WordCram
Hi Dan

I don´t understand very well your solution.
Can you be more explicit??

Best regards.

Dan Bernier

unread,
Jun 3, 2018, 2:02:25 PM6/3/18
to Roberto Weigand, WordCram
Hi Roberto,

Sure. But first, what are you trying to do? And, what part of the solution below is confusing you?

Cheers...

~
This message was made with 100% thumbs.
Reply all
Reply to author
Forward
0 new messages