-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
46 lines (36 loc) · 1.32 KB
/
encode.py
File metadata and controls
46 lines (36 loc) · 1.32 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
import re
import sys
from utils import char_to_ord, get_encoding_matrix, list_chunk
def encode(n, msg):
"""Encodes a given message into a matrix of encoded ordinals.
Args:
n (int): Size of the encoding matrix
msg (str): Given message
"""
validate_message(msg)
ord_matrix = msg_to_ord_matrix(n, msg)
A = get_encoding_matrix(n)
print("Input message:", ord_matrix, "", sep="\n")
encoded_message = np.matmul(ord_matrix, A)
print("Encoded message:", encoded_message, "", sep="\n")
def msg_to_ord_matrix(n, msg):
"""Converts given message to matrix of ordinals.
Args:
n (int): Width of the ordinal matrix
msg (str): Given message
Returns:
np.ndarray: Matrix of ordinals corresponding to `msg`
"""
msg = msg.lower()
num_list = [char_to_ord(c) for c in msg]
chunks = list(list_chunk(num_list, n))
return np.asarray(chunks, dtype=float).reshape(len(chunks), n)
def validate_message(msg):
"""Ensures that message contains only letters and spaces, panicking otherwise.
Args:
msg (str): Given message
"""
ALPHABETIC_AND_SPACE_ONLY_REGEX = re.compile("^[a-zA-Z\s]*$")
if not (ALPHABETIC_AND_SPACE_ONLY_REGEX.match(msg)):
sys.exit(f"The given message may only contain letters and spaces.")