Compsci 101, Fall 2017, Lab 10

The lab

In this lab you will:

There is code to snarf for lab 10, that code is also here.

Getting Credit for Lab 10

To get credit for this lab, you will need to do the following by Sunday night.


Mad Libs, Gladlibs, Tag-a-Story

During today's lab, you'll be running code and then modifying the program you get so that the computer will generate a random story as is done in the game 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 template 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 dread it.

You'll be using one tag-a-story to develop and debug your program. 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.

There are three conceptual parts to this lab. These are summarized here and then elaborated on below.

  1. Run Storyline.py and understand how it uses Replacement.py. You'll be asked several questions about the functions in each module. Some questions are high-level, a few are more about the code than about the functionality.
  2. Modify Storyline.py so that it offers the user a choice of what templates to use. You can do this by creating your own templates.
  3. Create your own template and upload the template and a story to the shared web-repository for Compsci 101 Tag-A-Story resources.

Importing, using, and Understanding Replacement.py

The code in Replacement.py is designed to make it possible to find replacements for tags like <noun> or <name> or <place> and so on. Answer these questions in the online form.

Part A:

First, explain why different stories are generated each time you run Storyline.py. Be general, but include reasons -- is it because of code in Storyline.py or in Replacement.py that the stories are different each time the program runs?

Part B:

In the function getReplacement three comments indicate three parts of the function. The function starts with a global declaration and ends with a return statement, with these three sections in between.

  1. What is the purpose of the expression: global repd?
  2. What is the general purpose of the code segment that starts with this comment. In answering, do you think replacements are loaded from the file system or from the web? Why?
    # load the global dictionary if it hasn't been
    
  3. What is the purpose of the code segment that starts with this comment:
    # shouldn't have <>, but check and be safe
    
  4. What is the purpose of the code segment that starts with this comment:
    # check on tag, if not a key, try plural
    
  5. Explain how the return statement works, both in terms of parameter tag and global repd as well as the function random.choice
  6. Find the call to getReplacement in the module Storyline.py -- explain the syntax of the call and its purpose as it's used in Storyline.py. In answering this, indicate whether you think the < and > tag delimiters are part of the string passed to getReplacement and what your evidence is for that.

Modifying Storyline.py

The main part of the program in Storyline.py loads the template you snarfed with file name labtemplate.txt. This template is not creative nor exciting nor did you write it! You should create two new templates. For each you can use Eclipse to create an untitled text file or a new file depending on which menu you use. Create both templates so that they are in the folder named templates that you snarfed.

Part C:

  1. Create a template using only the tags whose replacements you'll find in the tagreplacements folder: adjective, animal, color, emotive-verb, food, noun, place, verb.
  2. Create another template, you should create a new category of tag and store the replacements for the tag in a file in the tagreplacements folder, though your new template will be in the templates folder. If your tag is <car> you must store replacements in a file named car.txt

Modify Storyline.py so that it creates two stories: one for each of the templates you wrote. Be sure that the story with the new <tag> works if you added tag replacements. Answer these two questions on the lab form about your templates.

Part D:

  1. What code reads the replacements for the new tag you added in the tagreplacements folder? How did you determine this?
  2. Do you think the division of code between Storyline.py and Replacements.py makes sense? Why do you think it was done this way?

Uploading your own Story Template

You'll need to upload your own template. To do this visit this site: http://www.cs.duke.edu/csed/tag-a-story/upload/

where you'll need to authenticate with your netid. Pick a category and upload your file. You can verify that your file was uploaded by looking at the logfile http://www.cs.duke.edu/csed/tag-a-story/logs.txt. You should do this with the template you made that uses only existing categories.

After you're sure you understand how Replacements.py and Storyline.py work, you should create your own module: TemplateSource.py, to read/load the file

http://www.cs.duke.edu/csed/tag-a-story/logs.txt 
Then parse it to create a dictionary of categories to URLs and allow the user to choose a category and then pick a random story from that category and print the story generated by the template. This new module will be imported into Storyline.py if you can make TemplateSource.py work.

Answer these questions about the module, then it is optional to write the code that will make your ideas work.

Part E:

  1. Look at the format of each line in the log file here: http://www.cs.duke.edu/csed/tag-a-story/logs.txt How will you parse the information on one line to get the category and the URL that represents the template uploaded for this category.
  2. How will you write code (what libraries) to read the log file, read each line, and extract the category and the URL?
  3. How will you create a dictionary in which categories for templates are the keys and a list of URLs of templates is the value associated with the key.
  4. What functions should your module export so that the code in Storyline.py can simply call a function in TemplateSource.py with a category and get a template at random from that category.

Note writing the code for this part is optional, but you should attempt to answer these questions.

Submit

After submitting the lab form, then submit the code you wrote with ambient/websubmit. Use lab10 as the submission folder. (Again, the code for Part E is optional).