hanzi 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,10 +1,9 @@
1
1
  = hanzi
2
2
 
3
- Convert Hanzi to pinyin. Unlike other similar gems, this includes tones and can accurately translate common words (others fail at things like 为什么). Uses cc-cedict for data. Does not yet have fallback for individual hanzi not in cedict.
4
-
3
+ Ruby gem for handling common Hanzi operations. Can convert to pinyin with proper tones for common words (other gems tend to fail on simple words), return english translation, or convert between simplified and traditional. Uses cc-cedict for data.
5
4
  Example usage:
6
5
 
7
- Hanzi.load_data
6
+ Hanzi.load_data # optional to pre-load data.
8
7
  Hanzi.to_pinyin('走红') # zou3hong2
9
8
  Hanzi.to_pinyin('簡單') # jian3dan1
10
9
  Hanzi.to_pinyin('为什么') # wei4shen2me5
@@ -14,6 +13,12 @@ Example usage:
14
13
  Hanzi.to_english('孔子') # Confucius (551-479 BC), Chinese thinker and social philosopher, also known as 孔夫子[Kong3 fu1 zi3]
15
14
  Hanzi.to_english('老子喜欢') # nil (can only translate exact word matches)
16
15
 
16
+ Hanzi.to_traditional('')
17
+ Hanzi.to_traditional('拉萨') # 拉薩
18
+ Hanzi.to_traditional('拉薩') # 拉薩
19
+ Hanzi.to_simplified('拉薩') # 拉萨
20
+ Hanzi.to_simplified('abc 拉萨') # nil (for now, only supports exact word matches)
21
+
17
22
  To run tests:
18
23
 
19
24
  rake test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/hanzi.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "hanzi"
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steve Jackson"]
data/lib/hanzi.rb CHANGED
@@ -77,6 +77,20 @@ class Hanzi
77
77
  entry[:english] if entry && entry[:english]
78
78
  end
79
79
 
80
+ def to_simplified(text)
81
+ load_data if @data.nil?
82
+
83
+ entry = find_hanzi_match(text)
84
+ entry[:simplified] if entry && entry[:simplified]
85
+ end
86
+
87
+ def to_traditional(text)
88
+ load_data if @data.nil?
89
+
90
+ entry = find_hanzi_match(text)
91
+ entry[:traditional] if entry && entry[:traditional]
92
+ end
93
+
80
94
  private
81
95
  def find_hanzi_match(text)
82
96
  entry = @data.find do |word|
data/test/test_hanzi.rb CHANGED
@@ -41,7 +41,27 @@ class TestHanzi < Test::Unit::TestCase
41
41
 
42
42
  def test_returns_nil_for_not_found_english_translation
43
43
  result = Hanzi.to_english('老子你好')
44
- assert_equal result, nil
44
+ assert_equal nil, result
45
+ end
46
+
47
+ def test_can_convert_from_simplified_to_traditional
48
+ result = Hanzi.to_traditional('拉萨')
49
+ assert_equal '拉薩', result
50
+ end
51
+
52
+ def test_can_convert_traditional_to_traditional
53
+ result = Hanzi.to_traditional('拉薩')
54
+ assert_equal '拉薩', result
55
+ end
56
+
57
+ def test_can_convert_traditional_to_simplified
58
+ result = Hanzi.to_simplified('拉薩')
59
+ assert_equal '拉萨', result
60
+ end
61
+
62
+ def test_cannot_convert_non_exact_words_simplified
63
+ result = Hanzi.to_simplified('拉萨喜欢')
64
+ assert_equal nil, result
45
65
  end
46
66
 
47
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanzi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  segments:
80
80
  - 0
81
- hash: -991267641606687232
81
+ hash: -552213835099124985
82
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements: