Last active
August 16, 2023 15:54
-
-
Save larvanitis/88b59769790241d942282e2fe42c254e to your computer and use it in GitHub Desktop.
Covert miss-typed EN <-> EL text | tags: keyboard, layout
This file contains hidden or 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
def make_uppercase_variants(dictionary: dict[str, str]): | |
upper = {} | |
for key, value in dictionary.items(): | |
upper[key.upper()] = value.upper() | |
return upper | |
def reverse_dictionary(dictionary: dict[str, str]): | |
return {value: key for key, value in dictionary.items()} | |
def create_replacements() -> dict[str, str]: | |
diaeresis_tonos = { | |
':;i': 'ΐ', | |
':;y': 'ΰ', | |
';:i': 'ΐ', | |
';:y': 'ΰ', | |
} | |
diaeresis = { | |
':i': 'ϊ', | |
':y': 'ϋ', | |
} | |
tonos = { | |
';a': 'ά', | |
';e': 'έ', | |
';h': 'ή', | |
';i': 'ί', | |
';o': 'ό', | |
';v': 'ώ', | |
';y': 'ύ', | |
} | |
letters = { | |
'a': 'α', | |
'b': 'β', | |
'c': 'ψ', | |
'd': 'δ', | |
'e': 'ε', | |
'f': 'φ', | |
'g': 'γ', | |
'h': 'η', | |
'i': 'ι', | |
'j': 'ξ', | |
'k': 'κ', | |
'l': 'λ', | |
'm': 'μ', | |
'n': 'ν', | |
'o': 'ο', | |
'p': 'π', | |
'r': 'ρ', | |
's': 'σ', | |
't': 'τ', | |
'u': 'θ', | |
'v': 'ω', | |
'w': 'ς', | |
'x': 'χ', | |
'y': 'υ', | |
'z': 'ζ', | |
} | |
q = { | |
'q': ';', | |
'Q': ':', | |
} | |
special = { | |
'¨': '::', | |
'´': ';;', | |
} | |
protocols = { | |
'ηττπ/': 'http://', | |
'φιλε/': 'file://', | |
'σση/': 'ssh://', | |
'φιση/': 'fish://', | |
'σψπ/': 'scp://', | |
} | |
return { | |
**diaeresis_tonos, | |
**make_uppercase_variants(diaeresis_tonos), | |
**reverse_dictionary(diaeresis_tonos), | |
**reverse_dictionary(make_uppercase_variants(diaeresis_tonos)), | |
**diaeresis, | |
**make_uppercase_variants(diaeresis), | |
**reverse_dictionary(diaeresis), | |
**reverse_dictionary(make_uppercase_variants(diaeresis)), | |
**tonos, | |
**make_uppercase_variants(tonos), | |
**reverse_dictionary(tonos), | |
**reverse_dictionary(make_uppercase_variants(tonos)), | |
**letters, | |
**make_uppercase_variants(letters), | |
**reverse_dictionary(letters), | |
**reverse_dictionary(make_uppercase_variants(letters)), | |
**q, | |
**reverse_dictionary(q), | |
**special, | |
**protocols, | |
} | |
def convert(text: str) -> str: | |
replacements = create_replacements() | |
max_key_length = max(len(key) for key in replacements.keys()) | |
output_replacements = [] | |
current_index = 0 | |
while not current_index >= len(text): | |
for key_length in range(max_key_length, 0, -1): | |
end_index = current_index + key_length | |
if end_index > len(text): | |
continue | |
key = text[current_index:end_index] | |
print(key) | |
replacement = replacements.get(key) | |
if replacement: | |
output_replacements.append(replacement) | |
current_index += key_length | |
break | |
if key_length == 1: | |
output_replacements.append(key) | |
current_index += key_length | |
return ''.join(output_replacements) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment