Replace words with different synonyms and print each combination in a text file

Hello,

My goal is to replace words from a String with synonyms. Right now the code can replace one word with another one and write the sentence in my text file. But I would like to have different opportunities to replace the words randomly and write the different sentences with the different combinations to my text file. Is there any way to do so? I am very new to Processing and couldn’t find any solution. So I would be grateful for any help. Thanks in advance!

Sophie

Oh and in addition another question would be: Is it possible to add random text sizes to the words as well? Many thanks!!

PrintWriter output;


String s = "Wie froh bin ich, daß ich weg bin! Bester Freund, was ist das Herz des Menschen! Dich zu verlassen, den ich so liebe, von dem ich unzertrennlich war, und froh zu sein!";

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


void draw() {

s = s.replaceAll("Wie", "Und");
s = s.replaceAll("froh", "ausgelassen");
s = s.replaceAll("bin", "bin");
s = s.replaceAll("ich", "ich");
s = s.replaceAll("daß", "daß");
s = s.replaceAll("weg", "fort");
s = s.replaceAll("Bester", "Erster");
s = s.replaceAll("Freund", "Vertrauter");
s = s.replaceAll("was", "was");
s = s.replaceAll("ist", "befindet");
s = s.replaceAll("das", "das");
s = s.replaceAll("Herz", "Zentrum");
s = s.replaceAll("des", "des");
s = s.replaceAll("Menschen", "Sterblichen");
s = s.replaceAll("verlassen", "entfernen");
s = s.replaceAll("liebe", "anbete");
s = s.replaceAll("unzertrennlich", "vertraut");
s = s.replaceAll("sein", "entstammen");

output = createWriter("positions.text");
output.print(s);


output.flush();
output.close();

stop();
}

for getting a random sentence every time try this

PrintWriter output;


String s = "Wie froh bin ich, daß ich weg bin! Bester Freund, was ist das Herz des Menschen! Dich zu verlassen, den ich so liebe, von dem ich unzertrennlich war, und froh zu sein!";

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


void draw() {
   if(random(100)<20)
      s = s.replaceAll("Wie", "Und");
   if(random(100)<20)
      s = s.replaceAll("froh", "ausgelassen");
   if(random(100)<20)
      s = s.replaceAll("bin", "bin");
   if(random(100)<20)
      s = s.replaceAll("ich", "ich");
   if(random(100)<20)
      s = s.replaceAll("daß", "daß");
   if(random(100)<20)
      s = s.replaceAll("weg", "fort");
   if(random(100)<20)
      s = s.replaceAll("Bester", "Erster");
   if(random(100)<20)
      s = s.replaceAll("Freund", "Vertrauter");
   if(random(100)<20)
      s = s.replaceAll("was", "was");
   if(random(100)<20)
      s = s.replaceAll("ist", "befindet");
   if(random(100)<20)
      s = s.replaceAll("das", "das");
   if(random(100)<20)
      s = s.replaceAll("Herz", "Zentrum");
   if(random(100)<20)
      s = s.replaceAll("des", "des");
   if(random(100)<20)
      s = s.replaceAll("Menschen", "Sterblichen");
   if(random(100)<20)
      s = s.replaceAll("verlassen", "entfernen");
   if(random(100)<20)
      s = s.replaceAll("liebe", "anbete");
   if(random(100)<20)
      s = s.replaceAll("unzertrennlich", "vertraut");
   if(random(100)<20)
      s = s.replaceAll("sein", "entstammen");

output = createWriter("positions.text");
output.print(s);


output.flush();
output.close();

stop();
}

Since u are new to processing I tried to write a simple solution in which each word now has a 20% chance of being replaced and for random text size use this reference
textSize() add textSize(random(50)); this might just do the trick let me know if it works or not

Do you mean to store this as a text file?? I don’t think that’s a good idea.
Instead you can just have them on the screen and save an image of it using save() or saveFrame();




// WILL OVERWRITE OLD IMAGES !!!!!

