openwferu-kotoba 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/openwfe/util/kotoba.rb +236 -0
  2. metadata +46 -0
@@ -0,0 +1,236 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2007, John Mettraux OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+ # $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
34
+ #
35
+
36
+ #
37
+ # "made in Japan"
38
+ #
39
+ # John Mettraux at openwfe.org
40
+ #
41
+
42
+ #
43
+ # = Kotoba
44
+ #
45
+ # This module contains methods for converting plain integers (base 10)
46
+ # into words that are easier to read and remember.
47
+ #
48
+ # For example, the equivalent of the (base 10) integer 1329724967 is
49
+ # "takeshimaya".
50
+ #
51
+ # Kotoba uses 70 of the syllables of the Japanese language, it is thus
52
+ # a base 10 to base 70 converter.
53
+ #
54
+ # Kotoba is meant to be used for generating human readable (or more
55
+ # easily rememberable) identifiers. Its first usage is within the
56
+ # OpenWFEru Ruby workflow and bpm engine for generating 'kawaii'
57
+ # business process intance ids.
58
+ #
59
+ # == Kotoba from the command line
60
+ #
61
+ # You can use Kotoba directly from the command line :
62
+ #
63
+ # $ ruby kotoba.rb kotoba
64
+ # 141260
65
+ # $ ruby kotoba.rb rubi
66
+ # 3432
67
+ # $ ruby kotoba.rb 2455
68
+ # nada
69
+ #
70
+ # might be useful when used from some scripts.
71
+ #
72
+ module Kotoba
73
+
74
+ V = %w{ a e i o u }
75
+ C = %w{ b d g h j k m n p r s t z }
76
+
77
+ SYL = []
78
+
79
+ C.each do |s|
80
+ V.each do |v|
81
+ SYL << s + v
82
+ end
83
+ end
84
+
85
+ SYL << "wa"
86
+ SYL << "wo"
87
+
88
+ SYL << "ya"
89
+ SYL << "yo"
90
+ SYL << "yu"
91
+
92
+ SPECIAL = [
93
+ [ "hu", "fu" ],
94
+ [ "si", "shi" ],
95
+ [ "ti", "chi" ],
96
+ [ "tu", "tsu" ],
97
+ [ "zi", "tzu" ]
98
+ ]
99
+
100
+ #SYL2 = SYL.collect do |syl|
101
+ # s = syl
102
+ # SPECIAL.each do |a, b|
103
+ # if s == a
104
+ # s = b
105
+ # break
106
+ # end
107
+ # end
108
+ # s
109
+ #end
110
+
111
+ #
112
+ # Turns the given integer into a Kotoba word.
113
+ #
114
+ def Kotoba.from_integer (integer)
115
+ s = from_i(integer)
116
+ to_special(s)
117
+ end
118
+
119
+ #
120
+ # Turns the given Kotoba word to its equivalent integer.
121
+ #
122
+ def Kotoba.to_integer (string)
123
+ s = from_special(string)
124
+ to_i(s)
125
+ end
126
+
127
+ #
128
+ # Turns a simple syllable into the equivalent number.
129
+ # For example Kotoba::to_number("fu") will yield 19.
130
+ #
131
+ def Kotoba.to_number (syllable)
132
+ SYL.each_with_index do |s, index|
133
+ return index if syllable == s
134
+ end
135
+ raise "did not find syllable '#{syllable}'"
136
+ end
137
+
138
+ #
139
+ # Given a Kotoba 'word', will split into its list of syllables.
140
+ # For example, "tsunashima" will be split into
141
+ # [ "tsu", "na", "shi", "ma" ]
142
+ #
143
+ def Kotoba.split (word)
144
+ word = from_special(word)
145
+ a = string_split(word)
146
+ a_to_special(a)
147
+ end
148
+
149
+ #
150
+ # Returns if the string is a Kotoba word, like "fugu" or
151
+ # "toriyamanobashi".
152
+ #
153
+ def Kotoba.is_kotoba_word (string)
154
+ begin
155
+ to_integer(string)
156
+ true
157
+ rescue #Exception => e
158
+ false
159
+ end
160
+ end
161
+
162
+ protected
163
+
164
+ def Kotoba.string_split (s, result=[])
165
+ return result if s.length < 1
166
+ result << s[0, 2]
167
+ string_split(s[2..-1], result)
168
+ end
169
+
170
+ def Kotoba.a_to_special (a)
171
+ a.collect do |syl|
172
+ SPECIAL.each do |a, b|
173
+ if syl == a
174
+ syl = b
175
+ break
176
+ end
177
+ end
178
+ syl
179
+ end
180
+ end
181
+
182
+ def Kotoba.to_special (s)
183
+ SPECIAL.each do |a, b|
184
+ s = s.gsub(a, b)
185
+ end
186
+ s
187
+ end
188
+
189
+ def Kotoba.from_special (s)
190
+ SPECIAL.each do |a, b|
191
+ s = s.gsub(b, a)
192
+ end
193
+ s
194
+ end
195
+
196
+ def Kotoba.from_i (integer)
197
+
198
+ return '' if integer == 0
199
+
200
+ mod = integer % SYL.length
201
+ rest = integer / SYL.length
202
+
203
+ return from_i(rest) + SYL[mod]
204
+ end
205
+
206
+ def Kotoba.to_i (s)
207
+ return 0 if s.length == 0
208
+ return SYL.length * to_i(s[0..-3]) + to_number(s[-2, 2])
209
+ end
210
+ end
211
+
212
+ #
213
+ # command line interface for Kotoba
214
+
215
+ if __FILE__ == $0
216
+ arg = ARGV[0]
217
+ if arg and arg != "-h" and arg != "--help"
218
+ begin
219
+ puts Kotoba::from_integer(Integer(arg))
220
+ rescue
221
+ puts Kotoba::to_integer(arg)
222
+ end
223
+ else
224
+ puts
225
+ puts "ruby #{$0} {arg}"
226
+ puts
227
+ puts " If the arg is a 'Kotoba' word, will turn it into the equivalent"
228
+ puts " integer."
229
+ puts " Else, it will consider the arg as an integer and attempt at"
230
+ puts " turning it into a Kotoba [word]."
231
+ puts
232
+ puts " Kotoba uses #{Kotoba::SYL.length} syllables."
233
+ puts
234
+ end
235
+ end
236
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: openwferu-kotoba
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.9.9
7
+ date: 2007-04-23 00:00:00 +09:00
8
+ summary: Turning (big) integers into japanese sounding words and vice versa
9
+ require_paths:
10
+ - lib
11
+ email: john at openwfe dot org
12
+ homepage: http://openwferu.rubyforge.org/kotoba.html
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: openwferu-kotoba
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - John Mettraux
31
+ files:
32
+ - lib/openwfe/util/kotoba.rb
33
+ test_files: []
34
+
35
+ rdoc_options: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ requirements: []
44
+
45
+ dependencies: []
46
+