zhongwen_tools 0.12.4 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/README.md +74 -165
  4. data/Rakefile +0 -1
  5. data/lib/zhongwen_tools/{string/caps.rb → caps.rb} +19 -1
  6. data/lib/zhongwen_tools/core.rb +19 -0
  7. data/lib/zhongwen_tools/core_ext/integer.rb +8 -0
  8. data/lib/zhongwen_tools/core_ext/string.rb +10 -0
  9. data/lib/zhongwen_tools/fullwidth.rb +102 -0
  10. data/lib/zhongwen_tools/integer_extension.rb +31 -0
  11. data/lib/zhongwen_tools/number/number_table.rb +44 -0
  12. data/lib/zhongwen_tools/number.rb +221 -0
  13. data/lib/zhongwen_tools/regex.rb +38 -22
  14. data/lib/zhongwen_tools/romanization/pinyin.rb +231 -0
  15. data/lib/zhongwen_tools/romanization/{pyn_to_py.rb → pinyin_table.rb} +2 -1
  16. data/lib/zhongwen_tools/romanization/romanization_table.rb +425 -0
  17. data/lib/zhongwen_tools/romanization.rb +199 -136
  18. data/lib/zhongwen_tools/{string/ruby19.rb → ruby_19.rb} +1 -2
  19. data/lib/zhongwen_tools/{conversion → script}/conversion_data +0 -0
  20. data/lib/zhongwen_tools/{conversion.rb → script.rb} +21 -34
  21. data/lib/zhongwen_tools/string_extension.rb +136 -0
  22. data/lib/zhongwen_tools/unicode.rb +25 -0
  23. data/lib/zhongwen_tools/uri.rb +14 -0
  24. data/lib/zhongwen_tools/version.rb +1 -1
  25. data/lib/zhongwen_tools/zhongwen.rb +29 -0
  26. data/lib/zhongwen_tools.rb +2 -3
  27. data/test/test_caps.rb +26 -0
  28. data/test/test_core.rb +13 -0
  29. data/test/test_fullwidth.rb +30 -0
  30. data/test/test_helper.rb +4 -12
  31. data/test/test_helpers/unload_zhongwen_tools_script.rb +5 -0
  32. data/test/test_integer_extension.rb +34 -0
  33. data/test/test_number.rb +79 -0
  34. data/test/test_pinyin.rb +68 -0
  35. data/test/test_regex.rb +41 -0
  36. data/test/test_romanization.rb +110 -133
  37. data/test/{test_conversion.rb → test_script.rb} +41 -44
  38. data/test/test_string_extension.rb +94 -0
  39. data/test/test_unicode.rb +27 -0
  40. data/test/test_uri.rb +16 -0
  41. data/test/test_zhongwen.rb +37 -0
  42. data/zhongwen_tools.gemspec +1 -1
  43. metadata +93 -52
  44. data/Gemfile.1.8.7 +0 -8
  45. data/lib/zhongwen_tools/conversion/string.rb +0 -19
  46. data/lib/zhongwen_tools/integer.rb +0 -28
  47. data/lib/zhongwen_tools/numbers.rb +0 -195
  48. data/lib/zhongwen_tools/regex/ruby18.rb +0 -15
  49. data/lib/zhongwen_tools/romanization/conversion_table.rb +0 -425
  50. data/lib/zhongwen_tools/romanization/detect.rb +0 -141
  51. data/lib/zhongwen_tools/romanization/string.rb +0 -36
  52. data/lib/zhongwen_tools/string/fullwidth.rb +0 -85
  53. data/lib/zhongwen_tools/string/ruby18.rb +0 -96
  54. data/lib/zhongwen_tools/string.rb +0 -164
  55. data/test/test_integer.rb +0 -31
  56. data/test/test_numbers.rb +0 -68
  57. data/test/test_string.rb +0 -133
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require 'uri'
3
+
4
+ module ZhongwenTools
5
+ module URI
6
+ def self.encode(str)
7
+ ::URI.encode str
8
+ end
9
+
10
+ def self.escape(str)
11
+ ::URI.escape(str, Regexp.new("[^#{ ::URI::PATTERN::UNRESERVED }]"))
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module ZhongwenTools
2
- VERSION = '0.12.4'
2
+ VERSION = '0.15.1'
3
3
  end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'zhongwen_tools/regex'
