ting 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ting.rb CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  $: << File.dirname(__FILE__)
8
8
 
9
+ require 'ting/version'
9
10
  require 'ting/support'
10
11
  require 'ting/groundwork'
11
12
  require 'ting/exception'
@@ -16,8 +17,6 @@ require 'ting/conversions'
16
17
  require 'ting/conversions/hanyu'
17
18
 
18
19
  module Ting
19
- VERSION = "0.2.1"
20
-
21
20
  class Reader
22
21
  def initialize(conv, tone)
23
22
  @conv = conv.to_s
@@ -12,7 +12,8 @@ module Ting
12
12
  klazz=Ting.const_get c
13
13
  begin
14
14
  CSV.open(DATA_DIR+c.downcase+'.csv', 'r').each do |name, *values|
15
- All << name.to_s unless All.index name || name =~ /name|standalone/i
15
+ next if name == "name"
16
+ All << name.to_s unless All.include?(name) || name =~ /standalone/i
16
17
  klazz.class_eval {attr_accessor name.to_sym}
17
18
  values.each_with_index do |v,i|
18
19
  klazz::All[i].send(name+'=', v)
@@ -29,11 +30,15 @@ module Ting
29
30
  @@rules=YAML::load(IO.read(DATA_DIR+'rules.yaml'))
30
31
 
31
32
  def self.parse(type, string)
32
- if (fin = Final::All.find {|f| f.respond_to?("#{type}_standalone") && f.send("#{type}_standalone") == string})
33
- TonelessSyllable.new(Initial::Empty, fin)
33
+ if (final = Final::All.find {|f| f.respond_to?("#{type}_standalone") && f.send("#{type}_standalone") == string})
34
+ TonelessSyllable.new(Initial::Empty, final)
34
35
  else
35
- Initial::All.find do |ini|
36
- Final::All.find do |fin|
36
+ finals = Final::All.dup
37
+ finals.unshift(finals.delete(Final::Uo)) #hack : move Uo to the front
38
+ #otherwise wadegiles parses 'lo' as Le+O rather than Le+Uo
39
+ #probably better to add a hardcoded 'overrule' table for these cases
40
+ Initial::All.each do |ini|
41
+ finals.each do |fin|
37
42
  next if TonelessSyllable.illegal?(ini,fin)
38
43
  return TonelessSyllable.new(ini,fin) if apply_rules(type, (ini.send(type)||'') + (fin.send(type)||'')) == string
39
44
  end
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module Ting
2
4
  module Conversions
3
5
  class Hanyu
@@ -20,7 +22,7 @@ module Ting
20
22
  valid_chars = []
21
23
  Ting.valid_combinations do |i,f|
22
24
  1.upto(5) do |tone|
23
- valid_chars += @tone.add_tone(Conversions.unparse(:hanyu,TonelessSyllable.new(i,f)), tone).chars
25
+ valid_chars += @tone.add_tone(Conversions.unparse(:hanyu,TonelessSyllable.new(i,f)), tone).uchars
24
26
  end
25
27
  end
26
28
  valid_chars.sort!.uniq!
@@ -30,7 +32,7 @@ module Ting
30
32
  def parse(string)
31
33
  result = []
32
34
  looking_at = []
33
- string.chars.each do |ch|
35
+ string.uchars.each do |ch|
34
36
  head, syll = parse_tail(looking_at)
35
37
  looking_at << ch
36
38
  if syll && !parse_tail(looking_at)
@@ -1,24 +1,27 @@
1
- hanyu: - match: (j|q|x)ü
2
- subst: \1u
3
- wadegiles: - match: pie\Z
4
- subst: pieh
5
- - match: (ts|ch)`eh
6
- subst: \1`e
7
- - match: ts`ih
8
- subst: tz`u
9
- - match: (k|k`|h)e\Z
10
- subst: \1o
11
- - match: (k|k`)ui\Z
12
- subst: \1uei
13
- - match: (sh|ch)o\Z
14
- subst: \1e
15
- - match: (ch|ch`|ts`|t|t`|l|n|j|s)uo
16
- subst: \1o
17
- - match: tsih
18
- subst: tzu
19
- - match: sih
20
- subst: ssu
21
- tongyong: - match: feng
22
- subst: fong
1
+ hanyu:
2
+ - match: (j|q|x)ü
3
+ subst: \1u
4
+ wadegiles:
5
+ - match: pie\Z
6
+ subst: pieh
7
+ - match: (ts|ch)`eh
8
+ subst: \1`e
9
+ - match: ts`ih
10
+ subst: tz`u
11
+ - match: (k|k`|h)e\Z
12
+ subst: \1o
13
+ - match: (k|k`)ui\Z
14
+ subst: \1uei
15
+ - match: (sh|ch)o\Z
16
+ subst: \1e
17
+ - match: (ch|ch`|ts`|t|t`|l|n|j|s)uo
18
+ subst: \1o
19
+ - match: tsih
20
+ subst: tzu
21
+ - match: sih
22
+ subst: ssu
23
+ tongyong:
24
+ - match: feng
25
+ subst: fong
23
26
 
