Python l33t Translator

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2435 Views - Last Post: 28 October 2012 - 03:00 AM Rate Topic: -----

#1 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Python l33t Translator

Posted 13 June 2012 - 08:55 AM

Hello,
I have recently been working on my own l33t translator and I thought you guys could check it out (It's a WIP so it won't be perfect).
Here's the Code:
#Leet Translator
#This will translate
#Normal english into
#l33t.
#Cladus - 13/06/2012
print("/t/t/t Python L33T Translator")
word = raw_input("Please enter a message: ")
leetmsg = word
leetwords = "aeioutsyou"
for letter in word:
    if letter in leetwords:
        leetmsg = leetmsg.replace('a', str(4))
        leetmsg = leetmsg.replace('e', str(3))
        leetmsg = leetmsg.replace('i', str(1))
        leetmsg = leetmsg.replace('o', str(0))
        leetmsg = leetmsg.replace('t', str(7))
        leetmsg = leetmsg.replace('s', '$')
        leetmsg = leetmsg.replace('you', 'j00')
print "Translated Message: ", leetmsg


Thank You,
Cladus

Is This A Good Question/Topic? 0
  • +

Replies To: Python l33t Translator

#2 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 13 June 2012 - 11:04 AM

Please tell me what you think :)
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Python l33t Translator

Posted 13 June 2012 - 11:55 AM

In line 6: If those /ts were meant to be tabs, it should have been \t with a backslash, not a forward slash.

In line 9: I'd rename the variable "leetwords" to "leetletters" or something like that. After all a, e, etc. aren't words.

In line 10ff: Your loop logic seems a bit wonky to me. Everything in the body of the if operates on the whole string. Basically there's no need for the loop (or the if for that matter) to be there at all. If you just remove lines 10 and 11 and unindent lines 11 through 18, the behavior of your program should stay exactly the same.

It also seems inelegant to me that you spell out the letters/phrases to be replaced manually in lines 11 through 18. That kind of defeats the purpose of having the leetwords list in the first place. What I'd do is replace the leetwords list with a dictionary that maps each letter or phrase to the letter or phrase that it should be replaced with. Then you can iterate over that dictionary and call replace once for each entry in it.

Alternatively you might also consider using regular expressions, so you only need to call re.sub once.
Was This Post Helpful? 2
  • +
  • -

#4 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Python l33t Translator

Posted 13 June 2012 - 12:44 PM

Technically this
leetmsg = leetmsg.replace('you', 'j00')


will never happen because this
leetmsg = leetmsg.replace('o', str(0))

Replaces all existing 'o' with '0'. Therefore any 'you' becomes 'y0u' before you can test for the word you.

You dont need the for loop since the replace method replaces all occurrences. A while loop to keep translating might also make it nice.

As always you could simplify it even more with a different loop and/or functions.
Was This Post Helpful? 3
  • +
  • -

#5 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 14 June 2012 - 05:41 AM

Thank for all your advice :)
I have updated the code. Here it is:
#Leet Translator
#This will translate
#Normal english into
#l33t.
#Cladus - 14/06/2012
print("\t\t\t Python L33T Translator")
word = raw_input("Please enter a message: ")
leetmsg = word
leet = {'e': '3', 's': '5', 'a': '4', 'you': 'j00', 'o': '0','E': '3', 'S': '5', 'A': '4', 'You': 'j00', 'O': '0', 't': '7', 'T': '7', 'i': '1', 'I': '1'}
for x, y in leet.items():
    leetmsg = leetmsg.replace(x, y)
print "Translated Message: ", leetmsg


Hope you like it. Please tell me what you think.
Thank You,
Cladus
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python l33t Translator

Posted 15 June 2012 - 11:11 AM

Looks good, although I admit I haven't had time to run it. I would make a few tweaks still. First, the formatting of your dictionary can be hard to read. If you reformatted it the way I have below, you'll see it's much easier to skim.

I also suspect there is a way to only have the lowercase key in your dictionary, but off the top of my head, I'm not sure how we could do that (and being at work, I can't really play with it).

#Leet Translator
#This will translate
#Normal english into
#l33t.
#Cladus - 14/06/2012
print("\t\t\t Python L33T Translator")
word = raw_input("Please enter a message: ")
leetmsg = word
leet = {
    'e': '3', 
    's': '5', 
    'a': '4', 
    'you': 'j00', 
    'o': '0',
    'E': '3', 
    'S': '5', 
    'A': '4', 
    'You': 'j00', 
    'O': '0', 
    't': '7', 
    'T': '7', 
    'i': '1', 
    'I': '1',}
for x, y in leet.items():
    leetmsg = leetmsg.replace(x, y)
print "Translated Message: ", leetmsg



Was This Post Helpful? 1
  • +
  • -

#7 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 16 June 2012 - 02:12 AM

Thanks I will get on to the Caps and reformat the code. Thanks for the help :)
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Python l33t Translator

Posted 16 June 2012 - 04:14 AM

Since you aren't actually using the dictionary as a dictionary, I wouldn't use it. Also, your order is important. You want to make sure you interpret "you" before "o" and "u" or it ain't gonna work.

Put your converter in a function for cleaner code. Since there's already an answer out here. I'd to it like this:
def toLeet(msg):
	leet = ( 
		(('you', 'You'), 'j00'),
		(('o', 'O'), '0'),
		(('i', 'I'), '1'),
		(('e', 'E'), '3'),
		(('s', 'S'), '5'),
		(('a', 'A'), '4'),
		(('t', 'T'), '7'),
		)
	for symbols, replaceStr in leet:
		for symbol in symbols:
			msg = msg.replace(symbol, replaceStr)
	return msg

print("\t\t\t Python L33T Translator")
word = raw_input("Please enter a message: ")
print "Translated Message: ", toLeet(word)


Was This Post Helpful? 2
  • +
  • -

#9 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 16 June 2012 - 07:35 AM

Thank You :). I really like the helpful community at DreamInCode!!
I will get on to that asap but right now I'm trying to dual boot Ubuntu with my Windows 7 Installation.
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python l33t Translator

Posted 16 June 2012 - 03:24 PM

don't forget that you can hit the Posted Image if you want to thank any of us.
Was This Post Helpful? 1
  • +
  • -

#11 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 17 June 2012 - 01:23 AM

I have created Version 2, With some modifications to baavgai's code.
Here it is:
#Leet Translator
#This will translate
#Normal english into
#l33t.
#Credit to: baavgai
#Cladus - 14/06/2012
def toLeet(text):
	leet = (
                (('are', 'Are'), 'r'),
                (('ate', 'Ate'), '8'),
                (('that', 'That'), 'tht'),
		(('you', 'You'), 'j00'),
		(('o', 'O'), '0'),
		(('i', 'I'), '1'),
		(('e', 'E'), '3'),
		(('s', 'S'), '5'),
		(('a', 'A'), '4'),
		(('t', 'T'), '7'),
		)
	for symbols, replaceStr in leet:
		for symbol in symbols:
			leettext = text.replace(symbol, replaceStr)
	return leettext

print("\t\t\t Python L33T Translator")
message = raw_input("Please enter a message: ")
print "Translated Message: ", toLeet(message)



Opps. The post above is NOT v2 it's v3.
Was This Post Helpful? 0
  • +
  • -

#12 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 17 June 2012 - 01:48 AM

Hey baavgai I have been thinking about uploading my code to Google Code just for fun but I would like to have your permission before I do it since I used your code. If you want I can put your name on the site!
Thank You,
Cladus
Was This Post Helpful? 0
  • +
  • -

#13 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 17 June 2012 - 03:15 AM

After testing the code It didnt seem to work. I will try to fix it!

After check through the code I found the errors.
Here's the code:
#Leet Translator V3.1
#This will translate
#Normal english into
#l33t.
#Credit to: baavgai
#Cladus - 15/06/2012
def toLeet(text):
	leet = (
                (('are', 'Are'), 'r'),
                (('ate', 'Ate'), '8'),
                (('that', 'That'), 'tht'),
		(('you', 'You'), 'j00'),
		(('o', 'O'), '0'),
		(('i', 'I'), '1'),
		(('e', 'E'), '3'),
		(('s', 'S'), '5'),
		(('a', 'A'), '4'),
		(('t', 'T'), '7'),
		)
	for symbols, replaceStr in leet:
		for symbol in symbols:
			text = text.replace(symbol, replaceStr)
	return text

print("\t\t\t Python L33T Translator")
message = raw_input("Please enter a message: ")
print "Translated Message: ", toLeet(message)






I am asking everyone who helped my with the code If I have permission to upload this it GitHub or Google Code.
I will make sure to give everyone credit.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Python l33t Translator

Posted 17 June 2012 - 03:51 AM

Anything I post to an open forum is open. Use it as you like. Glad you're having fun with it.
Was This Post Helpful? 0
  • +
  • -

#15 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Python l33t Translator

Posted 17 June 2012 - 03:54 AM

Thank You :) This forum is great I hope to become a regular user like you guys!!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2