Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 5.2 (design, piglatin.py). Before attempting to code this problem, crea

ID: 3705645 • Letter: E

Question

Exercise 5.2 (design, piglatin.py). Before attempting to code this problem, create a file design that contains some analysis of how you think the problem will be solved. Examples include but are not limited to: a flowchart of events in the program, pseudocode, or a step-by-step process written in plain English. If you choose to scan your design, please make sure that it is legible. Write a program that translates a file from English to pig latin. The rules for pig latin are as follows: For a word that begins with consonants, the initial consonant or consonant cluster is moved to the end of the word and "ay" is added as a suffix: "happy""appyhay ."glove""oveglay For words that begin with vowels, you add "way" to the end of the word: ."egg" "eggway" "inbox""inboxway" For your program, you must write a function that takes in one individual word and returns the translation to pig latin. Write another function that takes a string, which may be sentences (may contain the characters "a-zA-Z0" and space), and returns the translation of the sentence to pig latin. St "elloHay owhay areway ouyay The user must be able to specify the filename for the file to be translated and the filename that the progranm rip out any punctuation. For example, "Hello, how are ould translate into am should write to. For example: CSE/IT 107L Lab 10: Review and Markov Chains $ cat test.txt 2 Hello, how are you? $ python3 piglatin.py Enter English filenane >>> test.txt s Enter filename to write to >>> test piglatin.txt 6 Done cat test piglatin.txt s elloHay owhay arevay ouyay

Explanation / Answer

Design document :

Before approaching the problem to solve we need to understand what task we have to perfome then according to that we will proceed as it was asked to to add 'ay' at the end of words which started with consonent. and add 'way' at the end of words which started with vowels. So first we need to find out the first letter of the word is consonet ot vowel if its vowel then directly at the end add 'way'. If its consonent then we need to find out first occurance of vowel in the word and till there all starting part of word will be added at the end of word and 'ay' will also be added in this way we can write the program for this problem. And to process the input from file we need to remove the punctuations from the sentences for that seperatly we need to write a function.

CODE for piglatin.py

def main():
        # sentence = input('Type sentence and press ENTER: ')
        file1 = open("test.txt","r")
        sentence = file1.readline()
        sentence = sentence.split()
        for k in range(len(sentence)):
                i = sentence[k]
                if i[0] in ['a', 'e', 'i', 'o', 'u']:
                        sentence[k] = punctuation(sentence[k])
                        sentence[k] = i+'way'
                else:
                        sentence[k] = convert(punctuation(sentence[k]))
        return ' '.join(sentence)
        file1.close()


def convert(s):
            beginning = ""
            index = 0;
            for char in s:
               if char in ('a','e','i','o','u'):
                   end = str(s[index:])
                   break
               else:
                   beginning = beginning + char
               index = index + 1
            return str(end) + beginning + "ay"


# define punctuation
def punctuation(s):
                punctuations = '''!()-[]{};:'",<>./?@#$%^&*_~'''
                # remove punctuation from the string
                no_punct = ""
                for char in s:
                   if char not in punctuations:
                       no_punct = no_punct + char
                # display the unpunctuated string
                return(no_punct)

if __name__ == "__main__":
        x = main()
        file2 = open("test_piglatin.txt","w")
        file2.write(x)
        file2.close()
        print(x)