-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
275 lines (225 loc) · 10.2 KB
/
decoder.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'''
Created Sun May 1 22 2022
@author: henry-israel
Improved enigame decoder that more effecively uses classes
'''
import numpy as np
from itertools import groupby
import string
class TranslatorBase:
'''
Base class from which other translators will be built from
'''
def __init__(self, inputstr: str, delimiter: str=''):
'''
str : inputstr -> input string, to be translated
str : delimiter -> delimter to separate words/numbers
'''
self._inputstr=inputstr
self._delimiter=delimiter
#Special cases so we can get a couple of different versions of the input
#While inefficient it's probably useful to have these all defined here!
#Upper case version of regular string
self._inputstr_allcaps=self._inputstr.upper()
#Version of normal string with no delimiter
self._input_no_delim=self._inputstr.replace(delimiter, '')
#Upper case version of normal string with no delimiter
self._input_no_delim_allcaps=self._input_no_delim.upper()
#Version of the input where it's an array
if delimiter=='':
self._input_asarray=list(self._inputstr_allcaps)
else:
self._input_asarray=[str_i for str_i in self._inputstr_allcaps.split(delimiter)]
self._inputgrid=[]
self._ordered_asarray = sorted(self._input_asarray)
self._letterfreq={key: len(list(group)) for key, group in groupby(self._ordered_asarray)}
self._input_is_num=all([i.isdecimal() for i in self._input_asarray])
self._alphabet=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z", " "]
self._keyboardalphabet=["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S",
"D","F","G","H","J","K","L","Z","X","C","V","B","N","M", ' ']
self._morse_code_dict={ 'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-', ' ' : '/'}
@property
def get_input_all_caps(self):
return self._input_allcaps
@property
def get_input_no_delim(self):
return self._input_no_delim_allcaps
@property
def get_input_as_grid(self):
return self._inputgrid
@property
def get_char_freq(self):
return self._letterfreq
#Generic Stuff
def convert_to_grid(self):
'''
Checks if you can convert input string to a grid
'''
sqrtarrsize=len(self._input_asarray)**0.5
if sqrtarrsize==int(sqrtarrsize):
print(f"Input has {len(self._input_asarray)} entries, can grid-ify!")
self._inputgrid=np.array(self._input_asarray).reshape(int(sqrtarrsize), int(sqrtarrsize))
return True
else:
print("Can't convert input to grid")
return False
#Numerical stuff
def get_prime_factors(self):
if self._input_is_num:
return factorint(int(self._inputstr))
else:
return 0
def convert_from_base_to_dec(self, base):
return [int(i, base) for i in self._input_asarray]
def convert_from_base_to_base(self, base1: int, base2: int):
#convert from decimal to new base
if not self._input_is_num:
return 0
else:
dec_arr = self.convert_from_base_to_dec(base1)
return [self.change_dec_to_base(i) for i in dec_arr]
def change_dec_to_base(self, instr, base):
innum=int(instr)
rem_arr=[]
while innum>0:
innum, rem=divmod(innum, base)
rem_arr.append(rem)
result=''.join(str(r) for r in rem_arr)
return result
def convert_num_to_let(self, inarr, base=10):
#numerical array to array of letters!
if not self._input_is_num:
return 0
else:
num_arr=[int(i) for i in inarr]
if max(num_arr)>=27 or min(num_arr)<=0:
return 0
else:
return [self._alphabet[i-1] for i in num_arr]
def convert_let_to_num(self):
nums=[str(i) for i in range(1,len(self._alphabet)+1)]
letnumdict=dict(zip(self._alphabet,nums))
return [letnumdict[let_i] for let_i in self._input_asarray]
def convert_from_ascii(self, instr, base=10):
if not self._input_is_num or base>10:
return ' '
else:
return str(chr(int(instr, base)))
def convert_to_ascii(self, instr):
return [ord(c) for c in instr]
#Alphabetical
def morse_to_eng(self):
if not set(self._input_asarray).issubset(set(self._morse_code_dict.values())):
print("Input not morse code")
return 0
else:
#I'm an eegit
revdict=dict(zip(self._morse_code_dict.values(),self._morse_code_dict.keys()))
return ''.join(revdict[i] for i in self._inputstr.split())
def convert_to_keyboardpos(self, inarr):
if not set(inarr).issubset(self._alphabet):
print("Input must just be letters")
return 0
else:
transdict=dict(zip(self._alphabet, self._keyboardalphabet))
return ''.join(transdict[i] for i in inarr)
def convert_from_keyboardpos(self, inarr):
if not set(inarr).issubset(self._alphabet):
print("Input must just be letters")
return 0
else:
transdict=dict(zip(self._keyboardalphabet, self._alphabet))
return ''.join(transdict[i] for i in inarr)
def translate_from_hex(self, instr):
outhex=0
try:
outhex=bytearray.fromhex(instr).decode('utf-8')
except UnicodeDecodeError:
print("Input is not hex")
return outhex
def get_max_digit(self):
digarr=[]
for str_i in self._input_asarray:
for i in list(str_i):
digarr.append(int(i))
return max(digarr)
def __call__(self, maxbase: int=10, ishex=False):
morse_trans=self.morse_to_eng()
if morse_trans!=0:
print("It's in morse!")
print(f"Translates to {morse_trans}")
new_str=''.join(i for i in morse_trans)
self.__init__(new_str, '')
if ishex:
hex_trans=self.translate_from_hex(self._input_no_delim)
if hex_trans!=0:
print("Input is in hex!")
print(f"Input translates to {hex_trans}")
self.__init__(hex_trans,'')
isgrid=self.convert_to_grid()
print(f"Examining {self._input_asarray}")
if isgrid:
print(f"input as grid is : ")
for row in self._inputgrid:
print(row)
print(f"Character frequency is : {self._letterfreq}")
alphabet_trans=self.convert_num_to_let(self._input_asarray, 10)
dokb=False
if alphabet_trans!=0:
dokb=True
print(f"Alphabetic (num->letter) is {alphabet_trans}")
from_keyboard_trans=self.convert_to_keyboardpos(alphabet_trans)
to_keyboard_trans=self.convert_from_keyboardpos(alphabet_trans)
elif set(self._input_asarray).issubset(self._alphabet):
dokb=True
from_keyboard_trans=self.convert_to_keyboardpos(self._input_asarray)
to_keyboard_trans=self.convert_from_keyboardpos(self._input_asarray)
self._input_asarray=self.convert_let_to_num()
self._input_is_num=True
self._inputstr=''.join(i+' ' for i in self._input_asarray)
print(f"Letter->Base 10 numerical value is : {self._input_asarray}")
print(f"This sums to {np.sum(np.array(self._input_asarray).astype(int))}\n")
if dokb:
print(f"Alphabet->Keyboard pos : {to_keyboard_trans}")
print(f"Keyboard pos->Alphabet : {from_keyboard_trans}")
if self._input_is_num:
minbase=self.get_max_digit()+1
print(f"Detected numerical result, converting from base {minbase}->{maxbase} to 10 and looking at ascii, binary etc.\n")
#Numeric stuff
for base_i in range(minbase, maxbase+1):
print("------------------------------------------------------")
base_conv=self.convert_from_base_to_dec(base_i)
alphabet_trans_base=self.convert_num_to_let(base_conv, base_i)
if base_i<=10:
ascii_trans=[self.convert_from_ascii(i, base_i) for i in self._input_asarray]
try:
print(f"Ascii translation is '{''.join(i for i in ascii_trans)}' in base {base_i}")
except:
print("Couldn't get your ascii translation")
print(f"Now using base {base_i}")
print(f"{self._input_asarray} is {base_conv}")
if alphabet_trans_base!=0:
print(f"Alphabetic (num->letter) is {alphabet_trans_base}")
from_keyboard_trans_base=self.convert_to_keyboardpos(alphabet_trans_base)
to_keyboard_trans_base=self.convert_from_keyboardpos(alphabet_trans_base)
print(f"If we go from keyboard->alphabet we get {from_keyboard_trans_base}")
print(f"If we go from alphabet->keyboard we get {to_keyboard_trans_base}")
print("------------------------------------------------------\n")
if __name__=='__main__':
x=TranslatorBase("01101101 01110010 00100000 01110111 01101111 01110010 01101100 01100100 01110111 01101001 01100100 01100101",' ')
x(10)