Convert Words To Pig Latin (Python)

Python Pig Latin Translator

One of the early “practise” programs that Impractical Python (reviewed here, available from No Starch Press) is to convert words into Pig Latin. I’ve given it a shot and although I need to work on PEP-8, I managed to create a program that does it within 25 lines (including shebang line and comments):

—-

#!/bin/python3
# geektechstuff – Pig Latin (Impractical Python Excercise)
ay = ‘ay’
way = ‘way’
consonant = (‘B’,’C’,’D’,’F’,’G’,’H’,’J’,’K’,’L’,’M’,’N’,’P’,’Q’,’R’,’S’,’T’,’Y’,’V’,’X’,’Z’)
vowel = (‘A’,’E’,’I’,’O’,’U’)
user_word = input(‘Enter a word to translate to Pig Latin: ‘)
# getting first letter and making sure its a string and setting it to uppercase
first_letter = user_word[0]
first_letter = str(first_letter)
first_letter=first_letter.upper()
if first_letter in consonant:
   print(first_letter,’is a consonant’)
   length_of_word = len(user_word)
   remove_first_letter = user_word[1:length_of_word]
   pig_latin=remove_first_letter+first_letter+ay
   print(‘The word in Pig Latin is:’,pig_latin)
 elif first_letter in vowel:
   print(first_letter,’is a vowel’)
   pig_latin=user_word+way
   print(‘The word in Pig Latin is:’,pig_latin)
 else:
   print(‘I dont know what’,first_letter,’is’)
—-
Python Pig Latin Translator
Python Pig Latin Translator
I found creating the Pig Latin generator a fun little excercise. The books author (Lee Vaughan) managed to create a Pig Latin generator in 18 lines, this can be found at the books GitHub page – https://github.com/rlvaugh/Impractical_Python_Projects
I modified my original program so that it can change sentences rather than just words.
—-
#!/bin/python3
# geektechstuff – Pig Latin version 2 (Impractical Python Excercise)
ay = ‘ay’
way = ‘way’
consonant = (‘B’,’C’,’D’,’F’,’G’,’H’,’J’,’K’,’L’,’M’,’N’,’P’,’Q’,’R’,’S’,’T’,’Y’,’V’,’X’,’Z’)
vowel = (‘A’,’E’,’I’,’O’,’U’)
pig_latin_string =”
user_sentence = input(‘Enter a sentence to translate to Pig Latin: ‘)
words = user_sentence.split()
for user_word in words:
print(user_word)
# getting first letter and making sure its a string and setting it to uppercase
first_letter = user_word[0]
first_letter = str(first_letter)
first_letter=first_letter.upper()
if first_letter in consonant:
length_of_word = len(user_word)
remove_first_letter = user_word[1:length_of_word]
pig_latin=remove_first_letter+first_letter+ay
pig_latin_string=pig_latin_string+’ ‘+pig_latin
elif first_letter in vowel:
pig_latin=user_word+way
pig_latin_string=pig_latin_string+’ ‘+pig_latin
else:
print(‘?’)
print(pig_latin_string)
—-
Python Pig Latin Translator
Python Pig Latin Translator

And then added a GUI (Graphical User Interface) to make it slightly more user friendly.

#!/usr/bin/python3
# Pig Latin GUI (Inspired by Impractical Python)
# GeekTechStuff
from tkinter import *
from tkinter import ttk
# variables
ay = ‘ay’
way = ‘way’
consonant = (‘B’,’C’,’D’,’F’,’G’,’H’,’J’,’K’,’L’,’M’,’N’,’P’,’Q’,’R’,’S’,’T’,’Y’,’V’,’X’,’Z’)
vowel = (‘A’,’E’,’I’,’O’,’U’)
# creates Tkinter window
root = Tk()
# creates window title
root.title(‘GeekTechStuff Pig Latin’)
# stops the window being resized
root.resizable(0,0)
root.frame_box = ttk.Frame()
ttk.Label(root.frame_box,text = ‘Pig Latin Translator’, style = ‘Header.TLabel’).grid(row = 0, columnspan = 4)
ttk.Label(root.frame_box,text = ‘Text:’, style = ‘Header.TLabel’).grid(row = 3, column = 0)
ttk.Label(root.frame_box,text=’Translated Text:’,style=’Header.TLabel’).grid(row=4, column=0)
text_entry = ttk.Entry(root.frame_box, width=50)
text_entry.grid(row=3,column=1)
translated_text = ttk.Entry(root.frame_box, width=50)
translated_text.grid(row=4,column=1)
#functions
def translate_text():
pig_latin_string =”
user_sentence = text_entry.get()
user_sentence = str(user_sentence)
words = user_sentence.split()
for user_word in words:
first_letter = user_word[0]
first_letter = str(first_letter)
first_letter=first_letter.upper()
if first_letter in consonant:
length_of_word = len(user_word)
remove_first_letter = user_word[1:length_of_word]
pig_latin=remove_first_letter+first_letter+ay
pig_latin_string=pig_latin_string+’ ‘+pig_latin
elif first_letter in vowel:
pig_latin=user_word+way
pig_latin_string=pig_latin_string+’ ‘+pig_latin
else:
print(‘?’)
translated_text.insert(0, pig_latin_string)
translate_button = ttk.Button(root.frame_box,text=’Translate’,command = lambda: translate_text()).grid(row=5,column=0)
root.frame_box.pack()
root.mainloop()
Python Pig Latin Translator with GUI
Python Pig Latin Translator with GUI

All three versions of my code can be found at: https://github.com/geektechdude/Python_Pig_Latin/

2 thoughts on “Convert Words To Pig Latin (Python)

Comments are closed.