Class: Pinyin::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/pinyin.rb

Instance Method Summary collapse

Constructor Details

#initialize(conv, tone) ⇒ Reader

Returns a new instance of Reader.



24
25
26
27
# File 'lib/pinyin.rb', line 24

def initialize(conv, tone)
  @conv = conv.to_s
  @tone = Tones.const_get tone.to_s.camelcase
end

Instance Method Details

#parse(str) ⇒ Object Also known as: <<



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pinyin.rb', line 29

def parse(str)
  Conversions.tokenize(str).map do |s, pos|
    tone,syll = @tone.pop_tone(s)
    tsyll = Conversions.parse(@conv,syll)
    ini, fin = tsyll.initial, tsyll.final
    unless tone && fin && ini
      raise ParseError.new(s,pos),"Illegal syllable <#{s}> in input <#{str}> at position #{pos}." 
    end
    Syllable.new(ini, fin, tone)
  end
rescue Object => e
  raise ParseError.new(str,0), "Parsing of #{str.inspect} failed : #{e}"
end