hanzi_to_pinyin 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ class HanziToPinyin
2
+
3
+ # Unicode中汉字开始点
4
+ @@hanzi_unicode_start=19968
5
+ # Unicode中汉字的结束点
6
+ @@hanzi_unicode_end=40869
7
+ # 汉字 unicode 编码(16进制)
8
+ @@unicode = YAML.load(IO.read File.expand_path("../data/unicode_to_pinyin.yml",__FILE__))
9
+
10
+ def self.hanzi_2_pinyin(hanzi)
11
+ hanzi = hanzi.force_encoding("utf-8")
12
+ u_str = ''
13
+ hanzi.each_codepoint { |c|
14
+ if is_hanzi?(c)
15
+ unicode = c.to_s(16).upcase
16
+ u_str << @@unicode[unicode]
17
+ else
18
+ if c == 45 # -
19
+ u_str << "_"
20
+ else
21
+ u_str << c.chr.downcase
22
+ end
23
+ end
24
+ }
25
+ u_str
26
+ end
27
+
28
+ def self.is_hanzi?(hanzi_codepoint)
29
+ hanzi_codepoint>=@@hanzi_unicode_start&&hanzi_codepoint<=@@hanzi_unicode_end
30
+ end
31
+
32
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ require 'hanzi_to_pinyin'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hanzi_to_pinyin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - wxianfeng
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70237983291580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70237983291580
25
+ description: chinese hanzi to pinyin , just fetch first letter, written in Ruby.
26
+ email:
27
+ - wang.fl1429@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE.txt
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - hanzi_to_pinyin.gemspec
39
+ - lib/data/unicode_to_pinyin.yml
40
+ - lib/hanzi_to_pinyin.rb
41
+ - spec/spec_helper.rb
42
+ homepage: http://github.com/wxianfeng/hanzi_to_pinyin
43
+ licenses: []
44
+ post_install_message: ! " hanzi_to_pinyin is a tool for chinese hanzi to pinyin ,just
45
+ fetch first letter , writen in Ruby.\n \n http://github.com/wxianfeng/hanzi_to_pinyin\n\n
46
+ \ Enjoy!\n\n wxianfeng (wang.fl1429@gmail.com)\n\n"
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.6
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.10
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: chinese hanzi to pinyin , just fetch first letter.
68
+ test_files: []