Changing the order of letters in words

Did you ever come across the finding that even if the order of letters in words is changed just slightly you will most likely be able to read the text without much problems? I think that this is so interesting that I invested a considerable amount of time writing a software that reads a text, changes the words and outputs the result. Easier said than done, this post tries to explain what I’ve been working on.

The Eclipse project with the code for this post can be downloaded as tar.gz or zip. You can browse the code online here.

The idea

How exactly do you want to rearrange the letters in the words? How do you calculate whether a word has more changes compared to the original word than another one? Permutations, the Hamming and Levenshtein distances come to the rescue. I implemented the Steinhaus–Johnson–Trotter algorithm to calculate permutations and almost copied variants of the distance algorithms from somebody else.

When the software now reads a text it extracts the individual words, calculates all permutations of the letters and selects either a word with a small or large Levenshtein distance based on the user’s input. This way we can either generate a variant of the text that’s pretty easy or really hard to read. Since it might not be possible to calculate permutations for long words due to performance reasons the software may fall back to a Fisher–Yates shuffle.

Tests

Try running the software with a certain text yourself but from what I’ve seen so far it is really no problem to read a variant of a certain text if some letters are rearranged. If the Levenshtein distance is at its maximum or the letters are ordered by pure chance it may really get difficult though.

For example lets use the first sentence of the last section. Right now, the result looks like the following:

How eactlxy do you wnat to rearrgnae the ltteers in the wodrs?
How eltaxcy do you wnat to rgnaaerre the ltreets in the wrdos?

The first one uses a small, the second a larger Levenshtein distance. A future version of the software could produce words where the position of a character isn’t moved further away than one or two characters compared to its original position; this would make the words even easier to read.

Conclusion

It is really interesting to experiment with words and the results are stunning. In the end it is nice to see that we are able to adapt to rearranged letters in words, still being able to comprehend the text, reading it without much problems. Finally, we should wonder what our brain is capable of and try writing proper words without typos in the first place.

4 comments ↓

  • Hudson says:
    This could be used to generate a listing of typos to associate with a page. Maybe by embedding them in a non-displayed box and id’ing the div as related typos, for the engine to read but not for the user to see.
  • Hi Hudson,
    sure, that would be an interesting thing to do, thanks for sharing. This is such an interesting topic and generating more ideas can be really funny here.
  • Michael says:
    Nice task. Tried to do the same task in python, which generally happens to be more compact than java:

    #!
    import re, random
     
    s = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."
    words = re.split ("\W+", s)
     
    shuffeled = [x[0] + "".join(random.sample(x[1:-1], len(x) - 2)) + x[-1] for x in words if len(x) > 1]
    print " ".join(shuffeled)

     

  • Hi Michael,
    thanks for the code – nice work ;-)

Leave a Comment