Docsity

Practice Homework 4 - Introduction to Programming | EECS 12, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS12 Homework #4 Due: 11:59 PM, Friday, February 11, 2005
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):
1. TestGetToken( input file name )
2. MakeDigrams( input file name )
3. GeneratePseudoText( output file name, digrams, num tokens )
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:
1. OpenFile( input file name )
2. CloseFile()
3. GetToken()
4. SetSeed( seed )
5. NextRandomNumber()
6. PickRandomElement( list )
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:
1. Read chapters 12 and 13 of How to Think Like a Computer Scientist. You will not be using
Python’s ability to define new classes and methods in this assignment, but we will be discussing
it in class very soon.
2. Write a function called TestGetToken that accepts a single argument, which must be a string
specifying a text file that already exists on your computer; e.g. "C:/my python stuff/test.txt".
The function should pass this string to OpenFile, then proceed to call GetToken() repeatedly
until it returns the empty string. It should print out each string that GetToken() returns until
it returns the empty string, at which point the function should return None. Verify that the
functions OpenFile and GetToken (which have been supplied for you) work together to allow
you to read one token at a time from the given file, where “token” is given the same meaning
as in homework #2. Note that GetToken() also returns the new line characters as tokens, in
addition to words and punctuation. Be sure to understand how GetToken() works, as it will
be the basis of all that follows.
3. Write a function called MakeDigrams that accepts a single argument, which must be a string
specifying a text file that already exists on your computer (as above). This function is to
construct and return a dictionary consisting of “digrams,” which encode all consecutive pairs
1
pf3

Partial preview of the text

Download Practice Homework 4 - Introduction to Programming | EECS 12 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS12 Homework #4 Due: 11:59 PM, Friday, February 11, 2005

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):

  1. TestGetToken( input file name )
  2. MakeDigrams( input file name )
  3. GeneratePseudoText( output file name, digrams, num tokens )

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:

  1. OpenFile( input file name )
  2. CloseFile()
  3. GetToken()
  4. SetSeed( seed )
  5. NextRandomNumber()
  6. PickRandomElement( list )

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:

  1. Read chapters 12 and 13 of How to Think Like a Computer Scientist. You will not be using Python’s ability to define new classes and methods in this assignment, but we will be discussing it in class very soon.
  2. Write a function called TestGetToken that accepts a single argument, which must be a string specifying a text file that already exists on your computer; e.g. "C:/my python stuff/test.txt". The function should pass this string to OpenFile, then proceed to call GetToken() repeatedly until it returns the empty string. It should print out each string that GetToken() returns until it returns the empty string, at which point the function should return None. Verify that the functions OpenFile and GetToken (which have been supplied for you) work together to allow you to read one token at a time from the given file, where “token” is given the same meaning as in homework #2. Note that GetToken() also returns the new line characters as tokens, in addition to words and punctuation. Be sure to understand how GetToken() works, as it will be the basis of all that follows.
  3. Write a function called MakeDigrams that accepts a single argument, which must be a string specifying a text file that already exists on your computer (as above). This function is to construct and return a dictionary consisting of “digrams,” which encode all consecutive pairs

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.

  1. Write a function called GeneratePseudoText that accepts three arguments: a string containing an output file name, a dictionary containing digrams, and an integer specifying how many tokens to generate. The function is to use the digrams that are passed in to generate a sequence of tokens that is in some way similar to the text that was used to generate the digrams. Start by choosing a “random” key from the digram dictionary. For this, use the function PickRandomElement to select an element from the list of keys. Pick the next token by looking up the starting token in the dictionary of digrams, then randomly selecting one of the associated tokens (again, using the function PickRandomeElement). Then pick the next token using the same process, etc., until exactly the right number of tokens have been produced (i.e. as specified by the third argument passed to this function). Write all the generated tokens to the specified output file, formatted as described below. Note that this process will also insert punctuation and end-of-line characters (which start new lines), as these are tokens too. Running this function repeatedly on the same file will generate different results because of the “random” selections made. To reset the random numbers, use the SetSeed( seed ) function, which determines the sequence of random numbers that are generated by subsequent calls to NextRandomNumber(). Be sure to set the seed appropriately if you wish to compare your results to the results that are posted for you. (See the top line of each output file for the seed number used in each case.)
  2. Once you have the basic program working (i.e. generating the digrams and using them to create new text), you should work on formatting of the output file. You should string most tokens together on a single line, starting in column one, with a single space separating them, until a new line token is written to the file. Then, start again in column one. The exceptions to this rule are the punctuation symbols in the string ",.;:!?", which should have no space between them and the token before them. See the sample output files for examples. You need not treat quotation marks in any special way. When your output is looking nice, and you have checked that your program generates the correct output given the posted input files (and the appropriate seed numbers), run your (^1) Note that the word is digram, not diagram. There is only one “a” in digram.