24
-
27
+
@@ -144,8 +144,8 @@ module Ting
144
144
  [Initial::Group_1, Final::Group_V],
145
145
  [Initial::Group_3, Final::Group_V],
146
146
 
147
- #2008.05.26 lo is also valid!
148
- #[Initial::Group_2, [Final::O]], #Only bo, po, mo and fo are valid -o combinations
147
+ # For "咯 / lo5" to parse correctly we need to list "Le + O" as valid,
148
+ [Initial::Group_2 - [Initial::Le], [Final::O]], #Only bo, po, mo and fo are valid -o combinations
149
149
  [Initial::Group_3, [Final::O]],
150
150
  [Initial::Group_4, [Final::O]],
151
151
  [Initial::Group_5, [Final::O]],
data/lib/ting/string.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  class String
2
4
  PINYIN_CACHE={}
3
5
 
data/lib/ting/support.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class String
2
- def chars
2
+ def uchars
3
3
  self.unpack('U*').map{|c| [c].pack('U')}
4
4
  end
5
5
 
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module Ting
2
4
  module Tones
3
5
  class Accents < Tone
@@ -22,11 +24,16 @@ module Ting
22
24
  syll.gsub!('ü','v')
23
25
  tone %= MAX_TONE
24
26
  case syll
25
- when /a/ : syll.sub(/a/, tone_glyph(:a,tone))
26
- when /e/ : syll.sub(/e/, tone_glyph(:e,tone))
27
- when /o/ : syll.sub(/o/, tone_glyph(:o,tone))
28
- when /(i|u|v)/ : syll.sub($1, tone_glyph($1,tone))
29
- else syll
27
+ when /a/
28
+ syll.sub(/a/, tone_glyph(:a,tone))
29
+ when /e/
30
+ syll.sub(/e/, tone_glyph(:e,tone))
31
+ when /o/
32
+ syll.sub(/o/, tone_glyph(:o,tone))
33
+ when /(i|u|v)/
34
+ syll.sub($1, tone_glyph($1,tone))
35
+ else
36
+ syll
30
37
  end
31
38
  end
32
39
 
@@ -56,7 +63,7 @@ module Ting
56
63
  end
57
64
  end
58
65
 
59
- end
66
+ end
60
67
  end
61
68
  end
62
69
  end
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module Ting
2
4
  module Tones
3
5
  class Ipa < Tone
@@ -10,7 +12,9 @@ module Ting
10
12
  end
11
13
 
12
14
  def peek_tone(syll)
13
- return t if t = GLYPHS.index(syll.chars[-1])
15
+ if t = GLYPHS.index(syll.uchars[-1])
16
+ return t
17
+ end
14
18
  return NEUTRAL_TONE
15
19
  end
16
20
 
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module Ting
2
4
  module Tones
3
5
  class Marks < Tone
@@ -11,10 +13,14 @@ module Ting
11
13
 
12
14
  def peek_tone(syll)
13
15
  case syll
14
- when /ˊ/ : 2
15
- when /ˇ/ : 3
16
- when /ˋ/ : 4
17
- when /˙/ : NEUTRAL_TONE
16
+ when /ˊ/
17
+ 2
18
+ when /ˇ/
19
+ 3
20
+ when /ˋ/
21
+ 4
22
+ when /˙/
23
+ NEUTRAL_TONE
18
24
  else
19
25
  1
20
26
  end
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module Ting
2
4
  module Tones
3
5
  class Supernum < Tone
@@ -10,7 +12,9 @@ module Ting
10
12
  end
11
13
 
12
14
  def peek_tone(syll)
13
- return t if t = GLYPHS.index(syll.chars[-1])
15
+ if t = GLYPHS.index(syll.uchars[-1])
16
+ return t
17
+ end
14
18
  return NEUTRAL_TONE
15
19
  end
16
20
 
@@ -0,0 +1,3 @@
1
+ module Ting
2
+ VERSION = '0.3.0'
3
+ end
@@ -29,7 +29,7 @@ class TestCompare < Test::Unit::TestCase
29
29
  ito = CHART[0].index to.to_s
30
30
 
31
31
  CHART[1..-1].each do |vals|
32
- assert_equal(vals[ito].strip, writer << (reader << vals[ifrom].strip), "Converting from #{from} to #{to} value #{vals[ito]}")
32
+ assert_equal(vals[ito].strip, writer << (reader << vals[ifrom].strip), "Converting `#{vals[ifrom]}' from #{from} to #{to} value #{vals[ito]}")
33
33
  end
34
34
  end
35
35
  end
@@ -2,7 +2,9 @@ require 'test/unit'
2
2
  require 'ting'
3
3
  require 'yaml'
4
4
 
5
- $KCODE='u'
5
+ if RUBY_VERSION =~ /^1.8/
6
+ $KCODE='u'
7
+ end
6
8
 
7
9
  module HanyuCoverage
