<commit_before>require 'active_support/inflector/methods'

module Weighable
  class Weight
    attr_reader :value, :unit

    UNIT = {
      gram:      0,
      ounce:     1,
      pound:     2,
      milligram: 3,
      kilogram:  4,
      unit:      5
    }.freeze

    UNIT_ABBREVIATION = {
      gram:      'g',
      ounce:     'oz',
      pound:     'lb',
      milligram: 'mg',
      kilogram:  'kg',
      unit:      nil
    }.freeze

    ABBREVATION_ALIASES = {
      'g'  => :gram,
      'oz' => :ounce,
      'lb' => :pound,
      'mg' => :milligram,
      'kg' => :kilogram,
      'ea' => :unit,
      nil  => :unit
    }.freeze

    GRAMS_PER_OUNCE     = BigDecimal.new('28.34952')
    GRAMS_PER_POUND     = BigDecimal.new('453.59237')
    OUNCES_PER_POUND    = BigDecimal.new('16')
    MILLIGRAMS_PER_GRAM = BigDecimal.new('1000')
    KILOGRAMS_PER_GRAM  = BigDecimal.new('0.001')
    IDENTITY            = BigDecimal.new('1')

    MILLIGRAMS_PER_OUNCE    = GRAMS_PER_OUNCE * MILLIGRAMS_PER_GRAM
    KILOGRAMS_PER_OUNCE     = GRAMS_PER_OUNCE * KILOGRAMS_PER_GRAM
    MILLIGRAMS_PER_POUND    = GRAMS_PER_POUND * MILLIGRAMS_PER_GRAM
    KILOGRAMS_PER_POUND     = GRAMS_PER_POUND * KILOGRAMS_PER_GRAM
    KILOGRAMS_PER_MILLIGRAM = MILLIGRAMS_PER_GRAM**2

    CONVERSIONS = {
      UNIT[:unit] => {
        UNIT[:unit] => [:*, IDENTITY]
      },
      UNIT[:gram] => {
        UNIT[:gram]      => [:*, IDENTITY],
        UNIT[:ounce]     => [:/, GRAMS_PER_OUNCE],
        UNIT[:pound]     => [:/, GRAMS_PER_POUND],
        UNIT[:milligram] => [:*, MILLIGRAMS_PER_GRAM],
        UNIT[:kilogram]  => [:*, KILOGRAMS_PER_GRAM]
      },
      UNIT[:ounce] => {
        UNIT[:gram]      => [:*, GRAMS_PER_OUNCE],
        UNIT[:ounce]     => [:*, IDENTITY],
        UNIT[:pound]     => [:/, OUNCES_PER_POUND],
        UNIT[:milligram] => [:*, MILLIGRAMS_PER_OUNCE],
        UNIT[:kilogram]  => [:*, KILOGRAMS_PER_OUNCE]
      },
      UNIT[:pound] => {
        UNIT[:gram]      => [:*, GRAMS_PER_POUND],
        UNIT[:ounce]     => [:*, OUNCES_PER_POUND],
        UNIT[:pound]     => [:*, IDENTITY],
        UNIT[:milligram] => [:*, MILLIGRAMS_PER_POUND],
        UNIT[:kilogram]  => [:*, KILOGRAMS_PER_POUND]
      },
      UNIT[:milligram] => {
        UNIT[:gram]      => [:/, MILLIGRAMS_PER_GRAM],
        UNIT[:ounce]     => [:/, MILLIGRAMS_PER_OUNCE],
        UNIT[:pound]     => [:/, MILLIGRAMS_PER_POUND],
        UNIT[:milligram] => [:*, IDENTITY],
        UNIT[:kilogram]  => [:/, KILOGRAMS_PER_MILLIGRAM]
      },
      UNIT[:kilogram] => {
        UNIT[:gram]      => [:/, KILOGRAMS_PER_GRAM],
        UNIT[:ounce]     => [:/, KILOGRAMS_PER_OUNCE],
        UNIT[:pound]     => [:/, KILOGRAMS_PER_POUND],
        UNIT[:milligram] => [:*, KILOGRAMS_PER_MILLIGRAM],
        UNIT[:kilogram]  => [:*, IDENTITY]
      }
    }.freeze

    def self.parse(string)
      value, unit = string.split(' ')
      unit = ABBREVATION_ALIASES[unit]
      fail ArgumentError, 'invalid weight' if unit.nil?
      Weight.new(value, unit)
    end

    def initialize(value, unit)
      @value = value.to_d
      @unit  = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
    end

    def to(unit)
      new_unit = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
      operator, conversion = conversion(@unit, new_unit)
      new_value = @value.public_send(operator, conversion)
      Weight.new(new_value, unit)
    end

    UNIT.keys.each do |unit|
      unit = unit.to_s
      plural = ActiveSupport::Inflector.pluralize(unit)
      define_method "to_#{unit}" do
        to(unit)
      end
      alias_method "to_#{plural}", "to_#{unit}" unless unit == plural<commit_msg>Abbrevation<commit_after>require 'active_support/inflector/methods'

module Weighable
  class Weight
    attr_reader :value, :unit

    UNIT = {
      gram:      0,
      ounce:     1,
      pound:     2,
      milligram: 3,
      kilogram:  4,
      unit:      5
    }.freeze

    UNIT_ABBREVIATION = {
      gram:      'g',
      ounce:     'oz',
      pound:     'lb',
      milligram: 'mg',
      kilogram:  'kg',
      unit:      nil
    }.freeze

    ABBREVIATION_ALIASES = {
      'g'  => :gram,
      'oz' => :ounce,
      'lb' => :pound,
      'mg' => :milligram,
      'kg' => :kilogram,
      'ea' => :unit,
      nil  => :unit
    }.freeze

    GRAMS_PER_OUNCE     = BigDecimal.new('28.34952')
    GRAMS_PER_POUND     = BigDecimal.new('453.59237')
    OUNCES_PER_POUND    = BigDecimal.new('16')
    MILLIGRAMS_PER_GRAM = BigDecimal.new('1000')
    KILOGRAMS_PER_GRAM  = BigDecimal.new('0.001')
    IDENTITY            = BigDecimal.new('1')

    MILLIGRAMS_PER_OUNCE    = GRAMS_PER_OUNCE * MILLIGRAMS_PER_GRAM
    KILOGRAMS_PER_OUNCE     = GRAMS_PER_OUNCE * KILOGRAMS_PER_GRAM
    MILLIGRAMS_PER_POUND    = GRAMS_PER_POUND * MILLIGRAMS_PER_GRAM
    KILOGRAMS_PER_POUND     = GRAMS_PER_POUND * KILOGRAMS_PER_GRAM
    KILOGRAMS_PER_MILLIGRAM = MILLIGRAMS_PER_GRAM**2

    CONVERSIONS = {
      UNIT[:unit] => {
        UNIT[:unit] => [:*, IDENTITY]
      },
      UNIT[:gram] => {
        UNIT[:gram]      => [:*, IDENTITY],
        UNIT[:ounce]     => [:/, GRAMS_PER_OUNCE],
        UNIT[:pound]     => [:/, GRAMS_PER_POUND],
        UNIT[:milligram] => [:*, MILLIGRAMS_PER_GRAM],
        UNIT[:kilogram]  => [:*, KILOGRAMS_PER_GRAM]
      },
      UNIT[:ounce] => {
        UNIT[:gram]      => [:*, GRAMS_PER_OUNCE],
        UNIT[:ounce]     => [:*, IDENTITY],
        UNIT[:pound]     => [:/, OUNCES_PER_POUND],
        UNIT[:milligram] => [:*, MILLIGRAMS_PER_OUNCE],
        UNIT[:kilogram]  => [:*, KILOGRAMS_PER_OUNCE]
      },
      UNIT[:pound] => {
        UNIT[:gram]      => [:*, GRAMS_PER_POUND],
        UNIT[:ounce]     => [:*, OUNCES_PER_POUND],
        UNIT[:pound]     => [:*, IDENTITY],
        UNIT[:milligram] => [:*, MILLIGRAMS_PER_POUND],
        UNIT[:kilogram]  => [:*, KILOGRAMS_PER_POUND]
      },
      UNIT[:milligram] => {
        UNIT[:gram]      => [:/, MILLIGRAMS_PER_GRAM],
        UNIT[:ounce]     => [:/, MILLIGRAMS_PER_OUNCE],
        UNIT[:pound]     => [:/, MILLIGRAMS_PER_POUND],
        UNIT[:milligram] => [:*, IDENTITY],
        UNIT[:kilogram]  => [:/, KILOGRAMS_PER_MILLIGRAM]
      },
      UNIT[:kilogram] => {
        UNIT[:gram]      => [:/, KILOGRAMS_PER_GRAM],
        UNIT[:ounce]     => [:/, KILOGRAMS_PER_OUNCE],
        UNIT[:pound]     => [:/, KILOGRAMS_PER_POUND],
        UNIT[:milligram] => [:*, KILOGRAMS_PER_MILLIGRAM],
        UNIT[:kilogram]  => [:*, IDENTITY]
      }
    }.freeze

    def self.parse(string)
      value, unit = string.split(' ')
      unit = ABBREVIATION_ALIASES[unit]
      fail ArgumentError, 'invalid weight' if unit.nil?
      Weight.new(value, unit)
    end

    def initialize(value, unit)
      @value = value.to_d
      @unit  = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
    end

    def to(unit)
      new_unit = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
      operator, conversion = conversion(@unit, new_unit)
      new_value = @value.public_send(operator, conversion)
      Weight.new(new_value, unit)
    end

    UNIT.keys.each do |unit|
      unit = unit.to_s
      plural = ActiveSupport::Inflector.pluralize(unit)
      define_method "to_#{unit}" do
        to(unit)
      end
      alias_method "to_#{plural}", "to_#{unit}" unless unit == plural
{'fraction_non_alphanumeric': 0.15549525101763909, 'fraction_numerical': 0.008955223880597015, 'mean_word_length': 2.306864064602961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1645489', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2790, 'n_words': 397}