-
Notifications
You must be signed in to change notification settings - Fork 1
/
baudot.py
384 lines (342 loc) · 7.69 KB
/
baudot.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import textwrap, logging
from convertions import *
# http://codegolf.stackexchange.com/questions/94056/telegraphy-golf-decode-baudot-code
logging.basicConfig(level=logging.INFO)
SIGNS = {
"FS": "00010",
"LS": "00001",
"SP": "00011", # take fron ER
}
LETTERS = {
"A": "10000",
"B": "00110",
"C": "10110",
"D": "11110",
"E": "01000",
"F": "01110",
"G": "01010",
"H": "11010",
"I": "01100",
"J": "10010",
"K": "10011",
"L": "11011",
"M": "01011",
"N": "01111",
"O": "11100",
"P": "11111",
"Q": "10111",
"R": "00111",
"S": "00101",
"T": "10101",
"U": "10100",
"V": "11101",
"W": "01101",
"X": "01001",
"Y": "00100",
"Z": "11001"
}
FIGURES = {
"0": "11110",
"1": "10000",
"2": "01000",
"3": "00100",
"4": "10100",
"5": "11100",
"6": "10010",
"7": "01010",
"8": "00110",
"9": "10110",
"-": "00111",
"?": "01101",
":": "11001",
"(": "10011",
")": "01011",
".": "10001",
",": "00101", # take from S
"'": "11101",
"=": "11011",
"/": "11000",
"+": "11111",
"!": "11010" # take from H
}
def encodeBaudot(info):
rst = []
text = (info.upper()).split()
LC = 0 # letter count
FC = 0 # figure
for word in text:
s = []
for letter in word:
if letter in LETTERS:
if LC > 0:
s.append(LETTERS[letter])
else:
s.append(SIGNS['LS'])
s.append(LETTERS[letter])
LC += 1
FC = 0
elif letter in FIGURES:
if FC > 0:
s.append(FIGURES[letter])
else:
s.append(SIGNS['FS'])
s.append(FIGURES[letter])
FC += 1
LC = 0
rst.extend(s)
rst.append(SIGNS['SP'])
return rst[:-1]
def decodeBaudot(s):
#print 'baudot: ', s
if s != None:
WORDS = []
flag_L = False
flag_F = False
data = textwrap.wrap(s, 5)
for sign in data:
if sign == SIGNS['LS']:
#print 'letter'
flag_L = True
flag_F = False
elif sign == SIGNS['FS']:
flag_F = True
flag_L = False
#print 'figure'
elif sign == SIGNS['SP']:
#print 'space'
WORDS.extend(' ')
else:
if flag_L:
if sign in SIGNS.values():
if sign == SIGNS['FS']:
flag_L = False
flag_F = True
else:
flag_F = False
flag_L = True
else:
#flag_L = False
for letter, code in LETTERS.iteritems():
if code == sign:
#print letter
WORDS.extend(letter)
elif flag_F:
if sign in SIGNS.values():
if sign == SIGNS['LS']:
flag_F = False
flag_L = True
else:
flag_F = True
flag_L = False
else:
#flag_F = False
for figure, code in FIGURES.iteritems():
if code == sign:
#print figure
WORDS.extend(figure)
#return list2string(WORDS)
print list2string(WORDS)
# MEMO
# this decode version is associating with real time version
# TRY origindecodeBaudot
def _decodeBaudot(s):
if s != None:
WORDS = []
flag_L = False
flag_F = False
length = len(s)
n_FS = s.count(SIGNS['FS'])
n_LS = s.count(SIGNS['LS'])
#print n_LS
if n_LS > n_FS :
try:
begin = s.index(SIGNS['FS'])
s = s[begin:length]
data = tuple(textwrap.wrap(s, 5))
for sign in data:
if sign == SIGNS['LS']:
flag_L = True
flag_F = False
elif sign == SIGNS['FS']:
flag_F = True
flag_L = False
elif sign == SIGNS['SP']:
WORDS.extend(' ')
else:
if flag_L:
if sign in SIGNS.values():
if sign == SIGNS['FS']:
flag_L = False
flag_F = True
else:
flag_F = False
flag_L = True
else:
for letter, code in LETTERS.iteritems():
if code == sign:
WORDS.extend(letter)
elif flag_F:
if sign in SIGNS.values():
if sign == SIGNS['LS']:
flag_F = False
lag_L = True
else:
flag_F = True
flag_L = False
else:
for figure, code in FIGURES.iteritems():
if code == sign:
WORDS.extend(figure)
except ValueError:
logging.info('baudot -- error in first if')
pass
elif n_FS > n_LS:
try:
begin = s.index(SIGNS['LS'])
s = s[begin:length]
data = tuple(textwrap.wrap(s, 5))
#n = data.count(SIGNS['SP'])
#if n == 1:
for sign in data:
if sign == SIGNS['LS']:
flag_L = True
flag_F = False
elif sign == SIGNS['FS']:
flag_F = True
flag_L = False
elif sign == SIGNS['SP']:
WORDS.extend(' ')
else:
if flag_L:
if sign in SIGNS.values():
if sign == SIGNS['FS']:
flag_L = False
flag_F = True
else:
flag_F = False
flag_L = True
else:
for letter, code in LETTERS.iteritems():
if code == sign:
WORDS.extend(letter)
elif flag_F:
if sign in SIGNS.values():
if sign == SIGNS['LS']:
flag_F = False
lag_L = True
else:
flag_F = True
flag_L = False
else:
for figure, code in FIGURES.iteritems():
if code == sign:
WORDS.extend(figure)
except ValueError:
logging.info('baudot -- error in elif')
pass
else:
try:
index_FS = s.index(SIGNS['FS'])
index_LS = s.index(SIGNS['LS'])
#print index_LS, index_FS
if index_LS < index_FS:
begin = s.index(SIGNS['LS'])
s = s[begin:length]
data = tuple(textwrap.wrap(s, 5))
for sign in data:
if sign == SIGNS['LS']:
flag_L = True
flag_F = False
elif sign == SIGNS['FS']:
flag_F = True
flag_L = False
elif sign == SIGNS['SP']:
WORDS.extend(' ')
else:
if flag_L:
if sign in SIGNS.values():
if sign == SIGNS['FS']:
flag_L = False
flag_F = True
else:
flag_F = False
flag_L = True
else:
for letter, code in LETTERS.iteritems():
if code == sign:
WORDS.extend(letter)
elif flag_F:
if sign in SIGNS.values():
if sign == SIGNS['LS']:
flag_F = False
lag_L = True
else:
flag_F = True
flag_L = False
else:
for figure, code in FIGURES.iteritems():
if code == sign:
WORDS.extend(figure)
else:
begin = s.index(SIGNS['FS'])
s = s[begin:length]
data = tuple(textwrap.wrap(s, 5))
for sign in data:
if sign == SIGNS['LS']:
flag_L = True
flag_F = False
elif sign == SIGNS['FS']:
flag_F = True
flag_L = False
elif sign == SIGNS['SP']:
WORDS.extend(' ')
else:
if flag_L:
if sign in SIGNS.values():
if sign == SIGNS['FS']:
flag_L = False
flag_F = True
else:
flag_F = False
flag_L = True
else:
for letter, code in LETTERS.iteritems():
if code == sign:
WORDS.extend(letter)
elif flag_F:
if sign in SIGNS.values():
if sign == SIGNS['LS']:
flag_F = False
lag_L = True
else:
flag_F = True
flag_L = False
else:
for figure, code in FIGURES.iteritems():
if code == sign:
WORDS.extend(figure)
except ValueError:
logging.info('baudot -- error in else')
geo = []
gym = (".", "+", "-", ' ')
for i in WORDS:
if i.isdigit():
geo.extend(i)
elif i in gym:
geo.extend(i)
print list2string(geo)
#else:
# print 'nothing in baudot'
# return None
def main():
#text = str(raw_input('input the info:'))
#text = 'x=1.23 y=4.56'
#text = 'B(x=493.60, y=352.8)E'
text = 'x=1234567890'
info = list2string(encodeBaudot(text))
print info
decodeBaudot(info)
print originaldecodeBaudot(info)
if __name__ == '__main__':
#main()
pass