📜  Python| NLTK nltk.tokenize.ConditionalFreqDist()

📅  最后修改于: 2022-05-13 01:54:49.835000             🧑  作者: Mango

Python| NLTK nltk.tokenize.ConditionalFreqDist()

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

示例 #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)

输出 :

示例 #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)

输出 :