Need help to create pig latin translator in Python ‘Pig Latin’ Translation rules
ID: 664058 • Letter: N
Question
Need help to create pig latin translator in Python
‘Pig Latin’ Translation rules
1. If a word has no letters, don't translate it.
2. All punctuation should be preserved.
3. If the word begins with a capital letter, then the translated word should too.
4. Separate each word into two parts. The first part is called the prefix and extends from the beginning of the word up to, but not including, the first vowel. (The letter y will be considered a vowel for this.) The Rest of the word is called the stem.
5. The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters ‘ay’ to the end. For example, sandwich is composed of s + andwich which would translate to andwichsay.
6. If the word contains no consonants or starts with a vowel, let the prefix be empty and the word be the stem. The word ending should be yay instead of merely ay. For example, I would be Iyay and understood would be understoodyay.
Phase 1
Your first task is to produce a function called pl_prefix that takes one argument, a string containing a word, and returns the portion of the word up to, but not including the first vowel. For example, if you sent 'banana' to your function, it should return 'b'. Sending 'Sibley' should return 'S', 'stream' should return 'str', and 'break' should return 'br'. Test your function with other words and print out the results.
Phase 2
Using what you learned from Phase 1, write a function called pl_reverse that takes a single word as an argument and returns the word with the prefix and stem reversed.
Phase 3
Using what you learned from Phase 2, write a function called pl_translate that properly handles adding the ay and yay word endings, capital letters, and punctuation.
Phase 4
Using the function from Phase 3, write a function called pl_file that takes a string with an input file name and a string with an output file name and translates all the words in the input file according to the rules, then outputs this into the output file. Catch exceptions properly for files not existing, etc.
Phase 5
Have your program give the user a listing of all the text files (*.txt) in the current directory (cwd). Allow the user to specify which file they want to translate. Have your program save the output file in a directory called “translations” inside the current directory. (You can assume that directory exists, which means you may need to create it manually to test your code) Save the output file as [input file name] (pig).txt (So if the user chose to translate novel.txt you would save it as <cwd>/translations/novel(pig).txt).
Phase 6
Print the current day and time at the top of the output file. Print “Thank you for using the Pig Latin Translator” at the end of the output file.
Explanation / Answer
This question has lot of subparts. Can you please post 2 more questions.
VOWELS = ('a', 'e', 'i', 'o', 'u')
def convert_word(word):
first_letter = word[0]
if first_letter in VOWELS: # if word starts with a vowel...
return word + "hay" # then keep it as it is and add hay to the end
else:
return word[1:] + word[0] + "ay" # like the lab mentions, word[1:]
# returns the word except word[0]
def convert_sentence(sentence):
list_of_words = sentence.split(' ')
new_sentence = "" # we'll keep concatenating words to this...
for word in list_of_words:
new_sentence = new_sentence + convert_word(word) # ...like this
new_sentence = new_sentence + " " # but don't forget the space!
return new_sentence
text = raw_input() # nothing in the parentheses, because there's nothing else
# extra to tell the user before he is allowed to type
print
print convert_sentence(text)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.