Pig Latin

Tags: short, word

Pig Latin is a word game that transforms English words into a parody of Latin. In Pig Latin, if a word begins with a consonant, the speaker removes this letter and puts it at the end, followed by “ay.” For example, “pig” becomes “igpay” and “latin” becomes “atinlay.” Otherwise, if the word begins with a vowel, the speaker simply adds “yay” to the end of it. For example, “elephant” becomes “elephantyay” and “umbrella” becomes “umbrellayay.”

pig_latin.py
 1"""Pig Latin, by Al Sweigart al@inventwithpython.com
 2Translates English messages into Igpay Atinlay.
 3This code is available at https://nostarch.com/big-book-small-python-programming
 4Tags: short, word"""
 5
 6try:
 7    import pyperclip  # pyperclip copies text to the clipboard.
 8except ImportError:
 9    pass  # If pyperclip is not installed, do nothing. It's no big deal.
10 
11VOWELS = ('a', 'e', 'i', 'o', 'u', 'y')
12
13
14def main():
15    print('''Igpay Atinlay (Pig Latin)
16By Al Sweigart al@inventwithpython.com
17
18Enter your message:''')
19    pigLatin = englishToPigLatin(input('> '))
20
21    # Join all the words back together into a single string:
22    print(pigLatin)
23
24    try:
25        pyperclip.copy(pigLatin)
26        print('(Copied pig latin to clipboard.)')
27    except NameError:
28        pass  # Do nothing if pyperclip wasn't installed.
29
30
31def englishToPigLatin(message):
32    pigLatin = ''  # A string of the pig latin translation.
33    for word in message.split():
34        # Separate the non-letters at the start of this word:
35        prefixNonLetters = ''
36        while len(word) > 0 and not word[0].isalpha():
37            prefixNonLetters += word[0]
38            word = word[1:]
39        if len(word) == 0:
40            pigLatin = pigLatin + prefixNonLetters + ' '
41            continue
42
43        # Separate the non-letters at the end of this word:
44        suffixNonLetters = ''
45        while not word[-1].isalpha():
46            suffixNonLetters = word[-1] + suffixNonLetters
47            word = word[:-1]
48
49        # Remember if the word was in uppercase or titlecase.
50        wasUpper = word.isupper()
51        wasTitle = word.istitle()
52
53        word = word.lower()  # Make the word lowercase for translation.
54
55        # Separate the consonants at the start of this word:
56        prefixConsonants = ''
57        while len(word) > 0 and not word[0] in VOWELS:
58            prefixConsonants += word[0]
59            word = word[1:]
60
61        # Add the pig latin ending to the word:
62        if prefixConsonants != '':
63            word += prefixConsonants + 'ay'
64        else:
65            word += 'yay'
66
67        # Set the word back to uppercase or titlecase:
68        if wasUpper:
69            word = word.upper()
70        if wasTitle:
71            word = word.title()
72
73        # Add the non-letters back to the start or end of the word.
74        pigLatin += prefixNonLetters + word + suffixNonLetters + ' '
75    return pigLatin
76
77
78if __name__ == '__main__':
79    main()

https://inventwithpython.com/bigbookpython/project54.html