In this code I already coded counts as a dict beforehand. Here when I print my get_word_prob function, I get 0.0 as a result. When I print pneg, pword or others, I get name is not defined error. I guess my definition of these variables is not correct but could not find any other way to do so. It is probably easy but I am a total beginner.
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
"""
probability=float()
# Your code goes here
#P(word|polarity)=(p(polarity|word)*p(word))%p(polarity)
#P(word)=(counts[pos][word]+counts[neg][word])%counts[pos]+counts[neg]
for word in counts['pos']:
pword=float((counts['pos'][word]+counts['neg'][word])/sum(counts['pos'].values())+sum(counts['neg'].values()))
#p(pos)=counts[pos]%(counts[pos]+counts[neg])
ppos=float(sum(counts['pos'].values())/(sum(counts['pos'].values())+sum(counts['neg'].values())))
pneg=float(sum(counts['neg'].values())/(sum(counts['pos'].values())+sum(counts['neg'].values())))
#p(pos|word)=counts[pos][word]%(counts[pos][word]+counts[neg][word])
pposword=float(counts['pos'][word]/(counts['pos'][word]+counts['neg'][word]))
#p(neg|word)=counts[neg][word]%(counts[pos][word]+counts[neg][word])
pnegword=float(counts['neg'][word]/(counts['pos'][word]+counts['neg'][word]))
probability == float((pposword*pword)/ppos)
for word in counts['neg']:
probability == float((pnegword*pword)/pneg)
return probability # P(word|polarity)
#Do not change
#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
print(pneg)