<div>Dear Python Programmers,</div>
<div> </div>
<div>I am a Python newby and I need help with my code: I have done parts of it but I can't get what I need: I need to manipulate text to come up with word lengths and their frequency:ie </div>
<div> </div>
<div>how many 1-letter words in a text</div>
<div>how many 2-letter words in a text, etc</div>
<div> </div>
<div>I believe I am on the right path, but I can't get it right, I hope someone can shed some light: Code below.</div>
<div> </div>
<div>import string<br>import sys<br>def word_length(word):<br> for p in string.punctuation:<br> word = word.replace(p, "") # replace any punctuation symbol with empty string<br> return len(word)<br>
<br>def fileProcess(filename = open('input_text.txt', 'r')): <br># Need to show word count(ascending order) for each of the word lengths that has been encountered.<br> <br># <br> print ("Length \t" + "Count")#print header for all numbers<br>
freq = {} #empty dict to accumulate word count and word length <br> for line in filename:<br> for word in line.lower().split( ):#split lines into words and make lower case<br> wordlen = word_length(word)#run function to return length of each word<br>
freq[wordlen] = freq.get(wordlen, 0) + 1#increment the stored value if there is one, or initialize <br> print(word, wordlen, freq[wordlen]) </div>
<p>fileProcess()</p>