<commit_before>require "rmb_chinese_yuan/version"

begin
  require "pry"
rescue LoadError
end

module RMB
  NUMBERS       = %w(零 壹 贰 叁 肆 伍 陆 柒 捌 玖)
  META_UNIT     = %w(仟 佰 拾)
  PART_UNIT     = %w(亿 万)
  SECTION_UNIT  = %w(元 分)

  class << self
    def convert(money)
      number  = preprocess(money)
      parts   = split(number)
      words   = parts.map {|part| meta_convert(part) }
      join(parts, words)
    end

    # Public: Convert number under 9999
    #
    # number - String
    def meta_convert(number)
      return NUMBERS[0] if number.zero?

      res = ''

      thousand  = number / 1000
      hundred   = number / 100 % 10
      ten       = number / 10 % 10
      one       = number % 10

      # thousand
      res << [ NUMBERS[thousand], META_UNIT[0] ].join unless thousand.zero?

      # hundred
      if hundred.zero?
        if thousand.nonzero? && [ten, one].any?(&:nonzero?)
          res << NUMBERS[0]
        end
      else
        res << [ NUMBERS[hundred], META_UNIT[1] ].join
      end

      # ten
      if ten.zero?
        if hundred.nonzero? && one.nonzero?
          res << NUMBERS[0]
        end
      else
        res << [ NUMBERS[ten], META_UNIT[2] ].join
      end

      # one
      res << [ NUMBERS[one], META_UNIT[3] ].join unless one.zero?

      res
    end

    private

      # Private: Process for type conversion, and `,` substitution.
      #
      # money - String or positive Numberic, length should be less or equal than 12.
      def preprocess(money)
        money = money.to_s.gsub(',', '')

        raise ArgumentError, "Not a number<#{money}>" \
          unless money.match(/^\d+$/)

        raise ArgumentError, "Negative number<#{money}> passed" \
          if money.to_f < 0

        raise ArgumentError, "Length of money<#{money}> is longer than 12" \
          if money.to_s.length > 12

        money.to_i
      end

      # Split money into three parts, each part is under 9999
      def split(number)<commit_msg>Refact meta_convert<commit_after>require "rmb_chinese_yuan/version"

begin
  require "pry"
rescue LoadError
end

module RMB
  NUMBERS       = %w(零 壹 贰 叁 肆 伍 陆 柒 捌 玖)
  META_UNIT     = %w(仟 佰 拾)
  PART_UNIT     = %w(亿 万)
  SECTION_UNIT  = %w(元 分)

  class << self
    def convert(money)
      number  = preprocess(money)
      parts   = split(number)
      words   = parts.map {|part| meta_convert(part) }
      join(parts, words)
    end

    # Public: Convert number under 9999
    #
    # number - String
    def meta_convert(number)
      return NUMBERS[0] if number.zero?

      numbers = number.to_s.chars.map(&:to_i)
      numbers.unshift 0 while numbers.count < 4

      res = ''
      numbers.each_with_index do |n, i|
        if n.nonzero?
          res << [ NUMBERS[n], META_UNIT[i] ].join
        elsif !res.empty? && i+1 < 4 && numbers[i+1].nonzero?
          res << NUMBERS[0]
        end
      end

      res
    end

    private

      # Private: Process for type conversion, and `,` substitution.
      #
      # money - String or positive Numberic, length should be less or equal than 12.
      def preprocess(money)
        money = money.to_s.gsub(',', '')

        raise ArgumentError, "Not a number<#{money}>" \
          unless money.match(/^\d+$/)

        raise ArgumentError, "Negative number<#{money}> passed" \
          if money.to_f < 0

        raise ArgumentError, "Length of money<#{money}> is longer than 12" \
          if money.to_s.length > 12

        money.to_i
      end

      # Split money into three parts, each part is under 9999
      def split(number)
{'fraction_non_alphanumeric': 0.11048951048951049, 'fraction_numerical': 0.016503496503496504, 'mean_word_length': 1.952931461601982, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '320377', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1223, 'n_words': 341}