Skip to content

Commit a6feff2

Browse files
committed
Added codecs: dvorak, soundex, telegraph
1 parent c6787a1 commit a6feff2

7 files changed

Lines changed: 906 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,11 @@ This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `ba
446446

447447
- [X] `binhex`: BinHex 4.0 (`.hqx`), the binary-to-text encoding of the classic Mac OS
448448
- [X] `dna`: implements the 8 rules of DNA sequences (N belongs to [1,8])
449+
- [X] `dvorak`: text typed with the Dvorak keyboard layout instead of QWERTY
449450
- [X] `letter-indices`: encodes consonants and/or vowels with their corresponding indices
450451
- [X] `markdown`: unidirectional encoding from Markdown to HTML
452+
- [X] `soundex`: Soundex phonetic codes (decoding gives English words with the same codes)
453+
- [X] `telegraph`: Chinese telegraph code, with the codebooks of Mainland China and Taiwan
451454
- [X] `xx`: XXencode, a UUencode variant with an alphanumeric alphabet (`+`, `-`, digits and letters)
452455
- [X] `yenc`: Usenet binary-to-text encoding (raw, wrapped or framed with header, trailer and CRC32)
453456

‎docs/pages/enc/others.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ CACTCGGTCGGCCATATGTTCGGCCATATGTTCGTCTGTTCACTCGCCCATACACT
4848

4949
-----
5050

51+
### Dvorak
52+
53+
The [Dvorak Simplified Keyboard](https://en.wikipedia.org/wiki/Dvorak_keyboard_layout), patented in 1936, rearranges the letters and some punctuation of the QWERTY layout (US). Encoding replaces each character by the one of the same key with the Dvorak layout (as when typing with the QWERTY layout on a computer set to Dvorak), while decoding does the opposite. This [permutation of 35 keys](https://www.montefischer.com/2021/03/21/dvorak-group-action.html) (letters and the `-`, `=`, `[`, `]`, `;`, `'`, `,`, `.` and `/` keys, with their shifted characters) gets back to the text after 210 encodings.
54+
55+
**Codec** | **Conversions** | **Aliases** | **Comment**
56+
:---: | :---: | --- | ---
57+
`dvorak` | text <-> text typed with the Dvorak layout | `qwerty-dvorak`, `qwerty_to_dvorak` | other characters kept as is
58+
59+
```python
60+
>>> codext.encode("Hello, World!", "dvorak")
61+
'D.nnrw <rpne!'
62+
>>> codext.decode("D.nnrw <rpne!", "dvorak")
63+
'Hello, World!'
64+
```
65+
66+
-----
67+
5168
### Letter indices
5269

5370
This encodes consonants and/or vowels with their respective indices. This codec is case insensitive, strips white spaces and only applies to letters.
@@ -96,6 +113,46 @@ This is only for "encoding" (converting) Markdown to HTML.
96113

97114
-----
98115

