rufus-mnemo 1.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.txt +30 -19
  2. data/lib/rufus/mnemo.rb +161 -164
  3. data/lib/rufus-mnemo.rb +3 -0
  4. data/test/test.rb +75 -53
  5. metadata +9 -7
data/README.txt CHANGED
@@ -1,16 +1,14 @@
1
1
 
2
2
  = rufus-mnemo
3
3
 
4
- This gem was previously known as the "openwferu-kotoba" gem (http://jmettraux.wordpress.com/2007/04/23/gem-install-openwferu-kotoba/)
5
-
6
- It provides methods for turning integer into easier to remember 'words' and vice-versa.
4
+ This gem provides methods for turning integer into easier to remember 'words' and vice-versa.
7
5
 
8
6
  The module Rufus::Mnemo has all the explanation.
9
7
 
10
8
 
11
9
  == getting it
12
10
 
13
- sudo gem install rufus-mnemo
11
+ sudo gem install rufus-mnemo
14
12
 
15
13
  or at
16
14
 
@@ -19,18 +17,26 @@ http://rubyforge.org/frs/?group_id=4812
19
17
 
20
18
  == usage
21
19
 
22
- require 'rubygems'
23
- require 'rufus/mnemo'
24
-
25
- s = Rufus::Mnemo::from_integer 125704
26
-
27
- puts s
28
- # => 'karasu'
29
-
30
- i = Rufus::Mnemo::to_integer s
31
-
32
- puts i
33
- # => 125704
20
+ require 'rubygems'
21
+ require 'rufus/mnemo'
22
+
23
+ s = Rufus::Mnemo::from_integer 125704
24
+
25
+ puts s
26
+ # => 'karasu'
27
+
28
+ i = Rufus::Mnemo::to_integer s
29
+
30
+ puts i
31
+ # => 125704
32
+
33
+
34
+ == about negative integers
35
+
36
+ They are prefixed with the "wi" syllable (Thanks Stephan Wehner).
37
+
38
+ p Rufus::Mnemo::from_integer -173866
39
+ # => 'winamote'
34
40
 
35
41
 
36
42
  = dependencies
@@ -42,7 +48,7 @@ None.
42
48
 
43
49
  On the rufus-ruby list[http://groups.google.com/group/rufus-ruby] :
44
50
 
45
- http://groups.google.com/group/rufus-ruby
51
+ http://groups.google.com/group/rufus-ruby
46
52
 
47
53
 
48
54
  == issue tracker
@@ -52,9 +58,9 @@ http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
52
58
 
53
59
  == source
54
60
 
55
- http://rufus.rubyforge.org/svn/trunk/mnemo
61
+ http://github.com/jmettraux/rufus-mnemo
56
62
 
57
- svn checkout http://rufus.rubyforge.org/svn/trunk/mnemo
63
+ git checkout git://github.com/jmettraux/rufus-mnemo.git
58
64
 
59
65
 
60
66
  == author
@@ -63,6 +69,11 @@ John Mettraux, jmettraux@gmail.com
63
69
  http://jmettraux.wordpress.com
64
70
 
65
71
 
72
+ == the rest of Rufus
73
+
74
+ http://rufus.rubyforge.org
75
+
76
+
66
77
  == license
67
78
 
68
79
  MIT
data/lib/rufus/mnemo.rb CHANGED
@@ -1,6 +1,5 @@
1
- #
2
1
  #--
3
- # Copyright (c) 2007-2008, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2007-2009, John Mettraux, jmettraux@gmail.com
4
3
  #
5
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
6
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -8,10 +7,10 @@
8
7
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
8
  # copies of the Software, and to permit persons to whom the Software is
10
9
  # furnished to do so, subject to the following conditions:
11
- #
10
+ #
12
11
  # The above copyright notice and this permission notice shall be included in
13
12
  # all copies or substantial portions of the Software.
14
- #
13
+ #
15
14
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
15
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
16
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -19,224 +18,222 @@
19
18
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
19
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
20
  # THE SOFTWARE.
22
- #++
23
- #
24
-
25
- #
26
- # "made in Japan"
27
- #
28
- # John Mettraux at openwfe.org
29
21
  #
22
+ # Made in Japan.
23
+ #++
30
24
 
31
25
  module Rufus
32
26
 
33
- #
34
- # = Rufus::Mnemo
35
- #
36
- # This module contains methods for converting plain integers (base 10)
37
- # into words that are easier to read and remember.
38
- #
39
- # For example, the equivalent of the (base 10) integer 1329724967 is
40
- # "takeshimaya".
41
- #
42
- # Mnemo uses 70 of the syllables of the Japanese language, it is thus
43
- # a base 10 to base 70 converter.
44
- #
45
- # Mnemo is meant to be used for generating human readable (or more
46
- # easily rememberable) identifiers. Its first usage is within the
47
- # OpenWFEru Ruby workflow and bpm engine for generating 'kawaii'
48
- # business process instance ids.
49
- #
50
- # require 'rubygems'
51
- # require 'rufus/mnemo'
52
- #
53
- # s = Rufus::Mnemo::from_integer 125704
54
- #
55
- # puts s
56
- # # => 'karasu'
57
- #
58
- # i = Rufus::Mnemo::to_integer s
59
- #
60
- # puts i
61
- # # => 125704
62
- #
63
- #
64
- # == Mnemo from the command line
65
- #
66
- # You can use Mnemo directly from the command line :
67
- #
68
- # $ ruby mnemo.rb kotoba
69
- # 141260
70
- # $ ruby mnemo.rb rubi
71
- # 3432
72
- # $ ruby mnemo.rb 2455
73
- # nada
74
- #
75
- # might be useful when used from some scripts.
76
- #
77
- module Mnemo
78
-
79
- V = %w{ a e i o u }
80
- C = %w{ b d g h j k m n p r s t z }
27
+ #
28
+ # = Rufus::Mnemo
29
+ #
30
+ # This module contains methods for converting plain integers (base 10)
31
+ # into words that are easier to read and remember.
32
+ #
33
+ # For example, the equivalent of the (base 10) integer 1329724967 is
34
+ # "takeshimaya".
35
+ #
36
+ # Mnemo uses 70 of the syllables of the Japanese language, it is thus
37
+ # a base 10 to base 70 converter.
38
+ #
39
+ # Mnemo is meant to be used for generating human readable (or more
40
+ # easily rememberable) identifiers. Its first usage is within the
41
+ # OpenWFEru Ruby workflow and bpm engine for generating 'kawaii'
42
+ # business process instance ids.
43
+ #
44
+ # require 'rubygems'
45
+ # require 'rufus/mnemo'
46
+ #
47
+ # s = Rufus::Mnemo::from_integer 125704
48
+ #
49
+ # puts s
50
+ # # => 'karasu'
51
+ #
52
+ # i = Rufus::Mnemo::to_integer s
53
+ # # => 125704
54
+ #
55
+ #
56
+ # == Mnemo from the command line
57
+ #
58
+ # You can use Mnemo directly from the command line :
59
+ #
60
+ # $ ruby mnemo.rb kotoba
61
+ # 141260
62
+ # $ ruby mnemo.rb rubi
63
+ # 3432
64
+ # $ ruby mnemo.rb 2455
65
+ # nada
66
+ #
67
+ # might be useful when used from some scripts.
68
+ #
69
+ module Mnemo
70
+
71
+ VERSION = '1.1.0'
72
+
73
+ V = %w[ a e i o u ]
74
+ C = %w[ b d g h j k m n p r s t z ]
81
75
 
82
76
  SYL = []
83
77
 
84
78
  C.each do |s|
85
- V.each do |v|
86
- SYL << s + v
87
- end
79
+ V.each do |v|
80
+ SYL << s + v
81
+ end
88
82
  end
89
83
 
90
- SYL << "wa"
91
- SYL << "wo"
84
+ SYL << 'wa'
85
+ SYL << 'wo'
92
86
 
93
- SYL << "ya"
94
- SYL << "yo"
95
- SYL << "yu"
87
+ SYL << 'ya'
88
+ SYL << 'yo'
89
+ SYL << 'yu'
96
90
 
97
- SPECIAL = [
98
- [ "hu", "fu" ],
99
- [ "si", "shi" ],
100
- [ "ti", "chi" ],
101
- [ "tu", "tsu" ],
102
- [ "zi", "tzu" ]
91
+ SPECIAL = [
92
+ [ 'hu', 'fu' ],
93
+ [ 'si', 'shi' ],
94
+ [ 'ti', 'chi' ],
95
+ [ 'tu', 'tsu' ],
96
+ [ 'zi', 'tzu' ]
103
97
  ]
104
98
 
105
- #SYL2 = SYL.collect do |syl|
106
- # s = syl
107
- # SPECIAL.each do |a, b|
108
- # if s == a
109
- # s = b
110
- # break
111
- # end
112
- # end
113
- # s
114
- #end
99
+ NEG = 'wi'
100
+ NEGATIVE = /^#{NEG}(.+)$/
115
101
 
116
- #
117
102
  # Turns the given integer into a Mnemo word.
118
103
  #
119
104
  def Mnemo.from_integer (integer)
120
- s = from_i(integer)
121
- to_special(s)
105
+
106
+ return "#{NEG}#{from_integer(-integer)}" if integer < 0
107
+
108
+ s = from_i(integer)
109
+ to_special(s)
122
110
  end
123
111
 
124
- #
125
112
  # Turns the given Mnemo word to its equivalent integer.
126
- #
113
+ #
127
114
  def Mnemo.to_integer (string)
128
- s = from_special(string)
129
- to_i(s)
115
+
116
+ s = from_special(string)
117
+ to_i(s)
130
118
  end
131
119
 
132
- #
133
120
  # Turns a simple syllable into the equivalent number.
134
121
  # For example Mnemo::to_number("fu") will yield 19.
135
122
  #
136
123
  def Mnemo.to_number (syllable)
137
- SYL.each_with_index do |s, index|
138
- return index if syllable == s
139
- end
140
- raise "did not find syllable '#{syllable}'"
124
+
125
+ SYL.each_with_index do |s, index|
126
+ return index if syllable == s
127
+ end
128
+ raise "did not find syllable '#{syllable}'"
141
129
  end
142
130
 
143
- #
144
131
  # Given a Mnemo 'word', will split into its list of syllables.
145
- # For example, "tsunashima" will be split into
132
+ # For example, "tsunashima" will be split into
146
133
  # [ "tsu", "na", "shi", "ma" ]
147
134
  #
148
135
  def Mnemo.split (word)
149
- word = from_special(word)
150
- a = string_split(word)
151
- a_to_special(a)
136
+
137
+ word = from_special(word)
138
+ a = string_split(word)
139
+
140
+ a_to_special(a)
152
141
  end
153
142
 
154
- #
155
- # Returns if the string is a Mnemo word, like "fugu" or
143
+ # Returns if the string is a Mnemo word, like "fugu" or
156
144
  # "toriyamanobashi".
157
145
  #
158
146
  def Mnemo.is_mnemo_word (string)
159
- begin
160
- to_integer(string)
161
- true
162
- rescue #Exception => e
163
- false
164
- end
147
+
148
+ begin
149
+ to_integer(string)
150
+ true
151
+ rescue #Exception => e
152
+ false
153
+ end
165
154
  end
166
155
 
167
156
  private
168
157
 
169
- def Mnemo.string_split (s, result=[])
170
- return result if s.length < 1
171
- result << s[0, 2]
172
- string_split(s[2..-1], result)
173
- end
158
+ def Mnemo.string_split (s, result=[])
174
159
 
175
- def Mnemo.a_to_special (a)
176
- a.collect do |syl|
177
- SPECIAL.each do |a, b|
178
- if syl == a
179
- syl = b
180
- break
181
- end
182
- end
183
- syl
184
- end
185
- end
160
+ return result if s.length < 1
186
161
 
187
- def Mnemo.to_special (s)
188
- SPECIAL.each do |a, b|
189
- s = s.gsub(a, b)
190
- end
191
- s
192
- end
162
+ result << s[0, 2]
163
+
164
+ string_split(s[2..-1], result)
165
+ end
166
+
167
+ def Mnemo.a_to_special (a)
193
168
 
194
- def Mnemo.from_special (s)
195
- SPECIAL.each do |a, b|
196
- s = s.gsub(b, a)
197
- end
198
- s
169
+ a.collect do |syl|
170
+ SPECIAL.each do |a, b|
171
+ if syl == a
172
+ syl = b
173
+ break
174
+ end
199
175
  end
176
+ syl
177
+ end
178
+ end
200
179
 
201
- def Mnemo.from_i (integer)
180
+ def Mnemo.to_special (s)
202
181
 
203
- return '' if integer == 0
182
+ SPECIAL.inject(s) { |s, (a, b)| s.gsub(a, b) }
183
+ end
204
184
 
205
- mod = integer % SYL.length
206
- rest = integer / SYL.length
185
+ def Mnemo.from_special (s)
207
186
 
208
- from_i(rest) + SYL[mod]
209
- end
187
+ SPECIAL.inject(s) { |s, (a, b)| s.gsub(b, a) }
188
+ end
210
189
 
211
- def Mnemo.to_i (s)
212
- return 0 if s.length == 0
213
- SYL.length * to_i(s[0..-3]) + to_number(s[-2, 2])
214
- end
215
- end
190
+ def Mnemo.from_i (integer)
191
+
192
+ return '' if integer == 0
193
+
194
+ mod = integer % SYL.length
195
+ rest = integer / SYL.length
196
+
197
+ from_i(rest) + SYL[mod]
198
+ end
199
+
200
+ def Mnemo.to_i (s)
201
+
202
+ return 0 if s.length == 0
203
+
204
+ if m = s.match(NEGATIVE)
205
+ return -1 * to_i(m[1])
206
+ end
207
+
208
+ SYL.length * to_i(s[0..-3]) + to_number(s[-2, 2])
209
+ end
210
+ end
216
211
  end
217
212
 
218
213
  #
219
214
  # command line interface for Mnemo
220
215
 
221
216
  if __FILE__ == $0
222
- arg = ARGV[0]
223
- if arg and arg != "-h" and arg != "--help"
224
- begin
225
- puts Rufus::Mnemo::from_integer(Integer(arg))
226
- rescue
227
- puts Rufus::Mnemo::to_integer(arg)
228
- end
229
- else
230
- puts
231
- puts "ruby #{$0} {arg}"
232
- puts
233
- puts " If the arg is a 'Mnemo' word, will turn it into the equivalent"
234
- puts " integer."
235
- puts " Else, it will consider the arg as an integer and attempt at"
236
- puts " turning it into a Mnemo [word]."
237
- puts
238
- puts " Mnemo uses #{Rufus::Mnemo::SYL.length} syllables."
239
- puts
217
+
218
+ arg = ARGV[0]
219
+
220
+ if arg and arg != "-h" and arg != "--help"
221
+ begin
222
+ puts Rufus::Mnemo::from_integer(Integer(arg))
223
+ rescue
224
+ puts Rufus::Mnemo::to_integer(arg)
240
225
  end
226
+ else
227
+ puts
228
+ puts "ruby #{$0} {arg}"
229
+ puts
230
+ puts " If the arg is a 'Mnemo' word, will turn it into the equivalent"
231
+ puts " integer."
232
+ puts " Else, it will consider the arg as an integer and attempt at"
233
+ puts " turning it into a Mnemo [word]."
234
+ puts
235
+ puts " Mnemo uses #{Rufus::Mnemo::SYL.length} syllables."
236
+ puts
237
+ end
241
238
  end
242
239
 
@@ -0,0 +1,3 @@
1
+
2
+ require 'rufus/mnemo'
3
+
data/test/test.rb CHANGED
@@ -7,6 +7,8 @@
7
7
  # Sun Mar 18 13:29:37 JST 2007
8
8
  #
9
9
 
10
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
11
+
10
12
  require 'test/unit'
11
13
  require 'rufus/mnemo'
12
14
 
@@ -16,58 +18,78 @@ require 'rufus/mnemo'
16
18
 
17
19
  class MnemoTest < Test::Unit::TestCase
18
20
 
19
- #def setup
20
- #end
21
-
22
- #def teardown
23
- #end
24
-
25
- def test_0
26
-
27
- t = Time.now
28
- #puts t.to_f
29
-
30
- st = t.to_f * 1000 * 10
31
-
32
- #puts st
33
-
34
- st = Integer(st) % (10 * 1000 * 60 * 60)
35
- #st = 28340469
36
-
37
- s = Rufus::Mnemo::from_integer(st)
38
-
39
- st2 = Rufus::Mnemo::to_integer(s)
40
- s2 = Rufus::Mnemo::from_integer(st2)
41
-
42
- #puts st
43
- #puts s
44
-
45
- #puts st2
46
- #puts s2
47
-
48
- assert_equal s, s2
49
- assert_equal st, st2
50
-
51
- a = Rufus::Mnemo::split(s)
52
-
53
- assert_equal a.join, s
54
-
55
- #puts Rufus::Mnemo::to_integer("tunashima")
56
- #puts Rufus::Mnemo::to_integer("tsunashima")
57
-
58
- assert Rufus::Mnemo::is_mnemo_word("takeshi")
59
-
60
- assert Rufus::Mnemo::is_mnemo_word("tsunasima")
61
- assert Rufus::Mnemo::is_mnemo_word("tunashima")
62
-
63
- assert (not Rufus::Mnemo::is_mnemo_word("dsfadf"))
64
- assert (not Rufus::Mnemo::is_mnemo_word("takeshin"))
65
- end
66
- end
21
+ #def setup
22
+ #end
67
23
 
68
- #require 'pp'
69
- #pp Rufus::Mnemo::split(s2)
70
- #
71
- #puts Rufus::Mnemo::is_mnemo_word("asdfadsg")
72
- #puts Rufus::Mnemo::is_mnemo_word(s2)
24
+ #def teardown
25
+ #end
26
+
27
+ def test_0
28
+
29
+ t = Time.now
30
+ #puts t.to_f
31
+
32
+ st = t.to_f * 1000 * 10
33
+
34
+ #puts st
35
+
36
+ st = Integer(st) % (10 * 1000 * 60 * 60)
37
+ #st = 28340469
38
+
39
+ s = Rufus::Mnemo::from_integer(st)
40
+
41
+ st2 = Rufus::Mnemo::to_integer(s)
42
+ s2 = Rufus::Mnemo::from_integer(st2)
43
+
44
+ #puts st
45
+ #puts s
46
+
47
+ #puts st2
48
+ #puts s2
49
+
50
+ assert_equal s, s2
51
+ assert_equal st, st2
52
+
53
+ a = Rufus::Mnemo::split(s)
54
+
55
+ assert_equal a.join, s
56
+
57
+ #puts Rufus::Mnemo::to_integer("tunashima")
58
+ #puts Rufus::Mnemo::to_integer("tsunashima")
59
+
60
+ assert Rufus::Mnemo::is_mnemo_word("takeshi")
61
+
62
+ assert Rufus::Mnemo::is_mnemo_word("tsunasima")
63
+ assert Rufus::Mnemo::is_mnemo_word("tunashima")
64
+
65
+ assert (not Rufus::Mnemo::is_mnemo_word("dsfadf"))
66
+ assert (not Rufus::Mnemo::is_mnemo_word("takeshin"))
67
+ end
68
+
69
+ def test_zero
70
+
71
+ assert_equal '', Rufus::Mnemo::from_integer(0)
72
+ assert_equal 0, Rufus::Mnemo::to_integer('')
73
+ end
74
+
75
+ def test_negatives
76
+
77
+ assert_equal -35, Rufus::Mnemo::to_integer('wina')
78
+ assert_equal 'wina', Rufus::Mnemo::from_integer(-35)
79
+
80
+ assert_equal -1, Rufus::Mnemo::to_integer('wibe')
81
+ assert_equal 'wibe', Rufus::Mnemo::from_integer(-1)
82
+ end
83
+
84
+ def test_wi_bad_position
85
+
86
+ %w(wi wiwi bewi nawi nabewi nawibe nawiwi).each do |bad_mnemo|
87
+
88
+ error = assert_raise RuntimeError do
89
+ Rufus::Mnemo::to_integer(bad_mnemo)
90
+ end
91
+ assert_equal "did not find syllable 'wi'", error.to_s
92
+ end
93
+ end
94
+ end
73
95
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-mnemo
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.0"
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-25 00:00:00 +09:00
12
+ date: 2009-07-21 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description:
16
+ description: Turning (large) integers into japanese sounding words and vice versa
17
17
  email: jmettraux@gmail.com
18
18
  executables: []
19
19
 
@@ -22,12 +22,14 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README.txt
24
24
  files:
25
- - lib/rufus
26
25
  - lib/rufus/mnemo.rb
26
+ - lib/rufus-mnemo.rb
27
27
  - test/test.rb
28
28
  - README.txt
29
29
  has_rdoc: true
30
30
  homepage: http://rufus.rubyforge.org/rufus-mnemo
31
+ licenses: []
32
+
31
33
  post_install_message:
32
34
  rdoc_options: []
33
35
 
@@ -47,10 +49,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
49
  version:
48
50
  requirements: []
49
51
 
50
- rubyforge_project:
51
- rubygems_version: 0.9.5
52
+ rubyforge_project: rufus
53
+ rubygems_version: 1.3.2
52
54
  signing_key:
53
- specification_version: 2
55
+ specification_version: 3
54
56
  summary: Turning (large) integers into japanese sounding words and vice versa
55
57
  test_files:
56
58
  - test/test.rb