From 30f1d4fdc3d97987d6b3747aa32c71241bede1ba Mon Sep 17 00:00:00 2001 From: Al Sweigart Date: Wed, 11 Sep 2019 23:30:19 -0700 Subject: [PATCH] Refactored the "english translation" programs to be more consistent with each other. --- src/leetspeak.py | 8 +-- src/piglatin.py | 122 +++++++++++++++++++++++++++------------------- src/spongetext.py | 60 +++++++++++++---------- 3 files changed, 109 insertions(+), 81 deletions(-) diff --git a/src/leetspeak.py b/src/leetspeak.py index afbb1c8..4c385f9 100644 --- a/src/leetspeak.py +++ b/src/leetspeak.py @@ -24,10 +24,10 @@ def englishToLeetspeak(message): return ''.join(leet) def main(): - print('L3375P34]< (leetspeek)') - print('By Al Sweigart al@inventwithpython.com') - print() - print('Enter your leet message:') + print('''L3375P34]< (leetspeek) +By Al Sweigart al@inventwithpython.com + +Enter your leet message:''') english = input() print() leetspeak = englishToLeetspeak(english) diff --git a/src/piglatin.py b/src/piglatin.py index 9db20dd..379ec60 100644 --- a/src/piglatin.py +++ b/src/piglatin.py @@ -1,56 +1,76 @@ # Pig Latin by Al Sweigart, al@inventwithpython.com +# Translates English messages into Igpay Atinlay. -print('IGPAY ATINLAY (pig latin)') -print('By Al Sweigart al@inventwithpython.com') -print() - -message = input('Enter your message> ') +try: + import pyperclip +except ImportError: + pass # It's not a big deal if pyperclip is not installed. VOWELS = ('a', 'e', 'i', 'o', 'u', 'y') -pigLatin = [] # A list of the words in pig latin. -for word in message.split(): - # Separate the non-letters at the start of this word: - prefixNonLetters = '' - while len(word) > 0 and not word[0].isalpha(): - prefixNonLetters += word[0] - word = word[1:] - if len(word) == 0: - pigLatin.append(prefixNonLetters) - continue - - # Separate the non-letters at the end of this word: - suffixNonLetters = '' - while not word[-1].isalpha(): - suffixNonLetters += word[-1] - word = word[:-1] - - # Remember if the word was in uppercase or titlecase. - wasUpper = word.isupper() - wasTitle = word.istitle() - - word = word.lower() # Make the word lowercase for translation. - - # Separate the consonants at the start of this word: - prefixConsonants = '' - while len(word) > 0 and not word[0] in VOWELS: - prefixConsonants += word[0] - word = word[1:] - - # Add the pig latin ending to the word: - if prefixConsonants != '': - word += prefixConsonants + 'ay' - else: - word += 'yay' - - # Set the word back to uppercase or titlecase: - if wasUpper: - word = word.upper() - if wasTitle: - word = word.title() - - # Add the non-letters back to the start or end of the word. - pigLatin.append(prefixNonLetters + word + suffixNonLetters) - -# Join all the words back together into a single string: -print(' '.join(pigLatin)) +def englishToPigLatin(message): + pigLatin = '' # A string of the pig latin translation. + for word in message.split(): + # Separate the non-letters at the start of this word: + prefixNonLetters = '' + while len(word) > 0 and not word[0].isalpha(): + prefixNonLetters += word[0] + word = word[1:] + if len(word) == 0: + pigLatin = pigLatin + prefixNonLetters + ' ' + continue + + # Separate the non-letters at the end of this word: + suffixNonLetters = '' + while not word[-1].isalpha(): + suffixNonLetters += word[-1] + word = word[:-1] + + # Remember if the word was in uppercase or titlecase. + wasUpper = word.isupper() + wasTitle = word.istitle() + + word = word.lower() # Make the word lowercase for translation. + + # Separate the consonants at the start of this word: + prefixConsonants = '' + while len(word) > 0 and not word[0] in VOWELS: + prefixConsonants += word[0] + word = word[1:] + + # Add the pig latin ending to the word: + if prefixConsonants != '': + word += prefixConsonants + 'ay' + else: + word += 'yay' + + # Set the word back to uppercase or titlecase: + if wasUpper: + word = word.upper() + if wasTitle: + word = word.title() + + # Add the non-letters back to the start or end of the word. + pigLatin = pigLatin + prefixNonLetters + word + suffixNonLetters + ' ' + return pigLatin + + +def main(): + print('''IGPAY ATINLAY (Pig Latin) +By Al Sweigart al@inventwithpython.com + +Enter your message:''') + pigLatin = englishToPigLatin(input()) + + # Join all the words back together into a single string: + print(pigLatin) + + try: + pyperclip.copy(pigLatin) + print('(Copied pig latin to clipboard.)') + except NameError: + pass # Do nothing if pyperclip wasn't installed. + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/src/spongetext.py b/src/spongetext.py index c9a6d8c..217eee2 100644 --- a/src/spongetext.py +++ b/src/spongetext.py @@ -1,4 +1,5 @@ # sPoNgEtExT, bY aL sWeIGaRt Al@iNvEnTwItHpYtHoN.cOm +# Translates English messages into sPOnGEtExT. import random @@ -7,35 +8,42 @@ except ImportError: pass # It's not a big deal if pyperclip is not installed. -print('sPoNgEtExT') -print('bY aL sWeIGaRt Al@iNvEnTwItHpYtHoN.cOm') -print() -print('eNtEr YoUr MeSsAgE:') -message = input() -print() +def englishToSpongetext(message): + spongetext = '' + useUpper = False -spongetext = '' -useUpper = False + for character in message: + if not character.isalpha(): + spongetext += character + continue -for character in message: - if not character.isalpha(): - spongetext += character - continue + if useUpper: + spongetext += character.upper() + else: + spongetext += character.lower() - if useUpper: - spongetext += character.upper() - else: - spongetext += character.lower() + useUpper = not useUpper # Flip the case. - useUpper = not useUpper # Flip the case. + # Randomly flip the case again in 1 in 10 characters. + if random.randint(1, 10) == 1: + useUpper = not useUpper + return spongetext - # Randomly flip the case again in 1 in 10 characters. - if random.randint(1, 10) == 1: - useUpper = not useUpper -print(spongetext) -try: - pyperclip.copy(spongetext) - print('(cOpIed SpOnGeTexT to ClIpbOaRd.)') -except: - pass # Do nothing if pyperclip wasn't installed. +def main(): + print('''sPoNgEtExT +bY aL sWeIGaRt Al@iNvEnTwItHpYtHoN.cOm + +eNtEr YoUr MeSsAgE:''') + spongetext = englishToSpongetext(input()) + print() + print(spongetext) + + try: + pyperclip.copy(spongetext) + print('(cOpIed SpOnGeTexT to ClIpbOaRd.)') + except: + pass # Do nothing if pyperclip wasn't installed. + +if __name__ == '__main__': + main() \ No newline at end of file