3
+ module ZhongwenTools
4
+ module Zhongwen
5
+ def self.has_zh?(str)
6
+ return false unless str.class == String
7
+
8
+ regex = /(#{ ZhongwenTools::Regex.zh }|#{ ZhongwenTools::Regex.zh_punc })/
9
+ !str[regex].nil?
10
+ end
11
+
12
+ def self.zh?(str)
13
+ return false unless str.class == String
14
+
15
+ regex = /(#{ ZhongwenTools::Regex.zh }+|#{ ZhongwenTools::Regex.zh_punc }+|\s+)/
16
+ str.scan(regex).join == str
17
+ end
18
+
19
+ def self.has_zh_punctuation?(str)
20
+ return false unless str.class == String
21
+
22
+ !str[ZhongwenTools::Regex.zh_punc].nil?
23
+ end
24
+
25
+ def self.strip_zh_punctuation(str)
26
+ str.gsub(ZhongwenTools::Regex.zh_punc, '')
27
+ end
28
+ end
29
+ end
@@ -1,7 +1,6 @@
1
1
  # encoding: utf-8
2
- require 'zhongwen_tools/string'
3
- require 'zhongwen_tools/numbers'
4
- require 'zhongwen_tools/version'
2
+ require 'zhongwen_tools/script'
3
+ require 'zhongwen_tools/core'
5
4
 
6
5
  module ZhongwenTools
7
6
  end
data/test/test_caps.rb ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+ require 'zhongwen_tools/caps'
6
+
7
+ class TestCaps < Minitest::Test
8
+ def test_downcase
9
+ assert_equal @caps[:d], ZhongwenTools::Caps.downcase(@caps[:u])
10
+ assert_equal @caps[:d], ZhongwenTools::Caps.downcase(@caps[:c])
11
+ end
12
+
13
+ def test_upcase
14
+ assert_equal @caps[:u], ZhongwenTools::Caps.upcase(@caps[:d])
15
+ assert_equal @caps[:u], ZhongwenTools::Caps.upcase(@caps[:c])
16
+ end
17
+
18
+ def test_capitalize
19
+ assert_equal @caps[:c], ZhongwenTools::Caps.capitalize(@caps[:d])
20
+ end
21
+
22
+ def setup
23
+ @caps = { u: 'ĀLĀBÓ', d: 'ālābó', c: 'Ālābó' }
24
+ end
25
+ end
26
+
data/test/test_core.rb ADDED
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+
6
+ class TestCore < Minitest::Test
7
+ def test_core_ext_does_not_have_script_functions
8
+ load 'test_helpers/unload_zhongwen_tools_script.rb'
9
+ require 'zhongwen_tools/core'
10
+ require 'zhongwen_tools/core_ext/string'
11
+ assert_raises(NoMethodError){ '你们'.to_zht }
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+ require 'zhongwen_tools/fullwidth'
6
+ class TestFullwidth < Minitest::Test
7
+ def setup
8
+ @hw = 'hello'
9
+ @fw = 'hello'
10
+ @mixed = 'hello'
11
+ end
12
+
13
+ def test_halfwidth?
14
+ assert ZhongwenTools::Fullwidth.halfwidth? @hw
15
+ refute ZhongwenTools::Fullwidth.halfwidth? @fw
16
+ refute ZhongwenTools::Fullwidth.halfwidth? @mixed
17
+ end
18
+
19
+ def test_fullwidth?
20
+ refute ZhongwenTools::Fullwidth.fullwidth? @hw
21
+ assert ZhongwenTools::Fullwidth.fullwidth? @fw
22
+ assert ZhongwenTools::Fullwidth.fullwidth? @mixed
23
+ end
24
+
25
+ def test_to_halfwidth
26
+ assert_equal @hw, ZhongwenTools::Fullwidth.to_halfwidth(@fw)
27
+ assert_equal @hw, ZhongwenTools::Fullwidth.to_halfwidth(@mixed)
28
+ end
29
+ end
30
+
data/test/test_helper.rb CHANGED
@@ -12,17 +12,9 @@ rescue LoadError
12
12
  end
13
13
 
14
14
  gem 'minitest'
15
- require 'minitest/autorun'
16
- #require 'test/unit'
15
+ gem 'minitest-reporters'
17
16
 
18
- if RUBY_VERSION < '1.9'
19
- class Minitest::Test
20
- def refute(statement, message = '')
21
- assert !statement, message
22
- end
17
+ require 'minitest/autorun'
18
+ require "minitest/reporters"
23
19
 
24
- def skip
25
- return
26
- end
27
- end
28
- end
20
+ Minitest::Reporters.use! [Minitest::Reporters::SpecReporter.new]
@@ -0,0 +1,5 @@
1
+ module ZhongwenTools
2
+ [:Script].each do |sub_module|
3
+ remove_const(sub_module) if const_defined?(sub_module)
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+ require 'zhongwen_tools/integer_extension'
6
+
7
+ if RUBY_VERSION < '2.0.0'
8
+ Integer.send(:include, ZhongwenTools::IntegerExtension)
9
+ else
10
+ using ZhongwenTools
11
+ end
12
+
13
+ class TestIntegerExtention < Minitest::Test
14
+ def test_to_zh
15
+ assert_equal @yi[:zhs], @yi[:i].to_zh(:zhs)
16
+ assert_equal @yi[:zht], @yi[:i].to_zh(:zht)
17
+ end
18
+
19
+ def test_to_zht
20
+ assert_equal @yi[:zht], @yi[:i].to_zht
21
+ end
22
+
23
+ def test_to_zhs
24
+ assert_equal @yi[:zhs], @yi[:i].to_zhs
25
+ end
26
+
27
+ def test_to_pyn
28
+ assert_equal @yi[:pyn], @yi[:i].to_pyn
29
+ end
30
+
31
+ def setup
32
+ @yi = { i: 10_000, zhs: '一万', zht: '一萬', pyn: 'yi1-wan4' }
33
+ end
34
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+ require 'zhongwen_tools/number'
6
+ class TestNumber < Minitest::Test
7
+ def setup
8
+ @numbers = [
9
+ { zhs: '一万二千七', zht: '一萬二千七', i: 12_007, pyn: 'yi1-wan4-er4-qian1-qi1' },
10
+ { zhs: '三千六十三', zht: '三千六十三', i: 3_063, pyn: 'san1-qian1-liu4-shi2-san1' },
11
+ { zhs: '一百五十', zht: '一百五十', i: 150, pyn: 'yi1-bai2-wu3-shi2' },
12
+ #{ zhs: '三千亿', i: 300_000_000_000, pyn: '' },
13
+ ]
14
+
15
+ @liang_number = { zhs: '一万两千七', i: 12_007, pyn: '' }
16
+ @date_numbers = [
17
+ { zhs: '一九六六', i: 1966, pyn: '' },
18
+ { zhs: '二零零八', i: 2008, pyn: '' },
19
+ ]
20
+ end
21
+
22
+ def test_number?
23
+ assert ZhongwenTools::Number.number? '一'
24
+ assert ZhongwenTools::Number.number? 1
25
+
26
+ @numbers.map{ |n| n[:zhs]}.each do |zh|
27
+ assert ZhongwenTools::Number.number?(zh), "#{zh}"
28
+ end
29
+ end
30
+
31
+ def test_to_pyn
32
+ # same tests
33
+ @numbers.each do |n|
34
+ [:i, :pyn, :zhs, :zht].each do |type|
35
+ assert_equal n[:pyn], ZhongwenTools::Number.to_pyn(n[type])
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ def test_to_zhs
42
+ @numbers.each do |n|
43
+ [:i, :pyn, :zht, :zhs].each do |type|
44
+ assert_equal n[:zhs], ZhongwenTools::Number.to_zhs(n[type])
45
+ end
46
+ end
47
+ end
48
+
49
+ def test_to_zht
50
+ @numbers.each do |n|
51
+ [:i, :pyn, :zhs, :zht].each do |type|
52
+ assert_equal n[:zht], ZhongwenTools::Number.to_zht(n[type])
53
+ end
54
+ end
55
+ end
56
+
57
+ def test_to_zh
58
+ @numbers.each do |n|
59
+ [:i, :pyn, :zhs, :zht].each do |type|
60
+ assert_equal n[:zhs], ZhongwenTools::Number.to_zh(n[type], :zhs)
61
+ assert_equal n[:zht], ZhongwenTools::Number.to_zh(n[type], :zht)
62
+ end
63
+ end
64
+ end
65
+
66
+ def test_to_i
67
+ assert_equal @liang_number[:i], ZhongwenTools::Number.to_i(@liang_number[:zhs])
68
+
69
+ @numbers.each do |n|
70
+ [:pyn, :zhs, :zht].each do |type|
71
+ assert_equal n[:i], ZhongwenTools::Number.to_i(n[type])
72
+ end
73
+ end
74
+
75
+ @date_numbers.each do |n|
76
+ assert_equal n[:i], ZhongwenTools::Number.to_i(n[:zhs])
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+ require 'zhongwen_tools/romanization/pinyin'
6
+ class TestPinyin < Minitest::Test
7
+ def test_split_pyn
8
+ @split_words.each do |w|
9
+ assert_equal w[:split], ZhongwenTools::Romanization::Pinyin.split_pyn(w[:pyn])
10
+ end
11
+ end
12
+
13
+ def test_split_py
14
+ @split_words.each do |w|
15
+ assert_equal w[:split_py], ZhongwenTools::Romanization::Pinyin.split_py(w[:py])
16
+ end
17
+ end
18
+
19
+ def test_py?
20
+ @words.each do |w|
21
+ assert ZhongwenTools::Romanization::Pinyin.py?(w[:py]), w.inspect
22
+ refute ZhongwenTools::Romanization::Pinyin.py?(w[:pyn]), w.inspect
23
+ end
24
+ end
25
+
26
+ def test_pyn?
27
+ @words.each do |w|
28
+ refute ZhongwenTools::Romanization::Pinyin.pyn?(w[:py]), w.inspect
29
+ assert ZhongwenTools::Romanization::Pinyin.pyn?(w[:pyn]), w.inspect
30
+ end
31
+ end
32
+
33
+ def test_pyn_to_pinyin
34
+ [@hyphenated_words, @words].flatten.each do |word|
35
+ assert_equal word[:py], ZhongwenTools::Romanization::Pinyin.to_pinyin(word[:pyn])
36
+ assert_equal word[:py], ZhongwenTools::Romanization::Pinyin.to_py(word[:pyn])
37
+ end
38
+ end
39
+
40
+ def test_pinyin_to_pyn
41
+ @words.each do |word|
42
+ assert_equal word[:pyn], ZhongwenTools::Romanization::Pinyin.to_pyn(word[:py])
43
+ end
44
+ end
45
+
46
+ def setup
47
+ @hyphenated_words = [
48
+ {:pyn => 'A1-la1-bo2', :py => 'Ālābó'},
49
+ { :pyn => 'Mao2 Ze2-dong1', :py => 'Máo Zédōng' }
50
+ ]
51
+
52
+ @split_words = [
53
+ {:pyn => 'A1-la1-bo2', :py => 'Ālābó', :split => %w(A1 la1 bo2), split_py: %w(Ā lā bó) },
54
+ { :pyn => 'Mao2 Ze2-dong1', :py => 'Máo Zédōng', :split => %w(Mao2 Ze2 dong1), :split_py => %w(Máo Zé dōng) }
55
+ ]
56
+
57
+
58
+ @words = [
59
+ {:pyn => 'A1la1bo2', :py => 'Ālābó'},
60
+ { :pyn => 'ni3 hao3', :py => 'nǐ hǎo' },
61
+ { :pyn => 'Zhong1guo2', :py => 'Zhōngguó' },
62
+ { :pyn => 'chui1 niu3', :py => "chuī niǔ" },
63
+ { :pyn => 'Mao2 Ze2dong1', :py => 'Máo Zédōng' },
64
+ ]
65
+
66
+ @r = { :pyn => 'r5', :py => 'r' }
67
+ end
68
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require './test/test_helper'
5
+ require 'zhongwen_tools/regex'
6
+ class TestRegex < Minitest::Test
7
+
8
+ def test_pyn_regexes
9
+ assert 'hao3'[ZhongwenTools::Regex.pyn]
10
+ end
11
+
12
+ def test_py_regexes
13
+ assert 'hǎo'[ZhongwenTools::Regex.py ]
14
+ end
15
+
16
+ def test_numbers
17
+ str = '二'
18
+ assert str, str[ZhongwenTools::Regex.zh_numbers]
19
+
20
+ str = '两'
21
+ assert str[ZhongwenTools::Regex.zhs_numbers]
22
+ refute str[ZhongwenTools::Regex.zht_numbers]
23
+
24
+ str = '兩'
25
+ refute str[ZhongwenTools::Regex.zhs_numbers]
26
+ assert str[ZhongwenTools::Regex.zht_numbers]
27
+ end
28
+
29
+ def test_punctuation
30
+ refute '.'[ZhongwenTools::Regex.zh_punc]
31
+ assert '.'[ZhongwenTools::Regex.punc]
32
+ assert '。'[ZhongwenTools::Regex.zh_punc]
33
+ refute '。'[ZhongwenTools::Regex.punc]
34
+ end
35
+
36
+ def test_zh
37
+ assert '中'[ZhongwenTools::Regex.zh]
38
+ refute 'a'[ZhongwenTools::Regex.zh]
39
+ refute 'ご'[ZhongwenTools::Regex.zh]
40
+ end
41
+ end
@@ -1,168 +1,145 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
3
4
  require './test/test_helper'
4
- require 'zhongwen_tools/string'
5
5
  require 'zhongwen_tools/romanization'
6
-
7
- class String
8
- include ZhongwenTools::Romanization
9
- end
10
-
11
6
  class TestRomanization < Minitest::Test
12
7
 
13
- def test_pinyin
14
- assert_equal 'Zhōng wén','Zhong1 wen2'.to_pinyin
15
- assert_equal 'Zhōngwén', 'Zhong1-wen2'.to_pinyin
16
- assert_equal "Tiān'ānmén",'Tian1an1men2'.to_pinyin
17
- assert_equal @alabo[:py], @alabo[:pyn].to_pinyin
18
- assert_equal 'r', 'r5'.to_pinyin
19
- #wg -> py not yet implemented
20
- #mzd = "Mao Tse-tung"
21
- #assert_equal "Mao Zedong", mzd.to_pinyin(:wg)
22
- assert @alabo[:py].py?
23
- assert 'Ā-lā-bó'.py?
24
- assert 'Zhong1 wen2'.to_pinyin.py?
8
+ def test_romanization_modules
9
+ [:Pinyin, :ZhuyinFuhao, :WadeGiles, :Yale, :TongyongPinyin, :MPS2].each do |module_name|
10
+ assert ZhongwenTools::Romanization.const_defined?(module_name)
11
+ end
12
+ end
25
13
 
26
- @romanizations.each do |rom|
27
- rom.each do |type, entry|
28
- if type == :bopomofo
29
- assert_equal rom[:py].downcase, entry.to_pinyin(type).downcase, "to_pinyin(#{type}) should convert to pinyin."
30
- assert_equal rom[:py].downcase, entry.to_pinyin.downcase, "to_pinyin(#{type}) should convert to pinyin, but it isn't detected properly"
31
- else
32
- assert_equal rom[:py], entry.to_pinyin(type), "to_pinyin(#{type}) should convert to pinyin."
33
- assert_equal rom[:py], entry.to_pinyin, "to_pinyin(#{type}) should convert to pinyin, but it isn't detected properly" unless type == :typy
34
- end
14
+ def test_zhuyin_fuhao_method_names
15
+ @romanization_hashes.each do |module_name, method_names|
16
+ rom_module = ZhongwenTools::Romanization.const_get(module_name)
17
+ singleton_methods = rom_module.singleton_methods
18
+ method_names.map{ |x| "to_#{x}".to_sym }.each do |method_name|
19
+ message = "#{rom_module} should have method called #{ method_name }"
20
+ assert singleton_methods.include?(method_name), message
35
21
  end
36
22
  end
37
23
  end
38
24
 
39
- def test_pyn
40
- assert_equal 'ni3 hao3', @py.to_pyn(:py)
41
- assert_equal 'tian1an1men2', 'tian1an1men2'.to_py.to_pyn(:py)
42
-
43
- assert_equal 'yi4', 'yì'.to_pyn(:py)
25
+ def test_split_methods
26
+ @romanizations.each do |romanization|
27
+ length = romanization == @romanizations.last ? 3 : 2
44
28
 
45
- assert_equal 'ni3 hao3', 'ㄋㄧ3 ㄏㄠ3'.to_pyn(:bpmf)
46
- assert_equal 'ni3 hao3', 'ㄋㄧ3 ㄏㄠ3'.to_pyn
47
- assert_equal 'zhong1 guo2', 'chung1 kuo2'.to_pyn(:wg)
48
- assert_equal 'zhong1 guo2', 'chung1 kuo2'.to_pyn
49
- assert_equal 'chui1 niu3', 'chuei1 niou3'.to_pyn(:typy)
50
- assert_equal 'cao3 di4', 'tsau3 di4'.to_pyn(:mspy2)
51
- assert_equal 'cao3 di4', 'tsau3 di4'.to_pyn
52
- assert_equal 'cao3 di4', 'tsau3 di4'.to_pyn(:yale)
53
- assert_equal 'cao3 di4', 'tsau3 di4'.to_pyn
54
-
55
-
56
- assert_equal 'Wo3men5', "Wǒmen".to_pyn(:py)
57
- assert_equal 'hao3xue2', 'hǎoxué'.to_pyn(:py)
58
- assert_equal 'tai4re4', 'tàirè'.to_pyn(:py)
59
- assert_equal 'tai4tai5', "tàitai".to_pyn(:py)
60
- #assert_equal 'Wu1-lu2-ha1-nuo4-fu1', 'Wūlúhānuòfū'.to_pyn(:py)
61
- #"007:Dàpò Liàngzǐ Wēijī", "007: Da4po4 Liang4zi3 Wei1ji1"
29
+ assert_equal length, ZhongwenTools::Romanization::ZhuyinFuhao.split(romanization[:bpmf]).length
30
+ assert_equal length, ZhongwenTools::Romanization::WadeGiles.split(romanization[:wg]).length
31
+ assert_equal length, ZhongwenTools::Romanization::Yale.split(romanization[:yale]).length
32
+ assert_equal length, ZhongwenTools::Romanization::TongyongPinyin.split(romanization[:typy]).length
33
+ assert_equal length, ZhongwenTools::Romanization::MPS2.split(romanization[:mps2]).length
34
+ end
62
35
  end
63
36
 
64
37
  def test_zhuyin_fuhao
65
- assert_equal 'ㄋㄧ3 ㄏㄠ3', @str.to_bpmf
66
- assert_equal 'ㄋㄧ3 ㄏㄠ3', @str.to_bopomofo
67
- assert_equal 'ㄋㄧ3 ㄏㄠ3', @str.to_zhuyin
68
- assert_equal 'ㄇㄠ2 ㄗㄜ2 ㄉㄨㄥ1', @mzd.to_zhuyin_fuhao
69
- assert_equal 'ㄑㄧㄥ3 ㄏㄨㄟ2ㄉㄚ2 ㄨㄛ3 ㄉㄜ5 ㄨㄣ4ㄊㄧ2 .', @sent.to_zhuyin
70
- assert_equal 'ㄇㄠ2 ㄗㄜ2ㄉㄨㄥ1', @mzd2.to_zhuyin_fuhao
71
- assert 'ㄋㄧ3 ㄏㄠ3'.zyfh?
72
-
73
- assert_equal 'ㄋㄧ3 ㄏㄠ3', 'ni3 hau3'.to_bpmf(:yale)
74
-
75
- t = :bopomofo
76
- @romanizations.each do |rom|
77
- rom.each do |type, entry|
78
- #if type == :bopomofo
79
- assert_equal rom[t].downcase, entry.send("to_#{t}", type).downcase, "to_#{t}(#{type}) should convert to #{t}."
80
- assert_equal rom[t].downcase, entry.send("to_#{t}").downcase, "to_#{t}(#{type}) should convert to #{t}, but it isn't detected properly"
81
- #else
82
- #assert_equal rom[:t], entry.to_pinyin(type), "to_pinyin(#{type}) should convert to pinyin."
83
- #assert_equal rom[:t], entry.to_pinyin, "to_pinyin(#{type}) should convert to pinyin, but it isn't detected properly" unless type == :typy
84
- #end
85
- end
38
+ type = :bpmf
39
+ @romanizations.each do |romanization|
40
+ assert_equal type, ZhongwenTools::Romanization.romanization?(romanization[type])
41
+ assert_equal romanization[type], ZhongwenTools::Romanization::ZhuyinFuhao.to_bpmf(romanization[:pyn])
42
+ # NOTE: Zhuyin Fuhao doesn't encode capitilization information.
43
+ expected_result = romanization[:pyn].downcase.gsub('-', '')
44
+ py_result = romanization[:py].downcase
45
+ assert_equal expected_result, ZhongwenTools::Romanization::Pinyin::to_pyn(romanization[type])
46
+ assert_equal py_result, ZhongwenTools::Romanization::Pinyin::to_py(romanization[type])
86
47
  end
87
48
  end
88
49
 
89
50
  def test_wade_giles
90
- assert_equal 'kuo1', 'guo1'.to_wg
91
- assert_equal 'chung1 kuo2', 'zhong1 guo2'.to_wg
92
- assert_equal 'Mao2 Tse2 tung1', @mzd.to_wg
93
- assert_equal 'Mao2 Tse2-tung1', @mzd2.to_wade_giles
94
- assert_equal 'Mao2 Tse2-tung1 te5 mao2', 'Mao2 Ze2-dong1 de5 mao2'.to_wade_giles
95
-
96
- assert_equal 'ni3 hao3', 'ni3 hau3'.to_wg(:yale)
97
- end
98
-
99
- #def test_mspy2
100
- #skip
101
- #assert_equal '', @str.to_mspy2
102
- #end
103
-
104
- def test_typy
105
- #skip
106
- pyn = 'chui1 niu3'
107
- typy = 'chuei1 niou3'
108
- assert_equal typy, pyn.to_typy
109
- # FIXME: to_typy doesn't work with non-spaced pinyin.
110
- #assert_equal typy, typy.to_pyn(:typy)
111
- assert typy.typy?
112
- refute pyn.typy?
51
+ type = :wg
52
+ @romanizations.each do |romanization|
53
+ assert ZhongwenTools::Romanization::WadeGiles.wg?(romanization[type])
54
+ assert_equal romanization[type], ZhongwenTools::Romanization::WadeGiles.to_wg(romanization[:pyn])
55
+ expected_result = romanization[:pyn]
56
+ assert_equal expected_result, ZhongwenTools::Romanization::Pinyin::to_pyn(romanization[type], type)
57
+ assert_equal romanization[:py], ZhongwenTools::Romanization::Pinyin::to_py(romanization[type])
58
+ end
113
59
  end
114
60
 
115
61
  def test_yale
116
- assert_equal 'ni3 hau3', @str.to_yale
117
-
118
- assert_equal 'chwei1 nyou3', 'chuei1 niou3'.to_yale(:typy)
62
+ type = :yale
63
+ @romanizations.each do |romanization|
64
+ assert_equal type, ZhongwenTools::Romanization.romanization?(romanization[type])
65
+ assert_equal romanization[type], ZhongwenTools::Romanization::Yale.to_yale(romanization[:pyn])
66
+ expected_result = romanization[:pyn]
67
+ assert_equal expected_result, ZhongwenTools::Romanization::Pinyin::to_pyn(romanization[type])
68
+ assert_equal romanization[:py], ZhongwenTools::Romanization::Pinyin::to_py(romanization[type])
69
+ end
119
70
  end
120
71
 
121
- def test_romanization?
122
- assert_equal :pyn, @alabo[:pyn].romanization?
123
- assert_equal :py, @alabo[:py].romanization?
124
- assert_equal :zyfh, 'ㄋㄧ3 ㄏㄠ3'.romanization?
125
- assert_equal :wg, @mzd.to_wg(:pyn).romanization?
72
+ def test_tongyong_pinyin
73
+ type = :typy
74
+ @romanizations.each do |romanization|
75
+ assert ZhongwenTools::Romanization::TongyongPinyin.typy?(romanization[type])
76
+ assert_equal romanization[type], ZhongwenTools::Romanization::TongyongPinyin.to_typy(romanization[:pyn])
77
+ expected_result = romanization[:pyn]
78
+ assert_equal expected_result, ZhongwenTools::Romanization::Pinyin::to_pyn(romanization[type], type)
79
+ assert_equal romanization[:py], ZhongwenTools::Romanization::Pinyin::to_py(romanization[type], type)
80
+ end
126
81
  end
127
82
 
128
- def test_detect
129
- assert @str.pyn?
130
- assert " #{@str}".pyn?
131
- refute 'Ardanz'.pyn?
132
- refute @py.pyn?
83
+ def test_mps2
84
+ type = :mps2
133
85
 
134
- assert 'chung1 kuo2'.wg?
135
-
136
- assert @py.py?, "#{@py} should be pinyin. (#{@py.py?})" unless RUBY_VERSION < '1.9'
137
- assert 'chuei1 niou3'.typy?
138
- assert 'ㄋㄧ3 ㄏㄠ3'.bpmf?
139
- assert 'ni3 hau3'.yale?
140
- assert 'tsuen'.mps2?
141
- end
142
-
143
- def test_split_pyn
144
- assert_equal 'zhong1guo2'.split_pyn, %w(zhong1 guo2)
145
- assert_equal 'dong1xi'.split_pyn, %w(dong1 xi)
146
- assert_equal 'zhongguo'.split_pyn, %w(zhong guo)
147
- assert_equal 'dong1 xi1 '.split_pyn, %w(dong1 xi1)
148
- assert_equal @mzd2.split_pyn, %w(Mao2 Ze2 dong1)
86
+ @romanizations.each do |romanization|
87
+ assert ZhongwenTools::Romanization::MPS2.mps2?(romanization[type])
88
+ assert_equal romanization[type], ZhongwenTools::Romanization::MPS2.to_mps2(romanization[:pyn])
89
+ expected_result = romanization[:pyn]
90
+ assert_equal expected_result, ZhongwenTools::Romanization::Pinyin::to_pyn(romanization[type], type)
91
+ assert_equal romanization[:py], ZhongwenTools::Romanization::Pinyin::to_py(romanization[type], type)
92
+ end
149
93
  end
150
94
 
151
95
  def setup
96
+ @romanization_hashes = {
97
+ ZhuyinFuhao: %w(bpmf zhuyin_fuhao zhuyinfuhao zyfh zhyfh),
98
+ WadeGiles: %w(wg wade_giles),
99
+ Yale: ['yale'],
100
+ TongyongPinyin: %w(typy tongyong tongyong_pinyin),
101
+ MPS2: ['mps2']
102
+ }
103
+
152
104
  @romanizations = [
153
105
  # FIXME: bopomofo, tongyong pinyin, wade-giles tones are all wrong.
154
106
  # TODO: test IPA
155
- { :pyn => 'ni3 hao3', :py => 'nǐ hǎo', :bopomofo => 'ㄋㄧ3 ㄏㄠ3', :yale => 'ni3 hau3', :typy => 'ni3 hao3', :wg => 'ni3 hao3'},#, :ipa => ''}
156
- { :pyn => 'Zhong1guo2', :py => 'Zhōngguó', :bopomofo => 'ㄓㄨㄥ1ㄍㄨㄛ2', :yale => 'Jung1gwo2', :typy => 'Jhong1guo2', :wg => 'Chung1kuo2'},#, :ipa => ''}
157
- { :pyn => 'chui1 niu3', :py => "chuī niǔ", :bopomofo => "ㄔㄨㄟ1 ㄋㄧㄡ3", :yale => "chwei1 nyou3", :typy => "chuei1 niou3", :wg => "ch`ui1 niu3"},#, :ipa => ''}
158
- { :pyn => 'Mao2 Ze2-dong1', :py => 'Máo Zédōng', :bopomofo => 'ㄇㄠ2 ㄗㄜ2ㄉㄨㄥ1', :yale => 'Mau2 Dze2-dung1', :typy => 'Mao2 Ze2-dong1', :wg => 'Mao2 Tse2-tung1'},#, :ipa => ''}
107
+ {
108
+ pyn: 'ni3 hao3',
109
+ py: ' hǎo',
110
+ bpmf: 'ㄋㄧ3 ㄏㄠ3',
111
+ yale: 'ni3 hau3',
112
+ typy: 'ni3 hao3',
113
+ wg: 'ni3 hao3',
114
+ mps2: 'ni3 hau3'
115
+ },
116
+ {
117
+ pyn: 'Zhong1guo2',
118
+ py: 'Zhōngguó',
119
+ bpmf: 'ㄓㄨㄥ1ㄍㄨㄛ2',
120
+ yale: 'Jung1gwo2',
121
+ typy: 'Jhong1guo2',
122
+ wg: 'Chung1kuo2',
123
+ mps2: 'Jung1guo2',
124
+ },
125
+ {
126
+ pyn: 'chui1 niu3',
127
+ py: 'chuī niǔ',
128
+ bpmf: "ㄔㄨㄟ1 ㄋㄧㄡ3",
129
+ yale: "chwei1 nyou3",
130
+ typy: "chuei1 niou3",
131
+ wg: "ch`ui1 niu3",
132
+ mps2: 'chuei1 niou3'
133
+ },
134
+ {
135
+ pyn: 'Mao2 Ze2-dong1',
136
+ py: 'Máo Zédōng',
137
+ bpmf: 'ㄇㄠ2 ㄗㄜ2ㄉㄨㄥ1',
138
+ yale: 'Mau2 Dze2-dung1',
139
+ typy: 'Mao2 Ze2-dong1',
140
+ wg: 'Mao2 Tse2-tung1',
141
+ mps2: 'Mau2 Tze2-dung1'
142
+ }
159
143
  ]
160
-
161
- @str = 'ni3 hao3'
162
- @mzd = 'Mao2 Ze2 dong1'
163
- @mzd2 = 'Mao2 Ze2-dong1'
164
- @py = 'nǐ hǎo'
165
- @sent = 'Qing3 hui2-da2 wo3 de5 wen4-ti2 .'
166
- @alabo = {:pyn => 'A1-la1-bo2', :py => 'Ālābó'}
167
144
  end
168
145
  end