Module: Rufus::Mnemo

Defined in:
lib/rufus/mnemo.rb

Overview

Rufus::Mnemo

This module contains methods for converting plain integers (base 10) into words that are easier to read and remember.

For example, the equivalent of the (base 10) integer 1329724967 is “takeshimaya”.

Mnemo uses 70 of the syllables of the Japanese language, it is thus a base 10 to base 70 converter.

Mnemo is meant to be used for generating human readable (or more easily rememberable) identifiers. Its first usage is within the ruote Ruby workflow engine for generating ‘kawaii’ business process instance ids.

require 'rubygems'
require 'rufus/mnemo'

s = Rufus::Mnemo::from_integer 125704

puts s
  # => 'karasu'

i = Rufus::Mnemo::to_integer s
  # => 125704

Mnemo from the command line

You can use Mnemo directly from the command line :

$ ruby mnemo.rb kotoba
141260
$ ruby mnemo.rb rubi
3432
$ ruby mnemo.rb 2455
nada

might be useful when used from some scripts.

Constant Summary collapse

VERSION =
'1.2.3'
SYL =
%w[
  b d g h j k m n p r s t z
].product(%w[
  a e i o u
]).collect { |c, v| c + v }.concat(%w[
  wa wo ya yo yu
])
SPECIAL =
[
  [ 'hu', 'fu' ],
  [ 'si', 'shi' ],
  [ 'ti', 'chi' ],
  [ 'tu', 'tsu' ],
  [ 'zi', 'tzu' ]
]
NEG =
'wi'
NEGATIVE =
/^#{NEG}(.+)$/

Class Method Summary collapse

Class Method Details

._from_integer(integer) ⇒ Object

:nodoc:



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rufus/mnemo.rb', line 167

def self._from_integer(integer) # :nodoc:

  return '' if integer == 0

  mod = integer % SYL.length
  rest = integer / SYL.length

  rest = rest.to_i
  mod = mod.to_i
    # mathn prevention

  from_i(rest) + SYL[mod]
end

._to_i(s) ⇒ Object

:nodoc:



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rufus/mnemo.rb', line 181

def self._to_i(s) # :nodoc:

  if s.length == 0
    return 0
  end

  if m = s.match(NEGATIVE)
    return -1 * _to_i(m[1])
  end

  SYL.length * _to_i(s[0..-3]) + to_number(s[-2, 2])
end

.a_to_special(a) ⇒ Object



152
153
154
155
# File 'lib/rufus/mnemo.rb', line 152

def self.a_to_special(a)

  a.collect { |syl| (SPECIAL.assoc(syl) || [ nil, syl ])[1] }
end

.from_integer(integer) ⇒ Object Also known as: to_string, to_s, from_i

Turns the given integer into a Mnemo word.



94
95
96
97
98
99
# File 'lib/rufus/mnemo.rb', line 94

def self.from_integer(integer)

  return "#{NEG}#{from_integer(-integer)}" if integer < 0

  to_special(_from_integer(integer))
end

.from_special(s) ⇒ Object



162
163
164
165
# File 'lib/rufus/mnemo.rb', line 162

def self.from_special(s)

  SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(b, a) }
end

.is_mnemo_word(string) ⇒ Object

Returns if the string is a Mnemo word, like “fugu” or “toriyamanobashi”.



133
134
135
136
137
138
139
140
141
# File 'lib/rufus/mnemo.rb', line 133

def self.is_mnemo_word(string)

  begin
    to_integer(string)
    true
  rescue
    false
  end
end

.split(word) ⇒ Object

Given a Mnemo ‘word’, will split into its list of syllables. For example, “tsunashima” will be split into

“tsu”, “na”, “shi”, “ma”


123
124
125
126
127
128
# File 'lib/rufus/mnemo.rb', line 123

def self.split(word)

  a_to_special(
    string_split(
      from_special(word)))
end

.string_split(s, result = []) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/rufus/mnemo.rb', line 143

def self.string_split(s, result=[])

  return result if s.length < 1

  result << s[0, 2]

  string_split(s[2..-1], result)
end

.to_integer(string) ⇒ Object Also known as: to_i, from_string, from_s

Turns the given Mnemo word to its equivalent integer.



103
104
105
106
# File 'lib/rufus/mnemo.rb', line 103

def self.to_integer(string)

  _to_i(from_special(string))
end

.to_number(syllable) ⇒ Object

Turns a simple syllable into the equivalent number. For example Mnemo::to_number(“fu”) will yield 19.



111
112
113
114
115
116
117
# File 'lib/rufus/mnemo.rb', line 111

def self.to_number(syllable)

  SYL.each_with_index do |s, index|
    return index if syllable == s
  end
  raise "did not find syllable '#{syllable}'"
end

.to_special(s) ⇒ Object



157
158
159
160
# File 'lib/rufus/mnemo.rb', line 157

def self.to_special(s)

  SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(a, b) }
end