<commit_before>require "yaml"
require "csv"
require_relative "yaml_writer"

class RailsTranslationManager::Importer
  include YAMLWriter

  def initialize(locale, csv_path, import_directory)
    @csv_path = csv_path
    @locale = locale
    @import_directory = import_directory
  end

  def import
    csv = CSV.read(@csv_path, headers: true, header_converters: :downcase)
    data = {}
    csv.each do |row|
      key = row["key"]
      key_parts = key.split(".")
      if key_parts.length > 1
        leaf_node = (data[key_parts.first] ||= {})
        key_parts[1..-2].each do |part|
          leaf_node = (leaf_node[part] ||= {})
        end
        leaf_node[key_parts.last] = parse_translation(row["translation"])
      else
        data[key_parts.first] = parse_translation(row["translation"])
      end
    end

    write_yaml(import_yml_path, {@locale.to_s => data})
  end

  private

  def import_yml_path
    File.join(@import_directory, "#{@locale}.yml")
  end

  def parse_translation(translation)
    if translation =~ /^\[/
      values = translation.gsub(/^\[/, '').gsub(/\]$/, '').gsub("\"", '').split(/\s*,\s*/)<commit_msg>Use getters instead of instance variables<commit_after>require "yaml"
require "csv"
require_relative "yaml_writer"

class RailsTranslationManager::Importer
  include YAMLWriter

  attr_reader :locale, :csv_path, :import_directory

  def initialize(locale, csv_path, import_directory)
    @locale = locale
    @csv_path = csv_path
    @import_directory = import_directory
  end

  def import
    csv = CSV.read(csv_path, headers: true, header_converters: :downcase)
    data = {}
    csv.each do |row|
      key = row["key"]
      key_parts = key.split(".")
      if key_parts.length > 1
        leaf_node = (data[key_parts.first] ||= {})
        key_parts[1..-2].each do |part|
          leaf_node = (leaf_node[part] ||= {})
        end
        leaf_node[key_parts.last] = parse_translation(row["translation"])
      else
        data[key_parts.first] = parse_translation(row["translation"])
      end
    end

    write_yaml(import_yml_path, {locale.to_s => data})
  end

  private

  def import_yml_path
    File.join(import_directory, "#{locale}.yml")
  end

  def parse_translation(translation)
    if translation =~ /^\[/
      values = translation.gsub(/^\[/, '').gsub(/\]$/, '').gsub("\"", '').split(/\s*,\s*/)
{'fraction_non_alphanumeric': 0.14722104369961816, 'fraction_numerical': 0.002545608824777259, 'mean_word_length': 3.1441124780316345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '920465', 'n_tokens_mistral': 802, 'n_tokens_neox': 769, 'n_words': 156}