lib/ting.rb in ting-0.11.0 vs lib/ting.rb in ting-0.12.0

- old
+ new

@@ -48,18 +48,33 @@ hsh[syll] = Ting.writer(:hanyu, :accents).( Ting.reader(:hanyu, :numbers).(syll.downcase) ) end + # The longest syllables are six letters long (chuang, shuang, zhuang). + SYLLABLE_REGEXP = /[A-Za-züÜ]{1,6}\d?/ def pretty_tones(string) - string.gsub('u:','ü').gsub(/[A-Za-züÜ]{1,7}\d?/) do |syll| - SYLLABLE_CACHE[syll] + string = string.gsub('u:', 'ü') # (note that this implicitly dups the string) + # Scan through the string, replacing syllable by syllable. + pos = 0 + while match = string.match(SYLLABLE_REGEXP, pos) + syllable = match[0] + replacement = SYLLABLE_CACHE[syllable] + match_pos = match.begin(0) + # If this syllable starts with a vowel and is preceded by a letter (not whitespace or + # control characters), insert an apostrophe before it. + if match_pos > 0 && string[match_pos - 1] =~ /[[:alpha:]]/ && syllable =~ /^[AEOaoe]/ + replacement = "'" + replacement + end + string[match_pos, syllable.length] = replacement + pos = match_pos + replacement.length end + string end def bpmf(string) - string.gsub('u:','ü').scan(/[A-Za-züÜ]{1,7}\d?/).map do |m| + string.gsub('u:', 'ü').scan(SYLLABLE_REGEXP).map do |m| Ting.writer(:zhuyin, :marks).( Ting.reader(:hanyu, :numbers).(m.downcase) ) end.join(' ') end