In Morse code1, each character of a word is converted into a unique combination of dots (.) and dashes (-). The conversions of individual characters are separated by spaces. However, if we remove all spaces from the Morse code, we can come up with some new definitions.

We say that words are isomorse if they have the same Morse code. For example, the following three words are isomorse:

word Morse code
ADMITTED .- -.. -- .. - - . -..
EMIGATED . -- .. --. .- - . -..
PANACE .--. .- -. .- -.-. .

If we swap all dots and dashes in the Morse code of a word (dots become dashes and dashes become dots) and thus obtain the Morse code of another word, we say that these words are antimorse.

word Morse code swapped Morse code word
DECATHLON -.. . -.-. .- - .... .-.. --- -. .- --. -.-- .. --- -- .- -. .. .- AGYIOMANIA
EMERITUS . -- . .-. .. - ..- ... -. .- -. --- .-- .- - - NANOWATT
NOSTALGIA -. --- ... - .- .-.. --. .. .- .-.. .- -- . -. - .- - .. --- -. LAMENTATION
TORMENTERS - --- .-. -- . -. - . .-. ... . ... -.-. .- .-. --. --- - ESCARGOT
VERTEBRAL ...- . .-. - . -... .-. .- .-.. -- -.-- . -.-. - --- .--. -.-- MYECTOPY

If we reverse the order of the dots and dashes in the Morse code of a word and thus obtain the Morse code of another word, we say that these words are revomorse.

word Morse code reversed Morse code word
DISCLAIM -.. .. ... -.-. .-.. .- .. -- -- .. -. . .-. .- .-.. .. ... - MINERALIST
FRACTURE ..-. .-. .- -.-. - ..- .-. . .. -. - . .-. - .-- .. -. . -.. INTERTWINED
INTENSION .. -. - . -. ... .. --- -. .- -- -... .. . -. -.-. . AMBIENCE
REVIEWER .-. . ...- .. . .-- . .-. .- ..- -.. .. - .. ...- . AUDITIVE
WAVELETS .-- .- ...- . .-.. . - ... ... - .. .-. .-. ..- .--. STIRRUP

If the Morse code of a word is palindromic (reverse Morse code is the same as the Morse code itself) then we say that the word is a palinmorse.

word Morse code
DIATOMIST -.. .. .- - --- -- .. ... -
INTERNAL .. -. - . .-. -. .- .-..
PROTECTORATE .--. .-. --- - . -.-. - --- .-. .- - .
RESEARCHER .-. . ... . .- .-. -.-. .... . .-.
WINTERTIME .-- .. -. - . .-. - .. -- .

Of course we may also combine the forgoing, swapping all dots and dashes as well as reversing the order of dots and dashes.

Assignment

To determine the Morse code representation of words, the conversion of each character onto its corresponding unique combination of dots and dashes is recorded in a text file. Each line of that file consists of a character, followed by a space and the unique combination of dots and dashes for that character. Because conversion into Morse code makes no distinction between uppercase and lowercase characters, letters are always represented in uppercase in the text file. Your task:

Example

In the following interactive session we assume the text files morse.txt2 and words.txt3 to be located in the current directory.

>>> morse = morse_code('morse.txt4')
>>> morse['A']
'.-'
>>> morse['P']
'.--.'
>>> morse['?']
'..--..'

>>> text2morse('ADMITTED', morse)
'.- -.. -- .. - - . -..'
>>> text2morse('emigated', morse)
'. -- .. --. .- - . -..'
>>> text2morse('OBSESSIVE', morse)
'--- -... ... . ... ... .. ...- .'

>>> swap('.- -.. -- .. - - . -..')
'-. .-- .. -- . . - .--'
>>> swap('--- -... ... . ... ... .. ...- .')
'... .--- --- - --- --- -- ---. -'

>>> words = database('words.txt5', morse)
>>> words['.--..--..--.-..']
{'ADMITTED', 'EMIGATED', 'PANACE'}

>>> lookup('ADMITTED', morse, words)
{'ADMITTED', 'EMIGATED', 'PANACE'}
>>> lookup('NOSTALGIA', morse, words, swapped=True)
{'LAMENTATION'}
>>> lookup('DISCLAIM', morse, words, reversed=True)
{'GENERALIST', 'MINERALIST'}
>>> lookup('WINTERTIME', morse, words, reversed=True)
{'WINTERTIME'}
>>> lookup('NOTATION', morse, words, swapped=True, reversed=True)
{'DUDDER', 'BADDER', 'DUNLIN', 'BEGLUE'}
>>> lookup('GASTROSCOPE', morse, words, reversed=True, swapped=True)
{'GASTROSCOPE'}

Epilogue

Fittingly, the English word OBSESSIVE has a long run of dots (18) in its Morse code:

--- -... ... . ... ... .. ...- .

However, the record holder is LESSEESHIP with a run of 21 dots in its Morse code:

.-.. . ... ... . . ... .... .. .--.