<commit_before>var fs = require('fs');
var restclient = require('node-restclient');
var Twit = require('twit');
var express = require('express');
var deferred = require("deferred");
var app = express();
// Set the values of these before deploying.
var API_KEY, consumer_key, consumer_secret, access_token, access_token_secret;
var recent_retweets = [];
var $ = {
  Deferred : deferred
  , when : deferred
};



var lines;
try {
  lines = fs.readFileSync('templates.txt', 'utf-8');
  lines = lines.split('\n').map(function (line) {
    return line.length ? line : undefined;
  });
}
catch (err) {
  console.error("There was an error opening the file:");
  console.log(err);
}

/*
word types: "noun", "adjective", "verb", "adverb", "interjection", "pronoun", "preposition", "abbreviation", //"affix" no results, 
//"article" no results other than 'an' with a macron on the 'a' , // "auxiliary-verb" less useful, "conjunction", "definite-article", 
"family-name", "given-name", //"idiom", "imperative", "noun-plural", "noun-posessive", "past-participle", "phrasal-prefix", 
"proper-noun", "proper-noun-plural", "proper-noun-posessive", "suffix", "verb-intransitive", "verb-transitive"
*/
var blacklist = [];
try {
  var data = fs.readFileSync('badwords.txt', 'ascii');
  data.split('\n').forEach(function (line) {
    if(line.length>0) {
      blacklist.push(line.trim().toLowerCase());
    }
  });
}
catch (err) {
  console.error("There was an error opening the file:");
  console.log(err);
}

function isBlacklisted(data) {
  var result = false;
  for (var i=0;i<blacklist.length;i++) {
    if (data.trim().toLowerCase() === blacklist[i]) {
      result = true;
    }
  }
  return result;
}
var backref = function(n) {
  return function(value, index, listdfd) {
    return listdfd.then(function(pack) {
      var dfds = pack[0];
      var words = pack[1];
      var idxs = words.map(function(word, i) {
        return (/\{.*\}/).test(word) ? i : undefined;
      }).filter(function(i) {
        return i != null;
      });
      return dfds[idxs[n - 1]].then(function(word) {
        return words[idxs[n-1]].replace(/\{.*\}/, word);
      });
    });
  };
};

var gerund_func, number_func;
var processors = {
  "a" : function(value, index, listdfd) {
    return listdfd.then(function(pack) {
      var list = pack[0];
      return list[index + 1].then(function(word) {
        retval = /^[aeiou]/.test(word.trim()) ? "an" : "a";
        return retval;
      });
    });
  }
  , "noun-singular" : function(value, index, listdfd) {
    return defaultprocessor("noun", index, listdfd, "http://api.wordnik.com//v4/words.json/randomWords?" +
                  "hasDictionaryDef=true&includePartOfSpeech=noun&limit=1&" + 
                  "minCorpusCount=100&api_key=" + API_KEY + "&excludePartOfSpeech=noun-plural,proper-noun-plural");
  }
  , "n" : (number_func = function(max) {
    return function(value, index, listdfd) {
      return new $.Deferred().resolve(Math.floor(Math.random() * max) + 1);
    }
  })(1000)
  , "lc" : function(value, index, listdfd) {
    return listdfd.then(function(pack)  {
      var token = pack[1][index];
      var s = token.replace(/\{.*\}/, String.fromCharCode(65 + Math.floor(Math.random() * 26)));
      return new $.Deferred().resolve(s);
    });
  }
  , "vi-gerund" : (gerund_func = function(type) {
    return function(value, index, listdfd) {
      var d = $.Deferred();
      defaultprocessor(type, index, listdfd).then(function(word) {
        restclient.get(
          "http://api.wordnik.com/v4/word.json/" + word + "/relatedWords?"
          +"useCanonical=true&relationshipTypes=verb-form&limitPerRelationshipType=10&api_key="
          + API_KEY
          , function(vdata) {
            listdfd.then(function(pack) {
              var token = pack[1][index];
              var ws = (JSON.parse(vdata)[0] || {}).words;
              var w = ws && ws.filter(function(wd) {
                return (/ing$/).test(wd.word);
              })[0];
              w = w || (word + "ing");
              if(isBlacklisted(w)) {
                defaultprocessor(value, index, listdfd, getURL).then(function(ow) {
                  dfd.resolve(ow).done();
                });
              } else {
                d.resolve(token.replace(/\{.*\}/, w)).done();
              }
            });
          }
        );
      });
      return d.promise;
    };
  })("verb-intransitive")
  , "vt-gerund" : gerund_func("verb-transitive")
};
[1,2,3,4,5,6,7,8,9].forEach(function(i) {
  processors["$" + i] = backref(i);
});
[10,12,100,1000].forEach(function(i) {
  processors["n" + i] = number_func(i);
});

