Question: Write a program that reads a content of a text 2 . txt ( file under module ) . The program should create a dictionary
Write a program that reads a content of a texttxt file under module The program should create a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word 'the' appears times, the dictionary would contain an element with 'the' as the key and as the value.
Define Main
Create an empty dictionary
Prompt user to get the file
Open file in R mode
Use read method to read data from the file and split method to split the words
Add each unique word to dictionary with a counter of using a for loop
For each word in the text increase its counter in the dictionary
Display results
Expected output:
Enter the name of the input file: texttxt
word occurrences
were
this
In
at
biggest
assets
wheels
horsepower
famous
the texttxt file contains the following
No one is unaware of the name of that famous English shipowner, Cunard.
In this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with horsepower paddle wheels and a burden of metric tons.
Eight years later, the company's assets were increased by four horsepower ships at metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In the Cunard Co whose mailcarrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in this company owned twelve ships, eight with paddle wheels and four with propellers."
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
