The Wayback Machine - https://web.archive.org/web/20240526225621/https://www.geeksforgeeks.org/find-the-most-repeated-word-in-a-text-file/
Open In App

Find the most repeated word in a text file

Last Updated : 25 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files, and binary files (written in binary language,0s and 1s).

  • Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default.
  • Binary files: In this type of file, there is no terminator for a line, and the data is stored after converting it into machine-understandable binary language.

Here we are operating on the .txt file in Python. Through this program, we will find the most repeated word in a file.

Approach:

  • We will take the content of the file as input.
  • We will save each word in a list after removing spaces and punctuation from the input string.
  • Find the frequency of each word.
  • Print the word which has a maximum frequency.

Input File:

Below is the implementation of the above approach:

Python3




# Python program to find the most repeated word
# in a text file
 
# A file named "gfg", will be opened with the 
# reading mode.
file = open("gfg.txt","r")
frequent_word = ""
frequency = 0 
words = []
 
# Traversing file line by line
for line in file:
     
    # splits each line into
    # words and removing spaces
    # and punctuations from the input
    line_word = line.lower().replace(',','').replace('.','').split(" "); 
     
    # Adding them to list words
    for w in line_word: 
        words.append(w); 
         
# Finding the max occurred word
for i in range(0, len(words)): 
     
    # Declaring count
    count = 1; 
     
    # Count each word in the file 
    for j in range(i+1, len(words)): 
        if(words[i] == words[j]): 
            count = count + 1; 
 
    # If the count value is more
    # than highest frequency then
    if(count > frequency): 
        frequency = count; 
        frequent_word = words[i]; 
 
print("Most repeated word: " + frequent_word)
print("Frequency: " + str(frequency))
file.close();


Output:

Most repeated word: well
Frequency: 3


Similar Reads

Second most repeated word in a sequence in Python
Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence. (Considering no two words are the second most repeated, there will be always a single word). Examples: Input : {"aaa", "bbb", "ccc", "bbb", "aaa", "aaa"} Output : bbb Input : {"geeks", "for", "geeks", "for", "geeks", "aaa"} Outp
4 min read
Python program to read file word by word
Python is a great language for file handling, and it provides built-in functions to make reading files easy with which we can read file word by word. Read file word by wordIn this article, we will look at how to read a text file and split it into single words using Python. Here are a few examples of reading a file word by word in Python for a bette
2 min read
Find the first repeated word in a string in Python using Dictionary
Prerequisite : Dictionary data structure Given a string, Find the 1st repeated word in a string. Examples: Input : "Ravi had been saying that he had been there" Output : had Input : "Ravi had been saying that" Output : No Repetition Input : "he had had he" Output : he We have existing solution for this problem please refer Find the first repeated w
4 min read
Python - Count occurrences of each word in given text file
Many times it is required to count the occurrence of each word in a text file. To achieve so, we make use of a dictionary object that stores the word as the key and its count as the corresponding value. We iterate through each word in the file and add it to the dictionary with a count of 1. If the word is already present in the dictionary we increm
4 min read
Pulling a random word or string from a line in a text file in Python
File handling in Python is really simple and easy to implement. In order to pull a random word or string from a text file, we will first open the file in read mode and then use the methods in Python's random module to pick a random word. There are various ways to perform this operation: This is the text file we will read from: Method 1: Using rando
2 min read
Convert Text and Text File to PDF using Python
PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in
3 min read
Eliminating repeated lines from a file using Python
Let us see how to delete several repeated lines from a file using Python's File Handling power. If the file is small with a few lines, then the task of deleting/eliminating repeated lines from it could be done manually, but when it comes to large files, this is where Python comes to your rescue. Eliminating repeated lines from a file in PythonBelow
6 min read
Find line number of a specific string or substring or word from a .txt file in Python
Finding the line number of a specific string and its substring is a common operation performed by text editors or any application with some level of text processing capabilities. In this article, you will learn how to find line number of a specific string or substring or word from a .txt (plain text) file using Python. The problem in hand could be
4 min read
Python program for most frequent word in Strings List
Given Strings List, write a Python program to get word with most number of occurrences. Example: Input : test_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"] Output : gfg Explanation : gfg occurs 3 times, most in strings in total. Input : test_list = ["geeks love gfg", "geeks are best"] Output : geeks Explanation : geeks occurs 2
6 min read
Find most similar sentence in the file to the input sentence | NLP
In this article, we will find the most similar sentence in the file to the input sentence. Example: File content: "This is movie." "This is romantic movie" "This is a girl." Input: "This is a boy" Similar sentence to input: "This is a girl", "This is movie". Approach: Create a list to store all the unique words of the file.Convert all the sentences
2 min read