# 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.