Code

/*My code is extremely confusing and long. Half the time I didn't really know what I was doing. But it works. Slowly.*/

PFont myFont;
String sentence = "";
String userLettersName = "";
String userLettersAuthor = "";

int margin = 10;
int authorY = 20;

void setup(){
  size (700,500);
  myFont = loadFont ("CourierNewPSMT-12.vlw");
  textFont (myFont);
}


void getTheNewStory (String name) {
  name = name.trim();
  name = name.toLowerCase();
  println(name);
  String [] input = split (name, ',');
  String file = input[0] + ".txt";
  println (file);
  String [] loadOriginalStory = loadStrings (file);
  String OriginalStory = loadOriginalStory[0];
  int storyLength = OriginalStory.length();
  int newTokenCount = 1;
  boolean bNewToken = true;
  String [] newTokens = new String [storyLength/10];

  String [] tokens = split (OriginalStory, ' ');
  newTokens[0] = tokens [0];
  for (int j=1; j<tokens.length; j++){
    bNewToken = true;
    for (int k=j-1; k>=0; k--){
      if (tokens[j].equals(tokens[k])){
        bNewToken = false;
      }
    }
    if (bNewToken){
      newTokens[newTokenCount] = tokens[j];
      newTokenCount ++;
    }

  }

  float [][] tickCounter = new float [newTokenCount][newTokenCount];

  for (int mtx=0; mtx<tokens.length-1; mtx++){
    for (int mty=0; mty<newTokenCount; mty ++){
      for (int mtv=0; mtv<newTokenCount-1; mtv++){
        if (tokens[mtx].equals(newTokens[mty])){
          if (tokens[mtx+1].equals(newTokens[mtv])){
            tickCounter [mty][mtv] ++;
            //println (tokens[mtx] + " " + tokens[mtx+1] + " " + mty + " " + mtv);
            //println (newTokens[mty] + " " + newTokens[mtv] + " " + tickCounter [mty][mtv]);
          }
        }
      }

    }
  }

  float [] columnCount = new float [newTokenCount];
  for (int cpx=0; cpx<newTokenCount; cpx++){
    for (int cpy=0; cpy<newTokenCount; cpy++){
      columnCount[cpx] = columnCount[cpx] + tickCounter[cpx][cpy];
    }
  }

  float [][] probability = new float [newTokenCount][newTokenCount];
  for (int px=0; px<newTokenCount; px++){
    for (int py=0; py<newTokenCount; py++){
      if (tickCounter[px][py] > 0){
        probability [px][py] = tickCounter[px][py]/columnCount[px];
      }
    }
  }



  for (int px=0; px<newTokenCount; px++){
    for (int py=0; py<newTokenCount; py++){
      if (tickCounter[px][py] > 0){
        probability [px][py] = tickCounter[px][py]/columnCount[px];
      }
    }
  }

  String [] newStory = new String [600];
  newStory[0] = newTokens[0];

  for (int check=0; check<newStory.length-1; check++){
    float pickOne = random(0,1);
    for (int word=0; word<newTokenCount; word++){
      for (int cy=0; cy<newTokenCount; cy++){
        if (newStory[check] == newTokens[word]){
          if (pickOne < probability[word][cy]){
            newStory[check+1]=newTokens[cy];
            break;
          } 
          else {
            pickOne -= probability[word][cy];
          }
        }
      }
    }
  }
  String mainCharacter = "";
  String setting = "";
  String uniqueWord = "";
  
  if (input[0].equals("seuss")){
    mainCharacter = "Sam";
    setting = "box";
    uniqueWord = "eggs";
  }
  if (input[0].equals("chaucer")){
    mainCharacter = "he";
    setting = "Canturbury";
    uniqueWord = "lord";
  }
  if (input[0].equals("shakespeare")){
    mainCharacter = "Duncan";
    setting = "castle";
    uniqueWord = "courage";
  }
  if (input[0].equals("twain")){
    mainCharacter = "Tom";
    setting = "fence";
    uniqueWord = "whitewash";
  }
   if (input[0].equals("austen")){
    mainCharacter = "Darcy";
    setting = "Netherfield";
    uniqueWord = "dances";
  }
    if (input[0].equals("hawthorne")){
    mainCharacter = "Pearl";
    setting = "home";
    uniqueWord = "Puritans";
  }
    if (input[0].equals("cather")){
    mainCharacter = "He";
    setting = "Hanover";
    uniqueWord = "kitten";
  }
  
  for (int checkName=0; checkName<newStory.length; checkName++){
    if (newStory[checkName].equals(mainCharacter)){
      newStory[checkName] = input[1];
    }
     if (newStory[checkName].equals(setting)){
      newStory[checkName] = input[2];
    }
    if (newStory[checkName].equals(uniqueWord)){
      newStory[checkName] = input[3];
    }
  }
  
  String printFinal = "";
  for (int end=0; end<newStory.length; end++){
    if (newStory[end].equals(".") || newStory[end].equals(",") || newStory[end].equals("?") || newStory[end].equals(";")){
      printFinal += newStory[end];
    } 
    else {
      printFinal += (" " + newStory[end]);
    }
  }
  sentence = printFinal;
}

void draw(){
  background (255);
  fill(0);
  if (second() % 2 == 0){
    float cursorPosition = textWidth(userLettersAuthor)+ 75;
    line (cursorPosition+10, authorY+35, cursorPosition+10, authorY+47);
  }

  text ("Please choose an author from the following: Chaucer, Seuss, Shakespeare, Austen, Hawthorne, Cather, or Twain. Then type in your character's name, the main setting, and a random noun. Please put a comma in-between each word and use only lower case. For example: seuss,sam,tree,eggs ", margin, authorY, width-10, 100);
  text ("User input:" + userLettersAuthor, margin,authorY+45);

  text ("Your custom story:" + sentence, 10,70, width-10,height-10);

}

void keyPressed () {
  if (key == BACKSPACE) {
    if (userLettersAuthor.length() > 0){
      userLettersAuthor = userLettersAuthor.substring (0, userLettersAuthor.length()-1);
    }
  } 
  else if (textWidth(userLettersAuthor+key) < width){
    userLettersAuthor += key;
  }
  if (key == 10){
    getTheNewStory (userLettersAuthor);
  }
}

0904 - Show some Progress: Some tinkering perhaps

Statement:Upload some progress you've made on your final experiment/project.
Just to show off the final random story generator I managed to scrape together after much trial and error. This isn't actually my final project for this class.

hide statement