0

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)
Bron Bron
  • 13
  • 3
  • hello Bron Bron, can you give more information? What would be useful is a [MCVE]. Show an example of an input and any related errors. – Nathan McCoy Feb 05 '20 at 17:52
  • 1
    `pneg` is defined inside the `get_word_prob()` function. But you're trying to print it outside of that function, where it does not exist. – John Gordon Feb 05 '20 at 17:52
  • Your main program consists of a single `print` of a variable that exists nowhere else in the main program. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) -- such an example would have given you an immediate answer. – Prune Feb 05 '20 at 17:54
  • thanks. I understood my problem. now when I print ``` 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 ``` I only get 0.0 for each of them. – Bron Bron Feb 05 '20 at 18:13

0 Answers0