|
3 | 3 | """Galactic Alphabet Codec - Minecraft enchantment language content encoding. |
4 | 4 |
|
5 | 5 | 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. |
8 | 14 |
|
9 | 15 | This codec: |
10 | 16 | - en/decodes strings from str to str |
|
18 | 24 |
|
19 | 25 |
|
20 | 26 | __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"], |
23 | 46 | } |
24 | 47 |
|
25 | 48 |
|
26 | | -# source: https://shapecatcher.com |
| 49 | +# letters as most online translators write them |
27 | 50 | 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': "ᑎ⋂∩⨅⛫", |
35 | 60 | } |
| 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 | + |
36 | 77 |
|
| 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) |
37 | 113 |
|
38 | | -add_map("galactic", ENCMAP, ignore_case="encode", printables_rate=0., |
39 | | - pattern=r"^(?:galactic(?:[-_]alphabet)?|minecraft(?:[-_](?:enchantment|enchanting[-_]language))?)$") |
40 | 114 |
|
| 115 | +add("galactic", galactic_encode, galactic_decode, printables_rate=0., |
| 116 | + pattern=r"^(?:galactic(?:[-_]alphabet)?|minecraft(?:[-_](?:enchantment|enchanting[-_]language))?)$") |
0 commit comments