

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


In this assignment you will write a program that analyzes samples of text and then generates “pseudo- text” that mimics the original text. This will require you to use lists, dictionaries, files, and pseudo- random numbers. It will also require you to do some simple formatting of text. You are to create and submit a Python module named “hw4.py” containing definitions for the following functions (with precisely these spellings, including case):
You will also need to submit a file called hw4-output.txt containing sample output from your program, as specified below. To complete this assignment you will need a number of additional functions that have been provided for you. In addition to the GetTokens function that you have used in the past two assignments, you will also be using the following functions, which are defined in the file lib4.py on the downloads page:
Download the lib4.py file, and load it into the Python interpreter before you begin to work on your own functions (so that you can call the functions that have been provided for you). Alternatively, you may copy the contents of lib4.py into you hw4.py file, although this is not recommended. Here is what you need to do:
of tokens that appear in the given file.^1 Every token that appears in the file (with the possible exception of the very last token in the file) will appear as a key in this dictionary. Associated with each key is a list of all tokens that follow that token in the given file, including repetitions. For example, if the file "C:/junk.txt" contained the single line "The cat sat, then the cat ran.", not including the quotation marks, then MakeDigrams( "C:/junk.txt" ) should return the dictionary:
{”then” : [”the”], ”ran” : [”.”], ”, ” : [”then”], ”cat” : [”sat”, ”ran”], ”The” : [”cat”], ”the” : [”cat”], ”sat” : [”, ”]}
Notice that each token except the period appears as a key in this dictionary, because every token except the period is followed by another token. The set associated with each key contains all the tokens that follow the given token; because “the” is the only token that follows “then,” "then":["the"] is an item in the dictionary. Since both “sat” and “ran” follow the word “cat,” the key “cat” has a list of length two associated with it. In general, each list will contain as many elements as there are occurrences of the key that they are associated with. If “end” follows “the” three times, then “end” will appear three times in the list associated with the key “the.” The function MakeDigrams is to use OpenFile and GetToken to generate the sequence of tokens in the file, build the dictionary of digrams, then return this dictionary as the function’s value.