class Object

Constants

CONFIG_FILE_PATH

Public Instance Methods

read_number_sub(num, next_char) click to toggle source
# File lib/gimchi.rb, line 194
def read_number_sub num, next_char
  nconfig = @config[:number]

  if num == '0'
    return nconfig[:digits].first
  end

  num = num.gsub(',', '')
  next_char = next_char.to_s
  is_float = num.match(/[\.e]/) != nil

  # Alternative notation for integers with proper suffix
  alt = false
  if is_float == false &&
      nconfig[:alt_notation][:when_suffix].keys.include?(next_char.strip)
    max = nconfig[:alt_notation][:when_suffix][next_char.strip][:max]

    if max.nil? || num.to_i <= max
      alt = true
    end
  end

  # Sign
  sign = []
  negative = false
  if num =~ /^-/
    num = num.sub(/^-\s*/, '')
    sign << nconfig[:negative]
    negative = true
  elsif num =~ /^\+/
    num = num.sub(/^\+\s*/, '')
    sign << nconfig[:positive]
  end

  if is_float
    below = nconfig[:decimal_point]
    below = nconfig[:digits][0] + below if num.to_f < 1

    if md = num.match(/(.*)e(.*)/)
      dp = md[1].index('.')
      num = md[1].tr '.', ''
      exp = md[2].to_i

      dp += exp
      if dp > num.length
        num = num.ljust(dp, '0')
        num = num.sub(/^0+([1-9])/, "\\1")

        below = ""
      elsif dp < 0
        num = '0.' + '0' * (-dp) + num
      else
        num[dp, 1] = '.' + num[dp, 1]
      end
    end
    num.sub(/.*\./, '').each_char do | char |
      below += nconfig[:digits][char.to_i]
    end if num.include? '.'
    num = num.sub(/\..*/, '')
  else
    below = ""
  end

  tokens = []
  unit_idx = -1
  num = num.to_i
  while num > 0
    v = num % 10000

    unit_idx += 1
    if v > 0
      if alt == false || unit_idx >= 1
        str = ""
        # Cannot use hash as they're unordered in 1.8
        [[1000, '천'],
         [100,  '백'],
         [10,   '십']].each do | arr |
          u, sub_unit = arr
          str += (nconfig[:digits][v/u] if v/u != 1).to_s + sub_unit + ' ' if v / u > 0
          v %= u
        end
        str += nconfig[:digits][v] if v > 0

        raise RangeError, "number too large" unless nconfig[:units][unit_idx]
        tokens << str.sub(/ $/, '') + nconfig[:units][unit_idx]
      else
        str = ""
        tenfolds = nconfig[:alt_notation][:tenfolds]
        digits = nconfig[:alt_notation][:digits]
        alt_post_subs = nconfig[:alt_notation][:post_substitution]

        # Likewise.
        [[1000, '천'],
         [100,  '백']].each do |u, sub_unit|
          str += (nconfig[:digits][v/u] if v/u != 1).to_s + sub_unit + ' ' if v / u > 0
          v %= u
        end

        str += tenfolds[(v / 10) - 1] if v / 10 > 0
        v %= 10
        str += digits[v] if v > 0

        alt_post_subs.each do |p, s|
          str.gsub!(p, s)
        end if alt
        tokens << str.sub(/ $/, '') + nconfig[:units][unit_idx]
      end
    end
    num /= 10000
  end

  tokens += sign unless sign.empty?
  ret = tokens.reverse.join(' ') + below + next_char
  nconfig[:post_substitution].each do |p, s|
    ret.gsub!(p, s)
  end
  ret
end
setup() click to toggle source

@private

# File lib/gimchi.rb, line 314
def setup
  symbolize_keys = lambda do |val|
    case val
    when Hash
      {}.tap do |h|
        val.each do |k, v|
          k = k.gsub(' ', '_').to_sym if k =~ /[a-z0-9 ]/
          h[k] = symbolize_keys.call v
        end
      end
    when Array
      val.map { |v| symbolize_keys.call v }
    else
      val
    end
  end
  @config = symbolize_keys.call YAML.load(File.read CONFIG_FILE_PATH)

  [
    @config[:romanization][:post_substitution],
    @config[:number][:post_substitution],
    @config[:number][:alt_notation][:post_substitution]
  ].each do |r|
    r.keys.each do |k|
      r[Regexp.compile k.to_s] = r.delete k
    end
  end
  @config.freeze

  @pronouncer = Gimchi::Pronouncer.send :new, @config[:pronouncer], @config[:structure]

  @chosungs  = @config[:structure][:chosung]
  @jungsungs = @config[:structure][:jungsung]
  @jongsungs = @config[:structure][:jongsung]
  @chosung_set  = Set[*@chosungs]
  @jungsung_set = Set[*@jungsungs]
  @jongsung_set = Set[*@jongsungs]
  @all          = @chosung_set + @jungsung_set + @jongsung_set
end
str_length(str) click to toggle source
# File lib/gimchi.rb, line 190
def str_length str
  str.length
end