hangeul_comp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hangeul_comp.rb +96 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1fd6a31ef6d64616073ad7b66efe539de35a011
4
+ data.tar.gz: d2da0931a51656fd51a3e32dd19c757a25fb4146
5
+ SHA512:
6
+ metadata.gz: 5fdc4c24de46037c8d5eab8a5852db4927279f87a472aa2e9673fba59d3cdb9d70eb1837c6047f86f8f14b33cd1b09aecc5d8feb5d757122934317a2a18bc09e
7
+ data.tar.gz: 3060574bcb33820fc9d3b27d621e3b119f78ddca129db48505cd63e207caf3c89d68367d808edc6d4002307a179ef13b9cb008b5cd65d6f5766e6618acb7122b
@@ -0,0 +1,96 @@
1
+ # Override String class to add hangeul_comp methods!
2
+ class String
3
+
4
+ # Make combinative Hangeul characters to completive one.
5
+ #
6
+ # 조합형 코드로 작성된 문자를 완성형으로 변환합니다.
7
+ #
8
+ # Example:
9
+ # >> "\u1102\u1163\u11bc".ko_compose
10
+ # => "냥"
11
+
12
+ def ko_compose
13
+ string = self.split("")
14
+
15
+ result = ""
16
+
17
+ string.each_with_index do |char, i|
18
+ # 앞에서 이미 처리가 완료되어 nil이 된 경우 다음으로 넘어갑니다.
19
+ next unless char
20
+
21
+ # 글자에서 첫소리를 찾아냅니다. 첫소리로 시작하지 않는 경우 다음으로 넘어갑니다.
22
+ unless (0x1100 .. 0x1112).include? char.ord
23
+ result += char
24
+ next
25
+ end
26
+
27
+ # 다음 글자가 가운뎃소리인지 확인합니다.
28
+ if (0x1161 .. 0x1175).include? string[i+1].ord
29
+ # 가운뎃소리일 경우 킵해둡니다.
30
+ jungseong, string[i+1] = string[i+1], nil
31
+ else
32
+ # 가운뎃소리가 아닌 경우 현재 문자를 결과와 합치고, 다음으로 넘어갑니다.
33
+ result += char
34
+ next
35
+ end
36
+
37
+ i_choseong = char.ord - 0x1100
38
+ i_jungseong = jungseong.ord - 0x1161
39
+
40
+ # 다음 글자가 끝소리인지 확인합니다.
41
+ i_jongseong = if string[i+2] && ((0x11a8 .. 0x11c2).include? string[i+2].ord)
42
+ # 끝소리인 경우 keep해둡니다.
43
+ jongseong, string[i+2] = string[i+2], nil
44
+ jongseong.ord - 0x11a8 + 1
45
+ else
46
+ 0
47
+ end
48
+
49
+ # 합체!!!
50
+ char = [0xac00 + (i_choseong * 28 * 21) + (i_jungseong * 28) + i_jongseong].pack("U")
51
+ result += char
52
+ end
53
+
54
+ result
55
+ end
56
+
57
+ # make completive Hangeul characters to combinative one.
58
+ #
59
+ # 완성형 문자로 작성된 한글 문자를 조합형으로 변환합니다.
60
+ #
61
+ # Example:
62
+ # >> "꺄아".ko_decompose
63
+ # => "\u1101\u1163\u110b\u1161"
64
+
65
+ def ko_decompose
66
+ result = ""
67
+
68
+ self.each_char do |char, i|
69
+ unless (44032 .. 55203).include? char.ord
70
+ result += char
71
+ next
72
+ end
73
+
74
+ char = char.ord - 0xAC00
75
+
76
+ i_jongseong = char % 28
77
+ i_jungseong = ((char - i_jongseong) % (28 * 21)) / 28
78
+ i_choseong = (char / (28 * 21)).floor
79
+
80
+ jongseong = if i_jongseong != 0
81
+ 0x11a8 + i_jongseong - 1
82
+ else
83
+ nil
84
+ end
85
+
86
+ jungseong = i_jungseong + 0x1161
87
+ choseong = i_choseong + 0x1100
88
+
89
+ # 조금 더 깔끔하게 할 수 있는 방법이 있을까요....
90
+ result += [choseong].pack("U") + [jungseong].pack("U")
91
+ result += [jongseong].pack("U") if jongseong
92
+ end
93
+
94
+ result
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hangeul_comp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leo Sangwon Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Hangeul(한글) Composing/Decomposing gem
14
+ email: public@leo.re.kr
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/hangeul_comp.rb
20
+ homepage: http://github.com/devleoper/hangeul_comp
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Hangeul Composer/Decomposer
44
+ test_files: []