Coding Challenge #2! (115)

75 Name: #!/usr/bin/anonymous : 2006-03-02 12:19 ID:Heaven

Not even close to 4K, I was just really bored this evening... Too lazy to add command line arguments, however.

import urllib, urllib2, re

def babelfish(langpair, text):
url="http://babelfish.altavista.com/tr?lp="+langpair+"&trtext="+urllib.quote(text.encode('utf-8'))
html = urllib2.urlopen(urllib2.Request(url,None,{'Accept-charset':'utf-8'})).read()
m = re.compile("white class.s..div.style.padding.10px..([^<]*)</div",re.U).search(html)
return m.groups()[0].decode('utf-8').replace(u'\n',u' ')

def log(f, num, text, encoding='utf-8'):
out = (unicode(num)+u': '+text+u"\r\n")
print out.strip().encode('ascii','replace')
f.write(out.encode(encoding))
f.flush()

def mutate(text, steps, langpairs, outfile):
t = unicode(text)
f = open(outfile,"wb")
n = 1
log(f, 0, t)
history = [t]
br = False
while not br:
for lp in langpairs:
t = babelfish(lp, t)
log(f, n, t)
if n==steps: break
n += 1
if t in history:
br = True
break
history.append(t)
if n==steps: break
if br: log(f, n, u"[loop found]")
f.close()

mutate("Hay guys what's going on in this thread?", 20, ['en_ja','ja_en'], "nonsense.txt")
Name: Link:
Leave these fields empty (spam trap):
More options...
Verification: