import rita.*; import guicomponents.*; import java.lang.reflect.*; import org.w3c.dom.*; import org.w3c.dom.html.*; // CHAPTER 1: CONFIGURABLE VARIABLES AND VARIABLE OBJECTS // how high (tall) the word labels are int LABELHEIGHT = 20; // how much horizontal space is between word labels int SPACING = 10; // how much space is between rows of labels (i.e. lines of poetry) int SHIFTHEIGHT = 30; // how far the interaction options are from the top int OPTIONS_DISTANCE_FROM_TOP = 250; // how far the verse buttons are from the top int VERSE_DISTANCE_FROM_TOP = 275; // this is the initial data model // NOTE: for a standalone app, remove URL reference String dataFolder = "http://www.eddeaddad.net/egnoetry/"; //String dataFolder = ""; String dataShakespeare = "shakespeare-sonnets.txt"; String dataCarroll = "carroll-aliceInWonderland.txt"; String dataConrad = "conrad-heartOfDarkness.txt"; String nameShakespeare = "Shakespeare Sonnets"; String nameCarroll = "Alice In Wonderland"; String nameConrad = "Heart Of Darkness"; String fileToUse = dataCarroll; String oldFileToUse = dataCarroll; // the color of the control buttons int controlButtonsColor = #FCBDD8; // the color of the word buttons int wordButtonColor = #8FECF0; // the verse, which is made up of lines List verse = new ArrayList(); // the number of lines in the initial verse int linesInVerse= 4; // maximum possible number of lines in verse final int MAX_LINES_IN_VERSE = 8; final String FORM_FREEVERSE = "free verse"; final String FORM_QUATRAIN = "Quatrain"; final String FORM_EHAIKU = "eHaiku"; String currentForm = FORM_FREEVERSE; // an bigram model RiMarkov bigramModel; // a list of words that have been generated from the model, and are ready to use List wordsFromModel = new ArrayList(); // a random number generator Random generator = new Random(); // the text being displayed at the top RiText displayTop[] = new RiText[MAX_LINES_IN_VERSE]; // the button that changes model GButton changeModelButton; // CHAPTER 2: STARTING UP int LEFT_MARGIN = 50; void setup() { // the size of the display area size(900,600); // instantiate the bigram model bigramModel = new RiMarkov(this, 2); bigramModel.setUseSmoothing(false); bigramModel.loadFile(dataFolder + fileToUse); // this is the button to regenerate the whole poem GButton regenerateButton = new GButton(this, "Regenerate Verse ", LEFT_MARGIN, OPTIONS_DISTANCE_FROM_TOP, getWidth("Regenerate "), LABELHEIGHT ); regenerateButton.setTextAlign(GAlign.CENTER); regenerateButton.setTextAlign(GAlign.MIDDLE); regenerateButton.setColours(controlButtonsColor,controlButtonsColor,controlButtonsColor); regenerateButton.tag = "regenerate"; // this is the button to change the poetic form // probably should be a combobox, but I had problems with g4p's GCombo // and anyways it keeps the code simple GButton changeFormButton = new GButton(this, "Form: " + FORM_FREEVERSE + " ", 180, OPTIONS_DISTANCE_FROM_TOP, getWidth(FORM_FREEVERSE), LABELHEIGHT ); changeFormButton.setTextAlign(GAlign.CENTER); changeFormButton.setTextAlign(GAlign.MIDDLE); changeFormButton.setColours(controlButtonsColor,controlButtonsColor,controlButtonsColor); changeFormButton.tag = "formchange"; // this is the button to change the language model // probably should be a combobox, but I had problems with g4p's GCombo // and anyways it keeps the code simple changeModelButton = new GButton(this, "Model: " + nameCarroll + " ", 300, OPTIONS_DISTANCE_FROM_TOP, 200, LABELHEIGHT ); changeModelButton.setTextAlign(GAlign.CENTER); changeModelButton.setTextAlign(GAlign.MIDDLE); changeModelButton.setColours(controlButtonsColor,controlButtonsColor,controlButtonsColor); changeModelButton.tag = "modelchange"; // this is the button to try to import text from a web page (assuming an applet context) GButton importButton = new GButton(this, "Import Model ", 510, OPTIONS_DISTANCE_FROM_TOP, 90, LABELHEIGHT ); importButton.setTextAlign(GAlign.CENTER); importButton.setTextAlign(GAlign.MIDDLE); importButton.setColours(controlButtonsColor,controlButtonsColor,controlButtonsColor); importButton.tag = "importbutton"; // this is the button to try to export to a web page (assuming an applet context) GButton exportButton = new GButton(this, "Export Verse ", 610, OPTIONS_DISTANCE_FROM_TOP, 90, LABELHEIGHT ); exportButton.setTextAlign(GAlign.CENTER); exportButton.setTextAlign(GAlign.MIDDLE); exportButton.setColours(controlButtonsColor,controlButtonsColor,controlButtonsColor); exportButton.tag = "exportbutton"; // call the method that creates the first draft of the poem createVerse(); // we're not doing animation, so we don't need the display area to be continually redrawn noLoop(); } // draw the display void draw(){ background(250); } // CHAPTER 3: CREATING A VERSE FROM THE LANGUAGE MODEL AND PRINTING IT AS GBUTTONS // create a verse, // which is made up of several lines, // where each line has several GButtons containing changeable words void createVerse() { List tempRow = new ArrayList(); boolean isLastRow = false; String displayLine = new String(); String theLine = new String(); if ( currentForm == FORM_FREEVERSE ) { // there will be 2-8 lines, randomly determined linesInVerse = generator.nextInt( 7 ) + 2; } else if ( currentForm == FORM_QUATRAIN ) { linesInVerse = 4; } else if ( currentForm == FORM_EHAIKU ) { linesInVerse = 3; } // get rid of old lines, if the previous verse had more lines than this one for ( int x=linesInVerse; x 1) { println("number of completions: " + completions.length ); int numberToPick = generator.nextInt( completions.length ); newWord = completions[numberToPick]; //println("the completion is: " + newWord ); } else { // next, see if you found an (n-1, completion) completions = bigramModel.getCompletions(wordPre); if ( completions != null && completions.length > 0) { println("number of n-1 completions: " + completions.length ); int numberToPick = generator.nextInt( completions.length ); newWord = completions[numberToPick]; //println("the n-1 completion is: " + newWord ); } } // if not, just use the original word if ( completions == null || completions.length == 0) { newWord = wordChanging; println("there are no completions"); } } // now that we've figured out the new word... // set the button's new width and text buttonChanging.setWidth( getWidth( newWord ) ); buttonChanging.setText(newWord + " "); int newXPos = buttonChanging.getX() + getWidth( newWord ) + SPACING; // shift the following buttons if need be for ( int i=wordIndex+1; i 18 ) { newLanguageModelName = newLanguageModelCorpus.substring(0,17) + "..."; // TO DO: coordinate this better with file-reading language building // regenerate model bigramModel = new RiMarkov(this, 2); bigramModel.setUseSmoothing(false); bigramModel.loadText(newLanguageModelCorpus); // empty "next words generated from model" string wordsFromModel = new ArrayList(); changeModelButton.setText( "Model: " + newLanguageModelName ); redraw(); } } catch (Exception e) { e.printStackTrace(); } } String newLanguageModelCorpus = ""; String newLanguageModelName = ""; void seekModelText(Node root ) { if (root instanceof Element) { // if we've found the textarea, look through the children to get the text in the textarea if ( ((Element) root).getTagName().equals("TEXTAREA") ) { HTMLTextAreaElement h = (HTMLTextAreaElement)root; newLanguageModelCorpus = h.getValue().trim(); //println( "length of data model: " + dataModel.length() ); //println( "last several characters: " + dataModel.substring(dataModel.length()-20,dataModel.length()-1) ); //println( "data model is: " + h.getValue() ); //println( "data model is: " + root.getValue() ); } } if (root.hasChildNodes()) { NodeList children = root.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { seekModelText(children.item(i)); } } } } // attempts to export the poem into a web page // by calling a javascript function void exportPoem() { String poemAsLine = new String(); for (int i=0; i"; } try { String urlEncodedPoem = URLEncoder.encode(poemAsLine.toString(),"UTF-8"); java.applet.AppletContext context = getAppletContext(); if ( context != null ) { AppletContext a = getAppletContext(); URL url = new URL("http://www.eddeaddad.net/eGnoetry/output.html?" + urlEncodedPoem); a.showDocument(url,"_blank"); } } catch (Exception e) { println("unable to export"); } }