chinese_pinyin 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/chinese_pinyin.yml +26 -0
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/Rakefile +1 -3
- data/lib/chinese_pinyin.rb +8 -7
- data/lib/chinese_pinyin/version.rb +2 -1
- data/test/chinese_pinyin_test.rb +6 -0
- metadata +7 -7
- data/.travis.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23e4d70b5131cb483c5a9a6a542b1310ad4cad4535e8d295ff58ba9338f2b930
|
4
|
+
data.tar.gz: 1224699549ee0d9d644e1f96d084a3c17c7754f9236d2a21311a152fafd73b52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d913acf6b8b162685957081afc5ccc6a47337e1a5481bb347a27ae8748c16f87f56c367c0a064fe1b09246d288c7680064bb1f4673dcebce669f64e3c9b458d
|
7
|
+
data.tar.gz: 5974408e11538bc96644ef3b8f7b41c9b5a381719bd495347217eb5165a8771dc34d7d94c810bb0543b66aaa6a79c2d64aa268845187dad167244836cf032950
|
@@ -0,0 +1,26 @@
|
|
1
|
+
name: Chinese Pinyin
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches:
|
6
|
+
- 'master'
|
7
|
+
|
8
|
+
push:
|
9
|
+
branches:
|
10
|
+
- 'ruby3-support'
|
11
|
+
- 'master'
|
12
|
+
|
13
|
+
jobs:
|
14
|
+
build:
|
15
|
+
runs-on: ubuntu-latest
|
16
|
+
strategy:
|
17
|
+
matrix:
|
18
|
+
ruby: ['2.1', '2.5', '2.6', '2.7', '3.0']
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v1
|
21
|
+
- uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby }}
|
24
|
+
- name: Build and test with Rake
|
25
|
+
run: |
|
26
|
+
rake test
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ChinesePinyin
|
2
2
|
|
3
|
-
[![
|
3
|
+
[![Chinese Pinyin](https://github.com/flyerhzm/chinese_pinyin/actions/workflows/chinese_pinyin.yml/badge.svg)](https://github.com/flyerhzm/chinese_pinyin/actions/workflows/chinese_pinyin.yml)
|
4
4
|
|
5
5
|
Translate chinese hanzi to pinyin.
|
6
6
|
|
data/Rakefile
CHANGED
data/lib/chinese_pinyin.rb
CHANGED
@@ -16,17 +16,18 @@ class Pinyin
|
|
16
16
|
|
17
17
|
class <<self
|
18
18
|
attr_accessor :table
|
19
|
-
attr_accessor :
|
19
|
+
attr_accessor :ruby1
|
20
20
|
|
21
21
|
def init_table
|
22
22
|
return if @table
|
23
23
|
|
24
24
|
# Ruby 2.0以后默认即为UTF-8编码,使用新的码表以提升效率
|
25
|
-
@
|
26
|
-
datfile = @
|
25
|
+
@ruby1 = !!(RUBY_VERSION =~ /^1/)
|
26
|
+
datfile = @ruby1 ? 'Mandarin.dat' : 'pinyin-utf8.dat'
|
27
27
|
@table = {}
|
28
28
|
|
29
|
-
File.
|
29
|
+
file = File.join(File.dirname(__FILE__), "../data/#{datfile}")
|
30
|
+
File.open(file, "r:UTF-8",) do |file|
|
30
31
|
while line = file.gets
|
31
32
|
key, value = line.split(' ', 2)
|
32
33
|
@table[key] = value
|
@@ -50,7 +51,7 @@ class Pinyin
|
|
50
51
|
end
|
51
52
|
|
52
53
|
def translate(chars, options={})
|
53
|
-
chars = chars.
|
54
|
+
chars = chars.encode("UTF-8")
|
54
55
|
splitter = options.fetch(:splitter, ' ')
|
55
56
|
tonemarks = options.fetch(:tonemarks, false)
|
56
57
|
tone = options.fetch(:tone, false || tonemarks)
|
@@ -72,7 +73,7 @@ class Pinyin
|
|
72
73
|
is_english = false
|
73
74
|
|
74
75
|
chars.scan(/./).each do |char|
|
75
|
-
key = @
|
76
|
+
key = @ruby1 ? sprintf("%X", char.unpack("U").first) : char
|
76
77
|
|
77
78
|
if @table[key]
|
78
79
|
results << splitter if is_english
|
@@ -80,7 +81,7 @@ class Pinyin
|
|
80
81
|
is_english = false
|
81
82
|
pinyin = @table[key].chomp.split(' ', 2)[0]
|
82
83
|
|
83
|
-
pinyin.downcase!
|
84
|
+
pinyin.downcase! if @ruby1
|
84
85
|
pinyin.chop! unless tone
|
85
86
|
pinyin.capitalize! if camel
|
86
87
|
if tonemarks
|
data/test/chinese_pinyin_test.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
+
|
2
3
|
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
3
4
|
|
4
5
|
ENV["WORDS_FILE"] = File.dirname(__FILE__) + '/Words.dat'
|
@@ -10,6 +11,11 @@ class PinyinTest < Test::Unit::TestCase
|
|
10
11
|
assert_equal("shang hai very good o ye", Pinyin.t('上海very good哦耶'))
|
11
12
|
end
|
12
13
|
|
14
|
+
def test_t_with_frozen_string
|
15
|
+
assert_equal("zhong guo", Pinyin.t('中国'.freeze))
|
16
|
+
assert_equal("shen zhen", Pinyin.t('深圳'.freeze))
|
17
|
+
end
|
18
|
+
|
13
19
|
def test_t_with_splitter
|
14
20
|
assert_equal("zhong-guo", Pinyin.t('中国', splitter: '-'))
|
15
21
|
assert_equal("huangzhimin", Pinyin.t('黄志敏', splitter: ''))
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chinese_pinyin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
- Hong, Liang
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-04-18 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: translate chinese hanzi to pinyin.
|
15
15
|
email:
|
@@ -20,8 +20,8 @@ executables:
|
|
20
20
|
extensions: []
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
|
+
- ".github/workflows/chinese_pinyin.yml"
|
23
24
|
- ".gitignore"
|
24
|
-
- ".travis.yml"
|
25
25
|
- CHANGELOG.md
|
26
26
|
- Gemfile
|
27
27
|
- MIT-LICENSE
|
@@ -40,7 +40,7 @@ homepage: http://github.com/flyerhzm/chinese_pinyin
|
|
40
40
|
licenses:
|
41
41
|
- MIT
|
42
42
|
metadata: {}
|
43
|
-
post_install_message:
|
43
|
+
post_install_message:
|
44
44
|
rdoc_options: []
|
45
45
|
require_paths:
|
46
46
|
- lib
|
@@ -55,8 +55,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
55
|
- !ruby/object:Gem::Version
|
56
56
|
version: 1.3.6
|
57
57
|
requirements: []
|
58
|
-
rubygems_version: 3.
|
59
|
-
signing_key:
|
58
|
+
rubygems_version: 3.1.4
|
59
|
+
signing_key:
|
60
60
|
specification_version: 4
|
61
61
|
summary: translate chinese hanzi to pinyin.
|
62
62
|
test_files:
|
data/.travis.yml
DELETED