module KanNum

Constants

N_ary
N_ary_loose
N_keys
N_keys_loose
RE_NONNUM
RE_NUM
RE_NUM_O

Public Class Methods

convert_helper( num ) click to toggle source
# File lib/kannum.rb, line 239
def self.convert_helper( num )

  tmp = nil
  N_単位数.each_pair do |k,v|
    # $stderr.puts "#{__method__} #{k} #{v}"
    if num >= v
      tmp ||= k
      tmp = k if not(tmp.nil?) and v > N_単位数[tmp]
      # $stderr.puts "#{__method__} tmp: #{tmp}"
      next
    else
      break
    end
  end

  tmp
end
convert_phase0( num ) click to toggle source
# File lib/kannum.rb, line 257
def self.convert_phase0( num )
  ret = []

  tmp_num = num
  while tmp = convert_helper(tmp_num)

  u = N_単位数[tmp]
  r = tmp_num/u
  ret << [r.to_i, tmp]
  tmp_num -= r*u
  end

  ret << tmp_num if tmp_num != 0
  ret
end
convert_phase1( ary ) click to toggle source
# File lib/kannum.rb, line 273
def self.convert_phase1( ary )
  ary.flatten.join
end
num_to_str( num ) click to toggle source
# File lib/kannum.rb, line 277
def self.num_to_str( num )

  minus_f = false
  if num < 0
    num *= -1
    minus_f = true
  end

  tmp = convert_phase0(num)
  # $stderr.puts "{#{__method__}}result0: #{tmp}"
  ret = convert_phase1(tmp)
  # $stderr.puts "result1: #{ret}"

  if minus_f
    ret = "-"+ret
  end

  ret
end
numable?( str, dict = N_keys ) click to toggle source
# File lib/kannum.rb, line 78
def self.numable?( str, dict = N_keys )
  tmp_str = str.gsub(/[0-90-9]/,'')
  # tmp = dict.map{|e| e.keys}.inject(:+)
  tmp = dict
  #tmp += (0..9).to_a

  ret = true
  (0..(tmp_str.size-1)).each {|i|
    c = tmp_str[i]
    if tmp.include?(c)
      next
    else
      ret = false
      break
    end
  }
  ret
end
numable_loose?( str ) click to toggle source
# File lib/kannum.rb, line 96
def self.numable_loose?( str )
  numable?(str, N_keys_loose)
end
str_to_num( str ) click to toggle source
# File lib/kannum.rb, line 216
def self.str_to_num( str )

  minus_f = false
  if str =~ /^-/
    str = str.gsub(/^-/, '')
    minus_f = true
  end

  tmp = translate_phase0(str)
  # $stderr.puts "result0: #{tmp}"
  tmp = translate_phase1(tmp)
  # $stderr.puts "result1: #{tmp}"
  ret = translate_phase2(tmp)

  ret *= -1 if minus_f

  ret
end
translate_phase0( str ) click to toggle source

simple chr-to-chr translation: 0-9.

# File lib/kannum.rb, line 104
def self.translate_phase0( str )
  #str = str.tr("0-9", "0-9")

  tmp_str = str
  # str.split(//).each_with_index{|e,i|
  (0..(str.length-1)).each{|i|
    c = str[i]
    tmp = N_数[c]
    tmp_str[i] = tmp.to_s unless tmp.nil?
  }

  tmp_str
end
translate_phase1( str ) click to toggle source

create parse tree for str:

[ [[ num, Num, ... ], UnitNum], ... ]
# File lib/kannum.rb, line 125
def self.translate_phase1( str )
  ret = []
  tmp_ary = []
  units = N_単位数.keys

  # $stderr.puts "#{__method__} str: #{str}"
  # str.split(//).each do |c|
  (0..(str.length-1)).each do |i|
    c = str[i]

    if units.include?(c)
      tmp = tmp_ary.join
      # $stderr.puts "#{__method__} tmp: #{tmp}"
      #tmp = tmp.gsub(/([0-9]+)([^ 0-9])/, '\1 \2')
      tmp = tmp.gsub(/(#{RE_NUM})(#{RE_NONNUM})/, '\1 \2')
      # $stderr.puts "#{__method__} tmp: #{tmp}"
      #tmp = tmp.gsub(/([^ 0-9])([0-9]+)/, '\1 \2')
      tmp = tmp.gsub(/(#{RE_NONNUM})(#{RE_NUM})/, '\1 \2')

      #tmp = tmp.gsub(/([^ 0-9])([^ 0-9])/, '\1 \2')
      tmp = tmp.gsub(/(#{RE_NONNUM})(#{RE_NONNUM})/, '\1 \2')
      # $stderr.puts "#{__method__} tmp: #{tmp}"
      tmp = tmp.split(/\s+/)
      # $stderr.puts "#{__method__} tmp: #{tmp}"
      ret << [tmp, c]
      tmp_ary = []
      next
    end
    tmp_ary << c
    # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
  end
  ret << tmp_ary if tmp_ary.size > 0

  ret
end
translate_phase2( ary ) click to toggle source

calculate number due to parse tree.

Args

ary

parse tree with _phase1

Return

number

# File lib/kannum.rb, line 166
def self.translate_phase2( ary )
  ret = 0
  # $stderr.puts "{#{__method__}} ary: #{ary}"

  #
  ary.each do |ua|
    na, u = ua
    if na.class != Array
      na = ua
      u = '1'
    end

    tmp = 0
    tmp_ary = []

    na.each do |e|
      tmp_ary << e
      if e =~ /[0-9]+/
      else
        # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
        a = 1
        b = N_倍数[tmp_ary[-1]]
        #a = tmp_ary[0].to_i if tmp_ary.size > 1
        a = tmp_ary[0].to_f if tmp_ary.size > 1

        tmp += a*b
        tmp_ary = []
      end
    end
    # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
    #tmp += tmp_ary[0].to_i if tmp_ary.size != 0
    tmp += tmp_ary[0].to_f if tmp_ary.size != 0

    tmp_u = N_単位数[u]
    if tmp_u.nil?
      ret += tmp * 1
    else
      ret += tmp * tmp_u
    end

  end

  if ret - ret.to_i > 0.0
  else
    ret = ret.to_i
  end

  ret
end