借助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})
相關用法
- Python nltk.tokenize.SpaceTokenizer()用法及代碼示例
- Python nltk.tokenize.TabTokenizer()用法及代碼示例
- Python nltk.tokenize.LineTokenizer用法及代碼示例
- Python nltk.tokenize.StanfordTokenizer()用法及代碼示例
- Python nltk.tokenizer.word_tokenize()用法及代碼示例
- Python nltk.tokenize.SExprTokenizer()用法及代碼示例
- Python nltk.tokenize.mwe()用法及代碼示例
- Python nltk.TweetTokenizer()用法及代碼示例
- Python nltk.WhitespaceTokenizer用法及代碼示例
- Python NLTK tokenize.WordPunctTokenizer()用法及代碼示例
- Python NLTK tokenize.regexp()用法及代碼示例
注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 Python | NLTK nltk.tokenize.ConditionalFreqDist()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。