Entropy for text in python
<p>I need to find the entropy of a text.</p>

<p>I want to use the Shannon Entropy.</p>

<p>So my input is a dictionary consisting a text document of a book (all the words) and its frequencies. The output should be the entropy for the observed probabilities of all the words. Right now my code is like this: </p>

<pre><code>import math 

def shannon(string):
        prob = [ float(string.count(c)) / len(string) for c in dict.fromkeys(list(string)) ]
        entropy = sum([ p * math.log(p) / math.log(2.0) for p in prob ])
        return entropy
</code></pre>

<p>Where boe is the dictionary consisting of the words and frequencies and i first transfrom it into a string so that the code works. However, im not sure if this is the correct entropy for this case.</p>