tabular_code 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tabular_code.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Zejun Wu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # TabularCode
2
+
3
+ Convertion for tabular codes like
4
+ Chinese [quwei](http://zh.wikipedia.org/wiki/%E5%8C%BA%E4%BD%8D%E7%A0%81_%28%E5%9B%BD%E6%A0%87%29)
5
+ and
6
+ Japanese [kuten](http://ja.wikipedia.org/wiki/JIS_X_0208).
7
+
8
+ Supported row-cell notations:
9
+
10
+ | Module | Language | National standard | Alternative name |
11
+ |---------|--------------------|-------------------|------------------|
12
+ | Quwei | Simplified Chinese | GB 2312-80 | Quwei 区位 |
13
+ | Kuten | Japanese | JIS X 0208:1997 | Kuten 区点 |
14
+ | KSX1001 | Korean | KS X 1001:1992 | |
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'tabular_code'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install tabular_code
29
+
30
+ ## Usage
31
+
32
+ Example program:
33
+
34
+ require 'tabular_code'
35
+
36
+ puts Quwei.to_str([4444] * 44)
37
+ [Quwei, Kuten, KSX1001].each do |i|
38
+ p [i.from_char('一'), i.to_char(5050)]
39
+ end
40
+ p '我爱你'.quwei
41
+ p '愛してる'.kuten
42
+ p '사랑해요'.ksx1001
43
+
44
+ Example output:
45
+
46
+ 烫烫烫烫
47
+ 5027 说
48
+ 1676 傍
49
+ 7673 綱
50
+ [4650, 1614, 3667]
51
+ [1606, 423, 438, 475]
52
+ [2771, 2291, 3956, 3168]
53
+
54
+ See rdoc and rspec tests for more details.
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,26 @@
1
+ require "tabular_code/version"
2
+ require "tabular_code/base"
3
+ require "tabular_code/quwei"
4
+ require "tabular_code/kuten"
5
+ require "tabular_code/ksx1001"
6
+
7
+ Quwei = TabularCode::Quwei
8
+ Kuten = TabularCode::Kuten
9
+ KSX1001 = TabularCode::KSX1001
10
+
11
+ class String
12
+ # Returns an Array of quwei codes
13
+ def quwei
14
+ Quwei.from_str self
15
+ end
16
+
17
+ # Returns an Array of kuten codes
18
+ def kuten
19
+ Kuten.from_str self
20
+ end
21
+
22
+ # Returns an Array of ksx1001 codes
23
+ def ksx1001
24
+ KSX1001.from_str self
25
+ end
26
+ end
@@ -0,0 +1,62 @@
1
+ module TabularCode
2
+ class Error < RuntimeError
3
+ end
4
+
5
+ # Raised when conversion fails.
6
+ class ConversionError < Error
7
+ end
8
+
9
+ module Base
10
+ # Converts a character to corresponding row-cell notation.
11
+ #
12
+ # Returns +nil+ if no such notation exists.
13
+ def from_char(c)
14
+ c = c.encode(@encoding).ord rescue (return nil)
15
+ x = c / 0x100 - 0xA0
16
+ y = c % 0x100 - 0xA0
17
+ valid?(x, y) ? x * 100 + y : nil
18
+ end
19
+
20
+ # Converts a row-cell notation to corresponding character.
21
+ #
22
+ # Returns +nil+ if no such character exists.
23
+ def to_char(c)
24
+ x = c / 100
25
+ y = c % 100
26
+ c = valid?(x, y) ? (0xA0 + x) * 0x100 + (0xA0 + y) : nil
27
+ c.chr(@encoding).encode(Encoding::UTF_8) rescue (return nil)
28
+ end
29
+
30
+ # Converts a String to an Array of row-cell notations.
31
+ #
32
+ # If +:replace+ is given, replaces undefined characters with +:replace+.
33
+ # Otherwise, raises ConversionError for undefined characters.
34
+ def from_str(s, options = {:replace => nil})
35
+ ret = s.chars.map{|i| from_char(i)}
36
+ raise ConversionError if !options.has_key?(:replace) && ret.include?(nil)
37
+ ret.map{|i| i.nil? ? options[:replace] : i}
38
+ end
39
+
40
+ # Converts an Array of row-cell notations to a String.
41
+ #
42
+ # If +:replace+ is given, replaces undefined characters with +:replace+.
43
+ # Otherwise, raises ConversionError for undefined characters.
44
+ def to_str(s, options = {:replace => nil})
45
+ ret = s.map{|i| to_char(i)}
46
+ raise ConversionError if !options.has_key?(:replace) && ret.include?(nil)
47
+ ret.map{|i| i.nil? ? options[:replace] : i} * ''
48
+ end
49
+
50
+ protected
51
+ # :nodoc:
52
+ def encoding=(encoding)
53
+ @encoding = encoding
54
+ end
55
+
56
+ private
57
+ # :nodoc:
58
+ def valid?(x, y)
59
+ (1..94).include?(x) && (1..94).include?(y)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,32 @@
1
+ require "tabular_code/base"
2
+
3
+ module TabularCode
4
+ # KSX1001 code.
5
+ #
6
+ # [Language] Korean
7
+ # [National standard] KS X 1001:1992
8
+ # [Size] 8224 characters (2350 hangul, 4888 hanja)
9
+ # [Layout]
10
+ # * *01-02* General graphic characters (163 characters)
11
+ # * *03* Full-width ASCII characters (94 characters)
12
+ # * *04* Hangul letters (94 characters)
13
+ # * *05* Roman numerals (20 characters) and Greek letters (48 characters)
14
+ # * *06* Box drawing characters (68 characters)
15
+ # * *07* Units of measurement (79 characters)
16
+ # * *08* Latin capital letters (11 characters), ordinal indicator (2 characters), circled characters (69 characters) and vulgar fractions (9 characters)
17
+ # * *09* Latin small letters (16 characters), parenthesized characters (69 characters), superscripts (5 characters) and subscripts (4 characters)
18
+ # * *10* Hiragana (83 characters)
19
+ # * *11* Katakana (86 characters)
20
+ # * *12* Cyrillic letters (66 characters)
21
+ # * *16-40* Hangul characters (2350 characters)
22
+ # * *41* User-definable positions (94 positions)
23
+ # * *42-93* Hanja characters (4888 characters),
24
+ # with 268 duplicate hanja
25
+ # * *94* User-definable positions (94 positions)
26
+ # * Empty positions (424 positions)
27
+ module KSX1001
28
+ extend Base
29
+
30
+ self.encoding = Encoding::EUC_KR
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ require "tabular_code/base"
2
+
3
+ module TabularCode
4
+ # Kuten code.
5
+ #
6
+ # [Language] Japanese
7
+ # [National standard] JIS X 0208:1997
8
+ # [Size] 6879 characters (6355 kanji)
9
+ # [Layout]
10
+ # * *01-02* Special characters (147 characters)
11
+ # * *03* Digits (10 characters) and Latin letters (52 characters)
12
+ # * *04* Hiragana (83 characters)
13
+ # * *05* Katakana (86 characters)
14
+ # * *06* Greek letters (48 characters)
15
+ # * *07* Cyrillic letters (66 characters)
16
+ # * *08* Box drawing characters (32 characters)
17
+ # * *16-47* The kanji in level 1, arranged according to
18
+ # representative reading (2965 characters)
19
+ # * *48-84* The kanji in level 2, arranged according to
20
+ # primary radical and stroke count (3390 characters)
21
+ module Kuten
22
+ extend Base
23
+
24
+ self.encoding = Encoding::EUC_JP
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ require "tabular_code/base"
2
+
3
+ module TabularCode
4
+ # Quwei code.
5
+ #
6
+ # [Language] Simplified Chinese
7
+ # [National standard] GB 2312-80
8
+ # [Size] 7445 characters (6763 Chinese characters)
9
+ # [Layout]
10
+ # * *01* Comprising punctuation and other special characters (94 characters)
11
+ # * *02* Numerals (72 characters)
12
+ # * *03* Full-width ASCII characters (94 characters)
13
+ # * *04* Hiragana (83 characters)
14
+ # * *05* Katakana (86 characters)
15
+ # * *06* Greek letters (48 characters)
16
+ # * *07* Cyrillic letters (66 characters)
17
+ # * *08* Pinyin (26 characters) and Bopomofo (37 characters)
18
+ # * *09* Box drawing characters (76 characters)
19
+ # * *16-55* the first plane for Chinese characters,
20
+ # arranged according to pinyin (3755 characters)
21
+ # * *56-87* the second plane for Chinese characters,
22
+ # arranged according to radical and strokes (3008 characters)
23
+ module Quwei
24
+ extend Base
25
+
26
+ self.encoding = Encoding::EUC_CN
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module TabularCode
2
+ # :nodoc:
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tabular_code/ksx1001'
4
+
5
+ tests = {
6
+ '我' => 6818,
7
+ '愛' => 6881,
8
+ '안' => 3040,
9
+ '녕' => 1971,
10
+ 'あ' => 1002,
11
+ 'イ' => 1104,
12
+ 'A' => 333,
13
+ ' ' => 101,
14
+ '。' => 103
15
+ }
16
+
17
+ describe TabularCode::KSX1001 do
18
+ describe '.from_char' do
19
+ it 'converts valid character to corresponding code' do
20
+ tests.each do |i, j|
21
+ TabularCode::KSX1001.from_char(i).should == j
22
+ end
23
+ end
24
+
25
+ it 'converts invalid character to nil' do
26
+ %w(垚 爱 ℵ 펲).each do |i|
27
+ TabularCode::KSX1001.from_char(i).should be_nil
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '.to_char' do
33
+ it 'converts valid code to corresponding character' do
34
+ tests.each do |i, j|
35
+ TabularCode::KSX1001.to_char(j).should == i
36
+ end
37
+ end
38
+
39
+ it 'converts invalid code to nil' do
40
+ [-1, 32, 1234, 3600, 9999].each do |i|
41
+ TabularCode::KSX1001.to_char(i).should be_nil
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tabular_code/kuten'
4
+
5
+ tests = {
6
+ '我' => 1870,
7
+ '愛' => 1606,
8
+ '氷' => 4125,
9
+ '妛' => 5412,
10
+ 'あ' => 402,
11
+ 'イ' => 504,
12
+ 'A' => 333,
13
+ ' ' => 101,
14
+ '。' => 103
15
+ }
16
+
17
+ describe TabularCode::Kuten do
18
+ describe '.from_char' do
19
+ it 'converts valid character to corresponding kuten code' do
20
+ tests.each do |i, j|
21
+ TabularCode::Kuten.from_char(i).should == j
22
+ end
23
+ end
24
+
25
+ it 'converts invalid character to nil' do
26
+ %w(垚 爱 ℵ 한).each do |i|
27
+ TabularCode::Kuten.from_char(i).should be_nil
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '.to_char' do
33
+ it 'converts valid kuten code to corresponding character' do
34
+ tests.each do |i, j|
35
+ TabularCode::Kuten.to_char(j).should == i
36
+ end
37
+ end
38
+
39
+ it 'converts invalid kuten code to nil' do
40
+ [-1, 32, 1234, 3600, 9999].each do |i|
41
+ TabularCode::Kuten.to_char(i).should be_nil
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tabular_code/quwei'
4
+
5
+ tests = {
6
+ '我' => 4650,
7
+ '爱' => 1614,
8
+ '你' => 3667,
9
+ 'あ' => 402,
10
+ 'イ' => 504,
11
+ 'A' => 333,
12
+ ' ' => 101,
13
+ '。' => 103
14
+ }
15
+
16
+
17
+ describe TabularCode::Quwei do
18
+ describe '.from_char' do
19
+ it 'converts valid character to corresponding quwei code' do
20
+ tests.each do |i, j|
21
+ TabularCode::Quwei.from_char(i).should == j
22
+ end
23
+ end
24
+
25
+ it 'converts invalid character to nil' do
26
+ %w(圡 喆 ℵ 한).each do |i|
27
+ TabularCode::Quwei.from_char(i).should be_nil
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '.to_char' do
33
+ it 'converts valid quwei code to corresponding character' do
34
+ tests.each do |i, j|
35
+ TabularCode::Quwei.to_char(j).should == i
36
+ end
37
+ end
38
+
39
+ it 'converts invalid quwei code to nil' do
40
+ [-1, 32, 1234, 3600, 9999].each do |i|
41
+ TabularCode::Quwei.to_char(i).should be_nil
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '.from_str' do
47
+ it 'converts string to array of quwei codes' do
48
+ TabularCode::Quwei.from_str('榕溶熔').
49
+ should == [7337, 4060, 4059]
50
+ end
51
+
52
+ it 'replaces bad characters with nil' do
53
+ TabularCode::Quwei.from_str('镕榕溶熔塎').
54
+ should == [nil, 7337, 4060, 4059, nil]
55
+ end
56
+
57
+ it 'replaces bad characters with :replace' do
58
+ TabularCode::Quwei.from_str('镕榕溶熔塎', :replace => -1).
59
+ should == [-1, 7337, 4060, 4059, -1]
60
+ end
61
+
62
+ it 'raises error when :replace is empty' do
63
+ ->{ TabularCode::Quwei.from_str('镕榕溶熔塎', {}) }.
64
+ should raise_error(TabularCode::ConversionError)
65
+ end
66
+ end
67
+
68
+ describe '.to_str' do
69
+ it 'converts array of quwei codes to string' do
70
+ TabularCode::Quwei.to_str([*1601 .. 1605]).
71
+ should == '啊阿埃挨哎'
72
+ end
73
+
74
+ it 'ignores bad codes' do
75
+ TabularCode::Quwei.to_str([*1600 .. 1605]).
76
+ should == '啊阿埃挨哎'
77
+ end
78
+
79
+ it 'replaces bad codes with :replace' do
80
+ TabularCode::Quwei.to_str([*1600 .. 1605], :replace => '?').
81
+ should == '?啊阿埃挨哎'
82
+ end
83
+
84
+ it 'raises error when :replace is empty' do
85
+ ->{ TabularCode::Quwei.to_str([*1600 .. 1605], {}) }.
86
+ should raise_error(TabularCode::ConversionError)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tabular_code/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tabular_code"
8
+ gem.version = TabularCode::VERSION
9
+ gem.authors = ["Zejun Wu"]
10
+ gem.email = ["zejun.wu@gmail.com"]
11
+ gem.description = %q{Convertion for tabular codes like Chinese quwei and Japanese kuten}
12
+ gem.summary = %q{Convertion for tabular codes like quwei and kuten}
13
+ gem.homepage = "https://github.com/watashi/tabular_code"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabular_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zejun Wu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Convertion for tabular codes like Chinese quwei and Japanese kuten
15
+ email:
16
+ - zejun.wu@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/tabular_code.rb
28
+ - lib/tabular_code/base.rb
29
+ - lib/tabular_code/ksx1001.rb
30
+ - lib/tabular_code/kuten.rb
31
+ - lib/tabular_code/quwei.rb
32
+ - lib/tabular_code/version.rb
33
+ - spec/tabular_code/ksx1001_spec.rb
34
+ - spec/tabular_code/kuten_spec.rb
35
+ - spec/tabular_code/quwei_spec.rb
36
+ - tabular_code.gemspec
37
+ homepage: https://github.com/watashi/tabular_code
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.23
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Convertion for tabular codes like quwei and kuten
61
+ test_files:
62
+ - spec/tabular_code/ksx1001_spec.rb
63
+ - spec/tabular_code/kuten_spec.rb
64
+ - spec/tabular_code/quwei_spec.rb