lib/rufus/mnemo.rb in rufus-mnemo-1.2.0 vs lib/rufus/mnemo.rb in rufus-mnemo-1.2.1
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2007-2010, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2007-2011, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -66,30 +66,20 @@
#
# might be useful when used from some scripts.
#
module Mnemo
- VERSION = '1.2.0'
+ VERSION = '1.2.1'
- V = %w[ a e i o u ]
- C = %w[ b d g h j k m n p r s t z ]
+ 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
+ ])
- SYL = []
-
- C.each do |s|
- V.each do |v|
- SYL << s + v
- end
- end
-
- SYL << 'wa'
- SYL << 'wo'
-
- SYL << 'ya'
- SYL << 'yo'
- SYL << 'yu'
-
SPECIAL = [
[ 'hu', 'fu' ],
[ 'si', 'shi' ],
[ 'ti', 'chi' ],
[ 'tu', 'tsu' ],
@@ -99,30 +89,28 @@
NEG = 'wi'
NEGATIVE = /^#{NEG}(.+)$/
# Turns the given integer into a Mnemo word.
#
- def self.from_integer (integer)
+ def self.from_integer(integer)
return "#{NEG}#{from_integer(-integer)}" if integer < 0
- s = from_i(integer)
- to_special(s)
+ to_special(from_i(integer))
end
# Turns the given Mnemo word to its equivalent integer.
#
- def self.to_integer (string)
+ def self.to_integer(string)
- s = from_special(string)
- to_i(s)
+ to_i(from_special(string))
end
# Turns a simple syllable into the equivalent number.
# For example Mnemo::to_number("fu") will yield 19.
#
- def self.to_number (syllable)
+ def self.to_number(syllable)
SYL.each_with_index do |s, index|
return index if syllable == s
end
raise "did not find syllable '#{syllable}'"
@@ -130,58 +118,56 @@
# Given a Mnemo 'word', will split into its list of syllables.
# For example, "tsunashima" will be split into
# [ "tsu", "na", "shi", "ma" ]
#
- def self.split (word)
+ def self.split(word)
word = from_special(word)
a = string_split(word)
a_to_special(a)
end
# Returns if the string is a Mnemo word, like "fugu" or
# "toriyamanobashi".
#
- def self.is_mnemo_word (string)
+ def self.is_mnemo_word(string)
begin
to_integer(string)
true
- rescue #Exception => e
+ rescue
false
end
end
- private
+ def self.string_split(s, result=[])
- def self.string_split (s, result=[])
-
return result if s.length < 1
result << s[0, 2]
string_split(s[2..-1], result)
end
- def self.a_to_special (a)
+ def self.a_to_special(a)
a.collect { |syl| SPECIAL.find { |aa, bb| syl == bb } || syl }
end
- def self.to_special (s)
+ def self.to_special(s)
SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(a, b) }
end
- def self.from_special (s)
+ def self.from_special(s)
SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(b, a) }
end
- def self.from_i (integer)
+ def self.from_i(integer)
return '' if integer == 0
mod = integer % SYL.length
rest = integer / SYL.length
@@ -191,18 +177,24 @@
# mathn prevention
from_i(rest) + SYL[mod]
end
- def self.to_i (s)
+ def self.to_i(s)
- return 0 if s.length == 0
+ 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
+
+ class << self
+ alias to_s from_i
end
end
end
#