<commit_before>require 'ruby-dmm'
require 'highline/import'
require 'fileutils'

module DMM
  class Renamer
    def initialize(config, filename)
      @client = DMM.new(
        api_id: config["api_id"],
        affiliate_id: config["affiliate_id"],
        result_only: true,
        encode_options: {:invalid => :replace, :undef => :replace, :replace => " "}
      )
      @filename = filename
    end

    def process(noop: false)
      fetch
      result = choose do |menu|
        menu.prompt = "Select candidates."
        @last_response.items.uniq {|i| i.title}.each do |item|
          menu.choice(rename_pattern(item))
        end
        menu.choice("No Rename") { nil }
      end

      if result
        rename(result, noop)
      end
    end

    private

    def rename(to, noop)
      dir = File.dirname(@filename)
      if File.exist?(@filename)
        FileUtils.mv(@filename, File.join(dir, to), {noop: noop, verbose: true})
      else
        warn "#{@filename} is not found"
      end
    end

    def fetch
      @last_response = @client.order("date").limit(10).item_list(normalize_filename)
      if @last_response.items.empty?
        fetch_retry
      end
    end

    def fetch_retry
      @last_response = @client.order("date").limit(10).item_list(more_normalize_filename)
    end

    def rename_pattern(item)
      "(AV) [#{item.info.maker[0]["name"]}] #{item.title} - #{item.info.actress.map {|act| act["name"]}.join(" ")}#{extname}"
    end

    def normalize_filename
      stripped = @filename
        .gsub(/^\(.*?\)\s*/, "")
        .gsub(/^\[.*?\]\s*/, "")
      convert_charset(File.basename(stripped, ".*"))
    end

    def more_normalize_filename
      filename = normalize_filename.split(" - ").first
      convert_charset(filename)
    end

    def extname<commit_msg>rename_patternを外出し<commit_after>require 'ruby-dmm'
require 'highline/import'
require 'fileutils'

module DMM
  class Renamer
    def initialize(config, filename)
      @config = config
      @client = DMM.new(
        api_id: config["api_id"],
        affiliate_id: config["affiliate_id"],
        result_only: true,
        encode_options: {:invalid => :replace, :undef => :replace, :replace => " "}
      )
      @filename = filename
    end

    def process(noop: false)
      fetch
      result = choose do |menu|
        menu.prompt = "Select candidates."
        @last_response.items.uniq {|i| i.title}.each do |item|
          menu.choice(rename_pattern(item))
        end
        menu.choice("No Rename") { nil }
      end

      if result
        rename(result, noop)
      end
    end

    private

    def rename(to, noop)
      dir = File.dirname(@filename)
      if File.exist?(@filename)
        FileUtils.mv(@filename, File.join(dir, to), {noop: noop, verbose: true})
      else
        warn "#{@filename} is not found"
      end
    end

    def fetch
      @last_response = @client.order("date").limit(10).item_list(normalize_filename)
      if @last_response.items.empty?
        fetch_retry
      end
    end

    def fetch_retry
      @last_response = @client.order("date").limit(10).item_list(more_normalize_filename)
    end

    def rename_pattern(item)
      raise "rename_pattern is not found" unless @config["rename_pattern"]
      eval("\"" + @config["rename_pattern"] + "\"") + extname
    end

    def normalize_filename
      stripped = @filename
        .gsub(/^\(.*?\)\s*/, "")
        .gsub(/^\[.*?\]\s*/, "")
      convert_charset(File.basename(stripped, ".*"))
    end

    def more_normalize_filename
      filename = normalize_filename.split(" - ").first
      convert_charset(filename)
    end

    def extname
{'fraction_non_alphanumeric': 0.13754443532950505, 'fraction_numerical': 0.002461033634126333, 'mean_word_length': 2.4250936329588013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3053721', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1163, 'n_words': 248}