chinese_pinyin 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -0
- data/Rakefile +36 -0
- data/data/Mandarin.dat +25478 -0
- data/lib/chinese_pinyin.rb +40 -0
- data/test/{pinyin_test.rb → chinese_pinyin_test.rb} +0 -0
- data/test/test_helper.rb +1 -1
- metadata +7 -2
@@ -0,0 +1,40 @@
|
|
1
|
+
$KCODE = 'u'
|
2
|
+
|
3
|
+
class Pinyin
|
4
|
+
|
5
|
+
class <<self
|
6
|
+
attr_accessor :table
|
7
|
+
|
8
|
+
def init_table
|
9
|
+
return if @table
|
10
|
+
@table = {}
|
11
|
+
open(File.dirname(__FILE__) + '/../data/Mandarin.dat') do |file|
|
12
|
+
while line = file.gets
|
13
|
+
key, value = line.split(' ', 2)
|
14
|
+
@table[key] = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def translate(chars, splitter = ' ')
|
20
|
+
init_table
|
21
|
+
results = []
|
22
|
+
is_english = false
|
23
|
+
chars.scan(/./).each do |char|
|
24
|
+
key = sprintf("%X", char.unpack("U").first)
|
25
|
+
if @table[key]
|
26
|
+
results << splitter if is_english
|
27
|
+
results << @table[key].chomp.split(' ', 2)[0].slice(0..-2).downcase
|
28
|
+
results << splitter
|
29
|
+
is_english = false
|
30
|
+
else
|
31
|
+
results << char
|
32
|
+
is_english = true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
results.join('').chomp(splitter)
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method :t, :translate
|
39
|
+
end
|
40
|
+
end
|
File without changes
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chinese_pinyin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
@@ -23,6 +23,11 @@ extra_rdoc_files:
|
|
23
23
|
- README.md
|
24
24
|
files:
|
25
25
|
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- data/Mandarin.dat
|
28
|
+
- lib/chinese_pinyin.rb
|
29
|
+
- test/test_helper.rb
|
30
|
+
- test/chinese_pinyin_test.rb
|
26
31
|
has_rdoc: true
|
27
32
|
homepage: http://github.com/flyerhzm/chinese_pinyin
|
28
33
|
licenses: []
|
@@ -53,4 +58,4 @@ specification_version: 3
|
|
53
58
|
summary: translate chinese hanzi to pinyin.
|
54
59
|
test_files:
|
55
60
|
- test/test_helper.rb
|
56
|
-
- test/
|
61
|
+
- test/chinese_pinyin_test.rb
|