Module: Pinyin::Conversions

Defined in:
lib/pinyin/conversions.rb,
lib/pinyin/conversions/hanyu.rb

Defined Under Namespace

Classes: Hanyu

Constant Summary collapse

All =
[]
DATA_DIR =
File.dirname(__FILE__)+'/data/'
@@rules =

Substitution rules

YAML::load(IO.read(DATA_DIR+'rules.yaml'))

Class Method Summary collapse

Class Method Details

.parse(type, string) ⇒ Object



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

def self.parse(type, string)
  if (fin = Final::All.find {|f| f.respond_to?("#{type}_standalone") && f.send("#{type}_standalone") == string})
    TonelessSyllable.new(Initial::Empty, fin)
  else
    Initial::All.find do |ini|
      Final::All.find do |fin|
        next                                  if TonelessSyllable.illegal?(ini,fin)
        return TonelessSyllable.new(ini,fin)  if apply_rules(type, (ini.send(type)||'') + (fin.send(type)||'')) == string
      end
    end
  end
end

.tokenize(str) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/pinyin/conversions.rb', line 54

def self.tokenize(str)
  returning [] do |ary|
    str,pos = str.dup, 0
    while s=str.slice!(/[^' ]*/) and s != ""
      ary << [s.strip, pos]
      pos+=s.length
      str.slice!(/[' ]/)
    end
  end
end

.unparse(type, tsyll) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/pinyin/conversions.rb', line 44

def self.unparse(type, tsyll)
  if tsyll.initial.send(type)
    apply_rules(type, tsyll.initial.send(type) + (tsyll.final.send(type) || ''))
  elsif tsyll.final.respond_to?(type.to_s+'_standalone') && standalone = tsyll.final.send(type.to_s+'_standalone')
    standalone
  else
    apply_rules(type, tsyll.final.send(type))
  end
end