Typoglycemia: The PowerShell and the SQL

Typoglycemia is the ironic name, (derived from Hypoglycemia) given to the phenomenon that many readers can understand the meaning of words in a sentence even when the interior letters of each word are scrambled. They appear to recognize words by the outermost letters, the length, the letters used and the context.  As long as all the necessary letters are present, and the first and last letters remain the same, they appear to have little trouble reading the text, but reading speed slows as brain-processing resources are utilised.

Scrambling text between the first and last letter of every word has become a common homework puzzle for people learning computing. There is a perl one-liner that is said to perform this scrambling function on a string:

perl -ne's@\b(\w)(\w+)(\w)\b@"$1".(join"",sort{(rand)<=>0.7}split//,$2).$3@gex&&print;'

Can it be done in PowerShell? Well yes: It looks like one of those problems for which Regular expressions are ideal. In fact I suspect that it was invented purely to put RegExs in a good light. I’d be interested if you can do it in a shorter algorithm.

Now what about T-SQL? Is it possible to do the same thing in transact SQL? Well, it is: but is it pretty? Is it fast? Now, there’s a challenge!

added later…

Ok, so only one entry in the SQL version (at the time of writing), so I thought I ought to add my own attempt at a SQL Typoglycemia. It is done as a stored procedure because a function must be determinant, and this routine is likely to produce a different result every time!

The trick I used was to find every word with four or more letters using the wildcard search (LIKE and PATINDEX) and using this information to extract the letters and shuffle them using the NEWID() trick. Easy really. I bet you could write something faster or simpler!