Thursday, July 4, 2024
HomeLanguagesPythonPython | NLTK nltk.tokenize.ConditionalFreqDist()

Python | NLTK nltk.tokenize.ConditionalFreqDist()

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.




# import ConditionalFreqDist() method from nltk
from nltk.probability import ConditionalFreqDist
from nltk.tokenize import word_tokenize
     
# Create a reference variable for Class SExprTokenizer
tk = ConditionalFreqDist()
     
# Create a string input
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 :




# import ConditionalFreqDist() method from nltk
from nltk.probability import ConditionalFreqDist
from nltk.tokenize import word_tokenize
     
# Create a reference variable for Class SExprTokenizer
tk = ConditionalFreqDist()
     
# Create a string input
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})

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments