gimchi 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = %q{gimchi}
7
- gem.version = "0.2.0"
7
+ gem.version = "0.2.1"
8
8
  gem.authors = ["Junegunn Choi"]
9
9
  gem.email = ["junegunn.c@gmail.com"]
10
10
  gem.description = %q{A Ruby gem for Korean characters}
@@ -8,77 +8,11 @@ require 'gimchi/char'
8
8
  require 'gimchi/pronouncer'
9
9
 
10
10
  class Gimchi
11
- class << self
12
- def setup
13
- @@default ||= Gimchi.new
14
- end
15
-
16
- def Char ch
17
- @@default.kchar ch
18
- end
19
-
20
- [
21
- :decompose,
22
- :compose,
23
- :korean_char?,
24
- :complete_korean_char?,
25
- :kchar,
26
- :kchar?,
27
- :chosung?,
28
- :jungsung?,
29
- :jongsung?,
30
- :read_number,
31
- :pronounce,
32
- :romanize
33
- ].each do |sym|
34
- define_method(sym) do |*arg, &b|
35
- @@default.send sym, *arg, &b
36
- end
37
- end
38
- end
39
-
40
- CONFIG_FILE_PATH = File.expand_path('../../config/default.yml', __FILE__)
41
- attr_reader :config, :chosungs, :jungsungs, :jongsungs
42
-
43
- # Initialize Gimchi::Korean.
44
- def initialize
45
- symbolize_keys = lambda do |val|
46
- case val
47
- when Hash
48
- {}.tap do |h|
49
- val.each do |k, v|
50
- k = k.gsub(' ', '_').to_sym if k =~ /[a-z0-9 ]/
51
- h[k] = symbolize_keys.call v
52
- end
53
- end
54
- when Array
55
- val.map { |v| symbolize_keys.call v }
56
- else
57
- val
58
- end
59
- end
60
- @config = symbolize_keys.call YAML.load(File.read CONFIG_FILE_PATH)
11
+ class << self
12
+ attr_reader :chosungs, :jungsungs, :jongsungs
61
13
 
62
- [
63
- @config[:romanization][:post_substitution],
64
- @config[:number][:post_substitution],
65
- @config[:number][:alt_notation][:post_substitution]
66
- ].each do |r|
67
- r.keys.each do |k|
68
- r[Regexp.compile k.to_s] = r.delete k
69
- end
70
- end
71
- @config.freeze
72
-
73
- @pronouncer = Gimchi::Pronouncer.send :new, self
74
-
75
- @chosungs = config[:structure][:chosung]
76
- @jungsungs = config[:structure][:jungsung]
77
- @jongsungs = config[:structure][:jongsung]
78
- @chosung_set = Set[*@chosungs]
79
- @jungsung_set = Set[*@jungsungs]
80
- @jongsung_set = Set[*@jongsungs]
81
- @all = @chosung_set + @jungsung_set + @jongsung_set
14
+ def Char ch
15
+ kchar ch
82
16
  end
83
17
 
84
18
  # Decompose a Korean character into 3 components
@@ -144,11 +78,10 @@ class Gimchi
144
78
  ch.unpack('U').all? { | c | c >= 0xAC00 && c <= 0xD7A3 }
145
79
  end
146
80
 
147
- # Returns a Gimchi::Char object for the given Korean character.
148
- # @param [String] ch Korean character in String
149
- # @return [Gimchi::Char] Gimchi::Char instance
81
+ # @deprecated
82
+ # @private
150
83
  def kchar ch
151
- Gimchi::Char.new(self, ch)
84
+ Gimchi::Char.new(ch)
152
85
  end
153
86
 
154
87
  # Reads numeric expressions in Korean way.
@@ -206,7 +139,7 @@ class Gimchi
206
139
  :slur => false
207
140
  }.merge options
208
141
 
209
- rdata = config[:romanization]
142
+ rdata = @config[:romanization]
210
143
  post_subs = rdata[:post_substitution]
211
144
  rdata = [rdata[:chosung], rdata[:jungsung], rdata[:jongsung]]
212
145
 
@@ -252,12 +185,14 @@ class Gimchi
252
185
  end
253
186
 
254
187
  private
188
+ CONFIG_FILE_PATH = File.expand_path('../../config/default.yml', __FILE__)
189
+
255
190
  def str_length str
256
191
  str.length
257
192
  end
258
193
 
259
194
  def read_number_sub num, next_char
260
- nconfig = config[:number]
195
+ nconfig = @config[:number]
261
196
 
262
197
  if num == '0'
263
198
  return nconfig[:digits].first
@@ -374,8 +309,54 @@ private
374
309
  end
375
310
  ret
376
311
  end
312
+
313
+ # @private
314
+ def setup
315
+ symbolize_keys = lambda do |val|
316
+ case val
317
+ when Hash
318
+ {}.tap do |h|
319
+ val.each do |k, v|
320
+ k = k.gsub(' ', '_').to_sym if k =~ /[a-z0-9 ]/
321
+ h[k] = symbolize_keys.call v
322
+ end
323
+ end
324
+ when Array
325
+ val.map { |v| symbolize_keys.call v }
326
+ else
327
+ val
328
+ end
329
+ end
330
+ @config = symbolize_keys.call YAML.load(File.read CONFIG_FILE_PATH)
331
+
332
+ [
333
+ @config[:romanization][:post_substitution],
334
+ @config[:number][:post_substitution],
335
+ @config[:number][:alt_notation][:post_substitution]
336
+ ].each do |r|
337
+ r.keys.each do |k|
338
+ r[Regexp.compile k.to_s] = r.delete k
339
+ end
340
+ end
341
+ @config.freeze
342
+
343
+ @pronouncer = Gimchi::Pronouncer.send :new, @config[:pronouncer], @config[:structure]
344
+
345
+ @chosungs = @config[:structure][:chosung]
346
+ @jungsungs = @config[:structure][:jungsung]
347
+ @jongsungs = @config[:structure][:jongsung]
348
+ @chosung_set = Set[*@chosungs]
349
+ @jungsung_set = Set[*@jungsungs]
350
+ @jongsung_set = Set[*@jongsungs]
351
+ @all = @chosung_set + @jungsung_set + @jongsung_set
352
+ end
353
+ end
354
+ private
355
+ def initialize
356
+ raise NoMethodError, "Gimchi is a singleton class"
357
+ end
377
358
  end#Gimchi
378
359
 
379
360
  require 'gimchi/patch_1.8'
380
361
 
381
- Gimchi.setup
362
+ Gimchi.send :setup
@@ -13,13 +13,11 @@ class Gimchi
13
13
  # @return [String] Jongsung component of this character.
14
14
  attr_reader :jongsung
15
15
 
16
- # @param [Gimchi] gimchi Gimchi instance
17
16
  # @param [String] kchar Korean character string
18
- def initialize gimchi, kchar
19
- raise ArgumentError.new('Not a korean character') unless gimchi.korean_char? kchar
17
+ def initialize kchar
18
+ raise ArgumentError.new('Not a korean character') unless Gimchi.korean_char? kchar
20
19
 
21
- @gimchi = gimchi
22
- if @gimchi.complete_korean_char? kchar
20
+ if Gimchi.complete_korean_char? kchar
23
21
  c = kchar.unpack('U').first
24
22
  n = c - 0xAC00
25
23
  # '가' ~ '깋' -> 'ㄱ'
@@ -28,14 +26,14 @@ class Gimchi
28
26
  n = n % (21 * 28)
29
27
  n2 = n / 28;
30
28
  n3 = n % 28;
31
- self.chosung = @gimchi.chosungs[n1]
32
- self.jungsung = @gimchi.jungsungs[n2]
33
- self.jongsung = ([nil] + @gimchi.jongsungs)[n3]
34
- elsif @gimchi.chosung? kchar
29
+ self.chosung = Gimchi.chosungs[n1]
30
+ self.jungsung = Gimchi.jungsungs[n2]
31
+ self.jongsung = ([nil] + Gimchi.jongsungs)[n3]
32
+ elsif Gimchi.chosung? kchar
35
33
  self.chosung = kchar
36
- elsif @gimchi.jungsung? kchar
34
+ elsif Gimchi.jungsung? kchar
37
35
  self.jungsung = kchar
38
- elsif @gimchi.jongsung? kchar
36
+ elsif Gimchi.jongsung? kchar
39
37
  self.jongsung = kchar
40
38
  end
41
39
  end
@@ -43,23 +41,23 @@ class Gimchi
43
41
  # Recombines components into a korean character.
44
42
  # @return [String] Combined korean character
45
43
  def to_s
46
- @gimchi.compose chosung, jungsung, jongsung
44
+ Gimchi.compose chosung, jungsung, jongsung
47
45
  end
48
46
 
49
47
  # Sets the chosung component.
50
48
  # @param [String]
