ya_kansuji 0.1.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ChangeLog +0 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/lib/ya_kansuji/core_ext.rb +23 -0
- data/lib/ya_kansuji/formatter/simple.rb +29 -0
- data/lib/ya_kansuji/version.rb +3 -0
- data/lib/ya_kansuji.rb +103 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8cda61967cf509cb1681946b6ff0c7c82afa4f48fd307311f5a274a2e33dcc7a
|
4
|
+
data.tar.gz: 222c8e0a384159879eb9590ab25b6977daadc7f612ad42011368f920349d1960
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b69375378cb7bba1ba645714dee0798d9173d0cbbee8a5316088321f358b33990715a6812a0772a49bde7f48be5e354cdc3759d7c178187ac626e2c0471e97ad
|
7
|
+
data.tar.gz: 045e09e2fb3646791414a80453554745efaab69474f0ffd6644d3fd70e3545e77c10b2630daa2fd5cfd789fb521ccc184b55f44b766fbbc1e77a93d12b050de0
|
data/ChangeLog
ADDED
File without changes
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Tatsuki Sugiura
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# TODO: THIS GEM IS ALPHA VERSION, NOW.
|
2
|
+
|
3
|
+
# YaKansuji - もう一つの (やりすぎ) ruby 漢数字ライブラリ
|
4
|
+
|
5
|
+
Yet another (ultimate) Japanese Kansuji library for ruby.
|
6
|
+
|
7
|
+
YaKansuji は ruby 用の日本語の漢数字ライブラリです。 和暦ライブラリの [wareki](https://github.com/sugi/wareki) から派生していますが、アラビア数字混じりの漢数字表記など、かなり多彩な表現を数値と相互変換することができます。
|
8
|
+
|
9
|
+
日本語以外の漢数字に関しては一切サポートしていません。
|
10
|
+
|
11
|
+
## 機能 / Features
|
12
|
+
|
13
|
+
* 読み取り: 以下の混在したテキストを数値に変換出来ます
|
14
|
+
* 漢数字
|
15
|
+
* 全角数字 ()
|
16
|
+
* 単位なし (
|
17
|
+
* ゼロ ()
|
18
|
+
* 位取り (2億0010万, 千〇〇二)
|
19
|
+
* 大字 (
|
20
|
+
* カンマ表現
|
21
|
+
* 数値から漢数字, 漢字混じり数値文字列フォーマット
|
22
|
+
* 複数のビルトインフォーマッタ
|
23
|
+
* フォーマッタプラグイン機構
|
24
|
+
* 標準の String, Number クラスの拡張 (必要があれば)
|
25
|
+
|
26
|
+
## インストール / Installation
|
27
|
+
|
28
|
+
Gemfile に以下のようにして、bundle を実行するか、
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'ya_kansuji'
|
32
|
+
```
|
33
|
+
|
34
|
+
もしくは gem コマンドで直接インストールしてください。
|
35
|
+
|
36
|
+
$ gem install ya_kansuji
|
37
|
+
|
38
|
+
## 使い方 / Usage
|
39
|
+
|
40
|
+
### ビルトインクラス拡張
|
41
|
+
|
42
|
+
標準ではビルトインクラスの拡張をしません。
|
43
|
+
|
44
|
+
bundle でない場合は
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
|
51
|
+
### 読み取り (漢数字 → 数値) / Parse kansuji
|
52
|
+
|
53
|
+
TODO: Write usage instructions here
|
54
|
+
|
55
|
+
### フォーマット (数値 → 漢数字) /
|
56
|
+
|
57
|
+
## Development
|
58
|
+
|
59
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sugi/ya_kansuji.
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
70
|
+
|
71
|
+
## Author
|
72
|
+
|
73
|
+
Tatsuki Sugiura <sugi@nemui.org>
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ya_kansuji'
|
2
|
+
|
3
|
+
# Core extension by kansuji
|
4
|
+
class String
|
5
|
+
def to_number
|
6
|
+
YaKansuji.to_i self
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_num
|
10
|
+
YaKansuji.to_i self
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Core extension by kansuji
|
15
|
+
class Integer
|
16
|
+
def to_kansuji
|
17
|
+
YaKansuji.to_kan self
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_kan
|
21
|
+
YaKansuji.to_kan self
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Simple kansuji formatter
|
2
|
+
module YaKansuji
|
3
|
+
register_formatter :simple, lambda { |num, _options = {}|
|
4
|
+
return '零' if num.zero?
|
5
|
+
|
6
|
+
ret = ''
|
7
|
+
(UNIT_EXP4.reverse + ['']).each_with_index do |unit4, ridx4|
|
8
|
+
i4 = (num / 10_000**(UNIT_EXP4.size - ridx4)).to_i % 10_000
|
9
|
+
next if i4.zero?
|
10
|
+
|
11
|
+
if i4 == 1
|
12
|
+
ret += "一#{unit4}"
|
13
|
+
next
|
14
|
+
end
|
15
|
+
(UNIT_EXP3.reverse + ['']).each_with_index do |unit3, ridx3|
|
16
|
+
i3 = (i4 / 10**(UNIT_EXP3.size - ridx3)).to_i % 10
|
17
|
+
next if i3.zero?
|
18
|
+
|
19
|
+
if i3 == 1 && unit3 != ''
|
20
|
+
ret += unit3
|
21
|
+
else
|
22
|
+
ret += i3.to_s.tr('123456789', '一二三四五六七八九') + unit3
|
23
|
+
end
|
24
|
+
end
|
25
|
+
ret += unit4
|
26
|
+
end
|
27
|
+
ret
|
28
|
+
}
|
29
|
+
end
|
data/lib/ya_kansuji.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ya_kansuji/version'
|
4
|
+
|
5
|
+
# Yet another Kansuji library for ruby.
|
6
|
+
module YaKansuji
|
7
|
+
UNIT_EXP3 = %w(十 百 千).freeze
|
8
|
+
UNIT_EXP4 = %w(万 億 兆 京 垓 𥝱 穣 溝 澗 正 載 極 恒河沙 阿僧祇 那由他 不可思議 無量大数).freeze
|
9
|
+
NUM_ALT_CHARS = '〇一二三四五六七八九0123456789零壱壹弌弐貳貮参參弎肆伍陸漆質柒捌玖拾什陌佰阡仟萬秭'.freeze
|
10
|
+
NUM_NORMALIZED_CHARS = '01234567890123456789011122233345677789十十百百千千万𥝱'.freeze
|
11
|
+
REGEXP_PART = %r{
|
12
|
+
[
|
13
|
+
#{(NUM_ALT_CHARS + NUM_NORMALIZED_CHARS).chars.uniq.join}
|
14
|
+
#{(UNIT_EXP3 + UNIT_EXP4).find_all { |u| u.length == 1 }}
|
15
|
+
卄廿卅丗卌皕
|
16
|
+
] |
|
17
|
+
#{UNIT_EXP4.find_all { |u| u.length > 1 }.join('|')}
|
18
|
+
}x.freeze
|
19
|
+
REGEXP = /(?:#{REGEXP_PART})+/.freeze
|
20
|
+
@@formatters = {}
|
21
|
+
|
22
|
+
module_function
|
23
|
+
|
24
|
+
def to_i(str)
|
25
|
+
matched = REGEXP.match(str.to_s.tr(NUM_ALT_CHARS, NUM_NORMALIZED_CHARS)) or return 0
|
26
|
+
ret3 = 0
|
27
|
+
ret4 = 0
|
28
|
+
curnum = nil
|
29
|
+
matched[0].scan(REGEXP_PART).each do |c|
|
30
|
+
case c
|
31
|
+
when '1', '2', '3', '4', '5', '6', '7', '8', '9'
|
32
|
+
if curnum
|
33
|
+
curnum *= 10
|
34
|
+
else
|
35
|
+
curnum = 0
|
36
|
+
end
|
37
|
+
curnum += c.to_i
|
38
|
+
when '0'
|
39
|
+
curnum and curnum *= 10
|
40
|
+
when '卄', '廿'
|
41
|
+
ret3 += 20
|
42
|
+
curnum = nil
|
43
|
+
when '卅', '丗'
|
44
|
+
ret3 += 30
|
45
|
+
curnum = nil
|
46
|
+
when '卌'
|
47
|
+
ret3 += 40
|
48
|
+
curnum = nil
|
49
|
+
when '皕'
|
50
|
+
ret3 += 200
|
51
|
+
curnum = nil
|
52
|
+
when *UNIT_EXP4
|
53
|
+
if curnum
|
54
|
+
ret3 += curnum
|
55
|
+
curnum = nil
|
56
|
+
end
|
57
|
+
ret3 = 1 if ret3.zero?
|
58
|
+
ret4 += ret3 * 10**((UNIT_EXP4.index(c) + 1) * 4)
|
59
|
+
ret3 = 0
|
60
|
+
when *UNIT_EXP3
|
61
|
+
curnum ||= 1
|
62
|
+
ret3 += curnum * 10**(UNIT_EXP3.index(c) + 1)
|
63
|
+
curnum = nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
if curnum
|
67
|
+
ret3 += curnum
|
68
|
+
curnum = nil
|
69
|
+
end
|
70
|
+
ret4 + ret3
|
71
|
+
end
|
72
|
+
|
73
|
+
def register_formatter(sym, proc = nil, &block)
|
74
|
+
if block_given?
|
75
|
+
@@formatters[sym.to_sym] = block
|
76
|
+
elsif proc.respond_to? :call
|
77
|
+
@@formatters[sym.to_sym] = proc
|
78
|
+
else
|
79
|
+
raise ArgumentError, 'Registering invalid formatter.'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def formatter(sym)
|
84
|
+
@@formatters[sym.to_sym]
|
85
|
+
end
|
86
|
+
|
87
|
+
def formatters
|
88
|
+
@@formatters
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_kan(num, formatter = :simple, options = {})
|
92
|
+
num = num.to_i
|
93
|
+
if formatter.respond_to? :call
|
94
|
+
formatter.call num, options
|
95
|
+
elsif @@formatters[formatter.to_sym]
|
96
|
+
@@formatters[formatter.to_sym].call num, options
|
97
|
+
else
|
98
|
+
raise ArgumentError, "Unable to find formatter #{formatter}"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
require 'ya_kansuji/formatter/simple'
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ya_kansuji
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tatsuki Sugiura
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |
|
56
|
+
Pure ruby library of Japanese kansuji.
|
57
|
+
Bi-directional convert support between number and kanji string.
|
58
|
+
email:
|
59
|
+
- sugi@nemui.org
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ChangeLog
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- lib/ya_kansuji.rb
|
68
|
+
- lib/ya_kansuji/core_ext.rb
|
69
|
+
- lib/ya_kansuji/formatter/simple.rb
|
70
|
+
- lib/ya_kansuji/version.rb
|
71
|
+
homepage: https://github.com/sugi/ya_kansuji
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 2.0.0
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 1.3.1
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.7.6.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Japanese kansuji library for ruby
|
95
|
+
test_files: []
|