Python| NLTK nltk.tokenize.ConditionalFreqDist()
在nltk.tokenize.ConditionalFreqDist()
方法的帮助下,我们可以使用tokenize.ConditionalFreqDist()
方法计算句子中单词的频率。
Syntax : tokenize.ConditionalFreqDist()
Return : Return the frequency distribution of words in a dictionary.
示例 #1:
在这个例子中,我们可以看到,通过使用tokenize.ConditionalFreqDist()
方法,我们能够统计一个句子中单词的出现次数。
# 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)
输出 :
FreqDist({‘Geeks’: 2, ‘for’: 1})
示例 #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)
输出 :
FreqDist({‘G’: 2, ‘F’: 1})