Skip to content

Commit 6c842db

Browse files
authored
More information.
1) An optional parameter named save_stages is added to the constructor of the ArithmeticEncoding class. If True, then the intervals of each stage are saved in a list. Note that setting save_stages=True may cause memory overflow if the message is large 2) The decoded message returned by the decode() method is always a list. 3) The order of the returned values by the the encode() and decode() method changed. 4) Added an example in the example_image.py script to encode and decode an image.
1 parent 78d1a03 commit 6c842db

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

README.md

+33-15
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@ import pyae
2424

2525
## Instantiate the `ArithmeticEncoding` Class
2626

27-
Create an instance of the `ArithmeticEncoding` class. Its constructor accepts the frequency table which is a dictionary mapping each possible character to its frequency. According to this frequency table, the messages to be encoded/decoded must have only the 3 characters **a**, **b**, and **c**.
27+
Create an instance of the `ArithmeticEncoding` class. Its constructor accepts 2 arguments:
28+
29+
1. `frequency_table`: The frequency table as a dictionary where key is the symbol and value is the frequency.
30+
2. `save_stages`: If `True`, then the intervals of each stage are saved in a list. Note that setting `save_stages=True` may cause memory overflow if the message is large
31+
32+
According to the following frequency table, the messages to be encoded/decoded must have only the 3 characters **a**, **b**, and **c**.
2833

2934
```python
3035
frequency_table = {"a": 2,
3136
"b": 7,
3237
"c": 1}
3338

34-
AE = pyae.ArithmeticEncoding(frequency_table)
39+
AE = pyae.ArithmeticEncoding(frequency_table=frequency_table,
40+
save_stages=True)
3541
```
3642

3743
## Prepare a Message
@@ -44,23 +50,29 @@ original_msg = "abc"
4450

4551
## Encode the Message
4652

47-
Encode the message using the `encode()` method. It accepts the message to be encoded and the probability table. It returns the encoder and the encoded message (single double value).
53+
Encode the message using the `encode()` method. It accepts the message to be encoded and the probability table. It returns the encoded message (single double value) and the encoder stages.
4854

4955
```python
50-
encoder, encoded_msg = AE.encode(msg=original_msg,
56+
encoded_msg, encoder = AE.encode(msg=original_msg,
5157
probability_table=AE.probability_table)
5258
```
5359

5460
## Decode the Message
5561

56-
Decode the message using the `decode()` method. It accepts the encoded message, message length, and the probability table. It returns the decoder and the decoded message.
62+
Decode the message using the `decode()` method. It accepts the encoded message, message length, and the probability table. It returns the decoded message and the decoder stages.
5763

5864
```python
59-
decoder, decoded_msg = AE.decode(encoded_msg=encoded_msg,
65+
decoded_msg, decoder = AE.decode(encoded_msg=encoded_msg,
6066
msg_length=len(original_msg),
6167
probability_table=AE.probability_table)
6268
```
6369

70+
Note that the symbols in the decoded message are returned in a `list`. If the original message is a string, then consider converting the list into a string using `join()` function as follows.
71+
72+
```python
73+
decoded_msg = "".join(decoded_msg)
74+
```
75+
6476
# <u>IMPORTANT</u>: `double` Module
6577

6678
The floating-point numbers in Python are limited to a certain precision. Beyond it, Python cannot store any additional decimal numbers. This is why the project uses the double data type offered by the [`decimal` module](https://docs.python.org/2/library/decimal.html).
@@ -88,20 +100,22 @@ frequency_table = {"a": 2,
88100
"b": 7,
89101
"c": 1}
90102

91-
AE = pyae.ArithmeticEncoding(frequency_table)
103+
AE = pyae.ArithmeticEncoding(frequency_table=frequency_table,
104+
save_stages=True)
92105

93106
original_msg = "abc"
94107
print("Original Message: {msg}".format(msg=original_msg))
95108

96-
encoder, encoded_msg = AE.encode(msg=original_msg,
109+
encoded_msg, encoder = AE.encode(msg=original_msg,
97110
probability_table=AE.probability_table)
98111
print("Encoded Message: {msg}".format(msg=encoded_msg))
99112

100-
decoder, decoded_msg = AE.decode(encoded_msg=encoded_msg,
113+
decoded_msg, decoder = AE.decode(encoded_msg=encoded_msg,
101114
msg_length=len(original_msg),
102115
probability_table=AE.probability_table)
103116
print("Decoded Message: {msg}".format(msg=decoded_msg))
104117

118+
decoded_msg = "".join(decoded_msg)
105119
print("Message Decoded Successfully? {result}".format(result=original_msg == decoded_msg))
106120
```
107121

@@ -164,20 +178,22 @@ frequency_table = {"a": 2,
164178
"b": 7,
165179
"c": 1}
166180

167-
AE = pyae.ArithmeticEncoding(frequency_table)
181+
AE = pyae.ArithmeticEncoding(frequency_table=frequency_table,
182+
save_stages=True)
168183

169184
original_msg = "abc"*20
170185
print("Original Message: {msg}".format(msg=original_msg))
171186

172-
encoder, encoded_msg = AE.encode(msg=original_msg,
187+
encoded_msg, encoder = AE.encode(msg=original_msg,
173188
probability_table=AE.probability_table)
174189
print("Encoded Message: {msg}".format(msg=encoded_msg))
175190

176-
decoder, decoded_msg = AE.decode(encoded_msg=encoded_msg,
191+
decoded_msg, decoder = AE.decode(encoded_msg=encoded_msg,
177192
msg_length=len(original_msg),
178193
probability_table=AE.probability_table)
179194
print("Decoded Message: {msg}".format(msg=decoded_msg))
180195

196+
decoded_msg = "".join(decoded_msg)
181197
print("Message Decoded Successfully? {result}".format(result=original_msg == decoded_msg))
182198
```
183199

@@ -210,20 +226,22 @@ frequency_table = {"a": 2,
210226
"b": 7,
211227
"c": 1}
212228

213-
AE = pyae.ArithmeticEncoding(frequency_table)
229+
AE = pyae.ArithmeticEncoding(frequency_table=frequency_table,
230+
save_stages=True)
214231

215232
original_msg = "abc"*20
216233
print("Original Message: {msg}".format(msg=original_msg))
217234

218-
encoder, encoded_msg = AE.encode(msg=original_msg,
235+
encoded_msg, encoder = AE.encode(msg=original_msg,
219236
probability_table=AE.probability_table)
220237
print("Encoded Message: {msg}".format(msg=encoded_msg))
221238

222-
decoder, decoded_msg = AE.decode(encoded_msg=encoded_msg,
239+
decoded_msg, decoder = AE.decode(encoded_msg=encoded_msg,
223240
msg_length=len(original_msg),
224241
probability_table=AE.probability_table)
225242
print("Decoded Message: {msg}".format(msg=decoded_msg))
226243

244+
decoded_msg = "".join(decoded_msg)
227245
print("Message Decoded Successfully? {result}".format(result=original_msg == decoded_msg))
228246
```
229247

0 commit comments

Comments
 (0)