PrintWriter output;


String s = "Wie froh bin ich, daß ich weg bin! Bester Freund, was ist das Herz des Menschen! Dich zu verlassen, den ich so liebe, von dem ich unzertrennlich war, und froh zu sein!";

void setup() {
  size(1900, 760);
  background(255);
}


void draw() {
  if (random(100)<20)
    s = s.replaceAll("Wie", "Und");
  if (random(100)<20)
    s = s.replaceAll("froh", "ausgelassen");
  if (random(100)<20)
    s = s.replaceAll("bin", "bin");
  if (random(100)<20)
    s = s.replaceAll("ich", "ich");
  if (random(100)<20)
    s = s.replaceAll("daß", "daß");
  if (random(100)<20)
    s = s.replaceAll("weg", "fort");
  if (random(100)<20)
    s = s.replaceAll("Bester", "Erster");
  if (random(100)<20)
    s = s.replaceAll("Freund", "Vertrauter");
  if (random(100)<20)
    s = s.replaceAll("was", "was");
  if (random(100)<20)
    s = s.replaceAll("ist", "befindet");
  if (random(100)<20)
    s = s.replaceAll("das", "das");
  if (random(100)<20)
    s = s.replaceAll("Herz", "Zentrum");
  if (random(100)<20)
    s = s.replaceAll("des", "des");
  if (random(100)<20)
    s = s.replaceAll("Menschen", "Sterblichen");
  if (random(100)<20)
    s = s.replaceAll("verlassen", "entfernen");
  if (random(100)<20)
    s = s.replaceAll("liebe", "anbete");
  if (random(100)<20)
    s = s.replaceAll("unzertrennlich", "vertraut");
  if (random(100)<20)
    s = s.replaceAll("sein", "entstammen");

  output = createWriter("positions.text");

  int x=30;
  int y=200; 
  fill(0); 
  for (String wordIn : s.split(" ")) {
    textSize(random(9, 56)); 
    text (wordIn, x, y); 
    x+=textWidth(wordIn)+6;
    if (wordIn.indexOf("!") > 0) {
      x=30; 
      y+=90;
    }
  }
  //output.print(s);


  //output.flush();
  //output.close();

  stop();
  save("t1.jpg"); 
  noLoop();
}

here is a program I made : P, use the addWord(word,“syn1,syn2,syn3,…”) to add words. Good luck

Code
ArrayList<String> documented = new ArrayList<String>(), words = new ArrayList<String>();
ArrayList<ArrayList<String>> syn = new ArrayList<ArrayList<String>>();
String text = "the new world is a small world";
char acceptable[] = ("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789").toCharArray();

void setup() {
  size(600,600);
  addWord("small","meager,minuscule,paltry");
  addWord("new","novel,current,modern");
  printArray(documented);
  printArray(syn);
  splitWords();
  printArray(words);
  for(int i = 0; i < words.size(); i++) print(wordSwap(words.get(i))+ " ");
}
void draw() {
}
void addWord(String word, String syn_s) {
  documented.add(word);
  String syn_s2[] = split(word+","+syn_s,",");
  ArrayList<String> syn_s3 = new ArrayList<String>();
  for(int i = 0; i < syn_s2.length; i++) syn_s3.add(syn_s2[i]);
  syn.add(syn_s3);
}
void splitWords() {
  String words_[] = split(text, " ");
  for(int i = 0; i < words_.length; i++) words.add(purify(words_[i]));
}

String wordSwap(String word) {
  if(documented.contains(word)) {
    int pos = getIndex(documented,word);
    //if(pos != -1) return syn.get(pos).get(floor(random(syn.get(pos).size())));
    if(pos != -1) {
      return syn.get(pos).get(floor(random(syn.get(pos).size())));
    }
    //syn.get(index).get(floor(random(syn.get(index).size()))))
  }
  return word;
}


