|
| 1 | +""" |
| 2 | +Crea una función que sea capaz de transformar Español al lenguaje básico |
| 3 | + del universo Star Wars: el "Aurebesh". |
| 4 | +- Puedes dejar sin transformar los caracteres que no existan en "Aurebesh". |
| 5 | +- También tiene que ser capaz de traducir en sentido contrario. |
| 6 | +
|
| 7 | +¿Lo has conseguido? Nómbrame en twitter.com/mouredev y escríbeme algo en Aurebesh. |
| 8 | +
|
| 9 | +¡Que la fuerza os acompañe! |
| 10 | +""" |
| 11 | + |
| 12 | +class Aurebesh(): |
| 13 | + """ |
| 14 | + Translator between Spanish text and the Aurebesh alphabet |
| 15 | + (the written form of Galactic Basic in the Star Wars universe). |
| 16 | + |
| 17 | + Features: |
| 18 | + - Provides bidirectional translation: |
| 19 | + * from Spanish to Aurebesh (`to_aurebesh`) |
| 20 | + * from Aurebesh to Spanish (`to_espanol`) |
| 21 | + - Unknown characters (not present in Aurebesh) are preserved as-is. |
| 22 | + - Handles the special case "ch" as a single Aurebesh token. |
| 23 | + """ |
| 24 | + |
| 25 | + mapping_aurebesh: dict[str, str] = { |
| 26 | + "a": "Aurek", "b": "Besh", "c": "Cresh", "d": "Dorn", "e": "Enth", |
| 27 | + "f": "Forn", "g": "Grek", "h": "Herf", "i": "Isk", "j": "Jenth", |
| 28 | + "k": "Krill", "l": "Leth", "m": "Mern", "n": "Nern", "o": "Osk", |
| 29 | + "p": "Peth", "q": "Qek", "r": "Resh", "s": "Senth", "t": "Trill", |
| 30 | + "u": "Usk", "v": "Vev", "w": "Wesk", "x": "Xesh", "y": "Yirt", "z": "Zerek", |
| 31 | + "ch": "Cherek", ' ': '' |
| 32 | + } |
| 33 | + |
| 34 | + def to_aurebesh(self, text: str) -> str: |
| 35 | + """ |
| 36 | + Convert a Spanish string into Aurebesh. |
| 37 | +
|
| 38 | + Args: |
| 39 | + text (str): Input text in Spanish. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + str: The corresponding text in Aurebesh. Characters not present in the mapping |
| 43 | + will remain unchanged. |
| 44 | +
|
| 45 | + Notes: |
| 46 | + - Multi-character mappings (e.g., "ch") are handled first. |
| 47 | + - Output tokens are separated by spaces. |
| 48 | + """ |
| 49 | + |
| 50 | + self._validate_language(text) |
| 51 | + text_aurebesh: list[str] = [] |
| 52 | + text = text.lower() |
| 53 | + count: int = 0 |
| 54 | + while count < len(text): |
| 55 | + if count + 1 < len(text) and text[count:count + 2] in self.mapping_aurebesh: |
| 56 | + text_aurebesh.append(self.mapping_aurebesh[text[count:count + 2]]) |
| 57 | + count += 2 |
| 58 | + elif text[count] in self.mapping_aurebesh: |
| 59 | + text_aurebesh.append(self.mapping_aurebesh[text[count]]) |
| 60 | + count += 1 |
| 61 | + else: |
| 62 | + text_aurebesh.append(text[count]) |
| 63 | + count += 1 |
| 64 | + return " ".join(text_aurebesh) |
| 65 | + |
| 66 | + def _validate_language(self, text: str, aurebesh: bool=False) -> None: |
| 67 | + """ |
| 68 | + Validate that the input string is in the expected language format. |
| 69 | +
|
| 70 | + Args: |
| 71 | + text (str): Input text to validate. |
| 72 | + aurebesh (bool): If True, checks that the text only contains valid Aurebesh tokens. |
| 73 | + If False, performs a basic Spanish validation. |
| 74 | +
|
| 75 | + Raises: |
| 76 | + TypeError: If the input is not a string. |
| 77 | + ValueError: If the string is empty or contains invalid tokens for the given language. |
| 78 | + """ |
| 79 | + if not isinstance(text, str): |
| 80 | + raise TypeError("Solo se aceptan cadenas de texto.") |
| 81 | + if not text: |
| 82 | + raise ValueError("El texto no puede estar vacio.") |
| 83 | + |
| 84 | + if aurebesh: |
| 85 | + words = text.split() |
| 86 | + for word in words: |
| 87 | + if word not in self.mapping_aurebesh.values(): |
| 88 | + raise ValueError("El texto debe estar en aurebesh.") |
| 89 | + |
| 90 | + def to_espanol(self, text_aurebesh: str) -> str: |
| 91 | + """ |
| 92 | + Convert an Aurebesh string back into Spanish. |
| 93 | +
|
| 94 | + Args: |
| 95 | + text_aurebesh (str): Input text in Aurebesh, with tokens separated by spaces. |
| 96 | +
|
| 97 | + Returns: |
| 98 | + str: The corresponding text in Spanish. |
| 99 | +
|
| 100 | + Raises: |
| 101 | + ValueError: If the input is not valid Aurebesh. |
| 102 | +
|
| 103 | + Notes: |
| 104 | + - Unknown Aurebesh tokens are replaced with "?". |
| 105 | + - Empty string validation is enforced. |
| 106 | + """ |
| 107 | + self._validate_language(text_aurebesh, aurebesh=True) |
| 108 | + reverse_aurebesh = {v: k for k, v in self.mapping_aurebesh.items()} |
| 109 | + words = text_aurebesh.split(" ") |
| 110 | + text_spanish = [reverse_aurebesh.get(word, "?") for word in words] |
| 111 | + return "".join(text_spanish) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + aurebesh = Aurebesh() |
| 116 | + print(aurebesh.to_aurebesh("Hola mundo!!")) |
| 117 | + print(aurebesh.to_espanol("Herf Osk Leth Aurek Mern Usk Nern Dorn Osk")) |
0 commit comments