loading...

Piglatin in python and orgmode.

zeeter profile image Daniel Smith ・3 min read

Table of Contents

  1. Concept
  2. Finding vowels
  3. Stripping the first letter
  4. Putting it together

Concept

The Pig Latin translator is a program that takes a string and transforms it by
moving the first letter to the last and adding "ay" onto it.
However if the letter would be a vowel, the entire process is replaced with "way"
so:

Dog
Nyx
Puppy
Gizmo
Kitten
Luna-fish
Tuna-fish
Apple
Orange

Would become:

Ogday
Yxna
Uppypay
Izmogay
Ittenkay
Una-fishlay
Una-fishtay
Ppleway
Rangeway

I'm sure that implimenting full sentences are possible, but that does fall out of the scope of the
challenge so we will not be doing those for the time being.

Finding vowels

To find the vowels we simply need them in a nice and handy location.
In this case as a string.

vowels = "aeiou"

then we can simply check if the input string is infact a vowel by seeing if its in the string.

def isVowel(letter):

    vowels = "aeiou"
    return letter in vowels

Lets see if it worked!

def isVowel(letter):

    vowels = "aeiou"
    return letter in vowels
con = isVowel(letter.lower())

if con:
    return "{} is a vowel".format(letter.lower())
else:
    return "{} is not a vowel".format(letter.lower())
  • a is a vowel
  • b is not a vowel
  • abc is not a vowel

But to be good, lets actually make a docstring describing what this "function" does.

Checks to see if a letter is a vowel.
for example:
>>> isVowel("a")
True
>>> isVowel("b")
False

However as written, it wont take captial letters. But this is intended. to avoid weirdness with
casing, I'm turning everything into lowercase letters. so crisis averted for now.

Stripping the first letter

Now we can check vowels, but so what?
Well now we start actually possessing our word.
For this, we really don't need a function. But writing in orgmode's makes the notion of
code blocks as functions seem so easy.
It also makes testing each code block easier, so python can bite me on this one.
They aren't venomous snakes after all.

def stripFirst(word):

    first = word[0]
    rest = word[1:]

    return (first, rest)

And now time for our docstring:

Strip first takes a word and returns two strings.
One consisting of just the first letter and then the rest of the word.
Example:
>>> stripFirst("Daniel")
("D","aniel")

Putting it together

So I could further break the code down, but there wouldn't atleast in my opinion
be much value gained from that. Instead, I'm going to jump ahead a few steps and assemble the piglatin function.

def isVowel(letter):

    vowels = "aeiou"
    return letter in vowels
def stripFirst(word):

    first = word[0]
    rest = word[1:]

    return (first, rest)

def pigLatin(word):
    first, rest = stripFirst(word.lower())

    if isVowel(first):
    return rest + "way"
    else:
    return rest + first + "ay"

Technically, we're done.

def isVowel(letter):

    vowels = "aeiou"
    return letter in vowels
def stripFirst(word):

    first = word[0]
    rest = word[1:]

    return (first, rest)

def pigLatin(word):
    first, rest = stripFirst(word.lower())

    if isVowel(first):
    return rest + "way"
    else:
    return rest + first + "ay"


string = '{} is {} in piglatin'.format(word, pigLatin(word).title())

return string

I added in a bit for the post processing to make things a bit prettier.
In the future, I may extend this article a bit. Who knows?
I hope you enjoyed reading my ramblings a bit though.

Posted on Dec 10 '18 by:

zeeter profile

Daniel Smith

@zeeter

Howdy howdy, I'm Daniel mad scientist extraordinaire. I have been programming in python since 2012, and other things as I please.

Discussion

markdown guide
 

Awesome ! I was curious so I tried to implement it too in my free time, here is my a-little-more-concise solution !

def to_pig_latin(word: str) -> str:
    """convert a word to piglatin

    :param word: word to convert
    :return: the pigralitine version of the word
    """

    if word[0] in 'aeiou':
        complementary = 'way'
    else:
        complementary = f'{word[0]}ay'

    return f'{word[1:]}{complementary}'


if __name__ == '__main__':
    words = [
        'Dog',
        'Nyx',
        'Puppy',
        'Gizmo',
        'Kitten',
        'Luna-fish',
        'Tuna-fish',
        'Apple',
        'Orange',
    ]

    for word in words:
        print(f'{word} is {to_pig_latin(word).title()} in piglatin')
 

Oh thats good! Mine was meant to be entirely run inside this article using orgmode and such. I totally forgot to set it up so that you can tangle the file out.