Class: Ting::Tones::Accents

Inherits:
Ting::Tone show all
Defined in:
lib/ting/tones/accents.rb

Constant Summary collapse

UNICODE_TONE_GLYPHS =
{
  :a=>[97, 257, 225, 462, 224],
  :e=>[101, 275, 233, 283, 232],
  :i=>[105, 299, 237, 464, 236],
  :o=>[111, 333, 243, 466, 242],
  :u=>[117, 363, 250, 468, 249],
  :v=>[252, 470, 472, 474, 476]
}

Constants inherited from Ting::Tone

Ting::Tone::MAX_TONE, Ting::Tone::VALID_TONES

Class Method Summary collapse

Class Method Details

.add_tone(syll, tone) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ting/tones/accents.rb', line 23

def add_tone(syll, tone)
  syll = syll.sub('ü','v')
  tone = tone % MAX_TONE
  case syll
  when /a/
    syll.sub(/a/, tone_glyph(:a,tone))
  when /e/
    syll.sub(/e/, tone_glyph(:e,tone)).sub('v', 'ü')
  when /o/
    syll.sub(/o/, tone_glyph(:o,tone))
  when /(i|u|v)\z/
    syll.sub($1, tone_glyph($1,tone)).sub('v', 'ü')
  when /(i|u|v)/
    syll.sub($1, tone_glyph($1,tone)).sub('v', 'ü')
  else
    syll
  end
end

.peek_tone(syll) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ting/tones/accents.rb', line 42

def peek_tone(syll)
  candidates = each_tone_glyph.map do |vowel, tones|
    tone_glyph = syll.codepoints.find {|t| tones.include?(t)}
    normalize( tones.index(tone_glyph) ) if tone_glyph
  end.compact
  candidates.reject {|tone| tone == 5}.first || candidates.first
end

.pop_tone(syll) ⇒ Object

returns [ tone number, syllable without tone ] e.g. ni3 => [ 3, ‘ni’ ]



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ting/tones/accents.rb', line 52

def pop_tone(syll)
  tone = peek_tone(syll)
  unpacked = syll.codepoints.to_a
  each_tone_glyph do |vowel, tone_glyph_codepoints|
    tone_glyph_cp = tone_glyph_codepoints[tone % MAX_TONE]
    if unpacked.include? tone_glyph_cp
      unpacked[unpacked.index(tone_glyph_cp)] = vowel.to_s.unpack('U').first
      return [normalize(tone_glyph_codepoints.index(tone_glyph_cp)), unpacked.pack('U*').sub('v', 'ü')]
    end
  end
end

.tone_glyph(letter, tone) ⇒ Object



17
18
19
20
21
# File 'lib/ting/tones/accents.rb', line 17

def tone_glyph(letter,tone)
  if (u=UNICODE_TONE_GLYPHS[letter.to_sym][tone%MAX_TONE])
    [u].pack('U')
  end
end