<commit_before>/*
 * Copyright 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var fs = require('fs');
var path = require('path');
var parse = require('./lib/parser.js');
var assertIsString = require('./lib/util.js').assertIsString;

/**
 * Hyde's predefined constants in a Context
 *
 * @typedef {object} HydeContext
 * @property {string} targetroot
 * @property {string} sourceroot
 * @property {string} currentfile
 * @property {Date} time
 */

/**
 * The context for Hyde processing
 *
 * @typedef {object} ParseContext
 * @property {HydeContext} _hyde
 */

/** @type ParseContext */
var parseContext = {
  _hyde: {
    targetroot: '',
    sourceroot: '',
    currentfile: '',
    time: new Date(),
  },
};

/**
 * The walk function's callback
 *
 * @callback walk~callback
 * @param {Error} err error object
 * @param {string[]} results the paths of all files that were found
 */

/**
 * Walk a directory and its subdirectories, and call a method on each of the
 * files within
 * <p>
 * Found at http://stackoverflow.com/revisions/5827895/6
 *
 * @param {string} dir the directory to walk
 * @param {walk~callback} done The function to call on each file
 */
var walk = function(dir, done) {
  var results = [];
  fs.readdir(dir, function(err, list) {
    if (err) { return done(err); }
    var pending = list.length;
    if (!pending) { return done(null, results); }
    list.forEach(function(file) {
      file = path.resolve(dir, file);
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          walk(file, function(err, res) {
            results = results.concat(res);
            if (!--pending) { done(null, results); }
          });
        } else {
          results.push(file);
          if (!--pending) { done(null, results); }
        }
      });
    });
  });
};

/**
 * Deletes a path and all its contents.
 * <p>
 * Found at http://stackoverflow.com/revisions/12761924/4
 *
 * @param {string} dir the directory to be deleted
 */
var deleteDirectoryRecursive = function(dir) {
  var files = [];
  if (fs.existsSync(dir)) {
    files = fs.readdirSync(dir);
    files.forEach(function(file) {
      var curPath = path + '/' + file;
      if (fs.statSync(curPath).isDirectory()) { // Recurse
        deleteDirectoryRecursive(curPath);
      } else { // Delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(dir);
  }
};<commit_msg>using path.sep instead of slash literal<commit_after>/*
 * Copyright 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var fs = require('fs');
var path = require('path');
var parse = require('./lib/parser.js');
var assertIsString = require('./lib/util.js').assertIsString;

/**
 * Hyde's predefined constants in a Context
 *
 * @typedef {object} HydeContext
 * @property {string} targetroot
 * @property {string} sourceroot
 * @property {string} currentfile
 * @property {Date} time
 */

/**
 * The context for Hyde processing
 *
 * @typedef {object} ParseContext
 * @property {HydeContext} _hyde
 */

/** @type ParseContext */
var parseContext = {
  _hyde: {
    targetroot: '',
    sourceroot: '',
    currentfile: '',
    time: new Date(),
  },
};

/**
 * The walk function's callback
 *
 * @callback walk~callback
 * @param {Error} err error object
 * @param {string[]} results the paths of all files that were found
 */

/**
 * Walk a directory and its subdirectories, and call a method on each of the
 * files within
 * <p>
 * Found at http://stackoverflow.com/revisions/5827895/6
 *
 * @param {string} dir the directory to walk
 * @param {walk~callback} done The function to call on each file
 */
var walk = function(dir, done) {
  var results = [];
  fs.readdir(dir, function(err, list) {
    if (err) { return done(err); }
    var pending = list.length;
    if (!pending) { return done(null, results); }
    list.forEach(function(file) {
      file = path.resolve(dir, file);
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          walk(file, function(err, res) {
            results = results.concat(res);
            if (!--pending) { done(null, results); }
          });
        } else {
          results.push(file);
          if (!--pending) { done(null, results); }
        }
      });
    });
  });
};

/**
 * Deletes a path and all its contents.
 * <p>
 * Found at http://stackoverflow.com/revisions/12761924/4
 *
 * @param {string} dir the directory to be deleted
 */
var deleteDirectoryRecursive = function(dir) {
  var files = [];
  if (fs.existsSync(dir)) {
    files = fs.readdirSync(dir);
    files.forEach(function(file) {
      var curPath = dir + path.sep + file;
      if (fs.statSync(curPath).isDirectory()) { // Recurse
        deleteDirectoryRecursive(curPath);
      } else { // Delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(dir);
  }
};
{'fraction_non_alphanumeric': 0.13691890057685782, 'fraction_numerical': 0.00848320325755005, 'mean_word_length': 2.953722334004024, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3445229', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1752, 'n_words': 574}