Skip to content

Commit 73b1534

Browse files
committed
Improved codec: tap
1 parent 1f07d5f commit 73b1534

4 files changed

Lines changed: 37 additions & 54 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ o
288288
- [X] `radio`: aka NATO or radio phonetic alphabet
289289
- [X] `southpark`: converts letters to Kenny's language from Southpark (whitespace is also handled)
290290
- [X] `southpark-icase`: case insensitive variant of `southpark`
291+
- [X] `tap`: converts text to tap/knock code, commonly used by prisoners
291292
- [X] `tomtom`: similar to `morse`, using slashes and backslashes
292-
- [X] `tap` : converts tap/knock code commonly used by prisoners
293293

294294
#### Others
295295

codext/languages/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
from .navajo import *
88
from .radio import *
99
from .southpark import *
10-
from .tomtom import *
1110
from .tap import *
11+
from .tomtom import *
1212

codext/languages/tap.py

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,29 @@
1111

1212

1313
__examples__ = {
14-
'enc(tap)': {'this is a test' : '.... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....'},
15-
'dec(tap)': {'.... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....' : 'thisisatest'}
14+
'enc(tap|knock-code|tap_code)': {'this is a test' : ".... ...... ..... ........ ...⠀ ⠀.. ........ ...⠀ ⠀. ."
15+
"⠀ ⠀.... ..... ......... ....... ...."},
1616
}
17+
__guess__ = ["tap", "tap-inv"]
1718

1819

19-
def build_encmap(map) :
20-
dict = {}
21-
i = 0
22-
for col in range(1,6) :
23-
for row in range(1,6) :
24-
dict[map[i]] = "" + col * "." + " " + row * "."
20+
def __build_encmap(a):
21+
d, i = {}, 0
22+
for x in range(1,6):
23+
for y in range(1,6):
24+
d[a[i]] = x * "." + " " + y * "."
2525
i += 1
26-
dict['k'] = dict['c']
27-
return dict
28-
29-
def encode_tap(text, errors = 'strict') :
30-
map = 'abcdefghijlmnopqrstuvwxyz'
31-
ENCMAP = build_encmap(map)
32-
encoded = ""
33-
for i, letter in enumerate(text) :
34-
try :
35-
encoded += ENCMAP[letter.lower()]
36-
except KeyError :
37-
pass
38-
if i != len(text) - 1 and letter != ' ':
39-
encoded += ' '
40-
return encoded, len(text)
41-
42-
43-
def decode_tap(text, errors = 'ignore') :
44-
map = 'abcdefghijlmnopqrstuvwxyz'
45-
ENCMAP = build_encmap(map)
46-
decoded = ""
47-
for elem in text.split(" ") :
48-
try :
49-
decoded += next(key for key, value in ENCMAP.items() if value == elem)
50-
except StopIteration :
51-
print("Invalid character(s) in the input. This is what could be decoded :")
52-
return decoded, len(text)
53-
54-
55-
add("tap", encode_tap, decode_tap, ignore_case="both")
26+
d['k'], d[' '] = d['c'], " "
27+
return d
28+
29+
30+
31+
ENCMAP = {
32+
'': __build_encmap("abcdefghijlmnopqrstuvwxyz"),
33+
'inv': __build_encmap("abcdefghijlmnopqrstuvwxyz"[::-1]),
34+
}
35+
36+
37+
if PY3:
38+
add_map("tap", ENCMAP, ignore_case="both", sep="⠀", pattern=r"^(?:tap|knock)(?:[-_]code)?(|inv)$")
5639

docs/enc/languages.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,34 +166,34 @@ This encodes text according to Kenny's language in Southpark.
166166

167167
-----
168168

169-
### Tom-Tom
169+
### Tap
170170

171-
This codec is similar to morse. It converts text into slashes and backslashes.
171+
This codec implements the [tap/knock code](https://en.wikipedia.org/wiki/Tap_code) commonly used by prisoners. It uses 25 letters, "*k*" is encoded to the same token than "*c*".
172172

173173
**Codec** | **Conversions** | **Aliases** | **Comment**
174174
:---: | :---: | --- | ---
175-
`tomtom` | text <-> tom-tom encoded text | `tom-tom` | uses "`|`" as a separator
175+
`tap` | text <-> tap/knock encoded text | `knock`, `tap-code` | uses a large Unicode whitespace as a token separator ; Python 3 only
176176

177177
```python
178-
>>> codext.encode("this is a test", "tom-tom")
179-
'\\\\/\\ /\\\\ /\\\\\\ \\/\\ | /\\\\\\ \\/\\ | / | \\\\/\\ /\\ \\/\\ \\\\/\\'
180-
>>> codext.decode("\\\\/\\ /\\\\ /\\\\\\ \\/\\ | /\\\\\\ \\/\\ | / | \\\\/\\ /\\ \\/\\ \\\\/\\", "tomtom")
181-
'THIS IS A TEST'
178+
>>> codext.encode("this is a test", "tap")
179+
'.... ....⠀.. ...⠀.. ....⠀.... ...⠀ ⠀.. ....⠀.... ...⠀ ⠀. .⠀ ⠀.... ....⠀. .....⠀.... ...⠀.... ....'
180+
>>> codext.decode(".... ....⠀.. ...⠀.. ....⠀.... ...⠀ ⠀.. ....⠀.... ...⠀ ⠀. .⠀ ⠀.... ....⠀. .....⠀.... ...⠀.... ....", "knock")
181+
'this is a test'
182182
```
183183

184184
-----
185185

186-
### Tap
186+
### Tom-Tom
187187

188-
Converts tap/knock code [commonly used by prisoners](https://en.wikipedia.org/wiki/Tap_code). Uses 25 letters, 'k' codes as 'c'.
188+
This codec is similar to morse. It converts text into slashes and backslashes.
189189

190190
**Codec** | **Conversions** | **Aliases** | **Comment**
191191
:---: | :---: | --- | ---
192-
`tap` | text <-> tap/knock encoded text | `tap` | uses '&nbsp; &nbsp;' (double space) as a separator for letters. No spaces between words after decoding.
192+
`tomtom` | text <-> tom-tom encoded text | `tom-tom` | uses "`|`" as a separator
193193

194194
```python
195-
>>> codext.encode("this is a test", "tap")
196-
'.... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....'
197-
>>> codext.decode(".... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....", "tap")
198-
'thisisatest'
195+
>>> codext.encode("this is a test", "tom-tom")
196+
'\\\\/\\ /\\\\ /\\\\\\ \\/\\ | /\\\\\\ \\/\\ | / | \\\\/\\ /\\ \\/\\ \\\\/\\'
197+
>>> codext.decode("\\\\/\\ /\\\\ /\\\\\\ \\/\\ | /\\\\\\ \\/\\ | / | \\\\/\\ /\\ \\/\\ \\\\/\\", "tomtom")
198+
'THIS IS A TEST'
199199
```

0 commit comments

Comments
 (0)