Explanation
In this program, we need to find the most repeated word present in given text file. This can be done by opening a file in read mode using file pointer. Read the file line by line. Split a line at a time and store in an array. Iterate through the array and find the frequency of each word and compare the frequency with maxcount. If frequency is greater than maxcount then store the frequency in maxcount and corresponding word that in variable word. The content of data.txt file used in the program is shown below.
data.txt
A computer program is a collection of instructions that performs specific task when executed by a computer.
Computer requires programs to function.
Computer program is usually written by a computer programmer in programming language.
A collection of computer programs, libraries, and related data are referred to as software.
Computer programs may be categorized along functional lines, such as application software and system software.
Algorithm
- Variable maxCount will store the count of most repeated word.
- Open a file in read mode using file pointer.
- Read a line from file. Convert each line into lowercase and remove the punctuation marks.
- Split the line into words and store it in an array.
- Use two loops to iterate through the array. Outer loop will select a word which needs to be count. Inner loop will match the selected word with rest of the array. If match found, increment count by 1.
- If count is greater than maxCount then, store value of count in maxCount and corresponding word in variable word.
- At the end, maxCount will hold the maximum count and variable word will hold most repeated word.
Input:
file = open("data.txt", "r")
data.txt file content:
The term "computer" is derived from Latin word "computare" which means to calculate. Computer is a programmable electronic device. Computer accepts raw data as input and processes it with set of instructions to produce result as output. The history of computer begins with the birth of abacus which is believed to be the first computer.
Output:
Most repeated word: computer
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: