chinese_number_converter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 255d6c9b13a18cd7abcb3de33c6197393b5dcb2041999254e3bea2ffe0f30d72
4
+ data.tar.gz: c7e66a462954b439be201bfed2b1a9612712411158d83870577cd2565dfb3e02
5
+ SHA512:
6
+ metadata.gz: e500409e1bf88c4c28faf75c8eb1999e221390134739224787be7e9200356bbaf85385433fc8aa4a325f8b754ef3975d7fbe76fbed1b2a42e6f79f9207071b84
7
+ data.tar.gz: aefa2ee5ce20f5e1e58cce5c342998cbe10c0ba8b8bb848e6612c48fd75304269d4b1a5a408cbe4ee1851d04a56e311fe6d483e6cbbb013da37a8e442bc7282d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-07-23
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in chinese_number_converter.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chinese_number_converter (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.1)
11
+ json (2.7.2)
12
+ language_server-protocol (3.17.0.3)
13
+ parallel (1.25.1)
14
+ parser (3.3.4.0)
15
+ ast (~> 2.4.1)
16
+ racc
17
+ racc (1.8.0)
18
+ rainbow (3.1.1)
19
+ rake (13.2.1)
20
+ regexp_parser (2.9.2)
21
+ rexml (3.3.2)
22
+ strscan
23
+ rspec (3.13.0)
24
+ rspec-core (~> 3.13.0)
25
+ rspec-expectations (~> 3.13.0)
26
+ rspec-mocks (~> 3.13.0)
27
+ rspec-core (3.13.0)
28
+ rspec-support (~> 3.13.0)
29
+ rspec-expectations (3.13.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.13.0)
32
+ rspec-mocks (3.13.1)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.13.0)
35
+ rspec-support (3.13.1)
36
+ rubocop (1.65.0)
37
+ json (~> 2.3)
38
+ language_server-protocol (>= 3.17.0)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.3.0.2)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 2.4, < 3.0)
43
+ rexml (>= 3.2.5, < 4.0)
44
+ rubocop-ast (>= 1.31.1, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 2.4.0, < 3.0)
47
+ rubocop-ast (1.31.3)
48
+ parser (>= 3.3.1.0)
49
+ ruby-progressbar (1.13.0)
50
+ strscan (3.1.0)
51
+ unicode-display_width (2.5.0)
52
+
53
+ PLATFORMS
54
+ arm64-darwin-20
55
+
56
+ DEPENDENCIES
57
+ chinese_number_converter!
58
+ rake (~> 13.0)
59
+ rspec (~> 3.0)
60
+ rubocop (~> 1.21)
61
+
62
+ BUNDLED WITH
63
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 alanyeh20001
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,33 @@
1
+ # ChineseNumberConverter
2
+
3
+ Can chinese number be converted into number? Yep, you've come to the right place!
4
+ This is an easy tool for you to convert chinese number into number, for example
5
+
6
+ ChineseNumberConverter.convert_to_number('三百六十五')
7
+ #=> 365
8
+
9
+
10
+ ## Installation
11
+
12
+ Add gem to Gemfile.
13
+
14
+ gem 'chinese_number_converter'
15
+
16
+ Run `bundle install`.
17
+
18
+
19
+ ## Usage
20
+
21
+ Nothing fancy here, just copy and paste the following command, and you're done!
22
+
23
+ ChineseNumberConverter.convert_to_number('兩兆三千六百八十億七千一百二十')
24
+ #=> 2368000007120
25
+
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alanyeh20001/chinese_number_converter.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChineseNumberConverter
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "chinese_number_converter/version"
4
+
5
+ module ChineseNumberConverter
6
+ class Error < StandardError; end
7
+
8
+ CHINESE_NUMBER_MAPPINGS = {
9
+ '九' => 9, '玖' => 9, '八' => 8, '捌' => 8, '七' => 7, '柒' => 7, '六' => 6, '陸' => 6, '五' => 5,
10
+ '伍' => 5, '四' => 4, '肆' => 4, '三' => 3, '參' => 3, '叁' => 3, '二' => 2, '貳' => 2, '兩' => 2,
11
+ '一' => 1, '壹' => 1
12
+ }.freeze
13
+ CHINESE_COMPOUNDABLE_UNIT_MAPPINGS = {
14
+ '兆' => 1_000_000_000_000, '億' => 100_000_000, '萬' => 10_000
15
+ }.freeze
16
+ CHINESE_UNIT_MAPPINGS = {
17
+ '千' => 1000, '仟' => 1000, '百' => 100, '佰' => 100, '十' => 10, '拾' => 10
18
+ }.freeze
19
+ CHINESE_COMPOUNDABLE_UNITS_DESC = CHINESE_COMPOUNDABLE_UNIT_MAPPINGS.keys
20
+ CHINESE_UNITS_DESC = CHINESE_UNIT_MAPPINGS.keys
21
+
22
+ def self.convert_to_number(chinese_number)
23
+ chars = chinese_number.chars
24
+ group = {}
25
+ CHINESE_COMPOUNDABLE_UNITS_DESC.each do |unit|
26
+ if (unit_index = chars.index(unit))
27
+ set = chars.slice!(0..unit_index)
28
+ group[CHINESE_COMPOUNDABLE_UNIT_MAPPINGS[set.pop]] = set.join
29
+ end
30
+ break if chars.length.zero?
31
+ if chars.length <= 2
32
+ group[1] = chars.join.gsub('零')
33
+ break
34
+ end
35
+ end
36
+ if group.empty? || chars.length > 0
37
+ group[1] = chars.join
38
+ end
39
+
40
+ group.map do |base_unit, str|
41
+ chars = str.chars
42
+ chars.delete('零')
43
+ sub_sum = chars.each_slice(2).map do |number, unit|
44
+ if unit
45
+ CHINESE_NUMBER_MAPPINGS[number] * CHINESE_UNIT_MAPPINGS[unit]
46
+ else
47
+ CHINESE_NUMBER_MAPPINGS[number]
48
+ end
49
+ end.sum
50
+ base_unit * sub_sum
51
+ end.sum
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ module ChineseNumberConverter
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_number_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alanyeh20001
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.13.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.13.0
27
+ description: Convert number in chinese character, including chinese character aliases,
28
+ to number.
29
+ email:
30
+ - alanyeh20001@hotmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/chinese_number_converter.rb
44
+ - lib/chinese_number_converter/version.rb
45
+ - sig/chinese_number_converter.rbs
46
+ homepage: https://github.com/alanyeh20001/chinese_number_converter
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/alanyeh20001/chinese_number_converter
51
+ source_code_uri: https://github.com/alanyeh20001/chinese_number_converter
52
+ changelog_uri: https://github.com/alanyeh20001/chinese_number_converter/blob/main/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.4.10
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Convert chinese number to number.
72
+ test_files: []