51
49
  def chosung= c
52
50
  raise ArgumentError.new('Invalid chosung component') if
53
- c && @gimchi.chosung?(c) == false
54
- @chosung = c && c.dup.extend(Component).tap { |e| e.kor = @gimchi }
51
+ c && Gimchi.chosung?(c) == false
52
+ @chosung = c && c.dup.extend(Component).tap { |e| e.kor = Gimchi }
55
53
  end
56
54
 
57
55
  # Sets the jungsung component
58
56
  # @param [String]
59
57
  def jungsung= c
60
58
  raise ArgumentError.new('Invalid jungsung component') if
61
- c && @gimchi.jungsung?(c) == false
62
- @jungsung = c && c.dup.extend(Component).tap { |e| e.kor = @gimchi }
59
+ c && Gimchi.jungsung?(c) == false
60
+ @jungsung = c && c.dup.extend(Component).tap { |e| e.kor = Gimchi }
63
61
  end
64
62
 
65
63
  # Sets the jongsung component
@@ -67,8 +65,8 @@ class Gimchi
67
65
  # @param [String]
68
66
  def jongsung= c
69
67
  raise ArgumentError.new('Invalid jongsung component') if
70
- c && @gimchi.jongsung?(c) == false
71
- @jongsung = c && c.dup.extend(Component).tap { |e| e.kor = @gimchi }
68
+ c && Gimchi.jongsung?(c) == false
69
+ @jongsung = c && c.dup.extend(Component).tap { |e| e.kor = Gimchi }
72
70
  end
73
71
 
74
72
  # Returns Array of three components.
@@ -2,9 +2,11 @@ if RUBY_VERSION =~ /^1\.8\./
2
2
  $KCODE = 'U'
3
3
 
4
4
  class Gimchi
5
- private
5
+ class << self
6
+ private
6
7
  def str_length str
7
8
  str.scan(/./mu).length
8
9
  end
10
+ end
9
11
  end#Gimchi
10
12
  end
@@ -7,9 +7,9 @@ class Gimchi
7
7
  # @private
8
8
  class Pronouncer
9
9
  private
10
- def initialize gimchi
11
- @gimchi = gimchi
12
- @pconfig = gimchi.config[:pronouncer]
10
+ def initialize pconfig, structure
11
+ @pconfig = pconfig
12
+ @structure = structure
13
13
  end
14
14
 
15
15
  def pronounce! str, options = {}
@@ -17,7 +17,7 @@ class Gimchi
17
17
  "sequence_for_#{options[:each_char] ? '1' : '2'}".to_sym] - options[:except]
18
18
 
19
19
  # Dissecting
20
- @chars = str.each_char.map { |c| @gimchi.kchar(c) rescue c }
20
+ @chars = str.each_char.map { |c| Gimchi.kchar(c) rescue c }
21
21
  @orig_chars = @chars.dup
22
22
 
23
23
  # Padding
@@ -86,12 +86,12 @@ class Gimchi
86
86
 
87
87
  # shortcut
88
88
  def fortis_map
89
- @gimchi.config[:structure][:fortis_map]
89
+ @structure[:fortis_map]
90
90
  end
91
91
 
92
92
  # shortcut
93
93
  def double_consonant_map
94
- @gimchi.config[:structure][:double_consonant_map]
94
+ @structure[:double_consonant_map]
95
95
  end
96
96
 
97
97
  # 제5항: ‘ㅑ ㅒ ㅕ ㅖ ㅘ ㅙ ㅛ ㅝ ㅞ ㅠ ㅢ’는 이중 모음으로 발음한다.
@@ -299,7 +299,7 @@ class Gimchi
299
299
 
300
300
  word = @kc.to_s + @next_kc.to_s
301
301
  if map.keys.include? word
302
- new_char = @gimchi.kchar(map[word].scan(/./mu)[1])
302
+ new_char = Gimchi.kchar(map[word].scan(/./mu)[1])
303
303
  @next_kc.chosung = new_char.chosung
304
304
  @next_kc.jongsung = new_char.jongsung
305
305
 
@@ -226,4 +226,8 @@ class TestGimchi < Test::Unit::TestCase
226
226
 
227
227
  assert_equal 'ㅋ', Gimchi.compose(*ret)
228
228
  end
229
+
230
+ def test_singleton
231
+ assert_raise(NoMethodError) { Gimchi.new }
232
+ end
229
233
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gimchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-28 00:00:00.000000000 Z
12
+ date: 2013-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ansi