Question: Challenge question: Histogram of word lengths Write a function plot _ word _ length _ histogran ( filenane ) that reads a file and plots

Challenge question: Histogram of word lengths
Write a function plot_word_length_histogran(filenane) that reads a file and plots a histogram of the distribution of word lengths in that file. For example, a file named test toxt containing the following:
Here are some words of varying lengths, one of which is ostentatiously lengthy.
should produce the following histogram:
Once you have stored the whole contents of the file (as a string) in the variable contents, you should extract the words using:
words - re.findall([2-zA-z]+?', contents)
Requirements:
If your function receives an invalid filename, it should catch the exception and print the message "The file {filename} was not found." where (filename} is the name of the file given to the function.
The title should be "Word lengths in {filename]", where [filename} is the name of the file given to the function.
The x-label and y-label should be "Word length" and "Number of words" respectively.
You should set the x-ticks and their labels explicitly from 0 to 20(inclusive) in steps of 2.
The bins should each be 1 wide, starting at 1 and finishing at 20(inclusive). You must set these bins explicitly.
It is not a density plot - the vertical axis displays a count of how many words are in each bin.
Your plot must have grid lines.
Feel free to write your own test files. If you would like a large text file to test your program with, here is Mary Shelley's Frankenstein.
 Challenge question: Histogram of word lengths Write a function plot_word_length_histogran(filenane) that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!