<commit_before>var importDir = process.argv[2];

if (!importDir) {
  console.log('No directory path given. Aborting!');
  process.exit();
}

var apiPath = '../../api/'

var ContainerModel = require(apiPath + 'container/container.model')

var fs = require('fs');
var _  = require('lodash');

var containers = {};

console.log("Reading files form "+ importDir);

function parseFiles() {
  fs.readdir(importDir, function(err, files) {
    files.forEach(function(file) {
      var fullPath = importDir + '/' + file;
      fs.readFile(fullPath, function(er, data) {
        var content = JSON.parse(data);
        var lang = file.slice(0, -5);
        parseFile(containers, 'withoutNamespace', content, lang);
      });
    });
  });
}

function parseFile(container, accessor, content, lang) {
  for (var key in content) {
    var val = content[key];
    if (typeof val === 'string') {
      var cont = getContainer(container, accessor);
      addValToContainer(cont, key, val, lang);
    }
  }
}

function Container(name) {
  this.name = name;
  this.containers = [];
  this.values = [];
}

function Value(name) {
  this.name = name;
  this.translations = [];
}

function Translation(translation, lang) {
  this.translation = translation;
  this.lang = lang;

}

function getContainer(parent, name) {
  var cont = parent[name];
  if (!cont) cont = parent[name] = new Container(name);
  return cont;
}

function addValToContainer(container, val, trsl, lang) {
  var values = container.values;
  var value = _.find(values, function(el) {
    return el.name === val;
  })

  if (!value) {
    value = new Value(val);
    values.push(value);<commit_msg>Refactor to enable recursion<commit_after>var importDir = process.argv[2];

if (!importDir) {
  console.log('No directory path given. Aborting!');
  process.exit();
}

var apiPath = '../../api/'

var ContainerModel = require(apiPath + 'container/container.model')

var fs = require('fs');
var _  = require('lodash');

var top = { containers: [] };

console.log("Reading files form "+ importDir);

function parseFiles() {
  fs.readdir(importDir, function(err, files) {
    files.forEach(function(file) {
      var fullPath = importDir + '/' + file;
      fs.readFile(fullPath, function(er, data) {
        var content = JSON.parse(data);
        var lang = file.slice(0, -5);
        parseContent(top, 'withoutNamespace', content, lang);
      });
    });
  });
}

function parseContent(container, accessor, content, lang) {
  for (var key in content) {
    var val = content[key];
    var cont;
    if (typeof val === 'string') {
      cont = getContainer(container, accessor);
      addValToContainer(cont, key, val, lang);
    }
  }
}

function Container(name) {
  this.name = name;
  this.containers = [];
  this.values = [];
}

function Value(name) {
  this.name = name;
  this.translations = [];
}

function Translation(translation, lang) {
  this.translation = translation;
  this.lang = lang;
}

function getContainer(parent, name) {
  var conts = parent.containers;
  var cont = _.find(conts, function(el) {
    return el.name === name;
  });
  if (!cont) {
    cont = new Container(name);
    conts.push(cont);
  }
  return cont;
}

function addValToContainer(container, val, trsl, lang) {
  var values = container.values;
  var value = _.find(values, function(el) {
    return el.name === val;
  })

  if (!value) {
    value = new Value(val);
    values.push(value);
{'fraction_non_alphanumeric': 0.15517241379310345, 'fraction_numerical': 0.0017533606078316774, 'mean_word_length': 3.382842509603073, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1544609', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1102, 'n_words': 273}