Python | NLTK nltk.tokenize.ConditionalFreqDist()
Last Updated :
22 Nov, 2019
With the help of nltk.tokenize.ConditionalFreqDist()
method, we are able to count the frequency of words in a sentence by using tokenize.ConditionalFreqDist()
method.
Syntax : tokenize.ConditionalFreqDist()
Return : Return the frequency distribution of words in a dictionary.
Example #1 :
In this example we can see that by using tokenize.ConditionalFreqDist()
method, we are able to count the occurrence of words in a sentence.
from nltk.probability import ConditionalFreqDist
from nltk.tokenize import word_tokenize
tk = ConditionalFreqDist()
gfg = "Geeks for Geeks"
for word in word_tokenize(gfg):
condition = len (word)
tk[condition][word] + = 1
print (tk)
|
Output :
FreqDist({‘Geeks’: 2, ‘for’: 1})
Example #2 :
from nltk.probability import ConditionalFreqDist
from nltk.tokenize import word_tokenize
tk = ConditionalFreqDist()
gfg = "G F G"
for word in word_tokenize(gfg):
condition = len (word)
tk[condition][word] + = 1
print (tk)
|
Output :
FreqDist({‘G’: 2, ‘F’: 1})
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...