<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" id="owaParaStyle" style="display: none;">P {margin-top:0;margin-bottom:0;}</style>
</head>
<body dir="ltr" fpstyle="1" aria-label="Message body" tabindex="0">
<div name="divtagdefaultwrapper" id="divtagdefaultwrapper" style="font-family: Calibri,Arial,Helvetica,sans-serif; font-size: 12pt; color: #000000; margin: 0">
<div>I was given this code and I need to modify it so that it will:</div>
<div><br>
</div>
<div><span style="font-size: 12pt;">#1. Error handling for the files to ensure reading only .txt file</span></div>
<div>#2. Print a range of top words... ex: print top 10-20 words</div>
<div>#3. Print only the words with > 3 characters</div>
<div>#4. Modify the printing function to print top 1 or 2 or 3 ....</div>
<div>#5. How many unique words are there in the book of length 1, 2, 3 etc</div>
<div><br>
</div>
<div>I am fairly new to python and am completely lost, i looked in my book as to how to do number one but i cannot figure out what to modify and/or delete to add the print selection. This is the code:</div>
<div><br>
</div>
<div><br>
</div>
<div>import string</div>
<div><br>
</div>
<div>def process_file(filename):</div>
<div> hist = dict()</div>
<div> fp = open(filename)</div>
<div> for line in fp:</div>
<div> process_line(line, hist)</div>
<div> return hist</div>
<div><br>
</div>
<div>def process_line(line, hist):</div>
<div> line = line.replace('-', ' ')</div>
<div> </div>
<div> for word in line.split():</div>
<div> word = word.strip(string.punctuation + string.whitespace)</div>
<div> word = word.lower()</div>
<div> </div>
<div> hist[word] = hist.get(word, 0) + 1</div>
<div><br>
</div>
<div>def common_words(hist):</div>
<div> t = []</div>
<div> for key, value in hist.items():</div>
<div> t.append((value, key))</div>
<div> </div>
<div> t.sort(reverse=True)</div>
<div> return t</div>
<div><br>
</div>
<div>def most_common_words(hist, num=100):</div>
<div> t = common_words(hist)</div>
<div> print 'The most common words are:'</div>
<div> for freq, word in t[:num]:</div>
<div> print freq, '\t', word</div>
<div> </div>
<div>hist = process_file('emma.txt')</div>
<div>print 'Total num of Words:', sum(hist.values())</div>
<div>print 'Total num of Unique Words:', len(hist)</div>
<div>most_common_words(hist, 50)</div>
<div><br>
<div style="font-family:Tahoma; font-size:13px">Any help would be greatly appreciated because i am struggling in this class. Thank you in advance</div>
<div style="font-family:Tahoma; font-size:13px"><br>
</div>
<div style="font-family:Tahoma; font-size:13px">Respectfully,</div>
<div style="font-family:Tahoma; font-size:13px"><br>
</div>
<div style="font-family:Tahoma; font-size:13px">Ruben Pinedo
<div>Computer Information Systems</div>
<div>College of Business Administration </div>
<div>University of Texas at El Paso</div>
</div>
</div>
</div>
</body>
</html>