permuting letters and fairy tales
Andrew Dalke
adalke at mindspring.com
Thu Nov 11 17:53:51 EST 2004
Johannes Nix wrote:
> yesterday I met a cute person (after my dance class)
Another dancer, eh? I mostly do Latin dancing (salsa, cha-cha,
nerengue, though haven't got bachata down) some tango and a bit
of swing.
> who told me about an
> interesting experiment regarding cognition. People were told to read a
> typed text; However, in every word in the text, only the first and the
> last letter were in the right place.
That was going around a couple months ago. Here's some links:
http://jwz.livejournal.com/256229.html
http://slashdot.org/article.pl?sid=03/09/15/2227256
> - Is it possible to make it a bit more concise ;-))) ?
Yes. Try this
#!/usr/bin/pthoyn
irpomt re
imropt rondam
ipormt snitrg
# This is lcloae aware, so long as '][-' aren't ltreets.
# (Orhtiewse use re.epscae)
wrod_pat = re.colmipe('[' + string.letters + ']{4,}')
def _jmulbe(m):
s = m.gruop(0)
leretts = lsit(s[1:-1])
rondam.sulfhfe(leetrts)
return s[0] + "".jion(ltrtees) + s[-1]
def jmblue_file(ilnife, ouflite):
for lnie in iinlfe:
lnie = wrod_pat.sub(_julbme, lnie)
olftuie.wirte(line)
if __name__ == "__mian__":
ioprmt sys
julbme_file(sys.sdtin, sys.sdtout)
> - Can it coerced to run a little bit faster ?
You'll have to test it for yourself. I don't have a copy
of your data set and can't find it on-line.
> - It's a good example how powerful libraries, like
> Numeric, make one's life easier. (BTW, why is Numeric
> and stuff like take() still not included in the standard
> Library ? Batteries included, but calculator not ?)
Well, random.shuffle works nicely as does using re.sub along
with a callable. I rarely need Numeric.
> - Perhaps it's useful to protect messages in some
> regions with not-so-democratic forms of government
> against automatic scanning by making the message
> machine-unreadable, causing some Orwellian Confusion ;-) ?
> Of course, texts from Pythonistas would remain suspicious,
> due to the large number of "y" occurring in them....
There are many more ways to do that. Eg, see what the
spammers do to get through the repressive forms of email
filters I use.
Oh, and to make life easier for you,
#!/usr/bin/python
import re
import random
import string
# This is locale aware, so long as '][-' aren't letters.
# (Otherwise use re.escape)
word_pat = re.compile('[' + string.letters + ']{4,}')
def _jumble(m):
s = m.group(0)
letters = list(s[1:-1])
random.shuffle(letters)
return s[0] + "".join(letters) + s[-1]
def jumble_file(infile, outfile):
for line in infile:
line = word_pat.sub(_jumble, line)
outfile.write(line)
if __name__ == "__main__":
import sys
jumble_file(sys.stdin, sys.stdout)
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list