|
| 1 | +""" |
| 2 | +The task is to translate text from English to Pig Latin. |
| 3 | +The translation is defined using four rules, which look at the pattern of vowels |
| 4 | +and consonants at the beginning of a word. These rules look at each word's use |
| 5 | +of vowels and consonants: |
| 6 | +
|
| 7 | +vowels: the letters a, e, i, o, and u |
| 8 | +consonants: the other 21 letters of the English alphabet |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +def translate(text: str) -> str: |
| 13 | + """ |
| 14 | + Translate English text to Pig Latin. |
| 15 | +
|
| 16 | + :param text: The English text to translate |
| 17 | + :return: The text translated to Pig Latin |
| 18 | + """ |
| 19 | + words: list[str] = text.split(" ") |
| 20 | + return " ".join(process_word(word) for word in words) |
| 21 | + |
| 22 | + |
| 23 | +def process_word(text: str) -> str: |
| 24 | + """ |
| 25 | + Process a single word and convert it to Pig Latin using the four translation rules. |
| 26 | +
|
| 27 | + :param text: The English word to convert |
| 28 | + :return: The word converted to Pig Latin |
| 29 | + """ |
| 30 | + if not text: |
| 31 | + return "" |
| 32 | + |
| 33 | + # Rule 1 |
| 34 | + if is_vowel(text[0]) or text[:2] in ("xr", "yt"): |
| 35 | + # If a word begins with a vowel, |
| 36 | + # or starts with "xr" or "yt", |
| 37 | + # add an "ay" sound to the end of the word. |
| 38 | + return f"{text}ay" |
| 39 | + |
| 40 | + # Rule 3 |
| 41 | + if is_rule_3(text): |
| 42 | + # If a word starts with zero or more consonants followed by "qu", first move |
| 43 | + # those consonants (if any) and the "qu" part to the end of the word, and then |
| 44 | + # add an "ay" sound to the end of the word. |
| 45 | + i = text.index("qu") |
| 46 | + return f"{text[i + 2 :]}{text[: i + 2]}ay" |
| 47 | + |
| 48 | + # Rule 4 |
| 49 | + if is_rule_4(text): |
| 50 | + # If a word starts with one or more consonants followed by "y", first move the |
| 51 | + # consonants preceding the "y" to the end of the word, and then add an "ay" sound |
| 52 | + # to the end of the word. |
| 53 | + i = text.index("y") |
| 54 | + return f"{text[i:]}{text[:i]}ay" |
| 55 | + |
| 56 | + # Rule 2 |
| 57 | + if not is_vowel(text[0]): |
| 58 | + # If a word begins with one or more consonants, first move those consonants |
| 59 | + # to the end of the word and then add an "ay" sound to the end of the word. |
| 60 | + i = get_consonant_cluster_length(text) |
| 61 | + return f"{text[i + 1 :]}{text[: i + 1]}ay" |
| 62 | + |
| 63 | + raise ValueError(f"Unhandled word in Pig Latin translation: '{text}'") |
| 64 | + |
| 65 | + |
| 66 | +def is_rule_3(text: str) -> bool: |
| 67 | + """ |
| 68 | + Check if a word starts with zero or more consonants followed by "qu". |
| 69 | +
|
| 70 | + :param text: |
| 71 | + :return: |
| 72 | + """ |
| 73 | + if "qu" in text: |
| 74 | + if text[:2] == "qu": |
| 75 | + return True |
| 76 | + |
| 77 | + for char in text[: text.index("qu")]: |
| 78 | + if is_vowel(char): |
| 79 | + return False |
| 80 | + return True |
| 81 | + return False |
| 82 | + |
| 83 | + |
| 84 | +def is_rule_4(text: str) -> bool: |
| 85 | + """ |
| 86 | + Check if a word starts with one or more consonants followed by "y". |
| 87 | +
|
| 88 | + :param text: |
| 89 | + :return: |
| 90 | + """ |
| 91 | + if "y" in text and text[0] != "y": |
| 92 | + for char in text[: text.index("y")]: |
| 93 | + if is_vowel(char): |
| 94 | + return False |
| 95 | + return True |
| 96 | + return False |
| 97 | + |
| 98 | + |
| 99 | +def is_vowel(char: str) -> bool: |
| 100 | + """ |
| 101 | + Check if a character is a vowel (a, e, i, o, or u). |
| 102 | +
|
| 103 | + :param char: The character to check |
| 104 | + :return: True if the character is a vowel, False otherwise |
| 105 | + """ |
| 106 | + return char in "aeiou" |
| 107 | + |
| 108 | + |
| 109 | +def get_consonant_cluster_length(text: str) -> int: |
| 110 | + """ |
| 111 | + Find the length of the consonant cluster at the beginning of a word. |
| 112 | +
|
| 113 | + :param text: The word to analyze |
| 114 | + :return: The index of the last consonant in the initial consonant cluster |
| 115 | + """ |
| 116 | + i = 0 |
| 117 | + for n, char in enumerate(text): |
| 118 | + if not is_vowel(char): |
| 119 | + i = n |
| 120 | + else: |
| 121 | + break |
| 122 | + |
| 123 | + return i |
0 commit comments