var defaultprocessor = function(value, index, listdfd, getURL) {
 getURL = getURL || "http://api.wordnik.com//v4/words.json/randomWords?" +
                  "hasDictionaryDef=true&includePartOfSpeech=" + value + "&limit=1&" + 
                  "minCorpusCount=100&api_key=" + API_KEY;
 var dfd = $.Deferred();
 restclient.get(
  getURL,
  function(vdata) {
    listdfd.then(function(pack) {
      var token = pack[1][index];
      var word = JSON.parse(vdata)[0].word;
      if(isBlacklisted(word)) {
        defaultprocessor(value, index, listdfd, getURL).then(function(w) {
          dfd.resolve(w).done();
        });
      } else {
        dfd.resolve(token.replace(/\{.*\}/, word)).done();
      }
    });
  });
 return dfd.promise;
};


// I deployed to Nodejitsu, which requires an application to respond to HTTP requests
// If you're running locally you don't need this, or express at all.
app.get('/', function(req, res){
    res.send("<h1>Recent retweets</h1>" + ((recent_retweets && recent_retweets.length) ? recent_retweets.join("<br>\n") : "No retweets"));
});
app.listen(
  process.env.OPENSHIFT_NODEJS_PORT || process.env.OPENSHIFT_INTERNAL_PORT || 8080,
  process.env.OPENSHIFT_NODEJS_IP ||
                         process.env.OPENSHIFT_INTERNAL_IP);

// insert your twitter app info here
var T = new Twit({
  consumer_key:     consumer_key, 
  consumer_secret:  consumer_secret,
  access_token:     access_token,
  access_token_secret: access_token_secret
});


