12345678910111213141516171819202122232425262728293031323334353637383940 |
- from math import log
- charCount = {}
- numberOfBits = 0
- def count(line):
- global numberOfBits
- for c in line:
- # Size increases
- numberOfBits += 1
- # Count
- if c in charCount:
- charCount[c] += 1
- else:
- charCount[c] = 1
- with open("Data/hello.txt", 'rb') as f :
- print("Reading file")
- for l in f:
- count(l)
- print("The map :")
- print(charCount)
- print("Number of characters :")
- print(len(charCount))
- print("Number of chars :")
- print(numberOfBits)
- print("Computing entropy")
- entropy = 0.0
- for key in charCount:
- f = charCount[key] / numberOfBits
- entropy -= f * log(f, 2)
- print(entropy)
|