116+
### Soundex
117+
118+
[Soundex](https://www.archives.gov/research/census/soundex) indexes names by their sound, as used by the US National Archives: each word is written as its first letter followed by three digits for its next consonants (B, F, P and V give 1 ; C, G, J, K, Q, S, X and Z give 2 ; D and T give 3 ; L gives 4 ; M and N give 5 ; R gives 6), the vowels being dropped, the consecutive consonants with the same digit (including the first letter, or separated by H or W) giving a single digit, and the code being padded with zeros or truncated. Encoding replaces each word by its code, keeping the other characters. As Soundex is not reversible, decoding replaces each code by the most frequent word with this code from an English word list (9850 words, from the [lists of Ciphey](https://github.com/Ciphey/CipheyDists)), or, with the `-all` variant, by all these words in order of frequency.
119+
120+
**Codec** | **Conversions** | **Aliases** | **Comment**
121+
:---: | :---: | --- | ---
122+
`soundex` | text <-> Soundex codes | | decoding gives the most frequent English word for each code ; codes without word handled with the errors handling
123+
`soundex-all` | text <-> Soundex codes | `soundex_all` | decoding gives all the English words for each code (e.g. `[world|worldwide|worlds]`)
124+
125+
!!! warning "Decoding"
126+
127+
Decoding cannot give back the original words, only words with the same code (e.g. `Robert` gives `R163`, which gives `report`). Ciphey's own table of codes does not merge the first letter with the next consonant with the same digit (e.g. `cc` gives `C200` instead of `C000` and `Pfister` gives `P123` instead of `P236`), unlike the rules of the US National Archives followed by this codec.
128+
129+
```python
130+
>>> codext.encode("Robert Rupert Pfister", "soundex")
131+
'R163 R163 P236'
132+
>>> codext.decode("R163 W643", "soundex")
133+
'report world'
134+
```
135+
136+
-----
137+
138+
### Telegraph
139+
140+
The [Chinese telegraph code](https://en.wikipedia.org/wiki/Chinese_telegraph_code), designed in 1871, sends Chinese characters by telegraph as groups of four digits (from `0000` to `9999`), as given in a codebook. Mainland China (with the simplified characters) and Taiwan (with the traditional characters) use different codebooks, taken from the [Unihan database](https://www.unicode.org/reports/tr38/#kMainlandTelegraph) of Unicode. Encoding gives the groups of four digits of the characters, separated by spaces, while decoding accepts groups separated by whitespaces or not.
141+
142+
**Codec** | **Conversions** | **Aliases** | **Comment**
143+
:---: | :---: | --- | ---
144+
`telegraph` | Chinese text <-> groups of 4 digits | `chinese-telegraph`, `telegraph-mainland` | codebook of Mainland China (7078 characters)
145+
`telegraph-taiwan` | Chinese text <-> groups of 4 digits | `telegraph-tw` | codebook of Taiwan (9024 characters)
146+
147+
```python
148+
>>> codext.encode("中文信息", "telegraph")
149+
'0022 2429 0207 1873'
150+
>>> codext.decode("5618 1947 0948", "telegraph-taiwan")
151+
'蕭愛國'
152+
```
153+
154+
-----
155+
99156
### XXencode
100157

101158
XXencode works like UUencode, but with an alphabet made of "`+`", "`-`", digits and letters only. Each line encodes up to 45 bytes and starts with the character holding its number of bytes ; the lines are framed by a `begin` line and an `end` line.

‎src/codext/others/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: UTF-8 -*-
22
from .binhex import *
33
from .dna import *
4+
from .dvorak import *
45
from .kbshift import *
56
from .letters import *
67
from .markdown import *
8+
from .soundex import *
9+
from .telegraph import *
710
from .uuencode import *
811
from .xxencode import *
912
from .yenc import *

‎src/codext/others/dvorak.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Dvorak Codec - QWERTY to Dvorak keyboard layout content encoding.
3+
4+
The Dvorak Simplified Keyboard, patented by August Dvorak and William Dealey in 1936, rearranges the letters and some
5+
punctuation of the standard QWERTY layout (US) so that the most frequent letters are on the home row. Encoding replaces
6+
each character by the one of the same key with the Dvorak layout (as when typing with the QWERTY layout on a computer
7+
set to Dvorak), while decoding does the opposite (as when typing with the Dvorak layout on a computer set to QWERTY).
8+
This permutation of 35 keys (letters and the "-", "=", "[", "]", ";", "'", ",", "." and "/" keys, with their shifted
9+
characters) is made of cycles of 15, 14, 2, 2, 1 and 1 keys, hence it gets back to the text after 210 encodings.
10+
11+
This codec:
12+
- en/decodes strings from str to str
13+
- en/decodes strings from bytes to bytes
14+
- decodes file content to str (read)
15+
- encodes file content from str to bytes (write)
16+
17+
Reference: https://www.montefischer.com/2021/03/21/dvorak-group-action.html
18+
"""
19+
from ..__common__ import *
20+
21+
22+
__examples__ = {
23+
'enc(dvorak|qwerty-dvorak|qwerty_to_dvorak)': {'Hello, World!': "D.nnrw <rpne!", 'dvorak': "ekrpat"},
24+
'dec(dvorak)': {'D.nnrw <rpne!': "Hello, World!", 'Cbol.jyrp Mai': "Inspector Mag"},
25+
'enc-dec(dvorak)': ["@random{256}", "The quick brown fox jumps over the lazy dog: \"-=[];',./_+{}:<>?\""],
26+
}
27+
__guess__ = ["dvorak"]
28+
29+
30+
# characters of the keys of the QWERTY layout and of the Dvorak layout, unshifted then shifted (US)
31+
QWERTY = "-=qwertyuiop[]asdfghjkl;'zxcvbnm,./" "_+QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?"
32+
DVORAK = "[]',.pyfgcrl/=aoeuidhtns-;qjkxbmwvz" "{}\"<>PYFGCRL?+AOEUIDHTNS_:QJKXBMWVZ"
33+
_ENCODE, _DECODE = str.maketrans(QWERTY, DVORAK), str.maketrans(DVORAK, QWERTY)
34+
35+
36+
def dvorak_encode(text, errors="strict"):
37+
return ensure_str(text).translate(_ENCODE), len(text)
38+
39+
40+
def dvorak_decode(text, errors="strict"):
41+
return ensure_str(text).translate(_DECODE), len(text)
42+
43+
44+
add("dvorak", dvorak_encode, dvorak_decode, r"^(?:qwerty[-_](?:to[-_])?)?dvorak$", examples=__examples__,
45+
guess=__guess__, entropy=lambda e: e, printables_rate=lambda pr: pr, transitive=True)

0 commit comments

Comments
 (0)