Skip to content

Commit 4947ff1

Browse files
committed
Improved codec: galactic
1 parent 40eabfa commit 4947ff1

3 files changed

Lines changed: 98 additions & 20 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `ba
418418

419419
- [X] `braille`: well-known braille language (Python 3 only)
420420
- [X] `ipsum`: aka lorem ipsum
421-
- [X] `galactic`: aka galactic alphabet or Minecraft enchantment language (Python 3 only)
421+
- [X] `galactic`: aka Standard Galactic Alphabet or Minecraft enchantment language (as written by most online translators)
422422
- [X] `leetspeak`: based on minimalistic elite speaking rules
423423
- [X] `morse`: uses whitespace as a separator
424424
- [X] `navajo`: only handles letters (not full words from the Navajo dictionary)

‎docs/pages/enc/languages.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ It supports letters, digits and some special characters.
2323

2424
### Galactic
2525

26-
This implements the [Minecraft's enchanting table](https://www.thegamer.com/minecraft-enchantment-table-language-guide/) using resembling Unicode characters.
26+
This implements the Standard Galactic Alphabet, i.e. the language of [Minecraft's enchanting table](https://www.thegamer.com/minecraft-enchantment-table-language-guide/), using resembling Unicode characters as most online translators (e.g. LingoJam) do: one character for most letters, but `!¡` for *p*, `ℸ ̣ ` (with a combining dot below) for *t*, ` ̇/` (with a combining dot above) for *x* and `||` for *y*. Other ASCII characters are kept, except `!` and `|`.
27+
28+
When decoding, other lookalike characters and the shorter forms of *p*, *t*, *x* and *y* are also accepted, and ASCII characters (e.g. capital letters, which translators leave unchanged) are kept.
2729

2830
**Codec** | **Conversions** | **Aliases** | **Comment**
2931
:---: | :---: | --- | ---
30-
`galactic` | text <-> Minecraft enchantment symbols | `galactic-alphabet`, `minecraft_enchantment`, `minecraft-enchanting-language` | Python 3 only
32+
`galactic` | text <-> Minecraft enchantment symbols | `galactic-alphabet`, `minecraft_enchantment`, `minecraft-enchanting-language` | case-insensitive when encoding
3133

3234
```python
3335
>>> codext.encode("this is a test", "galactic")
34-
'ℸ₸╎߆ ╎߆ ᒋ ℸᒷ߆ℸ'
35-
>>> codext.decode("ℸ₸╎߆ ╎߆ ᒋ ℸᒷ߆ℸ", "galactic")
36-
'this is a test'
36+
'ℸ ̣ ⍑╎ᓭ ╎ᓭ ᔑ ℸ ̣ ᒷᓭℸ ̣ '
37+
>>> codext.decode("T⍑ᒷ ᑑ⚍╎ᓵꖌ ʖ∷𝙹∴リ ⎓𝙹 ̇/ ⋮⚍ᒲ!¡ᓭ 𝙹⍊ᒷ∷ ℸ ̣ ⍑ᒷ ꖎᔑ⨅|| ↸𝙹⊣.", "galactic")
38+
'The quick brown fox jumps over the lazy dog.'
3739
```
3840

3941
-----

‎src/codext/languages/galactic.py‎

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
"""Galactic Alphabet Codec - Minecraft enchantment language content encoding.
44
55
The Standard Galactic Alphabet was created by Tom Hall for the Commander Keen video games (1990) and later reused as the
6-
language of Minecraft's enchanting table. Each letter is replaced by a Unicode character resembling its glyph ; as
7-
there are no exact Unicode equivalents, several lookalike characters are accepted for most letters when decoding.
6+
language of Minecraft's enchanting table. Each letter is replaced by Unicode characters resembling its glyph, as most
7+
online translators (e.g. LingoJam) do: one character for most letters, but "!¡" for p, "ℸ ̣ " (with a combining dot
8+
below) for t, " ̇/" (with a combining dot above) for x and "||" for y. Other ASCII characters (digits, punctuation,
9+
whitespace) are kept, except "!" and "|" which cannot be told apart from the letters.
10+
11+
When decoding, several lookalike characters are also accepted for most letters, and the shorter forms of p, t, x and y
12+
("!", "ℸ", "̇" and "|"), "!" and "|" being kept as punctuation when the text uses the longer forms. ASCII characters
13+
(e.g. capital letters, which translators leave unchanged) are kept.
814
915
This codec:
1016
- en/decodes strings from str to str
@@ -18,23 +24,93 @@
1824

1925

2026
__examples__ = {
21-
'enc-dec(galactic|minecraft_enchanting_language)': ["test " + MASKS['l']],
22-
'enc(galactic-alphabet|minecraft)': {'Bad test#': None},
27+
'enc(galactic|minecraft_enchanting_language)': {
28+
'this is a test': "ℸ ̣ ⍑╎ᓭ ╎ᓭ ᔑ ℸ ̣ ᒷᓭℸ ̣ ",
29+
'The quick brown fox jumps over the lazy dog.': "ℸ ̣ ⍑ᒷ ᑑ⚍╎ᓵꖌ ʖ∷𝙹∴リ ⎓𝙹 ̇/ ⋮⚍ᒲ!¡ᓭ 𝙹⍊ᒷ∷ ℸ ̣ ⍑ᒷ ꖎᔑ⨅|| ↸𝙹⊣.",
30+
},
31+
'enc(galactic-alphabet|minecraft)': {'Bad test!': None, 'été': None},
32+
'dec(galactic)': {
33+
# as written by LingoJam, capital letters being left unchanged
34+
"Sꖎ╎↸ᒷ ℸ ̣ ⍑ᒷ ʖ╎ꖎꖎ ʖᒷℸ ̣ ∴ᒷᒷリ ℸ ̣ ⍑ᒷ ℸ ̣ ∴𝙹 ꖎᒷᔑ⍊ᒷᓭ. T⍑ᒷ ᓭ⍑ᔑꖌ|| ʖᔑ∷リ ⎓ᒷꖎꖎ ∴╎ℸ ̣ ⍑ ᔑ ꖎ𝙹⚍↸ ᓵ∷ᔑᓭ⍑.":
35+
"Slide the bill between the two leaves. The shaky barn fell with a loud crash.",
36+
'Mᒷᒷℸ ̣ ᒲᒷ ᔑℸ ̣ ℸ ̣ ⍑ᒷ ᒲᔑ!¡, ℸ ̣ ⍑ᒷ ꖌᒷ|| ᔑリ↸ ᔑ ℸ ̣ 𝙹∷ᓵ⍑!': "Meet me at the map, the key and a torch!",
37+
# shorter forms and lookalikes
38+
'ℸ̣⍑ᒷ ᔑ ̇/∷ᔑ||': "the axray",
39+
'!ᔑ|': "pay",
40+
'ᔑ\u0307/ᒷ ᒷ\u0307': "axe ex",
41+
'ℸ₸╎߆ ╎߆ ᒋ ℸᒷ߆ℸ': "this is a test",
42+
'⍑╎ ̷⠀ᔑ': "hix a",
43+
'ᔑ€': None,
44+
},
45+
'enc-dec(galactic)': ["test " + MASKS['l'], "the axe, the text and the toy: 42 apples", "t", "x", "tx yt"],
2346
}
2447

2548

26-
# source: https://shapecatcher.com
49+
# letters as most online translators write them
2750
ENCMAP = {
28-
'a': ["ᒋ", "ᔑ"], 'b': ["⦣", "ゝ", "ʖ"], 'c': ["ì", "ᓵ"], 'd': "↸", 'e': ["ᒷ", "Ŀ"], 'f': ["𝌁", "⎓"],
29-
'g': ["𐌝", "┤", "⫞", "⊣"], 'h': ["₸", "⍑", "╤"], 'i': "╎", 'j': ["⫶", "⁝", "ⵗ", "⋮"], 'k': "ꖌ", 'l': "ꖎ",
30-
'm': ["ᒲ", "⟓"], 'n': ["ソ", "リ"], 'o': ["⁊", "フ", "ㇷ", "𝙹"], 'p': ["ⅱ", "ij", "‼", "!"],
31-
'q': ["ᑑ", "⊐", "コ"], 'r': ["⸬", "∷", "⛚"], 's': ["߆", "𝈿", "ꝇ", "ᓭ"], 't': ["ℸ", "ヿ", "⅂", "Ꞁ"],
32-
'u': ["⚍", "⍨"], 'v': ["𝍦", "⍊", "╧"], 'w': ["∴", "⸫", "⛬"], 'x': ["ꜘ", "╱", " ̷", "⟋"],
33-
'y': ["║", "‖", "∥", "ǁ", "𝄁", "|"], 'z': ["ᑎ", "⋂", "∩", "⨅", "⛫"],
34-
' ': [" ", "⠀"],
51+
'a': "ᔑ", 'b': "ʖ", 'c': "ᓵ", 'd': "↸", 'e': "ᒷ", 'f': "⎓", 'g': "⊣", 'h': "⍑", 'i': "╎", 'j': "⋮", 'k': "ꖌ",
52+
'l': "ꖎ", 'm': "ᒲ", 'n': "リ", 'o': "𝙹", 'p': "!¡", 'q': "ᑑ", 'r': "∷", 's': "ᓭ", 't': "ℸ ̣ ", 'u': "⚍", 'v': "⍊",
53+
'w': "∴", 'x': " ̇/", 'y': "||", 'z': "⨅",
54+
}
55+
# one-character lookalikes accepted when decoding (source: https://shapecatcher.com)
56+
LOOKALIKES = {
57+
'a': "ᒋᔑ", 'b': "⦣ゝʖ", 'c': "ìᓵ", 'd': "↸", 'e': "ᒷĿ", 'f': "𝌁⎓", 'g': "𐌝┤⫞⊣", 'h': "₸⍑╤", 'i': "╎",
58+
'j': "⫶⁝ⵗ⋮", 'k': "ꖌ", 'l': "ꖎ", 'm': "ᒲ⟓", 'n': "ソリ", 'o': "⁊フㇷ𝙹", 'p': "ⅱij‼", 'q': "ᑑ⊐コ", 'r': "⸬∷⛚",
59+
's': "߆𝈿ꝇᓭ", 't': "ℸヿ⅂Ꞁ", 'u': "⚍⍨", 'v': "𝍦⍊╧", 'w': "∴⸫⛬", 'x': "ꜘ╱⟋", 'y': "║‖∥ǁ𝄁", 'z': "ᑎ⋂∩⨅⛫",
3560
}
61+
DECMAP = {c: l for l, cs in LOOKALIKES.items() for c in cs}
62+
DOT_BELOW, DOT_ABOVE, LONG_SOLIDUS = "̣", "̇", "̷"
63+
64+
65+
def galactic_encode(text, errors="strict"):
66+
_h, r = handle_error("galactic", errors), ""
67+
for i, c in enumerate(ensure_str(text)):
68+
l = c.lower()
69+
if l in ENCMAP:
70+
r += ENCMAP[l]
71+
elif c.isascii() and c not in "!|" and not c.isalpha():
72+
r += c
73+
else:
74+
r += _h(c, i)
75+
return r, len(text)
76+
3677

78+
def galactic_decode(text, errors="strict"):
79+
_h, t, r, i = handle_error("galactic", errors, decode=True), ensure_str(text), "", 0
80+
# when the longer forms of p, t, x or y are used, a lone "!" or "|" is punctuation
81+
long_forms = any(f in t for f in ["!¡", DOT_BELOW, DOT_ABOVE + "/", "||"])
82+
while i < len(t):
83+
c = t[i]
84+
if c == "ℸ":
85+
# "ℸ̣", "ℸ ̣" and "ℸ ̣ " are t, the space following the dot belonging to the letter
86+
if t[i+1:i+2] == DOT_BELOW:
87+
i += 1
88+
elif t[i+1:i+3] == " " + DOT_BELOW:
89+
i += 3 if t[i+3:i+4] == " " else 2
90+
r += "t"
91+
elif c == " " and t[i+1:i+3] == DOT_ABOVE + "/":
92+
r, i = r + "x", i + 2
93+
elif c == " " and t[i+1:i+2] == LONG_SOLIDUS:
94+
r, i = r + "x", i + 1
95+
elif c == DOT_ABOVE:
96+
r, i = r + "x", i + (t[i+1:i+2] == "/")
97+
elif c == "!" and t[i+1:i+2] == "¡":
98+
r, i = r + "p", i + 1
99+
elif c == "|" and t[i+1:i+2] == "|":
100+
r, i = r + "y", i + 1
101+
elif c in "!|":
102+
r += c if long_forms else "py"[c == "|"]
103+
elif c in DECMAP:
104+
r += DECMAP[c]
105+
elif c == "⠀":
106+
r += " "
107+
elif c.isascii():
108+
r += c
109+
else:
110+
r += _h(c, i)
111+
i += 1
112+
return r, len(text)
37113

38-
add_map("galactic", ENCMAP, ignore_case="encode", printables_rate=0.,
39-
pattern=r"^(?:galactic(?:[-_]alphabet)?|minecraft(?:[-_](?:enchantment|enchanting[-_]language))?)$")
40114

115+
add("galactic", galactic_encode, galactic_decode, printables_rate=0.,
116+
pattern=r"^(?:galactic(?:[-_]alphabet)?|minecraft(?:[-_](?:enchantment|enchanting[-_]language))?)$")

0 commit comments

Comments
 (0)