<commit_before>var generateCode = require('./lib/url');
var Q = require('q');

var dic = function(config){
  var code = generateCode(config);
  var timeout, default_mirror;

  if (config && config.timeout){
          if (typeof config.timeout != "number" || config.timeout < 0) throw new Error("timeout must be a positive integer");
          timeout = config.timeout;
  } else  { timeout = 200 }
  if (config && config.mirror){
    if (typeof config.mirror != "string") throw new Error("Default mirror input must be a string");
    if (!mapMirror(config.mirror)) throw new Error("Invalid mirror name");
    default_mirror = config.mirror
  } else { default_mirror = "usa" }


  return search;



  function search (text){
    var deferred = Q.defer();
    if(text === "") {throw new Error("Input string cannot be blank")}
    if(text === null || text === undefined) {throw "You must provide input that is not null or undefined"}
    if(typeof text != "string") {throw "Input must be a string"}
    /*request(encodeURI(code + text), function (error, response, body) {
      if (error)                            {deferred.reject(new Error(error));}
      else if (response.statusCode != 200)  {deferred.reject(new Error("status code: " + response.statusCode))}
      else                                  {deferred.resolve(body)}
    });*/
    return deferred.promise;
  }

  function mapMirror(key){
    var map = {
      "austrailia": "http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic?",
      "canada": "http://www.ottix.net/cgi-bin/wwwjdic/wwwjdic?",
      "germany": "http://wwwjdic.biz/cgi-bin/wwwjdic?",
      "japan": "http://gengo.com/wwwjdic/cgi-data/wwwjdic?",
      "usa": "http://www.edrdg.org/cgi-bin/wwwjdic/wwwjdic?",
      "sweden": "http://wwwjdic.se/cgi-bin/wwwjdic?"
    }
    return map[key]
  }




}



if (typeof module !== 'undefined') {
  module.exports = dic;
}<commit_msg>add random mirror switch on http timeout<commit_after>var http = require('http');
var generateCode = require('./lib/url');
var Q = require('q');


var dic = function(config){
  var code = generateCode(config);
  var timeout, default_mirror;

  if (config && config.timeout){
          if (typeof config.timeout != "number" || config.timeout < 0) throw new Error("timeout must be a positive integer");
          timeout = config.timeout;
  } else  { timeout = 500 }
  if (config && config.mirror){
    if (typeof config.mirror != "string") throw new Error("Default mirror input must be a string");
    if (!mapMirror(config.mirror)) throw new Error("Invalid mirror name");
    default_mirror = config.mirror
  } else { default_mirror = "usa" }


  return search;



  function search (text){
    var resolved = false;
    var counter = 0;
    var deferred = Q.defer();
    if(text === "") {throw new Error("Input string cannot be blank")}
    if(text === null || text === undefined) {throw "You must provide input that is not null or undefined"}
    if(typeof text != "string") {throw "Input must be a string"}
    
    //send first request
    var req = query(default_mirror);
    req.setTimeout(timeout, tryAnother);

    return deferred.promise;

    function query(mirror){
      return http.get(mapMirror(mirror) + code + text, function(res) {
        res.setEncoding('utf8');
        console.log("Got response: " + res.statusCode);
        resolved = true;
        var body = '';
        res.on('data', function(chunk){
          body += chunk;
        });
        res.on('end', function() {
          deferred.resolve(body);
        });
      }).on('error', function(e) {
        if (counter > 4) {deferred.reject(e)}
        console.log("Got error: " + e.message);
        counter++;
      });
    }
    function tryAnother(){
      if (!resolved){
        var arr = ['usa', 'japan', 'canada', 'germany', 'sweden', 'austrailia'];
        var num = Math.floor(Math.random()*6);
        console.log('trying mirror: ' + arr[num]);
        query(arr[num]);
      } else{
        console.log('already resolved--done!');
      }
    }

  }

  function mapMirror(key){
    var map = {
      "austrailia": "http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic?",
      "canada": "http://www.ottix.net/cgi-bin/wwwjdic/wwwjdic?",
      "germany": "http://wwwjdic.biz/cgi-bin/wwwjdic?",
      "japan": "http://gengo.com/wwwjdic/cgi-data/wwwjdic?",
      "usa": "http://www.edrdg.org/cgi-bin/wwwjdic/wwwjdic?",
      "sweden": "http://wwwjdic.se/cgi-bin/wwwjdic?"
    }
    return map[key]
  }




}



if (typeof module !== 'undefined') {
  module.exports = dic;
}
{'fraction_non_alphanumeric': 0.16403508771929826, 'fraction_numerical': 0.003289473684210526, 'mean_word_length': 2.7756622516556293, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 28, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5839277', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1433, 'n_words': 361}