Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add ReplaceableWriting demo to examples #485

Open
dhowe opened this issue Nov 24, 2017 · 1 comment
Open

Re-add ReplaceableWriting demo to examples #485

dhowe opened this issue Nov 24, 2017 · 1 comment
Assignees

Comments

@dhowe
Copy link
Owner

dhowe commented Nov 24, 2017

// needs Wordnet...
public class ReplaceableWriting extends PApplet
{
  RiText[] rts; 
  RiWordnet wordnet; 
  int charsPerLine = 60;

  String test = "Last Wednesday we decided to visit the zoo. We arrived the next morning after we breakfasted, cashed in our passes and entered. We walked toward the first exhibits. I looked up at a giraffe as it stared back at me. I stepped nervously to the next area. One of the lions gazed at me as he lazed in the shade while the others napped. One of my friends first knocked then banged on the tempered glass in front of the monkey’s cage. They howled and screamed at us as we hurried to another exhibit where we stopped and gawked at plumed birds. After we rested, we headed for the petting zoo where we petted wooly sheep who only glanced at us but the goats butted each other and nipped our clothes when we ventured too near their closed pen. Later, our tired group nudged their way through the crowded paths and exited the turnstiled gate. Our car bumped, jerked and swayed as we dozed during the relaxed ride home.";

  public void setup()
  {
    size(400, 400);
    RiText.defaults.alignment = LEFT;    
    wordnet = new RiWordnet(this);
    rts = RiText.createLines(this, test, 30, 50, charsPerLine);    
  }

  public void draw()
  {
    background(180);

    // substitute every 50 frames 
    if (frameCount % 50 == 1)   
      doSubstitution();
  }

  /*  replace a random word in the paragraph with one
   from wordnet with the same (basic) part-of-speech */
  void doSubstitution()
  {
    String[] words = test.split(" ");

    // loop from a random spot
    int count =  (int)random(0, words.length);
    for (int i = count; i < words.length; i++) 
    {
      // only words of 3 or more chars
      if (words[i].length()<3) continue;
        
      // get the pos
      String pos = wordnet.getBestPos(words[i].toLowerCase());        
      if (pos != null) 
      {
        // get the synset
        String[] syns = wordnet.getSynset(words[i], pos);
        if (syns == null) continue;

        // pick a random synonym
        String newStr = syns[(int)random(0, syns.length)];

        if (Character.isUpperCase(words[i].charAt(0)))              
          newStr = RiTa.upperCaseFirst(newStr); // keep capitals

        // and make a substitution
        test = RiText.regexReplace("\\b"+words[i]+"\\b", test, newStr);

        RiText.deleteAll();   // clean up the last batch

        // create a RiText[] from 'test' starting at (30,50) & going down
        rts = RiText.createLines(this, test, 30, 50, charsPerLine);      
        break;
      }
      if (count == words.length) count = 0;
    }       
  }


}
@dhowe dhowe self-assigned this Nov 24, 2017
@dhowe
Copy link
Owner Author

dhowe commented Sep 24, 2019

Or perhaps just use a map of words to arrays of substitutions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant