How to Modify "ReplaceWriting.pde" from RiTa toolkit?
in
Contributed Library Questions
•
11 months ago
Hello:
I'm a new Processing user. I've managed to make simple changes to a few examples like adding "saveStrings" to save text output to a file. I would like to modify "ReplaceWriting.pde" from the RiTa toolkit but I'm not sure how.
This is what I would like to do in four simple steps:
1 Remove all realtime screen output and timing options. Application progress via the console is sufficient.
2. Load a text file (instead of just processing a "String test" written into the original pde code)
3. Process every word in text file and replace with random synonym from Wordnet (if available) except those common words contained in a "stopwords.txt" file. This file would contain one word per line, for example, the, and, a, etc.
If it is too difficult to use a "stopwords" file then every word should be processed.
4. Save Wordnet processed output to a text file.
I would appreciate any modifications to the code below that will achieve this result or if you know of any other Processing code that will accomplish the same task.
Thanks,
I'm a new Processing user. I've managed to make simple changes to a few examples like adding "saveStrings" to save text output to a file. I would like to modify "ReplaceWriting.pde" from the RiTa toolkit but I'm not sure how.
This is what I would like to do in four simple steps:
1 Remove all realtime screen output and timing options. Application progress via the console is sufficient.
2. Load a text file (instead of just processing a "String test" written into the original pde code)
3. Process every word in text file and replace with random synonym from Wordnet (if available) except those common words contained in a "stopwords.txt" file. This file would contain one word per line, for example, the, and, a, etc.
If it is too difficult to use a "stopwords" file then every word should be processed.
4. Save Wordnet processed output to a text file.
I would appreciate any modifications to the code below that will achieve this result or if you know of any other Processing code that will accomplish the same task.
- // Requires: RiTa.WordNet
- import rita.*;
- import rita.wordnet.*;
- 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.";
- void setup()
- {
- size(480, 480);
- RiText.setDefaultAlignment(LEFT);
- wordnet = new RiWordnet(this);
- rts = RiText.createLines(this, test, 30, 50, charsPerLine);
- }
- 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;
- }
- }
Thanks,
1