Last active
November 3, 2022 13:08
-
-
Save harkabeeparolus/d7056c3704ee194da65d66f05716154a to your computer and use it in GitHub Desktop.
Decode the Swedish "Robber Language" into plain text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
"""Decode or encode the Swedish Robber Language. | |
Proof of concept by traal (Fredrik Mellström). | |
""" | |
import argparse | |
import re | |
import sys | |
DEFAULT_ROBBER_TEXT = "Rorövovarorsospoproråkoketot äror cocoololtot!" | |
def main(): | |
"""Run the main script.""" | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument("-e", "--encode", action="store_true") | |
parser.add_argument("text", nargs="*") | |
args = parser.parse_args() | |
description = "😊 -----> 🤠" if args.encode else "🤠 -----> 😊" | |
for text in args.text or [DEFAULT_ROBBER_TEXT]: | |
print(f"\n{text}\n{description}") | |
print(robber(text) if args.encode else derobber(text)) | |
CONSONANTS = "bcdfghjklmnpqrstvwxz" | |
ROBBER_RE = re.compile( | |
rf"(?: ( [{CONSONANTS}] ) o \1 ) | (.)", | |
re.VERBOSE | re.IGNORECASE | re.DOTALL, | |
) | |
def robber(plain_text: str) -> str: | |
"""Generate Robber Language.""" | |
return "".join((f"{x}o{x}" if x in CONSONANTS else x) for x in plain_text) | |
def derobber(robber_text: str) -> str: | |
"""Parse the Swedish Robber Language.""" | |
result = [] | |
for match in ROBBER_RE.findall(robber_text): | |
if match: | |
one, two = match | |
result.append(one or two) | |
return "".join(result) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment