<commit_before>'use strict';

const buscacursos = require('buscacursos-uc');
const MySet = require("collections/set");
const async = require('async');

const util = require('./lib/util');
const generator = require('./lib/initials-generator');

function all(callback) {
  getValidTwoInitials((err, r2s) => {
    if (err) return callback(err);
    r2s = r2s.map(r => r.initials);
    getValidThreeInitials(r2s, (err, r3s) => {
      if (err) return callback(err);
      r3s = r3s.map(r => r.initials);
      deepSearch(r3s, callback);
    });
  });
}

function getValidTwoInitials(callback) {
  let twoInitials = generator(2);
  util.tryInitials(twoInitials, 5, callback);
}

function getValidThreeInitials(twoInitials, callback) {
  const three = new MySet();
  twoInitials.forEach(a => {
    twoInitials.forEach(b => {
      if (a[1] === b[0]) {
        three.add(`${a[0]}${a[1]}${b[1]}`);
      }
    });
  });

  util.tryInitials(three.toArray(), 5, callback);
}

function deepSearch(threeInitials, callback) {
  async.mapLimit(threeInitials, 5, recursiveHandler, function(err, results) {
    if (err) callback(err);
    else callback(null, util.flatten(results));
  });
}

function recursiveHandler(initials, callback) {
  const newInitials = [
    initials + '0',
    initials + '1',
    initials + '2',
    initials + '3'
  ];
  buscacursos.fetch(util.querizer(initials)).then(courses => {
    // console.log(temp, courses.length);
    if (courses.length >= 50) {
      async.mapLimit(newInitials, newInitials.length, recursiveHandler, function(err, r2) {
        callback(err, r2);
      });
    } else {
      callback(null, courses);
    }
  }).catch(err => callback(err));
}

exports.generator = generator;
exports.getValidTwoInitials = getValidTwoInitials;
exports.getValidThreeInitials = getValidThreeInitials;
exports.deepSearch = deepSearch;
exports.all = all;
exports.initials = require('./initials');
<commit_msg>add more cases to deep search<commit_after>'use strict';

const buscacursos = require('buscacursos-uc');
const MySet = require("collections/set");
const async = require('async');

const util = require('./lib/util');
const generator = require('./lib/initials-generator');

function all(callback) {
  getValidTwoInitials((err, r2s) => {
    if (err) return callback(err);
    r2s = r2s.map(r => r.initials);
    getValidThreeInitials(r2s, (err, r3s) => {
      if (err) return callback(err);
      r3s = r3s.map(r => r.initials);
      deepSearch(r3s, callback);
    });
  });
}

function getValidTwoInitials(callback) {
  let twoInitials = generator(2);
  util.tryInitials(twoInitials, 5, callback);
}

function getValidThreeInitials(twoInitials, callback) {
  const three = new MySet();
  twoInitials.forEach(a => {
    twoInitials.forEach(b => {
      if (a[1] === b[0]) {
        three.add(`${a[0]}${a[1]}${b[1]}`);
      }
    });
  });

  util.tryInitials(three.toArray(), 5, callback);
}

function deepSearch(threeInitials, callback) {
  async.mapLimit(threeInitials, 5, recursiveHandler, function(err, results) {
    if (err) callback(err);
    else callback(null, util.flatten(results));
  });
}

function recursiveHandler(initials, callback) {
  buscacursos.fetch(util.querizer(initials)).then(courses => {
    // console.log(initials, courses.length);
    if (courses.length >= 50) {
      const newInitials = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(n => initials + n);
      async.mapLimit(newInitials, newInitials.length, recursiveHandler, function(err, r2) {
        callback(err, r2);
      });
    } else {
      callback(null, courses);
    }
  }).catch(err => callback(err));
}

exports.generator = generator;
exports.getValidTwoInitials = getValidTwoInitials;
exports.getValidThreeInitials = getValidThreeInitials;
exports.deepSearch = deepSearch;
exports.all = all;
exports.initials = require('./initials');

{'fraction_non_alphanumeric': 0.16419849311509482, 'fraction_numerical': 0.014549233567160301, 'mean_word_length': 4.006501950585175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '1802646', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1310, 'n_words': 251}