Re: Factor

Factor: the language, the theory, and the practice.


Thursday, February 7, 2013

#spelling #text

Typoglycemia is the name given to an internet meme that went around a few years ago, purporting to demonstrate that “readers can understand the meaning of words in a sentence even when the interior letters of each word are scrambled”.

Can you read this?

…it deosn’t mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist and lsat ltteer be in the rghit pclae.

Well, perhaps it isn’t completely true, but it at least is mostly true. How about an implementation of this in Factor?

We will start by building a word to “misspell” a string that is at least four letters long (ignoring any punctuation at the end) and randomizing all the characters except the first and last:

: misspell-word ( word -- word' )
    dup [ ",'.:;!?" member? not ] find-last drop 0 or
    dup 2 > [
        dupd head-slice dup [ Letter? ] all?
        [ rest-slice randomize ] when drop
    ] [ drop ] if ;

Next, we will “misspell” a line of text:

: misspell-line ( line -- line' )
    [ blank? ] split-when [ misspell-word ] map " " join ;

Finally, misspelling a block of text:

: misspell ( string -- string' )
    string-lines [ misspell-line ] map "\n" join ;

You can try it to show it works:

IN: scratchpad "this really works!" misspell .
"this relaly wroks!"

Maybe it would be easier to read if we just randomly swap two letters in the middle of each word, rather than fully randomizing it?