×

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

Rank Based Percentile GUI Calculator using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities...

3 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Python Struct

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.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Python Scikit-image | Image Processing Using Scikit-Image

What is Image Processing? The world is defined with images and, every image has its different specialties. An image can contain much-needed information that can be helpful in various ways. The process...

4 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Python open() function

Python open() function The open() function in Python opens a file and returns a corresponding file object. If the file cannot be opened, an OSError is raised. Syntax open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameter file It represents the path and name of the file mode...

2 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute 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.

How to Open a file in python with Path

In this tutorial, we will see rather than the traditional way of opening a file by locating or navigating it from our desktops; we will learn how to open the...

2 minutes read.

Joint Plot in Python

Introduction to Joint plots: The joint plot is the finest approach to evaluate both the specific distribution of each variable and also the connection between the two variables. Three different plots make...

4 minutes read.

Google Python Class

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.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python String isalpha() method

Python String isalpha() method The string.isalpha () method in Python returns a boolean value true if all characters in the  string are alphabetic else for any other value it returns false. Syntax isalpha()  Parameter NA Return This...

1 minute read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

How to Import Files in 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.

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.