chinese_number 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/chinese_number.gemspec +24 -0
- data/lib/chinese_number/parser.rb +99 -0
- data/lib/chinese_number/version.rb +3 -0
- data/lib/chinese_number.rb +12 -0
- data/spec/chinese_number_spec.rb +10 -0
- data/spec/parser_spec.rb +98 -0
- data/spec/spec_helper.rb +1 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 33a99a1175d29f6eb8e46a04ea4285e7215a6c78
|
4
|
+
data.tar.gz: b10f7f77505b3e2b456f3481a17c03d8b7e58d74
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dfac021e1e40ca625a6cdc5f4c4870dbfa079e9d67d624a38918d204c2c92947af3e362230eb23d0ce78d38697e90b8d6cba11cfbd7c4f2e3b03e3d39894cbfb
|
7
|
+
data.tar.gz: d98a6ec17f0592f3615b86b36dd55e9b6fce6189d39a23bc56c068d3d9ed255352890d74ec3a5c99fb60e3e7b67b39affad0bb0d9bdc52456f71791af1366e8a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 qhwa
|
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,48 @@
|
|
1
|
+
# ChineseNumber
|
2
|
+
|
3
|
+
This ruby gem helps you convert Chinse numbers into Arab numbers. For example:
|
4
|
+
|
5
|
+
~~~ruby
|
6
|
+
require 'chinse_number'
|
7
|
+
ChinseNumber.trans "一年有三百六十五天"
|
8
|
+
#=> "1年有365天"
|
9
|
+
~~~
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'chinese_number'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install chinese_number
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
You can use the top-level api:
|
28
|
+
~~~ruby
|
29
|
+
ChinseNumber.trans "我有十块钱"
|
30
|
+
#=> "我有10块钱"
|
31
|
+
|
32
|
+
ChinseNumber.trans "二〇一四年"
|
33
|
+
#=> "2014年"
|
34
|
+
~~~
|
35
|
+
|
36
|
+
or use the standalone parser:
|
37
|
+
~~~ruby
|
38
|
+
ChinseNumber::Parser.new.parse "一万二"
|
39
|
+
#=> 12000
|
40
|
+
~~~
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it ( http://github.com/<my-github-username>/chinese_number/fork )
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'chinese_number/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "chinese_number"
|
8
|
+
spec.version = ChineseNumber::VERSION
|
9
|
+
spec.authors = ["qhwa"]
|
10
|
+
spec.email = ["qhwa@163.com"]
|
11
|
+
spec.summary = %q{Translate Chinese numbers into Arabic numbers}
|
12
|
+
spec.description = %q{This ruby gem translate Chinse numbers into Arabic numbers. e.g. “一十” => 10.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
module ChineseNumber
|
4
|
+
|
5
|
+
class Parser
|
6
|
+
|
7
|
+
def self.generate_base_map
|
8
|
+
chinse_numbers = "一二三四五六七八九〇零".chars
|
9
|
+
digits = "12345678900".chars.map(&:to_i)
|
10
|
+
Hash.new.tap do |map|
|
11
|
+
chinse_numbers.each_with_index do |w, i|
|
12
|
+
digit = digits[i]
|
13
|
+
map[w] = digit
|
14
|
+
map[digit] = w
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.generate_multipers_map
|
20
|
+
chinse_multipers = {
|
21
|
+
'十' => 10,
|
22
|
+
'百' => 100,
|
23
|
+
'千' => 1000,
|
24
|
+
'万' => 10000,
|
25
|
+
'亿' => 10000_0000,
|
26
|
+
'兆' => 10000_0000_0000
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
DIGIT_MAP = generate_base_map.freeze
|
31
|
+
MULTIPERS = generate_multipers_map.freeze
|
32
|
+
DIGIT_TOKEN = Regexp.new( "[#{DIGIT_MAP.keys.join}]" )
|
33
|
+
MULTIPER_TOKEN = Regexp.new( "[#{MULTIPERS.keys.join}]" )
|
34
|
+
TOKEN = Regexp.new( "[#{(DIGIT_MAP.keys + MULTIPERS.keys).join}]+" )
|
35
|
+
|
36
|
+
def parse word
|
37
|
+
|
38
|
+
raise InvalidWord unless word =~ /\A#{TOKEN}\Z/
|
39
|
+
|
40
|
+
# 类似“二零一二” 这种短语,可以直接拼数字返回
|
41
|
+
unless word =~ MULTIPER_TOKEN
|
42
|
+
return word.gsub(/\S/) {|w| DIGIT_MAP[w]}.to_i
|
43
|
+
end
|
44
|
+
|
45
|
+
@scanner = StringScanner.new( word )
|
46
|
+
parts = []
|
47
|
+
|
48
|
+
while w = @scanner.scan( /\S/ )
|
49
|
+
case w
|
50
|
+
when DIGIT_TOKEN
|
51
|
+
num = DIGIT_MAP[w]
|
52
|
+
|
53
|
+
# 此处处理省略倍数的情况,例如
|
54
|
+
# "一万五"、"八万八"
|
55
|
+
if @scanner.eos? && parts.last && parts.last.factor >= 10
|
56
|
+
parts << MultipedNum.new( num, parts.last.factor / 10 )
|
57
|
+
else
|
58
|
+
parts << MultipedNum.new( num, 1 )
|
59
|
+
end
|
60
|
+
|
61
|
+
when MULTIPER_TOKEN
|
62
|
+
if parts.empty?
|
63
|
+
parts << MultipedNum.new( 1, 1 )
|
64
|
+
end
|
65
|
+
|
66
|
+
multiper = MULTIPERS[w]
|
67
|
+
|
68
|
+
if parts.last.factor <= multiper
|
69
|
+
parts.each do |part|
|
70
|
+
if part.factor <= multiper
|
71
|
+
part.factor *= multiper
|
72
|
+
end
|
73
|
+
end
|
74
|
+
else
|
75
|
+
parts << MultipedNum.new( 1, multiper )
|
76
|
+
end
|
77
|
+
|
78
|
+
else
|
79
|
+
raise UnexpectToken.new(w)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
parts.reduce(0) do |sum, part|
|
84
|
+
sum + part.to_i
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class MultipedNum < Struct.new(:base, :factor)
|
89
|
+
def to_i
|
90
|
+
base * factor
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class InvalidWord < RuntimeError
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
data/spec/parser_spec.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe ChineseNumber::Parser do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@parser = ChineseNumber::Parser.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it '可以解析连续的数字' do
|
10
|
+
test '一二三', 123
|
11
|
+
test '二零一二', 2012
|
12
|
+
test '二〇一二', 2012
|
13
|
+
end
|
14
|
+
|
15
|
+
it '可以解析个位数字' do
|
16
|
+
test '零', 0
|
17
|
+
test '〇', 0
|
18
|
+
|
19
|
+
'一二三四五六七八九'.each_char.to_a.each_with_index do |w, i|
|
20
|
+
test w, i + 1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it '可以解析两位数字' do
|
25
|
+
test '十', 10
|
26
|
+
test '十一', 11
|
27
|
+
test '一十一', 11
|
28
|
+
test '二十', 20
|
29
|
+
test '二十一', 21
|
30
|
+
test '九十八', 98
|
31
|
+
end
|
32
|
+
|
33
|
+
it '可以解析三位数字' do
|
34
|
+
test '百', 100
|
35
|
+
test '一百', 100
|
36
|
+
test '一百一十五', 115
|
37
|
+
test '一百十五', 115
|
38
|
+
test '一百零五', 105
|
39
|
+
end
|
40
|
+
|
41
|
+
it '可以解析四位数字' do
|
42
|
+
test '千', 1000
|
43
|
+
test '五千', 5000
|
44
|
+
test '五千八百', 5800
|
45
|
+
test '五千八百一十', 5810
|
46
|
+
test '五千八百一十六', 5816
|
47
|
+
test '五千零一十六', 5016
|
48
|
+
test '五千一十六', 5016
|
49
|
+
test '五千零六', 5006
|
50
|
+
end
|
51
|
+
|
52
|
+
it '可以解析省略部分倍数的数字' do
|
53
|
+
test '五千六', 5600
|
54
|
+
end
|
55
|
+
|
56
|
+
it '可以解析五位数字' do
|
57
|
+
test '万', 10000
|
58
|
+
test '一万', 10000
|
59
|
+
test '一万三', 13000
|
60
|
+
test '一万三千', 13000
|
61
|
+
test '一万三千八百', 13800
|
62
|
+
test '一万三千零八十', 13080
|
63
|
+
test '一万三千零八', 13008
|
64
|
+
test '一万零八', 10008
|
65
|
+
test '一万零', 10000
|
66
|
+
end
|
67
|
+
|
68
|
+
it '可以解析六位数字' do
|
69
|
+
test '十万', 10_0000
|
70
|
+
test '三十万八千', 30_8000
|
71
|
+
end
|
72
|
+
|
73
|
+
it '可以解析七位数字' do
|
74
|
+
test '一百万', 100_0000
|
75
|
+
test '一百二十万', 120_0000
|
76
|
+
test '一百二十四万', 124_0000
|
77
|
+
end
|
78
|
+
|
79
|
+
it '可以解析更大的数字' do
|
80
|
+
test '一百三十二万五千六百八十九亿八千万', 132_5689_8000_0000
|
81
|
+
end
|
82
|
+
|
83
|
+
it '可以解析一些特殊的数字' do
|
84
|
+
test '万万', 10000_0000
|
85
|
+
test '十万八千', 10_8000
|
86
|
+
end
|
87
|
+
|
88
|
+
it '对于不合法的文字抛出错误' do
|
89
|
+
expect {
|
90
|
+
@parser.parse('没有数字').should
|
91
|
+
}.to raise_error( ChineseNumber::Parser::InvalidWord )
|
92
|
+
end
|
93
|
+
|
94
|
+
def test word, expect_digit
|
95
|
+
@parser.parse( word ).should == expect_digit
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../lib/chinese_number'
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chinese_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- qhwa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-17 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: This ruby gem translate Chinse numbers into Arabic numbers. e.g. “一十”
|
56
|
+
=> 10.
|
57
|
+
email:
|
58
|
+
- qhwa@163.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- chinese_number.gemspec
|
69
|
+
- lib/chinese_number.rb
|
70
|
+
- lib/chinese_number/parser.rb
|
71
|
+
- lib/chinese_number/version.rb
|
72
|
+
- spec/chinese_number_spec.rb
|
73
|
+
- spec/parser_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.2.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Translate Chinese numbers into Arabic numbers
|
99
|
+
test_files:
|
100
|
+
- spec/chinese_number_spec.rb
|
101
|
+
- spec/parser_spec.rb
|
102
|
+
- spec/spec_helper.rb
|