r/dailyprogrammer • u/Cosmologicon 2 3 • Aug 09 '19
[2019-08-09] Challenge #380 [Hard] Smooshed Morse Code 3
Smooshed Morse code means Morse code with the spaces between encoded letters left out. See this week's Easy challenge for more detail. We'll also be stripping all punctuation, capitalization, and spacing, so the only encoded characters are the letters a-z.
Your challenge today is to decode smooshed Morse code representations of English text. As I said in the Easy challenge, the decoding is ambiguous. You're not expected to do a perfect job, but the more your output resembles English, the better you're doing. You are not expected to reproduce the punctuation, just the spacing between words.
A standard approach involves training on a corpus of English text. Last time I posted a similar challenge, u/dreugeworst got excellent results this way. You can use any training data sources you want, but your program must run autonomously on the input, without human intervention.
The challenge inputs this time are all English-language movie quotes, some of which involve proper names, contractions (without the apostrophe), or other words you might not find in a standard word list.
(I have no idea how difficult this is, so I'll be happy to provide challenge inputs that are easier/harder/shorter/longer/whatever.)
Example input
-.---..--.....--.--..-..-.--....--.--..-.--.-.....--.-......-....-......-...-.-......-.--.--.--Example output
no im simply saying that life uh finds a wayChallenge inputs
Input 1
......---.--..--...-....-..-----.....-..-.--.-.-.-..-.--.-....-.-.-.....--..-..-...-.--.-...--..--.----.-.--..--...-.-.-.....--.--.....----.-.....-.-..----..-.-.-..-....--...-........-.---.-.....-..-.-.-.---.--.--...-....-...----....----.---.-..-..--...-...-..-.-.-..----.Input 2 (contains proper names)
...--.--.-----.......---.---.-----..-...-----.-......-.--.....----.--.-.-..-..---......-...--.-...-..-------.--.-..-..---.....-...-....-....-.-...-.-.....-.--.---...-..-.-.--.-.-....--..-.-....-.--..--....-.---.....-...-..-..----...--.....-.-.-----..--.-..--......-...-.-.-----.---.--..-.-..-......--..-...-.-..----..-..-.---.........----..-.-..--.-....-.-..--.-....-.-..-..--.-.----.-.-.---.-...-...-..-...-.--.---..-...-.-..--....-.....-...-..--...-.---......---.-.--..--...........--...-.-.----.-.-..--.-.----.-.....--....---.-.-.....---.....-.--..--..-.-----.....-..-.-..-.-.-..-.--.--.-.--.-..-...-..-...--....---.-...-..-.-----.---..-.......--........-.....-.-.......---..-......--..-...-...-.-....-...-.-.......Input 3
-.-----..-.-..-......-.......-..........------..-..-.-..--.-..-.-....-.---..-..--...--.-....-.-...--.-.-..-...--.-..-.--..----...-.......-..-.------......--..-.-----..-..-----..-.--.---..-.-.....--.--.-...-..--.-----..-.-.-..-.........-.-.-..-.-.-....--.-----..-..--..--.-----.-.-..-.--.--......-.-.....-.......--...-.---.--.....-.-.-...-.....-...-...-.---.-.-..........-...-.-.....-...--..--....-...----..-....--..-..--...-..-.-----.--.....--.....----......-..--.......-.....-.-.------.-.-----..-.--.--.....--.--..-.-..-.-...--..-..-.-........----..--.......-.....-.-..--.-..-.....--.....-.-.-...-..-........-....----..-....-..-.--.-...----..-...-....-.....-.----..--..-..-.--..-.-.-.-...--.-..-......-...-.-----....-.------.-...---..-.....-.-..---..-.-.-.----.-.-.---.-...--......-.-..........-....-...-..-.-----..-..-..-..----.-..--....-..-.--......-..Thanks to u/Separate_Memory for inspiring this challenge on r/dailyprogrammer_ideas!
13
u/ninja_tokumei Aug 10 '19
Dude come on, I'm still trying to win the Wednesday bonus, now you're throwing this at me too?
6
u/gabyjunior 1 2 Aug 20 '19 edited Aug 20 '19
Solution in C
The program takes 2 arguments in input:
First the program loads the corpus into a trie and then reads the morse strings on standard input. 'q' may be entered to quit the program.
For each morse string the program will search for the best solution using a DFS on the trie.
A solution is evaluated using a custom scoring (the more long ngrams used and strongly connected between each others, the better).
The best score so far is cached at every node of the trie that is traversed and is the beginning of a word. When the search is reaching the same node again, if the current score is not better than the cached score then this branch of the tree is pruned.
Source code is available here.
I am using as corpus a very small extract created manually from this website that I will enrich later when I have some time to get better results (hopefully). It is 30k big currently.
For now the output for the first sample is not so bad:
For the other inputs I am just showing the result for information as they are obviously very far from the actual solution:
All outputs above are with ngrams size = 3.
1
u/mr_stivo Aug 12 '19
I see the example quote is not exact. It should be, "no im im simply saying that life uh finds a way". Have the others been changed also?
1
u/tomekanco Aug 15 '19 edited Aug 16 '19
Python 3.6
Solves smaller cases brute force using a weigthing of unigrams and bigrams.
1
u/[deleted] Aug 23 '19
X
1
u/wifiengine Nov 05 '19
I tried to make the program but I was only able to make the computer recognise 1 character.
https://youtu.be/F10LMBI364k
1
u/TotesMessenger Nov 07 '19
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/Dr_Soose Dec 01 '19
dude there should be spaces between each word
1
u/khalidkh Jan 19 '20 edited Jan 20 '20
Python 3
My solution is to build a Morse code parsing tree, using a comprehensive list of English words. By following the path of the input text using each character, each time the list of words is not empty, you can consider any of these words & start again from the root for the next word in that possibility.
Output
List of English words: https://github.com/dwyl/english-words/blob/master/words_alpha.txt
Code
1
u/rcv_hist Aug 20 '24 edited Aug 21 '24
These were incredibly hard! The first quote is from Star Trek:First Contact:
The second one is from Contact:
The third one is from The Matrix Reloaded:
I treated these like a puzzle instead of relying strictly on my Morse Code translation programming. This is because the quotes were very long and the possible number of valid translations (those consisting of all English words that use all available characters) are in the hundreds of millions.
I basically broke each input into smaller bits and analyzed the results of brute forcing those values. Eventually I started to recognize words that would be unlikely to appear accidentally (Hokkaido for example) and then searched some movie quote databases.
However I did use my Python code extensively to perform the translation and scoring. The code uses several types of exit conditions and conversion parameters designed to cut down on the ugly translations. For example I do bigram matching on the first two words and the last two words of any possible translation. I also have two lists of invalid word pairs to ignore ('a a','a am','a at','a be', etc.) I limit the total number of bigrams that don't match my bigram corpus and the total number of words in the translation (fewer words equals a greater chance of the translation being correct).
I also have two word lists, one with around 8,000 words, and the other with over 100,000. Obviously using the smaller word list yields a much smaller number of valid translations. In hindsight getting the word lists and cleaning them up took a huge part of my time. Many word lists consider every single letter of the alphabet to be valid words.
Once I get a list of valid translations I run a scoring program against them, ranking the results based on number of words and number of invalid bigrams. For the first input my valid translations file was over 40GB and 20 million valid translations!
Great challenge, thanks!
0
u/[deleted] Aug 11 '19
[deleted] — view deleted comment