布朗語料庫中使用條件概率分佈函式ConditionalFreqDist,可以檢視每個單詞在各新聞語料中出現的次數。這在微博情感分析中非常有用,比如判斷feature vector中代表positive or negative or neutral的各feature在每條tweet中出現的次數高低來判斷該tweet的情感極性。
from nltk.corpus import brown
cfd=nltk.ConditionalFreqDist(
(genre,word)
for genre in brown.categories()
for word in brown.words(categories=genre)
)
genres=['news','religion','hobbies','science_fiction','romance','humor']
modals=['can','could','may','might','must','will']
print cfd.tabulate(conditions=genres,samples=modals)
輸出結果:
can could may might must will
news 93 86 66 38 50 389
religion 82 59 78 12 54 71
hobbies 268 58 131 22 83 264
science_fiction 16 49 4 12 8 16
romance 74 193 11 51 45 43
humor 16 30 8 8 9 13
可以看出news分類中will一詞出現最多,humor分類中could出現次數最多。