Code

// In this program, you will assemble a random sentence
// using nouns, verbs and adjectives loaded in from text files. 
// You must fill in the makeSentence() function, below. 
// (The rest of the program need not be modified.)
// See the solution by Placebo to understand the goal. 
//
// This is one of the few homework assignments so far, where you 
// have been asked to load in external data. You will be loading:
// -- Three special text files, which contain nouns, verbs, and adjectives:
//    http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/nouns.txt
//    http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/verbs.txt
//    http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/adjectives.txt
// -- A special font file (.vlw format), which you must create by using the 
//    "Create Font..." tool found under the Processing "Tools" menu.
//
// To be able to load in these resources, you must create a folder called "data"
// inside your sketch folder. Go to the "Sketch" Menu, choose "View Sketch Folder", and
// create a folder called "data" next to your .pde file. Put your .vlw font and these three
// text (data) files in the "data" folder. Note that creating
// the font may also make this folder for you.
// 
// For more information, see the documentation for loadStrings() and loadFont().

String[] nouns;
String[] verbs;
String[] adjectives;

PFont myFont;
String sentence;
String exclamation;

//------------------------------------------------
void setup(){
  size(500,100); 
  myFont = loadFont("helvetica.vlw"); 
  textFont(myFont); 
  
  verbs           = loadStrings("verbs.txt");
  adjectives      = loadStrings("adjectives.txt");
  nouns           = loadStrings("nouns.txt"); 
  makeSentence();
}

//------------------------------------------------
void draw(){
  background(250,180,80);
  fill(255);
  text(sentence, 20,60);
  fill(100,20,60);
  text(exclamation,width-14*exclamation.length(),60);
}

//------------------------------------------------
void mousePressed(){
  makeSentence();
}
void keyPressed(){
  makeSentence();
}

//------------------------------------------------
void makeSentence(){
sentence=(nouns[(int)random(0,nouns.length)]+" "+verbs[(int)random(0,verbs.length)].toLowerCase()+" "+adjectives[(int)random(0,adjectives.length)].toLowerCase()+" "+nouns[(int)random(0,nouns.length)].toLowerCase()+"...");
exclamation=(nouns[(int)random(0,nouns.length)].toUpperCase()+"!!!");
}


0603 - Random Poetry: Loading array data from external files

Statement:

// In this program, you will assemble a random sentence
// using nouns, verbs and adjectives loaded in from text files. 
// You must fill in the makeSentence() function, below. 
// (The rest of the program need not be modified.)
// See the solution by Placebo to understand the goal. 
//
// This is one of the few homework assignments so far, where you 
// have been asked to load in external data. You will be loading:
// -- Three special text files, which contain nouns, verbs, and adjectives:
//    http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/nouns.txt
//    http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/verbs.txt
//    http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/adjectives.txt
// -- A special font file (.vlw format), which you must create by using the 
//    "Create Font..." tool found under the Processing "Tools" menu.
//
// To be able to load in these resources, you must create a folder called "data"
// inside your sketch folder. Go to the "Sketch" Menu, choose "View Sketch Folder", and
// create a folder called "data" next to your .pde file. Put your .vlw font and these three
// text (data) files in the "data" folder. Note that creating
// the font may also make this folder for you.
// 
// For more information, see the documentation for loadStrings() and loadFont().

String[] nouns;
String[] verbs;
String[] adjectives;

PFont myFont;
String sentence;

//------------------------------------------------
void setup(){
  size(500,100); 
  myFont = loadFont("Verdana-24.vlw"); 
  textFont(myFont); 
  
  verbs           = loadStrings("verbs.txt");
  adjectives      = loadStrings("adjectives.txt");
  nouns           = loadStrings("nouns.txt"); 
  makeSentence();
}

//------------------------------------------------
void draw(){
  background(255); 
  fill(0,0,0); 
  text(sentence, 20,60); 
}

//------------------------------------------------
void mousePressed(){
  makeSentence();
}
void keyPressed(){
  makeSentence();
}

//------------------------------------------------
void makeSentence(){
  // Your job is to fill in this function. 
  // The method should assemble a sensible English phrase
  // into the global String variable called "sentence"
  // using randomly-selected items from the globally-scoped 
  // "verbs", "adjectives" and "nouns" String arrays.
}

hide statement