chinese_pinyin 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,21 +1,40 @@
1
- # ChinesePinyin
1
+ ChinesePinyin
2
+ =============
2
3
 
3
4
  Translate chinese hanzi to pinyin.
4
5
 
5
- The dict is borrowed from http://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin/
6
+ The dict is borrowed from <http://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin/>
6
7
 
7
- ## Install
8
+ Install
9
+ -------
8
10
 
9
- <pre><code>sudo gem install chinese_pinyin</code></pre>
11
+ sudo gem install chinese_pinyin
10
12
 
11
- ## Usage
13
+ Usage
14
+ -----
12
15
 
13
- <pre><code>
14
- require 'rubygems'
15
- require 'chinese_pinyin'
16
+ require 'rubygems'
17
+ require 'chinese_pinyin'
16
18
 
17
- Pinyin.t('中国') => "zhong guo"
18
- Pinyin.t('中国', '-') => "zhong-guo"
19
- Pinyin.t('中国', '') => "zhongguo"
20
- Pinyin.t('你好world') => "ni hao world"
21
- </code></pre>
19
+ Pinyin.t('中国') => "zhong guo"
20
+ Pinyin.t('中国', '-') => "zhong-guo"
21
+ Pinyin.t('中国', '') => "zhongguo"
22
+ Pinyin.t('你好world') => "ni hao world"
23
+
24
+ Polyphone Issue
25
+ ---------------
26
+
27
+ use Words.dat to override default behavior.
28
+
29
+ by default
30
+
31
+ Pinyin.t('广州') => "yan zhou"
32
+
33
+ add file Words.dat
34
+
35
+ 广州|guang zhou
36
+
37
+ set ENV['WORDS_FILE'] for Words.dat
38
+
39
+ ENV['WORDS_FILE'] = "Words.dat path"
40
+ Pinyin.t('广州') => "guang zhou"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chinese_pinyin}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
12
- s.date = %q{2011-08-27}
12
+ s.date = %q{2011-10-08}
13
13
  s.description = %q{translate chinese hanzi to pinyin.}
14
14
  s.email = %q{flyerhzm@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -17,23 +17,19 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.files = [
19
19
  "README.md",
20
- "Rakefile",
21
- "VERSION",
22
- "chinese_pinyin.gemspec",
23
- "data/Mandarin.dat",
24
- "lib/chinese_pinyin.rb",
25
- "test/chinese_pinyin_test.rb",
26
- "test/test_helper.rb"
20
+ "Rakefile",
21
+ "VERSION",
22
+ "chinese_pinyin.gemspec",
23
+ "data/Mandarin.dat",
24
+ "lib/chinese_pinyin.rb",
25
+ "test/Words.dat",
26
+ "test/chinese_pinyin_test.rb",
27
+ "test/test_helper.rb"
27
28
  ]
28
29
  s.homepage = %q{http://github.com/flyerhzm/chinese_pinyin}
29
- s.rdoc_options = ["--charset=UTF-8"]
30
30
  s.require_paths = ["lib"]
31
31
  s.rubygems_version = %q{1.6.2}
32
32
  s.summary = %q{translate chinese hanzi to pinyin.}
33
- s.test_files = [
34
- "test/chinese_pinyin_test.rb",
35
- "test/test_helper.rb"
36
- ]
37
33
 
38
34
  if s.respond_to? :specification_version then
39
35
  s.specification_version = 3
@@ -2,7 +2,7 @@
2
2
  $KCODE = 'u' if RUBY_VERSION !~ /1\.9/
3
3
 
4
4
  class Pinyin
5
-
5
+
6
6
  class <<self
7
7
  attr_accessor :table
8
8
 
@@ -17,7 +17,23 @@ class Pinyin
17
17
  end
18
18
  end
19
19
 
20
+ def init_word_table
21
+ return if @words_table
22
+ @words_table = {}
23
+ if ENV["WORDS_FILE"]
24
+ open(ENV["WORDS_FILE"]) do |file|
25
+ while line = file.gets
26
+ key, value = line.sub("\n", "").split('|', 2)
27
+ @words_table[key] = value
28
+ end
29
+ end
30
+ end
31
+ end
32
+
20
33
  def translate(chars, splitter = ' ')
34
+ init_word_table
35
+ return @words_table[chars].gsub(' ', splitter) if @words_table[chars]
36
+
21
37
  init_table
22
38
  results = []
23
39
  is_english = false
data/test/Words.dat ADDED
@@ -0,0 +1 @@
1
+ 广州|guang zhou
@@ -1,6 +1,8 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'test_helper'
3
3
 
4
+ ENV["WORDS_FILE"] = File.dirname(__FILE__) + '/Words.dat'
5
+
4
6
  class PinyinTest < Test::Unit::TestCase
5
7
  def test_t
6
8
  assert_equal("zhong guo", Pinyin.t('中国'))
@@ -9,5 +11,7 @@ class PinyinTest < Test::Unit::TestCase
9
11
  assert_equal("huangzhimin", Pinyin.t('黄志敏', ''))
10
12
 
11
13
  assert_equal("zhong guo english ri", Pinyin.t('中国english日'))
14
+
15
+ assert_equal("guang-zhou", Pinyin.t('广州', '-'))
12
16
  end
13
17
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: chinese_pinyin
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Richard Huang
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-27 00:00:00 +08:00
13
+ date: 2011-10-08 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -29,6 +29,7 @@ files:
29
29
  - chinese_pinyin.gemspec
30
30
  - data/Mandarin.dat
31
31
  - lib/chinese_pinyin.rb
32
+ - test/Words.dat
32
33
  - test/chinese_pinyin_test.rb
33
34
  - test/test_helper.rb
34
35
  has_rdoc: true
@@ -36,8 +37,8 @@ homepage: http://github.com/flyerhzm/chinese_pinyin
36
37
  licenses: []
37
38
 
38
39
  post_install_message:
39
- rdoc_options:
40
- - --charset=UTF-8
40
+ rdoc_options: []
41
+
41
42
  require_paths:
42
43
  - lib
43
44
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -59,6 +60,5 @@ rubygems_version: 1.6.2
59
60
  signing_key:
60
61
  specification_version: 3
61
62
  summary: translate chinese hanzi to pinyin.
62
- test_files:
63
- - test/chinese_pinyin_test.rb
64
- - test/test_helper.rb
63
+ test_files: []
64
+