Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.1 kB
1
Indexable
Never
# Module that converts piglatin and english 
# Name: Anson Vattakunnel
# Student Number: VTTANS001
# Date: 12 April 2022
vowels = {"a", "e", "i", "o", "u"}

def to_pig_latin(sentence):
    words = sentence.split()
    #Iterates each word through test
    for i in range(len(words)):
        if words[i][0] in vowels:       #If it starts with a vowel then add way to the end
            words[i] = words[i] + "way"
        else:
            for j in range(len(words[i])):  #for loop that checks when the first vowel occurs
                if words[i][j] in vowels:
                    suffix = words[i][:j]
                    words[i] = words[i].replace(words[i][:j], "",1)
                    words[i] = words[i] + "a" + suffix + "ay"
                    break
            if words[i][-2:] != "ay":       #if no letters are vowels then add a and ay
                words[i] = "a" + words[i] + "ay"

    new_sentence = " ".join(words)      #Converts list to string            
    return new_sentence
  
def to_english(sentence):
    words = sentence.split()
    #Iterates each word through test
    for i in range(len(words)):
        if words[i][-3:] == "way":      #If it starts with a vowel then remove way from the end
            words[i] = words[i].replace("way", "")
        else:
            words[i] = words[i].replace(words[i][-2:], "")  #Removes ay at the end of each word
            words[i] = words[i][::-1]           #reverse the word to check first vowel from the back
            for j in range(len(words[i])):      #for loop that checks when the first vowel occurs
                if words[i][j] in vowels:
                    revprefix = words[i][:j]    #Word to be added
                    words[i] = words[i].replace(words[i][:j+1], "",1)   #Removes all the letters before the vowel
                    words[i] = words[i] + revprefix
                    words[i] = words[i][::-1]   #Reverses the word back to it's original way
                    break
    new_sentence = " ".join(words)              
    return new_sentence