Class: Ting::Conversions::Hanyu
- Inherits:
-
Object
- Object
- Ting::Conversions::Hanyu
- Defined in:
- lib/ting/conversions/hanyu.rb
Instance Method Summary collapse
-
#initialize(tone = :numbers, options = {}) ⇒ Hanyu
constructor
A new instance of Hanyu.
- #parse(string) ⇒ Object
- #parse_syllable(tone_syll) ⇒ Object
- #parse_tail(chars) ⇒ Object
- #valid_character_regexp ⇒ Object
- #valid_character_regexp! ⇒ Object
Constructor Details
#initialize(tone = :numbers, options = {}) ⇒ Hanyu
Returns a new instance of Hanyu.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/ting/conversions/hanyu.rb', line 6 def initialize(tone = :numbers, = {}) @options = @options[:preprocess] ||= lambda {|s| s.gsub(/u:|Ü/, 'ü').downcase } if Class === tone @tone = tone else @tone = Ting::Tones.const_get(Ting.camelize(tone.to_s)) end end |
Instance Method Details
#parse(string) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ting/conversions/hanyu.rb', line 32 def parse(string) result = [] looking_at = [] string.chars.each do |ch| head, syll = parse_tail(looking_at) looking_at << ch if syll && !parse_tail(looking_at) puts "-> #{syll.inspect}" result << head.to_s unless head.empty? result << syll looking_at = [ch] end end result end |
#parse_syllable(tone_syll) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ting/conversions/hanyu.rb', line 58 def parse_syllable(tone_syll) tone_syll = tone_syll.to_s tone_syll = @options[:preprocess].call(tone_syll) if @options[:preprocess] tone, syll = @tone.pop_tone(tone_syll) if tone && syll ini_fini = Conversions.parse(:hanyu,syll) if ini_fini p tone, syll, ini_fini ini, fini = ini_fini.initial, ini_fini.final end return Syllable.new(ini, fini, tone) if tone && ini && fini end end |
#parse_tail(chars) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/ting/conversions/hanyu.rb', line 48 def parse_tail(chars) 7.downto(1) do |i| head = chars[0...-i] tail = chars[-i..-1] syll = parse_syllable( tail ) return head, syll if syll end nil end |
#valid_character_regexp ⇒ Object
17 18 19 |
# File 'lib/ting/conversions/hanyu.rb', line 17 def valid_character_regexp @valid_character_regexp ||= valid_character_regexp! end |
#valid_character_regexp! ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ting/conversions/hanyu.rb', line 21 def valid_character_regexp! valid_chars = [] Ting.valid_combinations do |i,f| 1.upto(5) do |tone| valid_chars += @tone.add_tone(Conversions.unparse(:hanyu,Syllable.new(i,f)), tone).chars.to_a end end valid_chars.sort!.uniq! Regexp.new(valid_chars.map{|ch| Regexp.escape(ch)}.join('|')) end |