mirror of
https://github.com/creyD/entro.py.git
synced 2026-04-12 20:00:29 +02:00
Renamed variables improving compatability
This commit is contained in:
26
entro.py
26
entro.py
@@ -10,10 +10,10 @@ import argparse
|
|||||||
|
|
||||||
|
|
||||||
# Calculates the entropy of a given string (as described in the docstring)
|
# Calculates the entropy of a given string (as described in the docstring)
|
||||||
def calcEntropy(string):
|
def calculateEntropy(input_string):
|
||||||
alphabet, alphabet_size, entropy = {}, 0, 0
|
alphabet, alphabet_size, entropy = {}, 0, 0
|
||||||
|
|
||||||
for char in string:
|
for char in input_string:
|
||||||
if char in alphabet:
|
if char in alphabet:
|
||||||
alphabet[char] += 1
|
alphabet[char] += 1
|
||||||
else:
|
else:
|
||||||
@@ -22,24 +22,24 @@ def calcEntropy(string):
|
|||||||
|
|
||||||
for char in alphabet:
|
for char in alphabet:
|
||||||
alphabet[char] = alphabet[char] / alphabet_size
|
alphabet[char] = alphabet[char] / alphabet_size
|
||||||
entropy += alphabet[char] * math.log(alphabet[char], 2)
|
entropy -= alphabet[char] * math.log(alphabet[char], 2)
|
||||||
|
|
||||||
return -entropy, alphabet
|
return entropy, alphabet
|
||||||
|
|
||||||
|
|
||||||
# Outputs a given entropy including the original text and the alphabet with probabilities
|
# Outputs a given entropy including the original text and the alphabet with probabilities
|
||||||
def printEntropy(original, entropy, alphabet, simple):
|
def printEntropy(original_string, entropy_value, alphabet_dict, simple_bool):
|
||||||
print('---')
|
print('---')
|
||||||
if simple == False:
|
if simple_bool == False:
|
||||||
print('Content: ' + original)
|
print('Content: ' + original_string)
|
||||||
print('Probabilities: ' + str(alphabet))
|
print('Probabilities: ' + str(alphabet_dict))
|
||||||
print('Entropy: ' + str(entropy) + ' bits')
|
print('Entropy: ' + str(entropy_value) + ' bits')
|
||||||
print('---')
|
print('---')
|
||||||
|
|
||||||
|
|
||||||
# Reads a file by a given path
|
# Reads a file by a given path
|
||||||
def getFile(path):
|
def readEntropyFile(path_string):
|
||||||
f = open(path, 'r')
|
f = open(path_string, 'r')
|
||||||
content = f.read().replace('\n', ' ')
|
content = f.read().replace('\n', ' ')
|
||||||
f.close()
|
f.close()
|
||||||
return content.strip()
|
return content.strip()
|
||||||
@@ -70,7 +70,7 @@ for string in args.strings:
|
|||||||
|
|
||||||
# Add all the provided files to the list
|
# Add all the provided files to the list
|
||||||
for file in args.files:
|
for file in args.files:
|
||||||
string = getFile(file)
|
string = readEntropyFile(file)
|
||||||
queue.append(string)
|
queue.append(string)
|
||||||
|
|
||||||
# Interates over the collected strings and prints the entropies
|
# Interates over the collected strings and prints the entropies
|
||||||
@@ -83,5 +83,5 @@ for string in queue:
|
|||||||
if args.squash != False:
|
if args.squash != False:
|
||||||
string = string.replace(" ", "")
|
string = string.replace(" ", "")
|
||||||
|
|
||||||
a, b = calcEntropy(string)
|
a, b = calculateEntropy(string)
|
||||||
printEntropy(string, a, b, args.simple)
|
printEntropy(string, a, b, args.simple)
|
||||||
|
|||||||
Reference in New Issue
Block a user