<commit_before>require "metamorpher/refactorer/merger"
require "metamorpher/refactorer/replacement"
require "metamorpher/builder"
require "metamorpher/rewriting/rule"
require "metamorpher/drivers/ruby"

module Metamorpher
  module Refactorer
    def refactor(src, &block)
      replacements = reduce_to_replacements(driver.parse(src))
      Merger.new(src).merge(*replacements, &block)
    end

    def builder
      @builder ||= Builder.new
    end

    def driver
      @driver ||= Metamorpher::Drivers::Ruby.new
    end

    private

    def reduce_to_replacements(literal)
      [].tap do |replacements|
        rule.reduce(literal) do |original, rewritten|
          position = driver.source_location_for(original)
          new_code = driver.unparse(rewritten)
          replacements << Replacement.new(position, new_code)
        end<commit_msg>Refactor refactorer.<commit_after>require "metamorpher/refactorer/merger"
require "metamorpher/refactorer/replacement"
require "metamorpher/builder"
require "metamorpher/rewriting/rule"
require "metamorpher/drivers/ruby"

module Metamorpher
  module Refactorer
    def refactor(src, &block)
      literal = driver.parse(src)
      replacements = reduce_to_replacements(literal)
      Merger.new(src).merge(*replacements, &block)
    end

    def builder
      @builder ||= Builder.new
    end

    def driver
      @driver ||= Metamorpher::Drivers::Ruby.new
    end

    private

    def reduce_to_replacements(literal)
      [].tap do |replacements|
        rule.reduce(literal) do |original, rewritten|
          position = driver.source_location_for(original)
          new_code = driver.unparse(rewritten)
          replacements << Replacement.new(position, new_code)
        end
{'fraction_non_alphanumeric': 0.09567723342939481, 'fraction_numerical': 0.0, 'mean_word_length': 3.4512820512820515, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2619794', 'n_tokens_mistral': 571, 'n_tokens_neox': 546, 'n_words': 102}