Count the word in a document
Topics: Functions, Files Read and write, Dictionary
Suppose you are given an input file of a document that only contains English words, spaces, commas
(always followed with one space) and periods (always followed with one space). Your task is to read
the file, count the word frequency by ignoring the letter case, output the frequently used words (i.e.,
the words occurred more than once) and the most frequently used word among all of them.
Lab Scenario: Count the word in a document
1. The program reads from an already provided input file: “document.txt”, which contains
several paragraphs separated by an empty line.
2. You will perform the file open operation. And then perform the read operation with your choice of
read functions and read the content of the file.
3. Once you are done reading, start processing the contents of the file using a dictionary where the
key would be the word in lowercase and the corresponding values would be word frequency which
will be the number of occurrences in the documents. For example: “python”: 5
4. Once you are done building your new dictionary you are going to print the new dictionary to the
standard output. The output should only contain English words occurred more than once, and should
not contain any comma, period, or space. The output content should be as follows:
5. The first column of the output file is the word in lowercase and the second column is the frequency
of the word. The very last line of the output should be the most frequently used word among all (For
the given input file, there will be only one such word). No fancy formatting is required.
6. Please don't forget to close the file unless you use ‘with’ keyword.
7. For this lab, you are expected to use dictionary, functions and file I/O operations. There should be
at least 4 user_defined functions:
main(): A sample provided in the template file
file_read() to read the content of the input file
create_dict(): creates and returns a dictionary
display() to print the output
NOTE THE OUTPUT IS DISPLAYED BELOW CODE MUST FOLLOW OUTPUT
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images