Week 1, Extra Problem 2: Scrabble-scoring your files...
[15 points; individual or pair]

 

This problem offers the chance to interact with files in Python. In particular, it asks you to develop functions that will compute the scrabble score of the text inside arbitrary files.

The function to write... scoreFile( fileName )

For this problem you should write a function named scoreFile( fileName ) that takes in a string, which is the name of the file to be scored. Then, your function should open that file, read its contents, and compute the following:

In particular, scoreFile should return a list of these three quantities: the total score first, the number of letters second, and the average score per letter third.

For example, here are our answers using the two files named cow.txt and gburg.txt (downloadable below):

>>> scoreFile( 'gburg.txt' )

[225, 143, 1.5734265734265733]

 

>>> scoreFile( 'cow.txt' )

[83, 45, 1.8444444444444446]

 

File handling in Python

File-handling is not too bad in Python. To get started, you might want to download these two files to your desktop by right-clicking each link (control-clicking on a Mac):

cow.txt, an Ogden Nash poem

gburg.txt, the preamble to the Gettysburg Address

Here is a function that illustrates how to open and extract the contents of a file:

def printFileToScreen( fileName ):

""" simply prints the contents of the file to the screen

input: fileName, a string with the file's name

"""

f = open( fileName ) # f is the opened file

text = f.read() # text is the name of all of f's text

f.close() # this closes the file f - a good idea

 

print 'The file contains:'  # drumroll

print # blank line

print text # ta da!

 

You would be able to run this, as long as you're in the folder in which the files are located, by invoking

 

>>> printFileToScreen( 'cow.txt' )

With any changes to the file name you'd like! Try this out, and then modify the function above to begin implementing your scoreFile function.

Although this assignment does not require creating a file, here is an example showing that it is not difficult to do:

#

# an example that creates (or overwrites) a file

#

def writeToFile( fileName ):

""" a function demonstrating how to create a new file and

write to it. If the file (named fileName) already

exists, it will be overwritten

"""

f = open( fileName, 'w' ) # f is the file, opened for writing

print >> f, "Hello there from Python!"

x = 42

print >> f, x, "is the number of tiles in a game of scrabble."

 

f.close() # this closes the file f - a good idea

 

Running writeToFile('myfile.txt') produces no visible output, but when you open the file myfile.txt, you'll see what was printed:

Hello there from Python!

42 is the number of tiles in a game of scrabble.

 

Handling large recursive calls

If you run scoreFile on files with more than 1,000 characters, it may use more memory than Python allocates to the recursive stack (and crash). To get around this, you can add the following lines at the top of your hw1pr5.py file:

import sys

sys.setrecursionlimit(100000)

These lines allow Python to build a stack of up to 100,000 function calls -- or until the memory your operating system has given Python runs out. Usually the latter happens first, leading to IDLE hanging or crashing or producing the rather uninformative segmentation fault error. However, you should at least be able to get well past 1,000 characters.

 

Testing this problem

In addition to writing the above scoreFile function -- which should be in a file named hw1ec2.py, you should also run it on something other than the two examples above: at least one additional file you wrote (perhaps a hum paper) and another file of your choosing. Don't use Microsoft Word-formatted files - they and other word-processed files have lots of formatting characters in them. Instead, use plain-text files. Microsoft Word will reluctantly allow you to save existing files to plain-text, though it will warn you with several dialog boxes of the extreme risks you take by not using a Microsoft file format!

In a comment at the top of your hw1ec2.py file, report the results from the two files you chose.

Happy scrabbling!

 

Submitting your file

You should submit your hw1ec2.py file at Canvas.