int getIndex(ArrayList<String> reference, String item) {
  for(int i = 0; i < reference.size() && reference.contains(item); i++) if(reference.get(i).equals( item )) return i;
  return -1;
}

String purify(String input) {
  String newString = "";
  for (int i = 0; i < input.length(); i++) for (int j = 0; j < acceptable.length; j++) if (input.charAt(i) == acceptable[j]) {
    newString += acceptable[j];
    break;
  }
  return newString;
}

image

1 Like

Hello,

An example for you:

String words1[] = {"Wie", "Und", "froh", "ausgelassen", "bin", "ich", "daß"};

String s = "Wie froh bin ich, daß ich weg bin! Bester Freund, was ist das Herz des Menschen! Dich zu verlassen, den ich so liebe, von dem ich unzertrennlich war, und froh zu sein!";

void setup() 
  {
  for(int i=0; i<words1.length; i++)
    {
    print(words1[i] + " ");
    }
    
    int r1 = int( random(words1.length));
    int r2 = int( random(words1.length));
    s = s.replaceAll( words1[r1], words1[r2] );
    
    println(r1, r2, words1[r1], words1[r2]);
    println(s);
    
  noLoop();  
  }

You can also do this with:
https://processing.org/reference/StringList.html

:)

Many thanks! I try to understand what you did. Is there also a way to print every possible combination?

yes there is! Give me a few minutes, I’ll write a simple program that shows you how

edit: It is more difficult than expected. The main issue I found was the unconstant amount of things you need to change.
So if you have: a = {a,b,c} b = {b,c,d} c = {e,a,t},…
you need a for loop for each and every one of them.

so:
abe ace ade
aba aca ada
abt act adt

bbe ace ade
bba aca ada
bbt act at

cbe ace ade
cba aca ada
cbt act at

in short, you need an n amount of for loops.

here is a program with 2 variable segments:

Code (for 2 only)
String a[] = {"1","2","3","4"}, b[] = {"a","b","c","d"};
ArrayList<String> use = new ArrayList<String>();
void setup() {
  size(600,600);
}
void draw() {
  
  for(int i = 0; i < a.length; i++) {
    for(int j = 0; j < b.length; j++) {
      println(a[i],b[j]);
    }
  }
  
  noLoop();
}

1 a1 b1 c1 d
2 a2 b2 c2 d
3 a3 b3 c3 d
4 a4 b4 c4 d

1 Like

i found a way! Coding it rn

Thanks for the advice! I now wrote a code, where every line of my text just has a random text size on the screen. I don’t really understand how you managed to begin a new row when my line is longer than the width of the screen. Or how to write every line under the previous line and not in the next frame. Could you help me with that?

 PFont font;

  String [] lines;
  int index = 0;


void setup() {
  size(1100,800);
  font = createFont("Menlo-Regular", 20);
  String [] s = loadStrings("DieLeidendesjungenWerther.txt");
  String everything = join(s, " ");
  println(everything);
  lines = splitTokens(everything,",.?!;:");
  printArray(lines);
  frameRate(2);
}


void draw() {
  background(0);
  textFont(font);
  textSize(random(12,200));
  text(lines[index], 10, 200);
  index++;

}

I made the program! It will generate n options for a tower. Every tower is tall random(min,max). It will generate every possibility. For now, every tower’s floor are just numbers. (tower with 3 floor has numbers: 0,1,2, one with 5 floors has numbers: 0,1,2,3,4 and so on). Now just plug in the words!

this is the principle behind its workings:
image

Code
ArrayList<Integer> sizes = new ArrayList<Integer>();
int n = 5, min = 1, max = 5, path[] = new int[n];
boolean end = false;
void setup() {
  size(600, 600);
  for (int i = 0; i < n; i++) sizes.add((int)random(min, max));
  printArray(sizes);
  printArray(path);
}
void draw() {
  takePath(path);
  for (int i = 0; i < n; i++) {
    print(path[i]);
  }
  println();
  if (end) noLoop();
}

