Pig latin and python

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 
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.

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 
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()) 

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.

Читайте также:  Python check if object is iterable

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" 
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.

Источник

Python Program to Convert Sentence to Pig Latin

Summary: In this programming example, we will learn to convert a string or sentence into Pig Latin using Python programming.

What is Pig Latin?

It is said that Pig Latin is not any kind of language but is a language game that children use to speak in code. It is formed by altering the letters in a word.

Here’s how it works:

First, pick an English word. We’ll use dictionary.

Next, move the first consonant or consonant cluster to the end of the word: “ictionary-d”.

Now add “ay” to the end of the word: “ictionary-day”.

That’s all there is to it; you’ve formed a word in Pig Latin.

I recommend you to read more about Pig Latin on the wiki.

Now we have some idea what exactly is Pig Latin, so let’s try converting a whole sentence into its Pig Latin form.

sentence = input('Enter a Sentence: ').lower() words = sentence.split() for i, word in enumerate(words): ''' if first letter is a vowel ''' if word[0] in 'aeiou': words[i] = words[i]+ "ay" else: ''' else get vowel position and postfix all the consonants present before that vowel to the end of the word along with "ay" ''' has_vowel = False for j, letter in enumerate(word): if letter in 'aeiou': words[i] = word[j:] + word[:j] + "ay" has_vowel = True break #if the word doesn't have any vowel then simply postfix "ay" if(has_vowel == False): words[i] = words[i]+ "ay" pig_latin = ' '.join(words) print("Pig Latin: ",pig_latin)

Enter a Sentence: Pencil Programmer
Pig Latin: encilpay ogrammerpray

  1. Input a sentence using the input() method
  2. Split it into a list of words using split()
  3. Loop through the words and check if any of them begins with vowels.
    1. If yes, then concatenate “ay” and update the word in the word’s list.
    2. Else check if there is any vowel present in between the word.
      1. If yes then get the first vowel index position and postfix all the consonants present before that vowel to the end of the word along with “ay”.

      These are the steps following which we can easily convert any sentence into its corresponding Pig Latin form in Python.

      This Post Has 9 Comments

      when i try the above code, I have TypeError: ‘str’ object is not callable for line 6
      how do I convert the data?

      The code is working fine. Please check again.

      Are you able show me how to pseudocode before coding this Pig Latin form in Python?

      how can i do this without using break?

      Can you pseudocode this in Python please?

      FOR EACH word IN words
      APPEND(word, “-“)
      letter ← FIRST_LETTER(word)
      IF (IS_VOWEL(letter)) APPEND(word, “yay”)
      > ELSE APPEND(word, letter)
      APPEND(word, “ay”)
      REMOVE_FIRST(word)
      >
      >

      for each word in words: append "-" to word letter = first letter of word if letter is a vowel: append "yay" to word else: append letter to word append "ay" to word remove first letter of word 

      Thank you so much Adarsh Kumar!! I’m still in the basics.

      Источник

      Saved searches

      Use saved searches to filter your results more quickly

      You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

      Translate text into Pig Latin using Python

      License

      cjanis/python-pig-latin

      This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

      Name already in use

      A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

      Sign In Required

      Please sign in to use Codespaces.

      Launching GitHub Desktop

      If nothing happens, download GitHub Desktop and try again.

      Launching GitHub Desktop

      If nothing happens, download GitHub Desktop and try again.

      Launching Xcode

      If nothing happens, download Xcode and try again.

      Launching Visual Studio Code

      Your codespace will open once ready.

      There was a problem preparing your codespace, please try again.

      Latest commit

      Git stats

      Files

      Failed to load latest commit information.

      README.md

      Translate text into Pig Latin using Python

      This script converts English text in Pig Latin following these steps:

      • Strips non-alpha or single space characters from input text
      • Converts input text to all lower case
      • For words that start with consonants, moves all consonants before the first vowel to the end of the word («the» becomes «ethay» not «hetay»)
      • For words that start with vowels, nothing is changed, only the suffix is added
      • Adds suffix «ay» to the end of words

      Install with Pip/PyPi in the command line interface:

      In your Python code, import the library:

      Call the translator in your code:

      piglatin.translate('Your text goes here!') 

      Your text will be translated into Pig Latin and returned as a string.

      About

      Translate text into Pig Latin using Python

      Источник

      Simple Pig Latin codewars python

      Move the first letter of each word to the end of it, then add «ay» to the end of the word. Leave punctuation marks untouched.

      Examples

      pig_it('Pig latin is cool') # igPay atinlay siay oolcay pig_it('Hello world !') # elloHay orldway !

      Источник

Оцените статью