Pig Latin Rules with Vowels - python

so I'm trying to write a code that will check each word in a string if there is a vowel in the beginning. If there is a vowel (upper or lowercase) then it will return the word and "-way" appended. If it begins with a consonant, then it moves the first letter to the back and appends "-ay"
Ex: anchor = anchor-way, computer = omputer-cay
.This is what I have but it seems like it's returning all words with these conditions and it's not checking for vowels. What am I doing wrong?
def pig_latin(text):
say = []
vowel = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
words = text.split()
for vowel in words:
vowel = vowel[0:] + "-way"
say.append(vowel)
if vowel not in words:
vowel = vowel[1:] + "-" + vowel[0] + "ay"
say.append(vowel)
return " ".join(say)
if __name__ == '__main__':
result = pig_latin("hi how are you")
print(result)

When you write for vowel in words:, you aren't checking for vowels in words or doing any comparison. The expression for i in iterable: is a loop that will one-at-a-time set (in this case) i to each item in the iterable. In your case, it is setting the variable vowels to the first, then second, then third, ... item in your list called words. That is, it is overwriting your vowel list you created.
Try something like this.
def pig_latin(text):
say = []
vowel = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
words = text.split()
for word in words: # loop over each word in the list of words
if word[0] in vowel: # compare the first letter of the word against the vowel list
new_word = word + '-way'
else:
new_word = word[1:] + '-' + word[0] + 'ay'
say.append(new_word)
return " ".join(say)
if __name__ == '__main__':
result = pig_latin("hi how are you")
print(result)

there are a few things that you can do.
use the lower() function.
do not confuse vowels for words.
use enumerate to edit the existing list at the correct index.
This is a working version:
def pig_latin(sentance: str):
words = sentance.split()
vowels = ['a','e','i','o','u']
for i, word in enumerate(words):
if word[0].lower() in vowels:
words[i] = word + '-way'
else:
words[i] = word[1:] + '-' + word[0] + 'ay'
return words
r = pig_latin("hi how are you")
print(r)
returns
['i-hay', 'ow-hay', 'are-way', 'ou-yay']

Related

What is the most efficient way to write out my Pig Latin converter?

This is a pig Latin sentence converter and was wondering if I could make my code any smaller. Pig Latin is basically like if a word starts with a vowel then ad yay to the end of the word. If the word starts with a consonant then take the first letter and put it at the end of the word and then add ay to it.
Example: apple = appleyay & tree = reetay
string_name =input('Enter String: ').lower().strip()
string_name =string_name.split()
pig_sentence =[]
for word in string_name:
if word[0] in ['a', 'e', 'i', 'o', 'u']:
pig_word =word+'yay'
pig_sentence.append(pig_word)
else:
pig_word =word[1:] + word[0] + "ay"
pig_sentence.append(pig_word)
pig_sentence =' '.join(pig_sentence)
print(pig_sentence)
The following should also work.
print(" ".join([word+"yay" if word[0] in ['a', 'e', 'i', 'o', 'u'] else word[1:] + word[0] + "ay" for word in input('Enter String: ').lower().strip().split()]))
But, as you can see, this example isn't very readable. You might also want to use:
string_name =input('Enter String: ').lower().strip()
string_name =string_name.split()
pigsentence=" ".join([word+"yay" if word[0] in ['a', 'e', 'i', 'o', 'u'] else word[1:] + word[0] + "ay" for word in string_name])
print(pigsentence)
or just continue using the code you've now, as shorter code isn't always better, and you don't need to really worry about efficiency.

How do you locate/check the letter of each word in a file to see if it is a vowel or consonant?

