Skip to content

Add tap/knock language #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ o
- [X] `southpark`: converts letters to Kenny's language from Southpark (whitespace is also handled)
- [X] `southpark-icase`: case insensitive variant of `southpark`
- [X] `tomtom`: similar to `morse`, using slashes and backslashes
- [X] `tap` : converts tap/knock code commonly used by prisoners

#### Others

Expand Down
1 change: 1 addition & 0 deletions codext/languages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
from .radio import *
from .southpark import *
from .tomtom import *
from .tap import *

56 changes: 56 additions & 0 deletions codext/languages/tap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: UTF-8 -*-
"""Tap code - Tap/knock code encoding.

This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
"""
from ..__common__ import *


__examples__ = {
'enc(tap)': {'this is a test' : '.... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....'},
'dec(tap)': {'.... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....' : 'thisisatest'}
}


def build_encmap(map) :
dict = {}
i = 0
for col in range(1,6) :
for row in range(1,6) :
dict[map[i]] = "" + col * "." + " " + row * "."
i += 1
dict['k'] = dict['c']
return dict

def encode_tap(text, errors = 'strict') :
map = 'abcdefghijlmnopqrstuvwxyz'
ENCMAP = build_encmap(map)
encoded = ""
for i, letter in enumerate(text) :
try :
encoded += ENCMAP[letter.lower()]
except KeyError :
pass
if i != len(text) - 1 and letter != ' ':
encoded += ' '
return encoded, len(text)


def decode_tap(text, errors = 'ignore') :
map = 'abcdefghijlmnopqrstuvwxyz'
ENCMAP = build_encmap(map)
decoded = ""
for elem in text.split(" ") :
try :
decoded += next(key for key, value in ENCMAP.items() if value == elem)
except StopIteration :
print("Invalid character(s) in the input. This is what could be decoded :")
return decoded, len(text)


add("tap", encode_tap, decode_tap, ignore_case="both")

16 changes: 16 additions & 0 deletions docs/enc/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,19 @@ This codec is similar to morse. It converts text into slashes and backslashes.
'THIS IS A TEST'
```

-----

### Tap

Converts tap/knock code [commonly used by prisoners](https://en.wikipedia.org/wiki/Tap_code). Uses 25 letters, 'k' codes as 'c'.

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

```python
>>> codext.encode("this is a test", "tap")
'.... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....'
>>> codext.decode(".... .... .. ... .. .... .... ... .. .... .... ... . . .... .... . ..... .... ... .... ....", "tap")
'thisisatest'
```