Turning PigLatin into Full Translator! :)


Hi Guys!

Would anyone know how to render Pig Latin translation on full sentences? The course here was great, and it altered individual words in a really fun way.

All the best.


and then you want to translate every word this way? What characters do you want to allow?

there are some design choices to be made then


Hi, thanks for responding so quickly! I'd like to be able to translate, say, full paragraphs in English. I've learned how to translate individual words, which is great. And if you happen to know how to extend this to full paragraphs (i.e. taking full sentences from the user and translating it) that would be great. Thank you!


but there is a problem, a normal paragraph also contains other symbols (,.!?) and so on, what do you want to do with them? strip them out?

of course i can code such a thing, but then i would have to make such decisions. but lets say the user is very friendly and doesn't do this we can do it simple, let me quickly code this


then we get something like this:

pyg = 'ay'

original = str(raw_input('Enter a word:')).split()
for i in range(len(original)):
    original[i] = original[i][1:] + original[i][0] + pyg
print " ".join(original)

A good start :slight_smile:

you could decide to instead append to a new list, depending on what you want


This is great, thank you! And on what you wrote about punctuation- yes, it would be better if code could exclude punctuation marks. Is that possible?


yes, this is possible with re:

import re

pyg = 'ay'

original = str(raw_input('Enter a word:'))
original = re.sub(r'([^\s\w]|_)+', '', original).split()

for i in range(len(original)):
    original[i] = original[i][1:] + original[i][0] + pyg
print " ".join(original)

I tried to understand RE (regular expressions) but my understanding is basic, i found it on stackoverflow (here)


Reasoning should go something like:

I can translate one word, to translate a sentence I should therefore apply that process to each word.

And it'd be nice to leave punctuation where it is.

So what's needed then is to chop up the string into words and non-words, a bit of googling where you describe the behaviour you want is often fruitful, even more so if you have some idea of what exists.


Since it's regex we need something either matching words, or non-words, either will serve for this purpose. Both of those are already built in patterns in regex:'\W' for non-word characters and '\w' for word characters. For English one could also use '[a-zA-Z]+' (one or more characters a-z or A-Z)

>>> import re
>>> re.split(r'(\W+)', 'Hello there, my name is Bob.')
['Hello', ' ', 'there', ', ', 'my', ' ', 'name', ' ', 'is', ' ', 'Bob', '.', '']

Next step is then to apply translation to each element that is translatable, I believe testing that was part of the exercise, the regex pattern above could also be used.

That translating should probably be refactored into a function, and then just apply it to each element:

result = ''.join(translate(word) for word in my_chopped_up_stuff)


result = ''.join(map(translate, my_chopped_up_stuff))

That's assuming that the translate function leaves un-translatable strings unmodified


Hi guys, these have all been brilliantly helpful. Thank you!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.