Goal: So, my goal is to write a code to translate a word to pig latin and izzle languages. And then write another code "apply_language_game" which takes input some data and another input which is the language they want to transfer in (either pig latin or izzle)
Pig Latin: Pig Latin is one of the most famous English language games. You can convert an English word into Pig Latin by moving all the consonants up to the first vowel to the end of the word and then appending "ay". In ‘hello’, h is before first vowel so it will be pushed at the end of word ‘elloh’ and then appending ‘ay’ gives: ‘Ellohay’ In Chris: Chr are before first vowel so they will be pushed at the end of sentence. E.g. "hello, Chris how are you?" becomes "Ellohay, ischray owhay areay ouyay?"
Izzle: You can convert an English word into -izzle speak by replacing the last vowel and all its following consonants with "-izzle." E.g. "Merry Christmas" becomes "Mizzle Christmizzle." If a word only has one vowel like 'and' and 'a' or it doesn't contain a cardinal vowel, [a, e, i, o, u], like the word 'my' you can either replace the entire word with 'izzle' or append '-izzle' to the end of the word.
Codes: So, I have written codes to convert a word into pig latin and izzle. Here is pig_latin one:
def pig_latin(word):
word = list(word)
vowels = ['a', 'e', 'i', 'o', 'u']
vowel = [x for x in word if x in vowels]
vowel1 = vowel[0]
for i in word:
if word.index(i) < word.index(vowel1):
ind = word.index(i)
word.pop(ind)
word.append(i)
word.append("ay")
return "".join(word)
Here is izzle one:
def izzle(word):
word = list(word)
vowels = ['a', 'e', 'i', 'o', 'u']
vowel = [x for x in word if x in vowels]
if vowel == [] or len("".join(vowel)) == 1:
return "".join(word) + "izzle"
else:
vowel1 = vowel[-1]
for i in word:
if word.index(i) >= word.index(vowel1):
ind = word.index(i)
word.pop(ind)
word.append("izzle")
return "".join(word)
And here is the code for apply_language_game:
def apply_language_game(text, language_game):
text = text.split()
if language_game == pig_latin:
lst = [pig_latin(x) for x in text]
return " ".join(lst)
if language_game == izzle:
lst = [izzle(x) for x in text]
return " ".join(lst)
Now if I want to test my code by converting a file containing some data into pig latin and izzle. The file contains: "Fruits and Vegetables and Vegetables on a Budget and Vegetables at a Store and Vegetables to Clean Fruit and Vegetables"
The code works fine for pig latin but it doesn't work for izzle. Following Error is shown:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.4\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.4\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Dell/PycharmProjects/pythonHW3/word_analyzer.py", line 84, in <module>
print(apply_language_game(text, izzle))
File "C:/Users/Dell/PycharmProjects/pythonHW3/word_analyzer.py", line 67, in apply_language_game
lst = [izzle(x) for x in text]
File "C:/Users/Dell/PycharmProjects/pythonHW3/word_analyzer.py", line 67, in <listcomp>
lst = [izzle(x) for x in text]
File "C:/Users/Dell/PycharmProjects/pythonHW3/word_analyzer.py", line 40, in izzle
if word.index(i) >= word.index(vowel1):
ValueError: 'i' is not in list
Please tell me How can I solve this? Or if something is wrong with any of my code or I can how improve my code?
Since you are trying to mimic the pig_latin
function and modify it to give you an Izzle
translation, which is bit wrong as the logic to convert pig_latin is different and has cyclic shift of characters, while in Izzle
its more like find and replace,
Below function will work for you
def izzle(word):
word = list(word)
length = len(word)
vowels = ['a', 'e', 'i', 'o', 'u']
lastVowelIndex = int()
for i, char in enumerate(word[::-1]):
if char in vowels:
lastVowelIndex = length-i
break
return ''.join(word[:lastVowelIndex-1]) + 'izzle'