void takePath(int pPath[]) {
  pPath[n-1]++;
  for (int i = n-1; i >= 0; i--) {
    if (pPath[i] >= sizes.get(i)) {
      pPath[i] = 0;
      if (i > 0) {
        pPath[i-1]++;
      } else {
        end = true;
      }
    }
  }
  path = pPath;
}

That’s going into the next line

Sorry, but now it’s not

newWord(word,“syn1,syn2,syn3,…”)

anymore. Do I have to add the words with path?

Yeah, I used the wrong thing. There was a blackout where I live so in the end I had to rewrite the code several times because it didn’t save. I used different variable/funtion names. : P

example is:

But I don’t really understand where to put the words in the program that generates n options for the towers.

How are you sourcing your synonims are you using a dictionary of sort?

Yes I search for the synonyms in a dictionary

just create a tower by

ArrayList<ArrayList<String>> towers = new ArrayList<ArrayList<String>>();


void addTower(String info) {
   String a[] = split(info,",");
   ArrayList<String> tower = new ArrayList<String>();
   for(int i = 0; i < a.length; i++) tower.add(a[i]);
   towers.add(tower);
}
//after doing this, access it with
String acess(int towerID, int towerLevel) {
    return( towers.get(towerID).get(towerLevel) );
}
//so with: (I don't remember the names I used before)
void draw() {
    for(int i = 0; i < towers.size(); i++) {
         println( towers.get(i).path[i] );
    }
    noLoop();
}

(I am just referring to this part of the discussion, no mixing of words)

here is a version where you can hit a key to get a new screen

(text parts in different sizes)



PFont font;

String [] lines;
int index = 0;


void setup() {
  size(1700, 1000);
  font = createFont("Menlo-Regular", 20);
  // String [] s = loadStrings("DieLeidendesjungenWerther.txt");


  String everything; // = join(s, " ");
  everything = "Wie froh bin ich, daß ich weg bin! Bester Freund, was ist das Herz des Menschen! Dich zu verlassen, den ich so liebe, von dem ich unzertrennlich war, und froh zu sein!"; 

  println(everything);
  lines = splitTokens(everything, ",.?!;:");
  //  printArray(lines);
  textFont(font);
  frameRate(2);
}


void draw() {
  background(0);
  int y=7; 
  for (int index=0; index<lines.length; index++) {
    textSize(random(12, 200));
    float a1= textAscent() + textDescent();     // adding the textAscent() and textDescent() values will give you the total height of the line.

    y+=a1+13;
    text(lines[index], 10, y);
  }
  noLoop();
}

void keyPressed() {
  redraw();
}


In this version lines appear one by one

PFont font;

String [] lines;
int index = 0;

int y=7; 

boolean stopFlag=false;

void setup() {
  size(1700, 1000);
  font = createFont("Menlo-Regular", 20);
  // String [] s = loadStrings("DieLeidendesjungenWerther.txt");

  String everything; // = join(s, " ");
  everything = "Wie froh bin ich, daß ich weg bin! Bester Freund, was ist das Herz des Menschen! Dich zu verlassen, den ich so liebe, von dem ich unzertrennlich war, und froh zu sein!"; 

  println(everything);
  lines = splitTokens(everything, ",.?!;:");
  //  printArray(lines);
  textFont(font);
  frameRate(2);
  background(0);
}

void draw() {

  if ( ! stopFlag ) {
    textSize(random(12, 200));
    float a1= textAscent() + textDescent();     // adding the textAscent() and textDescent() values will give you the total height of the line.
    y+=a1+13;
    text(lines[index], 10, y);
  }

  if (index<lines.length-1) {
    index++;
  } else {
    stopFlag=true;
  }
}

void keyPressed() {
  // reset 
  println("key");
  background(0);
  clear();
  index=0;
  y=7;
  stopFlag=false;
}

line break is not when the
line is longer than the width of the screen
but I just display the content of the lines array item by item.

Chrisir