我有2个词典:counts ['pos'](正向推文中的单词及其编号)和counts ['neg'](与负向推文相同)。这个问题要求我定义一个给我P(word | polarity)的函数。我的代码实际上有效,但没有给我想要的答案。
possum=sum(counts['pos'].values())
negsum=sum(counts['neg'].values())
def get_word_prob(counts, word, polarity):
"""
calculates the probability of a word given a polarity
Parameters:
counts (dict): the dictionaries 'pos' and 'neg' which count word occurances
word (str): the word you want to get the probability for
polarity (str): wither 'pos' or 'neg'
Returns:
probability (float): the probability of a word given a polarity
"""
# Your code goes here
if word not in counts[polarity]:
return 0
if word in counts['pos']:
probability = counts['pos'][word] / possum
if word in counts['neg']:
probability = counts['neg'][word] / negsum
#Divide the count of the given word by the total sum
return probability # P(word|polarity)
print(get_word_prob(counts, "great", "pos")) # should print 0.00255902660421998
print(get_word_prob(counts, "glad", "neg")) # should print 0.00012164155275442091
print(get_word_prob(counts, "wugs", "neg")) # should print 0
请记住,我只能更改此部分:
if word not in counts[polarity]:
return 0
if word in counts['pos']:
probability = counts['pos'][word] / possum
if word in counts['neg']:
probability = counts['neg'][word] / negsum