
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find frequency of each word in a string in Python
As a part of text analytics, we frequently need to count words and assign weightage to them for processing in various algorithms, so in this article we will see how we can find the frequency of each word in a given sentence. We can do it with three approaches as shown below.
Using Counter
We can use the Counter() from collections module to get the frequency of the words. Here we first apply the split() to generate the words from the line and then apply the most_common ().
Example
from collections import Counter line_text = "Learn and practice and learn to practice" freq = Counter(line_text.split()).most_common() print(freq)
Running the above code gives us the following result −
[('and', 2), ('practice', 2), ('Learn', 1), ('learn', 1), ('to', 1)]
Using FreqDist()
The natural language tool kit provides the FreqDist function which shows the number of words in the string as well as the number of distinct words. Applying the most_common() gives us the frequency of each word.
Example
from nltk import FreqDist text = "Learn and practice and learn to practice" words = text.split() fdist1 = FreqDist(words) print(fdist1) print(fdist1.most_common())
Running the above code gives us the following result −
<FreqDist with 5 samples and 7 outcomes> [('and', 2), ('practice', 2), ('Learn', 1), ('learn', 1), ('to', 1)]
Using Dictionary
In this approach we store the words of the line in a dictionary. Then we apply the count() to get the frequency of each word. Then zip the words with the word frequency values. The final result is shown as a dictionary.
Example
text = "Learn and practice and learn to practice" words = [] words = text.split() wfreq=[words.count(w) for w in words] print(dict(zip(words,wfreq)))
Running the above code gives us the following result:
{'Learn': 1, 'and': 2, 'practice': 2, 'learn': 1, 'to': 1}
- Related Articles
- Find frequency of each word in a string in Java
- Find frequency of each word in a string in C#
- Python – Word Frequency in a String
- Frequency of each character in String in Python
- C program to find frequency of each digit in a string
- Program to reverse the position of each word of a given string in Python
- Python - Find the length of the last word in a string
- Find the first repeated word in a string in Python?
- Print first letter of each word in a string in C#
- Python Program to Get word frequency in percentage
- How to find the frequency of a particular word in a cell of an excel table using Python?
- Python – Find the frequency of numbers greater than each element in a list
- Find the first repeated word in a string in Python using Dictionary
- Getting first letter of each word in a String using regex in Java
- Print first letter of each word in a string using C# regex
