lib/ting.rb in ting-0.3.0 vs lib/ting.rb in ting-0.9.0

- old
+ new

@@ -1,92 +1,67 @@ +# -*- coding: utf-8 -*- + # Handle several romanization systems for Mandarin Chinese # # Author:: Arne Brasseur (arne@arnebrasseur.net) -# Copyright:: Copyright (c) 2007-2010, Arne Brasseur +# Copyright:: Copyright (c) 2007-2014, Arne Brasseur # Licence:: GNU General Public License, v3 -$: << File.dirname(__FILE__) - require 'ting/version' -require 'ting/support' require 'ting/groundwork' require 'ting/exception' require 'ting/tones' require 'ting/conversion' require 'ting/conversions' require 'ting/conversions/hanyu' +require 'ting/procable' +require 'ting/reader' +require 'ting/writer' +require 'ting/converter' + module Ting - class Reader - def initialize(conv, tone) - @conv = conv.to_s - @tone = Tones.const_get tone.to_s.camelcase - @cache = {} - end + class << self - def parse(str) - return @cache[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}" + def reader(format, tones) + Reader.new(format,tones) end - alias :<< :parse - end - - class Writer - def initialize(conv, tone) - @conv = conv.to_s - @tone = Tones.const_get tone.to_s.camelcase - @cache = {} + def writer(format, tones) + Writer.new(format,tones) end - def generate(py) - conv=lambda {|syll| @tone.add_tone(Conversions.unparse(@conv,syll),syll.tone)} - return @cache[py] ||= if py.respond_to? :map - py.map(&conv).join(' ') - else - conv.call(py) - end + def from(from, from_tone) + Converter.new(from, from_tone, nil, nil) end - alias :<< :generate - alias :unparse :generate - end - - class Converter - def initialize(from, from_tone, to, to_tone) - @reader = Reader.new(from, from_tone) - @writer = Writer.new(to, to_tone) + def camelize(str) + str = str.dup + str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase } + str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase } + str end - def convert(str) - @writer.unparse @reader.parse(str) + SYLLABLE_CACHE = Hash.new do |hsh, syll| + hsh[syll] = Ting.writer(:hanyu, :accents).( + Ting.reader(:hanyu, :numbers).(syll.downcase) + ) end - alias :<< :convert - end - - class <<self - READERS={} - WRITERS={} - def reader(format, tones) - return READERS[[format, tones]] ||= Reader.new(format,tones) + def pretty_tones(string) + string.gsub('u:','ü').gsub(/[A-Za-züÜ]{1,5}\d/) do |syll| + SYLLABLE_CACHE[syll] + end end - def writer(format, tones) - return WRITERS[[format, tones]] ||= Writer.new(format,tones) + + def bpmf(string) + string.gsub('u:','ü').scan(/[A-Za-züÜ]{1,5}\d/).map do |m| + Ting.writer(:zhuyin, :marks).( + Ting.reader(:hanyu, :numbers).(m.downcase) + ) + end.join(' ') end - end + end end - - -Pinyin = Ting #legacy support