// generative exquisite corpse crawling 5 txts with content from blogs! // Petra farinha // based on Daniel Shiffman A2Z class examples! package corpse; import java.io.*; import java.util.regex.*; import processing.core.*; import a2z.*; public class midterm4 extends PApplet{ String output = ""; // the result string PFont f; String firstword; String typing=""; int counttyping= 0; public void setup() { size(500,500); f = createFont("OfficinaSans",12,true); } public void draw(){ background(0); textFont(f); text("GENERATIVE EXQUISITE CORPSE", 20,50); text("Enter a word to start: ", 20,70); text(typing,150,70); if(typing != null && key=='\n'&& counttyping==0){ String[] filenames = {"dreams.txt","elim.txt","nicegirl.txt","swerdloof.txt", "tifany.txt"}; generatetxt(filenames); counttyping++; } //animate txt text(output, 20,100); } public void keyPressed(){ if( key == '\n' ){ firstword = typing; typing = ""; counttyping =0; }else{ typing = typing + key; } } public String makePath(String s) { return "/Users/petrafarinha/Documents/workspace/exquisitecorpse/" + s; } public void generatetxt (String [] paths) { try { String[] contents = new String[paths.length]; for (int i = 0; i < paths.length; i++) { // get files //System.out.println(makePath(paths[i])); A2ZFileReader fr = new A2ZFileReader(makePath(paths[i])); contents[i] =fr.getContent(); // put them into a single line String regex="(\\r|\\n)"; Pattern p= Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(contents[i]); while(m.find()) { contents[i] = m.replaceAll(" "); } System.out.println(contents[i] + "\n"); } // variables for generate txt StringBuffer result = new StringBuffer(); // add the result to the StringBuffer int countArray = 0; // increments the array - go for one text to the other int wordCounter = 0; // increments every time a word in found int totalCounter = 0; // to stop if the program goes into a infinite loop // generate text = process while(wordCounter<5){ //System.out.println(wordCounter + " " + countArray); if (contents[countArray].contains(firstword)){ // word + next 11 words String regex = "\\s" + firstword + "(\\W+\\w+){11}"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(contents[countArray]); if(m.find()) { result.append(m.group()); output = result.toString(); output = output.trim(); // get the last word String [] words = output.split("\\s"); int len = words.length; String lastword = words[len-1]; firstword = lastword; //increments word counter wordCounter++; } } // increments array and go back to 0 countArray = (countArray + 1) % 5; // to avoid infinite loops totalCounter++; if (totalCounter > 100) { break; } } System.out.println(output+"\n"); //improve!! String regexF = "\\b(\\w+|\\w+\\W\\w+)\\b\\W+\\1(\\s|\\W)"; // Regex that matches double words Pattern pF = Pattern.compile(regexF, Pattern.CASE_INSENSITIVE); // Compile Regex Matcher mF = pF.matcher(output); // Create Matcher while (mF.find()) { output = output.replaceAll(mF.group(),"\n"+ mF.group(1)+ " "); } // Capitalize first letter String first = output.substring(0,1).toUpperCase(); String last = "."; output = first + output.substring(1,output.length()) + last; String [] regexs = {"\\(\\w+\\)", "\\)|\\(", "(\"([^\"]*)\"){1,10}", "\""}; for(int a=0;a