8
10
  grid=YAML.load(IO.read(File.dirname(__FILE__)+'/../lib/ting/data/valid_pinyin.yaml'))
@@ -22,7 +24,7 @@ module HanyuCoverage
22
24
  end
23
25
 
24
26
  def test_unparse_#{hanyu}
25
- ts=*@reader.parse('#{hanyu}')
27
+ ts=@reader.parse('#{hanyu}').first
26
28
  assert_not_nil(ts, 'Reader<:hanyu, :no_tone>#parse("#{hanyu}") returned nil')
27
29
  assert_equal(Initial::#{iname}, ts.initial, 'Wrong initial for `#{hanyu}`, expected Initial::#{iname}')
28
30
  assert_equal(Final::#{fname}, ts.final, 'Wrong final for `#{hanyu}`, expected Final::#{fname}')
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ting
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 19
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Arne Brasseur
@@ -14,13 +15,12 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-04-04 00:00:00 +02:00
18
- default_executable:
18
+ date: 2012-05-06 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
- description: Ting can convert between various systems for phonetically writing Mandarin Chinese. It can also handle various representation of tones, so it can be used to convert pinyin with numbers to pinyin with tones.
21
+ description: Ting can convert between various phonetic representations of Mandarin Chinese. It can also handle various representation of tones, so it can be used to convert pinyin with numbers to pinyin with tones.
22
22
  email:
23
- - arne@arnebrasseur.net
23
+ - arne.brasseur@gmail.com
24
24
  executables: []
25
25
 
26
26
  extensions: []
@@ -28,38 +28,39 @@ extensions: []
28
28
  extra_rdoc_files:
29
29
  - README.rdoc
30
30
  - History.txt
31
+ - TODO
31
32
  files:
32
33
  - History.txt
33
34
  - README.rdoc
34
35
  - Rakefile
35
36
  - TODO
36
- - examples/cgiform/cgiform.rb
37
- - examples/cgiform/template.rhtml
38
- - examples/hello.rb
39
- - lib/ting.rb
40
- - lib/ting/conversion.rb
37
+ - lib/ting/tones.rb
41
38
  - lib/ting/conversions.rb
39
+ - lib/ting/tones/supernum.rb
40
+ - lib/ting/tones/accents.rb
41
+ - lib/ting/tones/numbers.rb
42
+ - lib/ting/tones/ipa.rb
43
+ - lib/ting/tones/no_tones.rb
44
+ - lib/ting/tones/marks.rb
45
+ - lib/ting/version.rb
42
46
  - lib/ting/conversions/hanyu.rb
47
+ - lib/ting/groundwork.rb
48
+ - lib/ting/exception.rb
49
+ - lib/ting/conversion.rb
50
+ - lib/ting/string.rb
51
+ - lib/ting/support.rb
52
+ - lib/ting.rb
43
53
  - lib/ting/data/comparison.csv
44
54
  - lib/ting/data/final.csv
45
55
  - lib/ting/data/initial.csv
46
56
  - lib/ting/data/paladiy.txt
47
- - lib/ting/data/rules.yaml
48
57
  - lib/ting/data/valid_pinyin.yaml
49
- - lib/ting/exception.rb
50
- - lib/ting/groundwork.rb
51
- - lib/ting/string.rb
52
- - lib/ting/support.rb
53
- - lib/ting/tones.rb
54
- - lib/ting/tones/accents.rb
55
- - lib/ting/tones/supernum.rb
56
- - lib/ting/tones/ipa.rb
57
- - lib/ting/tones/marks.rb
58
- - lib/ting/tones/no_tones.rb
59
- - lib/ting/tones/numbers.rb
60
- - test/test_comparison.rb
58
+ - lib/ting/data/rules.yaml
59
+ - examples/hello.rb
60
+ - examples/cgiform/cgiform.rb
61
+ - examples/cgiform/template.rhtml
61
62
  - test/test_hanyu_coverage.rb
62
- has_rdoc: true
63
+ - test/test_comparison.rb
63
64
  homepage: http://github.com/arnebrasseur/ting
64
65
  licenses: []
65
66
 
@@ -70,23 +71,27 @@ rdoc_options:
70
71
  require_paths:
71
72
  - lib
72
73
  required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
73
75
  requirements:
74
76
  - - ">="
75
77
  - !ruby/object:Gem::Version
78
+ hash: 3
76
79
  segments:
77
80
  - 0
78
81
  version: "0"
79
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
80
84
  requirements:
81
85
  - - ">="
82
86
  - !ruby/object:Gem::Version
87
+ hash: 3
83
88
  segments:
84
89
  - 0
85
90
  version: "0"
86
91
  requirements: []
87
92
 
88
93
  rubyforge_project:
89
- rubygems_version: 1.3.6
94
+ rubygems_version: 1.8.10
90
95
  signing_key:
91
96
  specification_version: 2
92
97
  summary: A conversion library for Chinese transcription methods like Hanyu Pinyin, Bopomofo and Wade-Giles.