User Tools

Site Tools


wiki:piglatin

English to Pig-Latin Translator

DISCLAIMER - FOR ENTERTAINMENT PURPOSES ONLY - NOT AN AUTHORIZED DOD TRANSLATION APPLICATION

# A Pig-Latin Translator (A.K.A.: "Fun with Functions")

def threeConsonants(englishWord):
    '''
    DOCSTRING: This function returns True only if the first three characters are consonants
    INPUT: The input is a word from the English phrase
    OUTPUT: The output is True or False
    '''
    return (englishWord[0].lower() in "bcdfghjklmnpqrstvwxyz") and (englishWord[1].lower() in "bcdfghjklmnpqrstvwxyz") and (englishWord[2].lower() in "bcdfghjklmnpqrstvwxyz")

def twoConsonants(englishWord):
    '''
    DOCSTRING: This function returns True only if the first two characters are consonants
    INPUT: The input is a word from the English phrase
    OUTPUT: The output is True or False
    '''
    return (englishWord[0].lower() in "bcdfghjklmnpqrstvwxyz") and (englishWord[1].lower() in "bcdfghjklmnpqrstvwxyz")

def oneConsonant(englishWord):
    '''
    DOCSTRING: This function returns True only if the first characters is a consonant
    INPUT: The input is a word from the English phrase
    OUTPUT: The output is True or False
    '''
    return englishWord[0].lower() in "bcdfghjklmnpqrstvwxyz"

def vowel(englishWord):
    '''
    DOCSTRING: This function returns True only if the first character is a vowel
    INPUT: The input is a word from the English phrase
    OUTPUT: The output is True or False
    '''
    return englishWord[0].lower() in "aeiou"

def translate(englishWord, letters=0):
    '''
    DOCSTRING: This function creates the translated word
    INPUT: The input is the English word to be translated and the number of letters to slice from the front of the word
    OUTPUT: The output is the new piglatin word
    '''
    pigLatinWord = []
    pigLatinWord.append(englishWord[letters:]+englishWord[0:letters:] + "ay")
    return pigLatinWord

print("English / Pig-Latin Translator")
english = "not_empty"

while len(english) > 0:
    english = input("Enter a phrase to translate into Pig-Latin or press enter to quit: \n").split()
    translation = []

# This section checks each word in the English phrase to see if the first 3 characters are consonants
# and to see if the first character is a vowel.  If the first character is neither, it doesn't translate the word.
    for word in english:       
        if len(word)>3 and threeConsonants(word):
            translation.append(translate(word, 3))
        elif len(word)>2 and twoConsonants(word):
            translation.append(translate(word, 2))
        elif len(word)>1 and oneConsonant(word):
            translation.append(translate(word, 1))
        elif vowel(word):
            translation.append(translate(word))
        else:
            translation.append(word) # Don't translate anything if the first character is not a letter, just append it

# Each word is a list of characters, so the translation phrase is a list of lists
# This section iterates through each character, word by word, to create a clean string to print
    pigLatin = ""
    for word in translation:
        for letter in word:
            pigLatin += letter
        pigLatin += " "

    print(pigLatin)
    print()
print("Thank you for playing.")
wiki/piglatin.txt · Last modified: 2019/09/01 19:52 by paul