×

Word frequency Python

Word frequency Python

In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches which we are going to use to write such a program. In the output of the program, the user will get the word frequency for each word used in the sentence.

Example::

"An apple a day keeps the doctor away but doctor should also eat an apple every day."

In the above-given sentence:

  • An occurred two times
  • Apple occurred two times
  • A occurred only one time
  • Doctor occurred two times
  • Day also occurred two times, etc.

So, we can see that that's how the occurrence of the words or word frequency from a sentence is counted.

We will write a program where we will get the frequency of each word present in the sentence in the way mentioned above as an output of it. We will use the following approaches in our program to get the word frequency in the output:

1). Using the Python list approach

2). Using Python set approach

3). Using the Python dictionary approach

Let's learn about all these approaches in detail and write a Python program with each of them.

Method 1: Using Python list:

In this method, we will follow the following steps for developing a Python program to get the word frequency in the output:

Step 1: We will get the sentence or set of words as string type from the user in the input form.

Step 2: We will split the words of the given string into a list by using string.split() function on the string with delimiter space.

Step 3: Now, we will initialize an empty list in the program.

Step 4: After that, if any word is not present in the new empty list that we have initialized then, we will append the word from the given string.

Step 5: We will use iteration on the new list and then use the count () function.

Step 6: We will print the frequency of each word in the output by the print statement.

Now, let's understand the implementation of these steps or approach by using them in the following program:

Example – 

 # Define a default function for word frequency
 def worfreq (String):
 # Break the string into a list of words using string.split () function
 String = String.split ()
 NewList = [] # initializing an empty list
 # Using for loops till all the given values in string
 for j in String:
 # Checking the duplicity using empty list
 if j not in NewList:
 # Appending values in the empty list
 NewList.append (j)
 # Iterating over the new list
 for j in range (0, len (NewList)):
 # Using count () function and printing result in the output
 print ('Frequency of', NewList [j], 'word is :', String.count (NewList [j]))
 def main ():
         # Taking sentence as user input
 mkr = input ("Enter your input sentence: ")
 worfreq (mkr) # calling out default function
 if __name__=="__main__":
 main () # calling the main function 

Output:

 Enter your input sentence: An apple is mango but mango is not an apple
 Frequency of An word is : 2
 Frequency of apple word is : 2
 Frequency of is word is : 2
 Frequency of mango word is : 2
 Frequency of but word is : 1
 Frequency of not word is : 1 

As we can see in the output, we have got word frequency for every word that we have given in the sentence as the user input in the program.

Method 2: Using Python set

In the Python set approach, we will use to store the collection of unique words from the sentence provided in the program. We will use the following steps in this method to write the program:

Step 1: As the first method, here we will also take the sentence in string form as user input and split the words from the given sentence using string.split() function with delimiter space.

Step 2: Now, we will use the set() function to remove all the duplicate or multiple occurred words from the sentence and store the unique words inside the sentence.

Step 3: After that, we will iterate over the defined set of unique words and use the count () function to get the word frequency.

Step 4: We will use the print statement to print the word frequency as a result in output.

Let's understand this method by implementing the above steps in the following example program:

Example – 

 # Define a default function for word frequency
 def worfreq (String):
     # Break the given string into a list of words using string.split () function
     NewList = String.split ()
     # Defining a set for unique words of the sentence
     UqWords = set (NewList)
     # Iterating over the set of unique words 
     for words in UqWords :
         # Using count () function for word frequency
         print ('Frequency of ', words , 'word is:', NewList.count (words)) # printing result in output
 def main ():
         # Taking sentence as user input
 mkr = input ("Enter your input sentence: ")
 worfreq (mkr) # calling out default function
 if __name__=="__main__":
 main () # calling the main function 

Output:

 Enter your input sentence: An apple is mango but mango is not An apple
 Frequency of but word is: 1
 Frequency of apple word is: 2
 Frequency of not word is: 1
 Frequency of is word is: 2
 Frequency of An word is: 2
 Frequency of mango word is: 2 

Method 3: Using Python dictionary

In this method, we will use the Python dictionary and its functions to get the word frequency from a given sentence. Now, let's understand the implementation of this approach by using it in the following program:

Example – 

 # Define a default function for word frequency
 def count (elemen):
 # Checking if words in the sentence have '.' at last and if so then we will ignore it
 if elemen [-1] == '.':
 elemen = elemen [0:len (elemen) - 1]
 # Using dictionary keys for checking duplicity in given sentence
 if elemen in dictation:
 dictation [elemen] += 1
 # Defining elements inside the empty dictionary
 else:
 dictation.update ({elemen: 1})
 # Taking sentence as user input
 mkr = input ("Enter your input sentence: ")
 # Define an empty dictionary
 dictation = {}
 # Splitting all words of the given string using string.split ()
 NewList = mkr.split ()
 # Counting each word from string using count function
 for elemen in NewList:
 count (elemen)
 # Getting the word frequency as key values from new dictionary 
 for AllKey in dictation: # iterating over keys from dictionary
 print ("Frequency of ", AllKey, "word is", end = " ")
 print (":", end = " ")
 print (dictation [AllKey], end = " ") # printing result in output
 print () 

Output:

 Enter your input sentence: An apple is mango but mango is not An apple
 Frequency of An word is : 2 
 Frequency of apple word is : 2 
 Frequency of is word is : 2 
 Frequency of mango word is : 2 
 Frequency of but word is : 1 
 Frequency of not word is : 1 

Explanation – 

As we can see in the output, we have got word frequency for each word that is present in the sentence that we gave as the user input in the program. So, that’s how we can use the dictionary approach in a Python program to get the word frequency.


Related Topics

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Get index of element in array in python

Index : Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also...

4 minutes read.

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

Find key from value in dictionary python

Python: Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a...

5 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Why learn Python?

All things considered, learning python would be extraordinary, it'll acquaint you with the universe of dynamic programming dialects, if you're somebody who has had a semester of involvement with C. Python...

4 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.