Starting from:

$20

String Manipulation, File Reading & Writing

1 “Mad Libs” (100 points) A Mad Lib is a story template that has many missing words, each indicated by a label giving the category (part of speech) of the missing word. For example, a story template will contain the word “NOUN” in many places instead of specific nouns, the word “ADJECTIVE” in many places instead of specific adjectives, etc. Before reading the story, the reader asks listeners to provide the missing words without indicating where and how the words will be used in the story. If, for example, this is the story template: 1 Once upon a NOUN, three PLURAL NOUN VERB PAST in the NOUN. The reader would first ask “Give me a noun” to which the listener might reply “piano”. The reader would then ask “Give me a plural noun” and the listener might say “hamburgers”. The reader would next ask “Give me a past tense verb” and the listener might say “frolicked”. Finally the reader would ask “Give me a noun” and the listener might say “dumpster”. The reader replaces the missing words with those provided by the listener and reads the result: Once upon a piano, three hamburgers frolicked in the dumpster. At this point everyone in the room giggles until their eyes water. For this assignment you will write a program that reads a Mad Lib story template from a file (template files will be provided to you). The Mad Lib template files may contain any of the following part of speech labels: • NOUN • PLURAL NOUN • VERB • VERB PAST • ADJECTIVE The program asks the user to supply each of the needed words and writes out the completed version of the story to a new file (Note: the label VERB on its own indicates present tense). You may assume that the two part of speech labels with two words (PLURAL NOUN and VERB PAST) will never be split over two lines in the file. The program will also print a simple numerical analysis of the contents of the file (explained below). 1.1 Details Create a new file called “madlib.py”. In the file: 1.) Write a function called print report that takes a single string parameter for the name of a file. The function will: i.) Read the given file and print a report giving the following information: a.) the total number of vowels in the file (for this assignment ‘y’ is not a vowel.) b.) the total number of consonants in the file c.) the total number of white spaces in the file (spaces, tabs, newlines) d.) the total number of punctuation characters in the file (for this assignment assume that punctuation is anything that is neither a letter from the alphabet nor a white space) e.) the total number of characters in the file f.) the percent of the file composed of vowels, consonants, white spaces, and punctuation characters. ii.) The function should print the report in a pretty table as shown in the following example (in the example the file used was named “test.txt”). 2 ---------test.txt-------Vowels: 25 Consonants: 21 Whitespace: 34 Punctuation: 42 ------------------------Total: 122 Percent vowels: 20.5 Percent consonants: 17.2 Percent spaces: 27.9 Percent punctuation: 34.4 ========================= • For the percents you’ll need to use the built in round function. Here’s an example: round(3.14159265, 4) → 3.1416 – Experiment with it in a shell until you know how to use it. - Hint: it might be a good idea to create your own text file to test this code on. For example, you might first try it with a file that has exactly 5 vowels in it and nothing else... - Test your function and verify it works correctly before moving on. 2.) Write a function called replace parts of speech that takes two parameters. The first is a string containing part of speech labels that need to be replaced by words (e.g. “The ADJECTIVE NOUN in the NOUN VERB PAST.”). The second is a string indicating which part of speech label to replace, e.g. “NOUN”. i.) For each occurrence of the given part of speech in the given string ask the user for a word of the appropriate type. ii.) Replace the part of speech label with the word provided by the user iii.) After replacing all parts of speech of the indicated type, return the new version of the given string - For example, calling the function like this: replace parts of speech("the NOUN VERB PAST the NOUN", "NOUN") would ask the user to enter two nouns. Assuming the user entered “dog” for the first noun and “duck” for the second, the function would return: "the dog VERB PAST the duck" Note: When asking the user for words, your prompts should like these examples: Enter noun: Enter plural noun: Enter verb: Enter verb past: Enter adjective: - Test your function and verify it works correctly before moving on. 3.) Write a function called complete mad lib that takes a string parameter for the name of a Mad Lib template file to read. The function will: 3 i.) Read the indicated template file line by line and • Replace all part of speech labels with words entered by the user – you’ll need to figure out how to use the function you wrote to accomplish this ii.) Write the completed story out to a new file: • the new file should have the same name as the original file, prepended with the string “MAD ”) (e.g. if the original file was named “tortoise.txt” the new file should be “MAD tortoise.txt”) - Test your function and verify it works correctly before moving on. 4.) In main do the following: i.) Ask the user to enter the name of a Mad-Lib template file (assume the file is in the same directory as this program) ii.) Call your function to print the character report on that file iii.) Call your function to have the user complete the Mad Lib story 5.) Verify that your documentation makes sense and that you’ve added documentation to each of your functions. 6.) Verify that your program works 7.) Upload your file to the Program 7 dropbox folder on D2L

More products