39 lines
880 B
Python
39 lines
880 B
Python
import math
|
|
|
|
|
|
def Q_statistik():
|
|
with open('Text.txt', 'r', encoding='utf-8') as file:
|
|
text = file.read()
|
|
|
|
freq = {}
|
|
for char in text:
|
|
if char in freq:
|
|
freq[char] += 1
|
|
else:
|
|
freq[char] = 1
|
|
|
|
total_chars = sum(freq.values())
|
|
|
|
prob = {}
|
|
info_content = {}
|
|
entropy = 0
|
|
|
|
for char, count in freq.items():
|
|
prob[char] = count / total_chars
|
|
info_content[char] = -math.log2(prob[char])
|
|
entropy += prob[char] * info_content[char]
|
|
|
|
return freq, prob, info_content, entropy
|
|
|
|
|
|
def main():
|
|
frequencies, probabilities, information_content, entropy = Q_statistik()
|
|
print("Häufigkeiten:", frequencies)
|
|
print("Wahrscheinlichkeiten:", probabilities)
|
|
print("Informationsgehalt:", information_content)
|
|
print("Entropie:", entropy)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|