chinese_number 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/lib/chinese_number/parser.rb +12 -4
- data/lib/chinese_number/version.rb +1 -1
- data/spec/chinese_number_spec.rb +11 -4
- data/spec/parser_spec.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2c5087a769a7e00ba5a52c4d750c84824c2ad9a
|
4
|
+
data.tar.gz: b7251a093f675f3f07612f2bed763670d31b799d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9943d3aa18ce4d17aa9b1ac471d41df4fa6d136efc01c27086b2715b7ab2fbea658750ce684c86f285c8101698a178f1d5f5d7a28a97b33e209c1dec3259f3fb
|
7
|
+
data.tar.gz: cb6c27b0f0cf5aceacc53534b4d1114e114bf125cea106018f35bdbd13109323b56b4da0aef232f658b238fe24c51c2b2ad37ff4f799cb991907ac06a8780e9a
|
data/README.md
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
这个 ruby gem 可以用来解析汉语描述的数字,转换成阿拉伯数字。例如:
|
4
4
|
|
5
5
|
~~~ruby
|
6
|
-
require '
|
7
|
-
|
6
|
+
require 'chinese_number'
|
7
|
+
ChineseNumber.trans "一年有三百六十五天"
|
8
8
|
#=> "1年有365天"
|
9
9
|
~~~
|
10
10
|
|
@@ -29,10 +29,10 @@ ChinseNumber.trans "一年有三百六十五天"
|
|
29
29
|
~~~ruby
|
30
30
|
require 'chinese_number'
|
31
31
|
|
32
|
-
|
32
|
+
ChineseNumber.trans "我有十块钱"
|
33
33
|
#=> "我有10块钱"
|
34
34
|
|
35
|
-
|
35
|
+
ChineseNumber.trans "二〇一四年"
|
36
36
|
#=> "2014年"
|
37
37
|
|
38
38
|
ChineseNumber.find "每分钟六十秒的速度前进二十四小时"
|
@@ -45,7 +45,7 @@ ChineseNumber.extract "每分钟六十秒的速度前进二十四小时"
|
|
45
45
|
或者独立的 Parser 类:
|
46
46
|
|
47
47
|
~~~ruby
|
48
|
-
parser =
|
48
|
+
parser = ChineseNumber::Parser.new
|
49
49
|
parser.parse "一万二"
|
50
50
|
#=> 12000
|
51
51
|
|
@@ -7,10 +7,10 @@ module ChineseNumber
|
|
7
7
|
attr_reader :parts
|
8
8
|
|
9
9
|
def self.generate_base_map
|
10
|
-
|
11
|
-
digits
|
10
|
+
chinese_numbers = "一两二三四五六七八九〇零".chars
|
11
|
+
digits = "122345678900".chars.map(&:to_i)
|
12
12
|
Hash.new.tap do |map|
|
13
|
-
|
13
|
+
chinese_numbers.each_with_index do |w, i|
|
14
14
|
d = digits[i]
|
15
15
|
map[w] = map[d.to_s] = d
|
16
16
|
end
|
@@ -18,7 +18,7 @@ module ChineseNumber
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.generate_multipers_map
|
21
|
-
|
21
|
+
{
|
22
22
|
'十' => 10,
|
23
23
|
'百' => 100,
|
24
24
|
'千' => 1000,
|
@@ -40,6 +40,7 @@ module ChineseNumber
|
|
40
40
|
|
41
41
|
DIGIT_MAP = generate_base_map.freeze
|
42
42
|
MULTIPERS = generate_multipers_map
|
43
|
+
ARABIC_NUMBER = /\d/
|
43
44
|
DIGIT_TOKEN = Regexp.new( "[#{DIGIT_MAP.keys.join}]" )
|
44
45
|
MULTIPER_TOKEN = Regexp.new( "[#{MULTIPERS.keys.join}]" )
|
45
46
|
TOKEN = Regexp.new( "[#{(DIGIT_MAP.keys + MULTIPERS.keys).join}]+" )
|
@@ -58,6 +59,13 @@ module ChineseNumber
|
|
58
59
|
|
59
60
|
while w = @scanner.scan( /\S/ )
|
60
61
|
case w
|
62
|
+
when ARABIC_NUMBER
|
63
|
+
if @scanner.check(ARABIC_NUMBER)
|
64
|
+
w << @scanner.scan( /\d+/ )
|
65
|
+
handle_digit w.to_i
|
66
|
+
else
|
67
|
+
handle_digit DIGIT_MAP[w]
|
68
|
+
end
|
61
69
|
when DIGIT_TOKEN
|
62
70
|
handle_digit DIGIT_MAP[w]
|
63
71
|
when MULTIPER_TOKEN
|
data/spec/chinese_number_spec.rb
CHANGED
@@ -3,16 +3,23 @@ require_relative 'spec_helper'
|
|
3
3
|
describe ChineseNumber do
|
4
4
|
|
5
5
|
it '可以转换普通字符串' do
|
6
|
-
ChineseNumber.trans('我口袋里没有一千块钱').
|
7
|
-
ChineseNumber.trans('一年有三百六十五天').
|
6
|
+
expect(ChineseNumber.trans('我口袋里没有一千块钱')).to eql '我口袋里没有1000块钱'
|
7
|
+
expect(ChineseNumber.trans('一年有三百六十五天')).to eql '1年有365天'
|
8
|
+
end
|
9
|
+
|
10
|
+
it '可以转换"数字+倍数"的混合' do
|
11
|
+
expect(ChineseNumber.trans('250万')).to eql '2500000'
|
12
|
+
expect(ChineseNumber.trans('20万')).to eql '200000'
|
13
|
+
expect(ChineseNumber.trans('20万五千')).to eql '205000'
|
14
|
+
expect(ChineseNumber.trans('20万零五千')).to eql '205000'
|
8
15
|
end
|
9
16
|
|
10
17
|
it '可以抽出数字' do
|
11
|
-
ChineseNumber.extract('每分钟六十秒的速度前进二十四小时').
|
18
|
+
expect(ChineseNumber.extract('每分钟六十秒的速度前进二十四小时')).to eql [60, 24]
|
12
19
|
end
|
13
20
|
|
14
21
|
it '可以寻找字符串内的中文数字' do
|
15
|
-
ChineseNumber.find('每分钟六十秒的速度前进二十四小时').
|
22
|
+
expect(ChineseNumber.find('每分钟六十秒的速度前进二十四小时')).to eql [
|
16
23
|
{ '六十' => 60 },
|
17
24
|
{ '二十四' => 24 }
|
18
25
|
]
|
data/spec/parser_spec.rb
CHANGED
@@ -88,7 +88,7 @@ describe ChineseNumber::Parser do
|
|
88
88
|
|
89
89
|
it '对于不合法的文字抛出错误' do
|
90
90
|
expect {
|
91
|
-
@parser.parse('没有数字')
|
91
|
+
@parser.parse('没有数字')
|
92
92
|
}.to raise_error( ChineseNumber::Parser::InvalidWord )
|
93
93
|
end
|
94
94
|
|
@@ -101,7 +101,7 @@ describe ChineseNumber::Parser do
|
|
101
101
|
end
|
102
102
|
|
103
103
|
def test word, expect_digit
|
104
|
-
@parser.parse( word ).
|
104
|
+
expect(@parser.parse( word )).to eql expect_digit
|
105
105
|
end
|
106
106
|
|
107
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chinese_number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- qhwa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
94
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.6.11
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Translate Chinese numbers into Arabic numbers
|