telegraph 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Juanjo Bazán
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = Telegraph
2
+
3
+ Telegraph is a Ruby gem that provides straightforward text-to-morse and morse-to-text translators
4
+
5
+ It uses as reference the document 'RECOMMENDATION ITU-R M.1677' from the International Telecommunication Union, Radiocommunication Sector (ITU-R), the United Nations agency for information and communication technology issues.
6
+
7
+ == Getting started
8
+
9
+ Install the gem:
10
+ $ [sudo] gem install telegraph
11
+
12
+ Then depending on your project you may:
13
+
14
+ require the gem (if a ruby project):
15
+ require 'telegraph'
16
+ or add it to your Gemfile (if you're on Rails):
17
+ gem 'telegraph', :git => 'git://github.com/xuanxu/telegraph.git'
18
+
19
+
20
+ == Usage
21
+
22
+ The simplest way to read or write morse code is using directly the two methods defined in Telegraph:
23
+
24
+ # Converts text to morse characters:
25
+ Telegraph.text_to_morse("Hello world") #=> ".... . .-.. .-.. --- .-- --- .-. .-.. -.."
26
+
27
+ # Reads morse characters into text:
28
+ Telegraph.morse_to_text(".... . .-.. .-.. --- .-- --- .-. .-.. -..") #=> hello world"
29
+
30
+ In the Telegraph module you will find defined also the most commonly used prosigns:
31
+
32
+ # Error sign:
33
+ Telegraph::Error #=> "........"
34
+
35
+ #The complete list of prosigns:
36
+ Telegraph::Error
37
+ Telegraph::Understood
38
+ Telegraph::Invitation_to_transmit
39
+ Telegraph::Wait
40
+ Telegraph::End_of_work
41
+ Telegraph::Starting_signal
42
+
43
+ All the generated morse code will use the character '.' as short signal (aka dot) and the character '-' as long signal (aka dash).
44
+
45
+ If you need to write or read using different characters as short and long signals you can do it instantiating your own Telegraph::MorseTransmission class:
46
+
47
+ # User defined dot & dash (:short and :long defaults to '.' and '-')
48
+ morser = Telegraph::MorseTransmission.new(:short =>'x', :long => '3')
49
+
50
+ and then using the methods to read/write:
51
+
52
+ # write morse code with custom signals
53
+ morser.text_to_morse("Hello world") #=> "xxxx x x3xx x3xx 333 x33 333 x3x x3xx 3xx"
54
+
55
+ # read morse code with custom signals
56
+ morser.morse_to_text("xxxx x x3xx x3xx 333 x33 333 x3x x3xx 3xx") #=> hello world"
57
+ morser.error #=> "xxxxxxxx"
58
+
59
+ == Credits
60
+
61
+ Author:: Juanjo Bazán
62
+ Copyright:: Copyright (c) 2010 Juanjo Bazán
63
+ License:: Released under the MIT license.
64
+
@@ -0,0 +1,130 @@
1
+ # encoding: UTF-8
2
+ module MorseCharacters
3
+
4
+ # Official characters from the RECOMMENDATION ITU-R M.1677: International Morse code.
5
+ ITU_Morse = {
6
+ "a" => ".-",
7
+ "b" => "-...",
8
+ "c" => "-.-.",
9
+ "d" => "-..",
10
+ "e" => ".",
11
+ "f" => "..-.",
12
+ "g" => "--.",
13
+ "h" => "....",
14
+ "i" => "..",
15
+ "j" => ".---",
16
+ "k" => "-.-",
17
+ "l" => ".-..",
18
+ "m" => "--",
19
+ "n" => "-.",
20
+ "o" => "---",
21
+ "p" => ".--.",
22
+ "q" => "--.-",
23
+ "r" => ".-.",
24
+ "s" => "...",
25
+ "t" => "-",
26
+ "u" => "..-",
27
+ "v" => "...-",
28
+ "w" => ".--",
29
+ "x" => "-..-",
30
+ "y" => "-.--",
31
+ "z" => "--..",
32
+ "0" => "-----",
33
+ "1" => ".----",
34
+ "2" => "..---",
35
+ "3" => "...--",
36
+ "4" => "....-",
37
+ "5" => ".....",
38
+ "6" => "-....",
39
+ "7" => "--...",
40
+ "8" => "---..",
41
+ "9" => "----.",
42
+ "." => ".-.-.-",
43
+ "," => "--..--",
44
+ ":" => "---...",
45
+ "?" => "..--..",
46
+ "’" => ".----.",
47
+ "–" => "-....-",
48
+ "-" => "-....-",
49
+ "/" => "-..-.",
50
+ "(" => "-.--.",
51
+ ")" => "-.--.-",
52
+ "“" => ".-..-.",
53
+ "”" => ".-..-.",
54
+ "\""=> ".-..-.",
55
+ "=" => "-...-",
56
+ "+" => ".-.-.",
57
+ "@" => ".--.-."}
58
+
59
+ # Some international characters not included in the ITU-R specification.
60
+ International_extensions = {
61
+ "ä" => ".-.-",
62
+ "æ" => ".-.-",
63
+ "ą" => ".-.-",
64
+ "à" => ".--.-",
65
+ "å" => ".--.-",
66
+ "ç" => "-.-..",
67
+ "ĉ" => "-.-..",
68
+ "ć" => "-.-..",
69
+ "š" => "----",
70
+ "ĥ" => "----",
71
+ "ð" => "..--.",
72
+ "ś" => "...-...",
73
+ "è" => ".-..-",
74
+ "ł" => ".-..-",
75
+ "é" => "..-..",
76
+ "đ" => "..-..",
77
+ "ę" => "..-..",
78
+ "ĝ" => "--.-.",
79
+ "ĵ" => ".---.",
80
+ "ź" => "--..-.",
81
+ "ñ" => "--.--",
82
+ "Ñ" => "--.--",
83
+ "ń" => "--.--",
84
+ "ö" => "---.",
85
+ "ø" => "---.",
86
+ "ó" => "---.",
87
+ "ŝ" => "...-.",
88
+ "þ" => ".--..",
89
+ "ü" => "..--",
90
+ "ŭ" => "..--",
91
+ "ż" => "--..-"
92
+ }
93
+
94
+ # The space between morse words is seven spaces.
95
+ Space_between_words = {" " => " "}
96
+
97
+ # Default dot signal: '.'
98
+ DOT = '.'
99
+
100
+ # Default dot signal: '-'
101
+ DASH = '-'
102
+
103
+ # Character mapping to write morse code.
104
+ LETTERS_TO_MORSE = Space_between_words.merge(International_extensions).merge(ITU_Morse)
105
+
106
+ # Character mapping to read morse code.
107
+ MORSE_TO_LETTERS = (Space_between_words.merge(ITU_Morse)).invert
108
+
109
+ MORSE_TO_LETTERS.default = ""
110
+ LETTERS_TO_MORSE.default = ""
111
+
112
+ # Prosign for 'Error'
113
+ Error = "........"
114
+
115
+ # Prosign for 'Understood'
116
+ Understood = "...-."
117
+
118
+ # Prosign for 'Invitation to transmit'
119
+ Invitation_to_transmit = "-.-"
120
+
121
+ # Prosign for 'Wait'
122
+ Wait = ".-..."
123
+
124
+ # Prosign for 'End of work'
125
+ End_of_work = "...-.-"
126
+
127
+ # Prosign for 'Starting signal'
128
+ Starting_signal = "-.-.-"
129
+
130
+ end
data/lib/telegraph.rb ADDED
@@ -0,0 +1,134 @@
1
+ # encoding: UTF-8
2
+
3
+ # Telegraph module provides a simple text-to-morse, morse-to-text translator.
4
+ #
5
+ # It uses as reference the document <t>RECOMMENDATION ITU-R M.1677</t> from the
6
+ # International Telecommunication Union, Radiocommunication Sector (ITU-R), the United
7
+ # Nations agency for information and communication technology issues.
8
+ #
9
+ module Telegraph
10
+ include MorseCharacters
11
+
12
+ # Transforms a string of plain text into a string of morse code
13
+ # using the character '.' as the short signal and the character '-' as the long signal.
14
+ # The encode is made following the official RECOMMENDATION ITU-R M.1677.
15
+ # The returned string uses a single space as letter separator and a seven spaces gap as word separator.
16
+ #
17
+ # Telegraph.text_to_morse("Hello world") #=> ".... . .-.. .-.. --- .-- --- .-. .-.. -.."
18
+ #
19
+ def self.text_to_morse(text)
20
+ text.strip.downcase.split(" ").inject([]){|morse_words, word|
21
+ morse_words << word.chars.inject(""){|morse_word, character| [morse_word, LETTERS_TO_MORSE[character]].join(" ").strip}
22
+ }.join(LETTERS_TO_MORSE[" "]).strip
23
+ end
24
+
25
+ # Transforms a string of morse code into a string of text decoding using the RECOMMENDATION ITU-R M.1677.
26
+ #
27
+ # It expects the character '.' as the short signal, the character '-' as the long signal,
28
+ # a space as separation between letters and seven spaces as separation between words.
29
+ #
30
+ # The returned string is completly downcased.
31
+ #
32
+ # Telegraph.morse_to_text(".... . .-.. .-.. --- .-- --- .-. .-.. -..") #=> hello world"
33
+ #
34
+ def self.morse_to_text(morse)
35
+ morse.split(LETTERS_TO_MORSE[" "]).inject([]){|words, morse_word|
36
+ words << morse_word.split(" ").inject(""){|word, morse_char| word + MORSE_TO_LETTERS[morse_char]}
37
+ }.join(" ")
38
+ end
39
+
40
+ # Telegraph::MorseTransmission class provides a simple text-to-morse, morse-to-text translator using
41
+ # user defined characters for short and long signals (defaults to '.' and '-').
42
+ #
43
+ # It uses as reference the document <t>RECOMMENDATION ITU-R M.1677</t> from the
44
+ # International Telecommunication Union, Radiocommunication Sector (ITU-R), the United
45
+ # Nations agency for information and communication technology issues.
46
+ #
47
+ class MorseTransmission
48
+ attr_accessor(:short, :long)
49
+
50
+ # When instantiating the class two options can be provided to set the signals
51
+ # to use by the new instance when reading and writing morse code:
52
+ #
53
+ # * <tt>:short</tt> - The character to use as dot or short signal. Default: '.'
54
+ # * <tt>:long</tt> - The character to use as dash or long signal. Default: '-'
55
+ #
56
+ def initialize(signals={})
57
+ @short = signals[:short] || Telegraph::DOT
58
+ @long = signals[:long] || Telegraph::DASH
59
+ end
60
+
61
+ # Transforms a string of plain text into a string of morse code
62
+ # using as short and long signals the characters defined by the user when the class was instantiated.
63
+ #
64
+ # The returned string uses a single space as letter separator and a seven spaces gap as word separator.
65
+ #
66
+ # morser = Telegraph::MorseTransmission.new(:short =>'x', :long => '3')
67
+ # morser.text_to_morse("Hello world") #=> "xxxx x x3xx x3xx 333 x33 333 x3x x3xx 3xx"
68
+ #
69
+ def text_to_morse(text)
70
+ morse = Telegraph.text_to_morse(text)
71
+ dot_dash(morse)
72
+ end
73
+
74
+ # Transforms a string of morse code into a string of text decoding using the RECOMMENDATION ITU-R M.1677.
75
+ #
76
+ # It expects as short and long signals the characters defined by the user when the class was instantiated,
77
+ # a space as separation between letters and seven spaces as separation between words.
78
+ #
79
+ # The returned string is completly downcased.
80
+ #
81
+ # morser = Telegraph::MorseTransmission.new(:short =>'x', :long => '3')
82
+ # morser.morse_to_text("xxxx x x3xx x3xx 333 x33 333 x3x x3xx 3xx") #=> hello world"
83
+ #
84
+ def morse_to_text(morse)
85
+ morse = undo_dot_dash(morse.strip)
86
+ Telegraph.morse_to_text(morse)
87
+ end
88
+
89
+ # Returns the 'Error' sign using the signal characters defined for this instance.
90
+ def error
91
+ dot_dash(Telegraph::Error)
92
+ end
93
+
94
+ # Returns the 'Understood' sign using the signal characters defined for this instance.
95
+ def understood
96
+ dot_dash(Telegraph::Understood)
97
+ end
98
+
99
+ # Returns the 'Invitation to transmit' sign using the signal characters defined for this instance.
100
+ def invitation_to_transmit
101
+ dot_dash(Telegraph::Invitation_to_transmit)
102
+ end
103
+
104
+ # Returns the 'Wait' sign using the signal characters defined for this instance.
105
+ def wait
106
+ dot_dash(Telegraph::Wait)
107
+ end
108
+
109
+ # Returns the 'End of work' sign using the signal characters defined for this instance.
110
+ def end_of_work
111
+ dot_dash(Telegraph::End_of_work)
112
+ end
113
+
114
+ # Returns the 'Starting signal' sign using the signal characters defined for this instance.
115
+ def starting_signal
116
+ dot_dash(Telegraph::Starting_signal)
117
+ end
118
+
119
+ private
120
+
121
+ def dot_dash(text) #:nodoc:
122
+ text = text.gsub(Telegraph::DOT, @short) unless @short == Telegraph::DOT
123
+ text = text.gsub(Telegraph::DASH, @long) unless @long == Telegraph::DASH
124
+ text
125
+ end
126
+
127
+ def undo_dot_dash(morse) #:nodoc:
128
+ morse = morse.gsub(@short, Telegraph::DOT) unless @short == Telegraph::DOT
129
+ morse = morse.gsub(@long, Telegraph::DASH) unless @long == Telegraph::DASH
130
+ morse
131
+ end
132
+ end
133
+
134
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegraph
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ version: "1.0"
9
+ platform: ruby
10
+ authors:
11
+ - "Juanjo Baz\xC3\xA1n"
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-11-09 00:00:00 +01:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rspec
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 0
31
+ version: 2.1.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Telegraph provides a simple text-to-morse, morse-to-text translator.
35
+ email: jjbazan@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - MIT-LICENSE.txt
44
+ - README.rdoc
45
+ - lib/morse_characters.rb
46
+ - lib/telegraph.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/xuanxu/telegraph
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.rdoc
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Ruby gem to read and write Morse code
81
+ test_files: []
82
+