Skip to content

Commit

Permalink
Added English text to pig Latin converter
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansafdarktk committed Oct 21, 2023
1 parent a1d4821 commit b4c1e2d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions text_to_pig_latin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
This program converts English text to Pig-Latin. In Pig-Latin, we take the first letter of each word,
move it to the end, and add 'ay'. If the first letter is a vowel, we simply add 'hay' to the end.
The program preserves capitalization and title case.
For example:
- "Hello" becomes "Ellohay"
- "Image" becomes "Imagehay"
- "My name is John Smith" becomes "Ymay amenay ishay Ohnjay Mithsmay"
"""


def pig_latin_word(word):
vowels = "AEIOUaeiou"

if word[0] in vowels:
return word + "hay"
else:
return word[1:] + word[0] + "ay"

def pig_latin_sentence(text):
words = text.split()
pig_latin_words = []

for word in words:
# Preserve capitalization
if word.isupper():
pig_latin_words.append(pig_latin_word(word).upper())
elif word.istitle():
pig_latin_words.append(pig_latin_word(word).title())
else:
pig_latin_words.append(pig_latin_word(word))

return ' '.join(pig_latin_words)

user_input = input("Enter some English text: ")
pig_latin_text = pig_latin_sentence(user_input)
print("\nPig-Latin: " + pig_latin_text)

0 comments on commit b4c1e2d

Please sign in to comment.