Find most repeated word in a string python

1from collections import Counter
2
3# Example phrase 
4phrase = "John is the son of John second. Second son of John second is William second."
5split_phrase = phrase.split()
6
7# create counter object
8Counter = Counter(split_phrase)
9
10most_occured_words = Counter.most_common(4)
11  
12print(most_occured_words)

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.

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

  1. Variable maxCount will store the count of most repeated word.
  2. Open a file in read mode using file pointer.
  3. Read a line from file. Convert each line into lowercase and remove the punctuation marks.
  4. Split the line into words and store it in an array.
  5. 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.
  6. If count is greater than maxCount then, store value of count in maxCount and corresponding word in variable word.
  7. At the end, maxCount will hold the maximum count and variable word will hold most repeated word.

Solution

Python

Output:

 Most repeated word: computer

C

Output:

Most repeated word: computer

JAVA

Output:

Most repeated word: computer

C#

Output:

Most repeated word: computer

PHP

Output:

Most repeated word: computer

Next Topic#

Skip to content

How to find most repeated word in a string in Python?

from collections import Counter

# Example phrase 
phrase = "John is the son of John second. Second son of John second is William second."
split_phrase = phrase.split()

# create counter object
Counter = Counter(split_phrase)

most_occured_words = Counter.most_common(4)
  
print(most_occured_words)

Related

algorithm for finding most repeated words in a list of strings, algorithm for finding most repeated words in a list or strings, find most duplicate string in list python, find most repeated character in string python, find out most repeated character in a string python, find the most repeated character in a string in python, find the most repeated string in an array python, find the repeated words in string python, finding the most repeated character in given string in python, how to find most repeated word in a string in python, how to find repeated letters in a string python, how to find the most repeated word in a stringin python, most repeated character in a string python, most repeated word in a string python, pick longest non repeated characters from string in python, print the most repeated letter in python, python find most consecutive same letters in a string, python most repeated characters in string, python number of repetition of a most repeated character in string, python: Python program to find the second most repeated word in a given string, python:find the second most repeated word in a given string, python:find the second most repeated word in a givenstring, repeated string solution in python, word with repeated letteras python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Find most repeated word in a string python

Change font size

Post navigation

How do I find the most repeated words in a string in Python?

Use the Counter() function (which gives the frequency of words as a key-value pairs), to calculate the frequency (number of times the word has occurred) of all the words. Create a variable to store the maximum frequency. Loop in the above words frequency dictionary using the for loop.

How do you find the most repeated word in a string?

Algorithm.
STEP 1: START..
STEP 2: DEFINE String line, word = "".
STEP 3: SET count =0, maxCount =0..
STEP 4: DEFINE ArrayList<String> words..
STEP 5: USE File Reader to open file in read mode..
STEP 6: READ line from file..
STEP 7: By looping, CONVERT each line into lower case..
STEP 8: REMOVE the punctuation marks..

How many times is a word repeated in a string Python?

Python String count() Method The count() method returns the number of times a specified value appears in the string.

How do you find the number of occurrences of a word in a string in Python?

The count() method returns the number of occurrences of a substring in the given string..
substring - string whose count is to be found..
start (Optional) - starting index within the string where search starts..
end (Optional) - ending index within the string where search ends..