Scripts/Morse

From J Wiki
Jump to navigation Jump to search

Download the script:
File:Morse.ijs (for J 6.0.X)
File:Morse Jv5.ijs (for J 5.0.4)

Conversion of text to basic textual representation in Morse code.

Examples:

   tomorse 'AYYLU'
.- -.-- -.-- .-.. ..-
   tomorse 'Intl. Morse Code of 1848'
.. -. - .-.. .-.-.-   -- --- .-. ... .   -.-. --- -.. .   --- ..-.   .---- ---.. ....- ---..

Conversion of text to boolean representation of Morse encoding.

Examples:

   1 tomorseboolean 'SOS'
1 0 1 0 1 0 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0
   tomorseboolean 'BT'
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
   tomorseboolean 'bat'
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0

Source for tomorse (with shortened text-to-morse mapping)

fullmap =:	<;._2 (0 : 0)

A .-
B -...
C -.-.
D -..
E .
F ..-.
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.

)
NB. This is an abbreviated version of the map.  Complete map is in attached script.

charkey =:	{."1 > fullmap	NB. charkey is unboxed text list.

morsemap =:	}. &. > fullmap	NB. morsemap elements remain boxed.

spacesmin =: 3 : 0
NB. consecutive spaces reduced to single space character.
y#~ '  ' -.@E. y
)

tomorse =:  3 : 0
}. ; morsemap {~ charkey i. toupper spacesmin y
)

Contributed by Tracy B. Harms

References