Class: Pinyin::Conversions::Hanyu
- Inherits:
-
Object
- Object
- Pinyin::Conversions::Hanyu
- Defined in:
- lib/pinyin/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.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/pinyin/conversions/hanyu.rb', line 4 def initialize(tone = :numbers, = {}) @options = @options[:preprocess] ||= lambda {|s| s.gsub(/u:|Ü/, 'ü').downcase } if Class === tone @tone = tone else @tone = Pinyin::Tones.const_get(tone.to_s.camelcase) end end |
Instance Method Details
#parse(string) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pinyin/conversions/hanyu.rb', line 30 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pinyin/conversions/hanyu.rb', line 56 def parse_syllable(tone_syll) tone_syll = tone_syll.to_s tone_syll = @options[:preprocess].call(tone_syll) if @options[:preprocess] # p tone_syll 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
46 47 48 49 50 51 52 53 54 |
# File 'lib/pinyin/conversions/hanyu.rb', line 46 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
15 16 17 |
# File 'lib/pinyin/conversions/hanyu.rb', line 15 def valid_character_regexp @valid_character_regexp ||= valid_character_regexp! end |
#valid_character_regexp! ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pinyin/conversions/hanyu.rb', line 19 def valid_character_regexp! valid_chars = [] Pinyin.valid_combinations do |i,f| 1.upto(5) do |tone| valid_chars += @tone.add_tone(Conversions.unparse(:hanyu,TonelessSyllable.new(i,f)), tone).chars end end valid_chars.sort!.uniq! Regexp.new(valid_chars.map{|ch| Regexp.escape(ch)}.join('|')) end |