当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python NLTK nltk.tokenize.ConditionalFreqDist()用法及代码示例


借助nltk.tokenize.ConditionalFreqDist()方法,我们可以使用以下方法计算句子中单词的出现频率tokenize.ConditionalFreqDist()方法。

用法: tokenize.ConditionalFreqDist()
返回: 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})



相关用法


注:本文由纯净天空筛选整理自Jitender_1998大神的英文原创作品 Python | NLTK nltk.tokenize.ConditionalFreqDist()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。