Basically, I have a text file. I need to be able to check each word in the text file to see if the word starts with a vowel or consonant.
I used this code to read and then split the file into a list of words:
with open("textfile.txt") as file:
text = file.read()
text = text.split()
However from this, I am unsure how to drill down to the letter level and make the vowel/consonant check on each word. How do I check the first letter of each word in the list to see if it is a vowel or consonant?
This post tells me how to check for vowels:
checking if the first letter of a word is a vowel
So my question really is, how do I make the check on the word/letter level?
Thanks!!
A string, alike any other sequence in Python, is subscriptable.
So for a word, you can get the first letter by doing word[0]
Then, from other other post you already know, how to check if it is vowel or consonant.
You can do that for every word in your text by looping over them.
words_starting_with_vowels = []
words_starting_with_consonants = []
vowels = ['a', 'e', 'i', 'o', 'u']
for word in text: # loop over all words
lower_case_letter = word[0].lower()
if lower_case_letter in vowels:
words_starting_with_vowels.append(word)
else:
words_starting_with_consonants.append(word)
You need to select the first character in your word and compare it against an array of vowels, or you can use the startswith() method:
vowels = ('a','e','i','o','u','A','E','I','O','U')
if text.startswith(vowels):
print("starts with vowel")
else:
print("starts with consonant")
text.split() will return an array of words. You need to iterate over this and check if each word starts with a vowel.
for word in text:
if word.startswith(('A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u')):
print("It's a vowel") # Or do something else with the word
else:
print("Not a vowel")
Instead of word.startswith() You could also use the solution in the post you linked.
for word in text:
if word[0].lower() in ['a', 'e', 'i', 'o', 'u']:
print("It's a vowel")
else:
print("Not a vowel")

split string at vowel + surrounding consonants

I am new to coding and this is my first try. I want to divide words into syllables from a phonetic language.
Rules to make syllables out of words from a phonetic language:
consider all consonants until the first vowel, consider that vowel.
repeat.
Example:
ma - ri- a
a - le - ksa - nda - r
This is how far I've come :
word = 'aleksandar'
vowels = ['a','e','i','o','u']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
for vowel in vowels:
if vowels in word:
index_1 = int(word.index(vowel)) - 1
index_2 = int(word.index(vowel)) + 1
print(word[index_1:index_2])
else:
print(consonants)
IDK what is going wrong, please help!
Thanks in advance :)
This should solve your problem
word = 'aleksandar'
vowels = ['a','e','i','o','u']
new_word = ""
for letter in word:
if letter in vowels:
new_word += letter + "-"
else:
new_word += letter
print(new_word)
i have changed your code little bit and it works perfectly!!!
word = 'aleksandar'
word = list(word)
vowels = ['a','e','i','o','u']
s = ""
syllables = [ ]
for i in range(len(word)):
if word[i] not in vowels:
s = s + word[i]
else:
s = s + word[i]
syllables.append(s)
s = ""
print(syllables)
and the output is :
['a', 'le', 'ksa', 'nda']

replace vowel with another vowel

I need to replace the vowel of a word with another vowel. Using list comprehension, my code is only replacing the vowels in the word with block replace with "aeiou", how should I do to make the result look like,
for example, 'hom' will be ['ham', 'hem', 'him', 'hum']?
Thanks in advance!
word = "hom"
vowels = "aeiou"
words = [''.join([i if i not in vowels else 'aeiou' for i in word])]
print(words)
code below replace a vowel randomly with another vowel. sometimes it may replace it with same vowel
import random
"".join([random.choice(vowels) if c in vowels else c for c in word])
try this:
import random
word = "hom"
vowels_lst = ['a', 'e', 'i', 'o', 'u']
for i in range(len(word)):
if word[i] in vowels_lst:
rand_let = random.choice(vowels_lst)
new_word = word.replace(word[i], rand_let)
print(new_word)

String manipulation in for loops

So I just learned how to manipulate single letters in a for loop from code academy.
But let's say I made a function and wanted this function to manipulate the vowels of an user inputted word and replace the vowel with four consecutive copies of itself. How would I go about that?
Expected output:
>>>Exclamation("car")
caaaar
>>>Exclamation("hello")
heeeelloooo
So far I have:
word = input("Enter a word: ")
vowels= ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
for char in word:
if char in vowels:
print(____,end='') #here I am unsure of how to replace it with consecutive copies of itself
else:
print(char,end='')
Your print statement can be:
print(4 * char,end='') # Or how many ever times you want to repeat it.
If word is 'car', this code:
>>> for char in word:
... if char in vowels:
... print(4 * char, end='')
... else:
... print(char, end='')
...
prints
caaaar
Note: You can include only the lower case vowels in your vowels list and in your if condition, check if char.lower() is in vowels.

Categories