Guest User

decode spaceless morse code

a guest
Dec 9th, 2014
1,555
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var dict = {
  2.   '.-': 'A',
  3.   '-...': 'B',
  4.   '-.-.': 'C',
  5.   '-..': 'D',
  6.   '.': 'E',
  7.   '..-.': 'F',
  8.   '--.': 'G',
  9.   '....': 'H',
  10.   '..': 'I',
  11.   '.---': 'J',
  12.   '-.-': 'K',
  13.   '.-..': 'L',
  14.   '--': 'M',
  15.   '-.': 'N',
  16.   '---': 'O',
  17.   '.--.': 'P',
  18.   '--.-': 'Q',
  19.   '.-.': 'R',
  20.   '...': 'S',
  21.   '-': 'T',
  22.   '..-': 'U',
  23.   '...-': 'V',
  24.   '.--': 'W',
  25.   '-..-': 'X',
  26.   '-.--': 'Y',
  27.   '--..': 'Z',
  28.   '.----': '1',
  29.   '..---': '2',
  30.   '...--': '3',
  31.   '....-': '4',
  32.   '.....': '5',
  33.   '-....': '6',
  34.   '--...': '7',
  35.   '---..': '8',
  36.   '----.': '9',
  37.   '-----': '0'
  38. };
  39.  
  40. var fs = require('fs');
  41. var words = fs.readFileSync('/usr/share/dict/words-2500').toString().split('\n');
  42. var trie = { complete: false };
  43.  
  44. for(var n = 0;n < words.length;n++) {
  45.   var word = words[n].toUpperCase();
  46.   if(word.replace(/[A-Z0-9]/g, '').length > 0) {
  47.     console.log(word);
  48.     continue;
  49.   }
  50.   if(word.length < 2 && word != 'I' && word != 'A')
  51.     continue;
  52.   var cur = trie;
  53.   for(var i_char = 0;i_char < word.length;i_char++) {
  54.     if(!cur[word[i_char]]) cur[word[i_char]] = { complete: false };
  55.     cur = cur[word[i_char]];
  56.   }
  57.   cur.complete = true;
  58. }
  59.  
  60. var isWord = function(str) {
  61.   var cur = trie;
  62.   for(var i = 0;i < str.length;i++) {
  63.     if(!cur[str[i]]) return false;
  64.     cur = cur[str[i]];
  65.   }
  66.   return cur.complete;
  67. };
  68. var isPrefix = function(str) {
  69.   var cur = trie;
  70.   for(var i = 0;i < str.length;i++) {
  71.     if(!cur[str[i]]) return false;
  72.     cur = cur[str[i]];
  73.   }
  74.   return true;
  75. };
  76.  
  77. var isWordList = function(wordList) {
  78.   if(wordList.length == 0) return true;
  79.   for(var i = 0;i < wordList.length - 1;i++) {
  80.     if(!isWord(wordList[i]))
  81.       return false;
  82.   }
  83.   if(!isPrefix(wordList[wordList.length - 1]))
  84.     return false;
  85.   return true;
  86. };
  87.  
  88. var isCompleteWordList = function(wordList) {
  89.   for(var i = 0;i < wordList.length;i++) {
  90.     if(!isWord(wordList[i]))
  91.       return false;
  92.   }
  93.   return true;
  94. };
  95.  
  96. var appendToWordList = function(wordList, c) {
  97.   var tmp;
  98.   var newWordLists = [];
  99.   if(wordList.length > 0) {
  100.     tmp = wordList.slice(0);
  101.     tmp[tmp.length - 1] = tmp[tmp.length - 1] + c;
  102.     newWordLists.push(tmp);
  103.   }
  104.   tmp = wordList.slice(0);
  105.   tmp.push(c);
  106.   newWordLists.push(tmp);
  107.   return newWordLists;
  108. };
  109.  
  110. var decodeWords = function(code) {
  111.   var cache = {
  112.     '0': [[]]
  113.   };
  114.   for(var start = 0;start < code.length;start++) {
  115.     for(var len = 1;len < 6;len++) {
  116.       if(start + len > code.length) continue;
  117.       if(!cache[start + len]) cache[start + len] = [];
  118.       var curCode = code.slice(start, start + len);
  119.       if(dict[curCode]) {
  120.         for(var i_start = 0;i_start < cache[start].length;i_start++) {
  121.           wordList = cache[start][i_start];
  122.           newWordLists = appendToWordList(wordList, dict[curCode]);
  123.           for(var i_newWordLists = 0;i_newWordLists < newWordLists.length;i_newWordLists++) {
  124.             if(isWordList(newWordLists[i_newWordLists]))
  125.               cache[start + len].push(newWordLists[i_newWordLists]);
  126.           }
  127.         }
  128.       }
  129.     }
  130.   }
  131.   var fin = [];
  132.   var possibleWords = cache[code.length];
  133.   for(var i_possibleWords = 0;i_possibleWords < possibleWords.length;i_possibleWords++) {
  134.     if(isCompleteWordList(possibleWords[i_possibleWords]))
  135.       fin.push(possibleWords[i_possibleWords]);
  136.   }
  137.   fin.sort(function(a, b){return a.length - b.length;});
  138.   return fin.map(function(a) {return a.join(' ')});
  139. };
  140.  
  141. module.exports = decodeWords;
RAW Paste Data