Flag Semaphore Encoder Your goal is to write a flag semaphore encoder, which will convert a given sentence into the corresponding flag semaphore characters, according to the semaphore system described on Wikipedia.

Assume that the input is a single sentence provided through stdin (or equivalent). Your output should be a series of semaphore characters, with each row representing one word from the sentence. You only need to deal with the alphabet (A-Z) and should ignore all other non-space characters, but you must be able to handle both uppercase and lowercase. Your output is allowed to contain extra whitespace.

Semaphore characters must be displayed as a 3x3 square, with an O in the middle and the flag positions represented by the characters | - / \. Each semaphore character must be separated from adjacent characters by a space, and each row must be separated by a blank line. Don't worry about wrapping for words that may be too long for your display - pretend that your lines have infinite length.

Sample input:

abcdefg hijklmn opqrstu vwxyz


Sample output:

        \    |    /
 O  -O   O   O   O   O-  O
/|   |   |   |   |   |   |\

    \    |   |    /
-O   O   O-  O   O   O-  O
/   /       /   /   /   / \

\    |    /         \|  \ /
-O  -O  -O  -O- -O   O   O
                  \ 

 |    /   / \ 
 O   O-  O   O-  O-
  \       \       \


Sample input:

This is Code Golf.


Sample output:

\|      \ 
 O  -O   O  -O 
    /   /     \

\      
 O  -O 
/     \

\   \    |    /
 O  -O   O   O 
 |       |   |

    \     /  
 O  -O   O   O-
 |\     /    |


Since this is code-golf, the shortest solution wins.
Python, 244 238 233 232

e='abhioptuwycdjmnsqxzfgvklebr'
for w in raw_input().split():
 for i in 0,3,6,9:print' '.join(''.join((' '+'\|/-O-/|\ '[j])[`j`in'4'+'6736031025071568328578162735'[e.find(c):][:2]]for j in range(i,9)[:3])for c in w if c.lower()in e)


This uses a favorite trick of mine: single-track encoding.  I've labeled the semaphore bits (sbits)

\|/     012
- -  -&gt; 3 5
/|\     678


to obtain the following chart of which sbits occur for which letter:

0: ciotuy
1: djkptv
2: elquwx
3: bhopqrs
5: fjmrwyz
6: ahiklmn
7: abcdefg
8: gnsvxz


each letter occurs exactly twice in the chart, since the signalman has two arms.  Then, I view this as a graph on the letters a-z, with edges between letters sharing sbits, with the edges labeled according to the shared sbit.  Ideally, I'd find a Hamilton path through this graph, such that subsequent edges don't have the same label.  No such paths exist... so you'll note that the variable e contains the letter b twice.

With my nearly-Hamilton path e, I construct an array d of sbit labels used in the traversal of e.  Then, to figure out where to put her arms, the signalman need only find the desired letter in the following handy chart

abhioptuwycdjmnsqxzfgvklebr
6736031025071568328578162735


whence her arms go in the position directly below, and below&amp;right of the letter.