//Step 1: List deferred resolves to the list of deferreds, one for each token.
//Step 2: wait on each token.
//Step 3: when all tokens have resolves (some being dependent on others, or REST calls), render.
function makeSnowclone() {
  var listdfd = new $.Deferred();
  var line = lines[Math.floor(Math.random() * 281)];<commit_msg>Logging and ignoring if express fails to init<commit_after>var fs = require('fs');
var restclient = require('node-restclient');
var Twit = require('twit');
var express = require('express');
var deferred = require("deferred");
var app = express();
// Set the values of these before deploying.
var API_KEY, consumer_key, consumer_secret, access_token, access_token_secret;
var recent_retweets = [];
var $ = {
  Deferred : deferred
  , when : deferred
};



var lines;
try {
  lines = fs.readFileSync('templates.txt', 'utf-8');
  lines = lines.split('\n').map(function (line) {
    return line.length ? line : undefined;
  });
}
catch (err) {
  console.error("There was an error opening the file:");
  console.log(err);
}

/*
word types: "noun", "adjective", "verb", "adverb", "interjection", "pronoun", "preposition", "abbreviation", //"affix" no results, 
//"article" no results other than 'an' with a macron on the 'a' , // "auxiliary-verb" less useful, "conjunction", "definite-article", 
"family-name", "given-name", //"idiom", "imperative", "noun-plural", "noun-posessive", "past-participle", "phrasal-prefix", 
"proper-noun", "proper-noun-plural", "proper-noun-posessive", "suffix", "verb-intransitive", "verb-transitive"
*/
var blacklist = [];
try {
  var data = fs.readFileSync('badwords.txt', 'ascii');
  data.split('\n').forEach(function (line) {
    if(line.length>0) {
      blacklist.push(line.trim().toLowerCase());
    }
  });
}
catch (err) {
  console.error("There was an error opening the file:");
  console.log(err);
}

function isBlacklisted(data) {
  var result = false;
  for (var i=0;i<blacklist.length;i++) {
    if (data.trim().toLowerCase() === blacklist[i]) {
      result = true;
    }
  }
  return result;
}
var backref = function(n) {
  return function(value, index, listdfd) {
    return listdfd.then(function(pack) {
      var dfds = pack[0];
      var words = pack[1];
      var idxs = words.map(function(word, i) {
        return (/\{.*\}/).test(word) ? i : undefined;
      }).filter(function(i) {
        return i != null;
      });
      return dfds[idxs[n - 1]].then(function(word) {
        return words[idxs[n-1]].replace(/\{.*\}/, word);
      });
    });
  };
};

var gerund_func, number_func;
var processors = {
  "a" : function(value, index, listdfd) {
    return listdfd.then(function(pack) {
      var list = pack[0];
      return list[index + 1].then(function(word) {
        retval = /^[aeiou]/.test(word.trim()) ? "an" : "a";
        return retval;
      });
    });
  }
  , "noun-singular" : function(value, index, listdfd) {
    return defaultprocessor("noun", index, listdfd, "http://api.wordnik.com//v4/words.json/randomWords?" +
                  "hasDictionaryDef=true&includePartOfSpeech=noun&limit=1&" + 
                  "minCorpusCount=100&api_key=" + API_KEY + "&excludePartOfSpeech=noun-plural,proper-noun-plural");
  }
  , "n" : (number_func = function(max) {
    return function(value, index, listdfd) {
      return new $.Deferred().resolve(Math.floor(Math.random() * max) + 1);
    }
  })(1000)
  , "lc" : function(value, index, listdfd) {
    return listdfd.then(function(pack)  {
      var token = pack[1][index];
      var s = token.replace(/\{.*\}/, String.fromCharCode(65 + Math.floor(Math.random() * 26)));
      return new $.Deferred().resolve(s);
    });
  }
  , "vi-gerund" : (gerund_func = function(type) {
    return function(value, index, listdfd) {
      var d = $.Deferred();
      defaultprocessor(type, index, listdfd).then(function(word) {
        restclient.get(
          "http://api.wordnik.com/v4/word.json/" + word + "/relatedWords?"
          +"useCanonical=true&relationshipTypes=verb-form&limitPerRelationshipType=10&api_key="
          + API_KEY
          , function(vdata) {
            listdfd.then(function(pack) {
              var token = pack[1][index];
              var ws = (JSON.parse(vdata)[0] || {}).words;
              var w = ws && ws.filter(function(wd) {
                return (/ing$/).test(wd.word);
              })[0];
              w = w || (word + "ing");
              if(isBlacklisted(w)) {
                defaultprocessor(value, index, listdfd, getURL).then(function(ow) {
                  dfd.resolve(ow).done();
                });
              } else {
                d.resolve(token.replace(/\{.*\}/, w)).done();
              }
            });
          }
        );
      });
      return d.promise;
    };
  })("verb-intransitive")
  , "vt-gerund" : gerund_func("verb-transitive")
};
[1,2,3,4,5,6,7,8,9].forEach(function(i) {
  processors["$" + i] = backref(i);
});
[10,12,100,1000].forEach(function(i) {
  processors["n" + i] = number_func(i);
});

var defaultprocessor = function(value, index, listdfd, getURL) {
 getURL = getURL || "http://api.wordnik.com//v4/words.json/randomWords?" +
                  "hasDictionaryDef=true&includePartOfSpeech=" + value + "&limit=1&" + 
                  "minCorpusCount=100&api_key=" + API_KEY;
 var dfd = $.Deferred();
 restclient.get(
  getURL,
  function(vdata) {
    listdfd.then(function(pack) {
      var token = pack[1][index];
      var word = JSON.parse(vdata)[0].word;
      if(isBlacklisted(word)) {
        defaultprocessor(value, index, listdfd, getURL).then(function(w) {
          dfd.resolve(w).done();
        });
      } else {
        dfd.resolve(token.replace(/\{.*\}/, word)).done();
      }
    });
  });
 return dfd.promise;
};


// I deployed to Nodejitsu, which requires an application to respond to HTTP requests
// If you're running locally you don't need this, or express at all.
app.get('/', function(req, res){
    res.send("<h1>Recent retweets</h1>" + ((recent_retweets && recent_retweets.length) ? recent_retweets.join("<br>\n") : "No retweets"));
});
try {
  app.listen(
    process.env.OPENSHIFT_NODEJS_PORT || process.env.OPENSHIFT_INTERNAL_PORT || 8080,
    process.env.OPENSHIFT_NODEJS_IP ||
                         process.env.OPENSHIFT_INTERNAL_IP);
} catch(e) {
  console.error(e);
  //continue app. just forget about serving web
}
// insert your twitter app info here
var T = new Twit({
  consumer_key:     consumer_key, 
  consumer_secret:  consumer_secret,
  access_token:     access_token,
  access_token_secret: access_token_secret
});


//Step 1: List deferred resolves to the list of deferreds, one for each token.
//Step 2: wait on each token.
//Step 3: when all tokens have resolves (some being dependent on others, or REST calls), render.
function makeSnowclone() {
  var listdfd = new $.Deferred();
  var line = lines[Math.floor(Math.random() * 281)];
{'fraction_non_alphanumeric': 0.17370928088506454, 'fraction_numerical': 0.010602335586969883, 'mean_word_length': 2.929067310594627, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '444898', 'n_tokens_mistral': 4542, 'n_tokens_neox': 4379, 'n_words': 906}