Quoting David Richerby from the comments:
Since ⋅ represents E and − represents T, any Morse message without spaces can be interpreted as a string in $\{E,T\}^*$
Further, since A, I, M, and N are represented by the four possible combinations of two morse characters (⋅-, ⋅⋅, --, -⋅, respectively), any message without spaces can also be interpreted as a string in $\{A,I,M,N\}^*\{E,T\}?$. Note that for any Morse message of length > 1, this is distinct from David's interpretation. Thus, the only messages with unique interpretations are those of length 1 (and, I suppose, 0, if that counts as a message) - that is, ⋅, representing E, and -, representing T.
Here's some JavaScript that will tell you all possible interpretations of a string of .
and -
. Strings of up to length 22 run in under a second, but anything higher than that starts getting pretty slow - I wouldn't, for example, try to decode HELLO WORLD with it. You can pop open a JavaScript console in your browser, paste this in, and then call, for example, decode('......-...-..---')
. (In this example, entry #2446 is the intended string "HELLO".)
var decode = function(code) {
var cache = {
'0': ['']
};
for(var start = 0;start < code.length;start++) {
for(var len = 1;len < 6;len++) {
if(start + len > code.length) continue;
if(!cache[start + len]) cache[start + len] = [];
var curCode = code.slice(start, start + len);
if(dict[curCode]) {
for(var i_start = 0;i_start < cache[start].length;i_start++) {
cache[start + len].push(cache[start][i_start] + dict[curCode]);
}
}
}
}
return cache[code.length];
};
var dict = {
'.-': 'A',
'-...': 'B',
'-.-.': 'C',
'-..': 'D',
'.': 'E',
'..-.': 'F',
'--.': 'G',
'....': 'H',
'..': 'I',
'.---': 'J',
'-.-': 'K',
'.-..': 'L',
'--': 'M',
'-.': 'N',
'---': 'O',
'.--.': 'P',
'--.-': 'Q',
'.-.': 'R',
'...': 'S',
'-': 'T',
'..-': 'U',
'...-': 'V',
'.--': 'W',
'-..-': 'X',
'-.--': 'Y',
'--..': 'Z',
'.----': '1',
'..---': '2',
'...--': '3',
'....-': '4',
'.....': '5',
'-....': '6',
'--...': '7',
'---..': '8',
'----.': '9',
'-----': '0'
};
The code to prune it to only strings of real words is a bit longer, so I put it here. It runs under node.js and expects a file at /usr/share/dict/words-2500
. The dictionary I'm using can be found here. It is not naive - it prunes as it goes, so it runs much faster on larger inputs.
The dictionary consists of a top-2500 words list I found on the internet somewhere, minus some 1-, 2-, and 3- letter combinations that I deemed not words. This algorithm is sensitive to having too many short words to choose from, and slows down drastically if you allow, say, every individual letter as a word (I'm looking at you, /usr/share/dict/words
).
The algorithm finishes by sorting based on the number of words, so the "interesting" ones will hopefully be at the top. This works great on HELLO WORLD
, running in under a second and returning the expected phrase as the first hit. From this I also learned that DATA SCIENTIST
(the only other phrase I tried) morse codes the same as NEW REAL INDIA
.
Edit: I searched for more interesting ones for a few minutes. The words SPACES
and SWITCH
are morsagrams. So far they're the longest single-word pair I've found.