aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2015-09-05 19:51:29 -0500
committerBen Beltran <ben@nsovocal.com>2015-09-05 19:51:29 -0500
commit1a652aac0ac1efc9100dd25e7da1105a4d515dc4 (patch)
treee1b4f113c08d498c7488d8427e333abeb99d32e1 /lib
parent71164942dfbc59af88765fce7339fc61c77bad7b (diff)
Super dumb mega commit
Diffstat (limited to 'lib')
-rw-r--r--lib/Conjugator.js66
-rw-r--r--lib/InsultGenerator.js173
2 files changed, 239 insertions, 0 deletions
diff --git a/lib/Conjugator.js b/lib/Conjugator.js
new file mode 100644
index 0000000..5afdf15
--- /dev/null
+++ b/lib/Conjugator.js
@@ -0,0 +1,66 @@
+'use strict';
+
+
+var Promise = require('bluebird');
+var cheerio = require('cheerio');
+require('neon');
+var request = require('request');
+
+
+var Conjugator = Class({}, "Conjugator")({
+ prototype : {
+ _baseUrl: "http://www.spanishdict.com/conjugate/",
+ _selector: ".aa .neoConj tr td",
+
+ init : function init(config) {
+ config = config || {};
+
+ Object.keys(config).forEach(function (property) {
+ this[property] = config[property];
+ }, this);
+ },
+
+ conjugate : function conjugate(verb) {
+ return new Promise(function (resolve, reject) {
+ request(this._baseUrl + verb, function (err, res, body) {
+ var $, result, finalVerb ;
+ if (err) {
+ return reject(err);
+ }
+ $ = cheerio.load(body);
+ result = $('.vtable-word')[10];
+
+ if (!result) {
+ console.log("Verb not found: ", verb);
+ return reject(new Error("Not a valid verb"));
+ }
+
+ finalVerb = this._extractWord(result);
+
+ console.log(verb, finalVerb);
+ resolve(finalVerb);
+ }.bind(this));
+ }.bind(this));
+ },
+
+ _extractWord : function _extractWord(node) {
+ var word = "", components;
+
+ node.children.forEach(function (child) {
+ if (child.type === "text") {
+ word += child.data;
+ } else {
+ word += this._extractWord(child);
+ }
+ }, this);
+
+ word = word.split(",")[0]; // multiple conjugations, take 1
+ components = word.split(" "); // some special cases have two words
+ // use the last, why not
+
+ return components[components.length - 1];
+ }
+ }
+});
+
+module.exports = Conjugator;
diff --git a/lib/InsultGenerator.js b/lib/InsultGenerator.js
new file mode 100644
index 0000000..e5ad293
--- /dev/null
+++ b/lib/InsultGenerator.js
@@ -0,0 +1,173 @@
+'use strict';
+
+
+var fs = require('fs');
+
+var Promise = require('bluebird');
+require('neon');
+
+var Conjugator = require('./Conjugator');
+
+var InsultGenerator = Class({}, "InsultGenerator")({
+ prototype : {
+ file : null,
+ verbs: null,
+ nouns: null,
+
+ _maxTries: 10,
+ _loaded: false,
+ _verbRe: /^[0-9]+\-v\s+?lemma\s+?([^ ]+)/,
+ _nounRe: /^[0-9]+\-n\s+?lemma\s+?([^ ]+)/,
+ _conjugator: null,
+
+ init : function init(config) {
+ config = config || {};
+
+ Object.keys(config).forEach(function (property) {
+ this[property] = config[property];
+ }, this);
+
+ this._conjugator = new Conjugator();
+ },
+
+ generate : function generate(config) {
+ var selectedVerb;
+ return this._load()
+ .then(function () {
+ return this._getVerb();
+ }.bind(this)).then(function (verb) {
+ return this._conjugateVerb(verb);
+ }.bind(this)).then(function (conjugatedVerb) {
+ selectedVerb = conjugatedVerb;
+ return this._getNoun();
+ }.bind(this)).then(function (noun) {
+ return this._generateInsult(selectedVerb, noun);
+ }.bind(this));
+ },
+
+ _load : function _load() {
+ return this._loadFile()
+ .then(function () {
+ return this._loadVerbs();
+ }.bind(this)).then(function () {
+ return this._loadNouns();
+ }.bind(this));
+ },
+
+ _loadFile : function _loadFile() {
+ return new Promise(function (resolve, reject) {
+ if (this._loaded) {
+ return resolve();
+ }
+
+ fs.readFile(this.file, {encoding: 'utf8'}, function (err, contents) {
+ if (err) {
+ return reject(err);
+ }
+
+ this._contents = contents;
+ this._loaded = true;
+ resolve();
+ }.bind(this));
+ }.bind(this));
+ },
+
+ _loadVerbs : function _loadVerbs() {
+ return new Promise(function (resolve, reject) {
+ if (this.verbs) {
+ return resolve();
+ }
+
+ this.verbs = [];
+ this._contents.split('\n').forEach(function (line) {
+ var matches;
+ matches = line.match(this._verbRe);
+
+ if (matches) {
+ if (this.verbs.indexOf(matches[1]) === -1) {
+ this.verbs.push(matches[1])
+ }
+ }
+ }, this);
+
+ resolve();
+ }.bind(this));
+ },
+
+ _loadNouns : function _loadNouns() {
+ return new Promise(function (resolve, reject) {
+ if (this.nouns) {
+ return resolve();
+ }
+
+ this.nouns = [];
+ this._contents.split('\n').forEach(function (line) {
+ var matches;
+ matches = line.match(this._nounRe);
+
+ if (matches) {
+ if (this.nouns.indexOf(matches[1]) === -1) {
+ this.nouns.push(matches[1])
+ }
+ }
+ }, this);
+
+ resolve();
+ }.bind(this));
+ },
+
+ _getVerb : function _getVerb() {
+ var index;
+ index = Math.floor(Math.random()*this.verbs.length);
+ return Promise.resolve(this.verbs[index]);
+ },
+
+ _conjugateVerb : function _conjugateVerb(verb) {
+ return new Promise(function (resolve, reject) {
+ var tries;
+
+ tries = 0;
+
+ var attemptConjugation = function attemptConjugation(verb, tries) {
+ if (tries > this._maxTries) {
+ return reject(new Error("Couldn't find a proper verb"));
+ };
+
+ this._conjugator.conjugate(verb)
+ .then(function (conjugatedVerb) {
+ resolve(conjugatedVerb);
+ })
+ .catch(function (err) {
+ this._getVerb().then(function (verb) {
+ attemptConjugation.bind(this)(verb, tries + 1);
+ }.bind(this))
+ }.bind(this));
+ };
+
+ setTimeout(attemptConjugation.bind(this, verb, 0), 0);
+ }.bind(this));
+ },
+
+ _getNoun : function _getNoun() {
+ var index;
+ index = Math.floor(Math.random()*this.nouns.length);
+ return Promise.resolve(this.nouns[index]);
+ },
+
+ _generateInsult : function _generateInsult(verb, noun) {
+ return Promise.resolve(verb + this._pluralize(noun));
+ },
+
+ // Super dumb pluralizer
+ _pluralize : function _pluralize(noun) {
+ console.log(noun);
+ if (["a","e,","i","o","u"].indexOf(noun[noun.length - 1]) >= 0) {
+ return noun + "s"
+ }
+
+ return noun + "es";
+ }
+ }
+});
+
+module.exports = InsultGenerator;