-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
example2.py
34 lines (26 loc) · 1.29 KB
/
example2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pyae
# from decimal import getcontext
# Example for encoding a simple text message using the PyAE module.
# Create the frequency table.
frequency_table = {"a": 2,
"b": 3,
"c": 1,
"d": 4}
# Create an instance of the ArithmeticEncoding class.
AE = pyae.ArithmeticEncoding(frequency_table,
save_stages=True)
# Default precision is 28. Change it to do arithmetic operations with larger/smaller numbers.
# getcontext().prec = 28
original_msg = "bdab"
print("Original Message: {msg}".format(msg=original_msg))
# Encode the message
encoded_msg, encoder , interval_min_value, interval_max_value = AE.encode(msg=original_msg,
probability_table=AE.probability_table)
print("Encoded Message: {msg}".format(msg=encoded_msg))
# Decode the message
decoded_msg, decoder = AE.decode(encoded_msg=encoded_msg,
msg_length=len(original_msg),
probability_table=AE.probability_table)
print("Decoded Message: {msg}".format(msg=decoded_msg))
decoded_msg = "".join(decoded_msg)
print("Message Decoded Successfully? {result}".format(result=original_msg == decoded_msg))