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 OpenWFEru Ruby workflow and bpm 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.1'
- 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
- .a_to_special(a) ⇒ Object
- .from_i(integer) ⇒ Object (also: to_s)
-
.from_integer(integer) ⇒ Object
Turns the given integer into a Mnemo word.
- .from_special(s) ⇒ Object
-
.is_mnemo_word(string) ⇒ Object
Returns if the string is a Mnemo word, like “fugu” or “toriyamanobashi”.
-
.split(word) ⇒ Object
Given a Mnemo ‘word’, will split into its list of syllables.
- .string_split(s, result = []) ⇒ Object
- .to_i(s) ⇒ Object
-
.to_integer(string) ⇒ Object
Turns the given Mnemo word to its equivalent integer.
-
.to_number(syllable) ⇒ Object
Turns a simple syllable into the equivalent number.
- .to_special(s) ⇒ Object
Class Method Details
.a_to_special(a) ⇒ Object
153 154 155 156 |
# File 'lib/rufus/mnemo.rb', line 153 def self.a_to_special(a) a.collect { |syl| SPECIAL.find { |aa, bb| syl == bb } || syl } end |
.from_i(integer) ⇒ Object Also known as: to_s
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rufus/mnemo.rb', line 168 def self.from_i(integer) 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 |
.from_integer(integer) ⇒ Object
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_i(integer)) end |
.from_special(s) ⇒ Object
163 164 165 166 |
# File 'lib/rufus/mnemo.rb', line 163 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”.
134 135 136 137 138 139 140 141 142 |
# File 'lib/rufus/mnemo.rb', line 134 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 129 |
# File 'lib/rufus/mnemo.rb', line 123 def self.split(word) word = from_special(word) a = string_split(word) a_to_special(a) end |
.string_split(s, result = []) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/rufus/mnemo.rb', line 144 def self.string_split(s, result=[]) return result if s.length < 1 result << s[0, 2] string_split(s[2..-1], result) end |
.to_i(s) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rufus/mnemo.rb', line 182 def self.to_i(s) 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 |
.to_integer(string) ⇒ Object
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
158 159 160 161 |
# File 'lib/rufus/mnemo.rb', line 158 def self.to_special(s) SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(a, b) } end |