Last active
February 12, 2021 10:15
-
-
Save DenverCoder1/c65537cbe9e7e9138b86a5e82dfbec37 to your computer and use it in GitHub Desktop.
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
# Pig Latin Translator in Python | |
# Jonah Lawrence | |
# January 2020 | |
import re | |
def translate(message: str) -> str: | |
"""translate a message to pig latin""" | |
# common English digraphs | |
digraphs = {"bh", "bl", "br", "bw", "ch", "chr", "cl", "cr", "cz", | |
"dh", "dr", "dw", "fl", "fr", "fw", "gh", "gl", "gn", | |
"gr", "kh", "kl", "kn", "kr", "kw", "lh", "ll", "ph", | |
"pl", "pr", "ps", "pw", "qu", "rh", "sc", "sch", "schm", | |
"schw", "scr", "sh", "shl", "shm", "shn", "shqu", "shr", | |
"shw", "sk", "sl", "sm", "sn", "sp", "sph", "spr", | |
"squ", "st", "str", "sw", "szch", "tch", "th", "thr", | |
"tr", "ts", "tw", "vh", "vh", "vlwh", "wr", "xh", "zh"} | |
def translate_word(word: str) -> str: | |
"""translate an individual word to pig latin""" | |
# starts with a vowel | |
if word[0].lower() in {"a", "e", "i", "o", "u"}: | |
return word + "way" | |
# starts with a digraph, checking lengths largest to smallest | |
digraph_length = max(len(x) for x in digraphs) | |
while digraph_length >= 2: | |
if word[:digraph_length].lower() in digraphs: | |
# move digraph to the end | |
return word[digraph_length:] + word[:digraph_length] + "ay" | |
# otherwise, just shift the first letter | |
return word[1:] + word[0] + "ay" | |
# split sentence at spaces | |
sentence = message.split() | |
num_words = len(sentence) | |
# loop through words | |
for i in range(num_words): | |
# split non-alpha characters on ends into separate groups | |
nonAlphaOnEnds = r"^([^a-zA-Z]*)([a-zA-Z]+)([^a-zA-Z]*)$" | |
match = re.match(nonAlphaOnEnds, sentence[i]) | |
# if alpha characters were found | |
if match is not None: | |
# translate the alpha character section | |
translation = translate_word(match[2]) | |
# put back the non-alpha characters on the ends | |
sentence[i] = match[1] + translation + match[3] | |
# insert the spaces back into the text | |
return " ".join(sentence) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment