Compsci 101, Fall 2012, Lab 9

Use the Hand-In sheet and browse the code


Mad Libs were originally invented in the 1950's, and have appeared in numerous forms over the years (see wikipedia entry).
During today's lab, we'll be playing Mad Libs. This is a word game in which the player supplies words or phrases that fit various descriptions, and these words are then used in a story, often to comedic effect. Because the name Mad Lib is copyrighted, we'll use the name Tag-A-Story (you can call these Mad Libs yourself).

For example, suppose the tag-a-story is as follows.

The lab in Compsci 101 is <adjective> and I <emotive verb> it.

With this tag-a-story we might get any of the substitution stories below:

The lab in Compsci 101 is terrible and I love it.
The lab in Compsci 101 is long and I crave it.
The lab in Compsci 101 is squeamish and I despise it.
You'll be using one tag-a-story to develop and debug your program, then you'll be able to use any of the stories submitted by students in generating new stories. Note that words/phrases that will be replaced are delimited by angle-brackets: < and > as shown. We call these delimited words/phrases tags in this lab.

Start out by snarfing the code for this lab, or looking at the code directory.

There are six conceptual parts to this lab. Five correspond to a function you write in Madlib101.py and one is the creation of a story you upload.

  1. Complete template_to_string.
    This functiono reads the tag-a-story template file and creates one string from the file (which may consist of several lines).

    The code you get loops over the file and prints each line. Instead you should concatenate each line building one string you return.

  2. Complete read_tags.
    Read a line of tags and phrases for each tag, in the format shown in code/tags.txt:
      adjective: awesome, boring, stupid, amazing, fantastic, confusing,silly, big, yellow
      animal: dog, cat, human, lion, pony, squirrel
    
    The tag is the phrase to the left of the colon, and possible subsitutions are to the right of the colon, separated by commas. You should store the tag as the key in global dictionary tagstore, where the value is a list of substitutes for the tag.

    In the code you're given the tags and substitutes are printed. Change the code so that the key is used to associate a list of substitute phrases. Note that it's possible that the same tag appears multiple times in the file of tags, e.g., you may see the tag adjective more than once. Be sure to .strip() individual words stored in the list of words associated with each tag.

  3. Complete create_story.
    After completing the first two functions above this function should work by prompting the user for a response to each tag. Run the program to make sure things work. You may want to uncomment/comment the print statements to help debug your code.

    Replace user with auto and see what happens when you run the program more than once. Leave create_story in auto-generating mode.

  4. Create and complete print_out.
      def print_out(story,line_width)
    
    This function should print all the words in the story (the first parameter, a string of words) using the specified line width (an int, the second parameter).

    Loop over the individual words in story (split) and print each one on the same line (using print w, with a comma). Each time a word is printed update a count (int) with the number of characters printed. When this count is greater than line_width call print to goto the next line and reset the current count of the number of letters printed. You may want to print a space explicitly, don't forget to count this as one of the characters printed.

  5. Be Creative.
    You should write your own tag-a-story/Mad Lib and upload it to this site: http://www.cs.duke.edu/csed/tag-a-story/upload/. Pick a category and upload your file. You can verify that the file was uploaded by running the program URLreader.py and choose the file you uploaded. Or you can visit this website that stores the uploaded files.

  6. (time permitting) Allow the user to choose a tag-a-story template from the web -- do this by combining the code and functionality in URLreader.py with the MadLibs101.py module to generate a story from a URL.


Submit

Submit all your code and a README with group members using lab_9 as the submission name. Be sure to include all group members in the README as well as completing the handin page.