]>
Commit | Line | Data |
---|---|---|
1a652aac BB |
1 | 'use strict'; |
2 | ||
3 | ||
4 | var fs = require('fs'); | |
5 | ||
6 | var Promise = require('bluebird'); | |
7 | require('neon'); | |
8 | ||
9 | var Conjugator = require('./Conjugator'); | |
10 | ||
11 | var InsultGenerator = Class({}, "InsultGenerator")({ | |
12 | prototype : { | |
13 | file : null, | |
14 | verbs: null, | |
15 | nouns: null, | |
16 | ||
17 | _maxTries: 10, | |
18 | _loaded: false, | |
19 | _verbRe: /^[0-9]+\-v\s+?lemma\s+?([^ ]+)/, | |
20 | _nounRe: /^[0-9]+\-n\s+?lemma\s+?([^ ]+)/, | |
21 | _conjugator: null, | |
22 | ||
23 | init : function init(config) { | |
24 | config = config || {}; | |
25 | ||
26 | Object.keys(config).forEach(function (property) { | |
27 | this[property] = config[property]; | |
28 | }, this); | |
29 | ||
30 | this._conjugator = new Conjugator(); | |
31 | }, | |
32 | ||
33 | generate : function generate(config) { | |
34 | var selectedVerb; | |
35 | return this._load() | |
36 | .then(function () { | |
37 | return this._getVerb(); | |
38 | }.bind(this)).then(function (verb) { | |
39 | return this._conjugateVerb(verb); | |
40 | }.bind(this)).then(function (conjugatedVerb) { | |
41 | selectedVerb = conjugatedVerb; | |
42 | return this._getNoun(); | |
43 | }.bind(this)).then(function (noun) { | |
44 | return this._generateInsult(selectedVerb, noun); | |
45 | }.bind(this)); | |
46 | }, | |
47 | ||
48 | _load : function _load() { | |
49 | return this._loadFile() | |
50 | .then(function () { | |
51 | return this._loadVerbs(); | |
52 | }.bind(this)).then(function () { | |
53 | return this._loadNouns(); | |
54 | }.bind(this)); | |
55 | }, | |
56 | ||
57 | _loadFile : function _loadFile() { | |
58 | return new Promise(function (resolve, reject) { | |
59 | if (this._loaded) { | |
60 | return resolve(); | |
61 | } | |
62 | ||
63 | fs.readFile(this.file, {encoding: 'utf8'}, function (err, contents) { | |
64 | if (err) { | |
65 | return reject(err); | |
66 | } | |
67 | ||
68 | this._contents = contents; | |
69 | this._loaded = true; | |
70 | resolve(); | |
71 | }.bind(this)); | |
72 | }.bind(this)); | |
73 | }, | |
74 | ||
75 | _loadVerbs : function _loadVerbs() { | |
76 | return new Promise(function (resolve, reject) { | |
77 | if (this.verbs) { | |
78 | return resolve(); | |
79 | } | |
80 | ||
81 | this.verbs = []; | |
82 | this._contents.split('\n').forEach(function (line) { | |
83 | var matches; | |
84 | matches = line.match(this._verbRe); | |
85 | ||
86 | if (matches) { | |
87 | if (this.verbs.indexOf(matches[1]) === -1) { | |
88 | this.verbs.push(matches[1]) | |
89 | } | |
90 | } | |
91 | }, this); | |
92 | ||
93 | resolve(); | |
94 | }.bind(this)); | |
95 | }, | |
96 | ||
97 | _loadNouns : function _loadNouns() { | |
98 | return new Promise(function (resolve, reject) { | |
99 | if (this.nouns) { | |
100 | return resolve(); | |
101 | } | |
102 | ||
103 | this.nouns = []; | |
104 | this._contents.split('\n').forEach(function (line) { | |
105 | var matches; | |
106 | matches = line.match(this._nounRe); | |
107 | ||
108 | if (matches) { | |
109 | if (this.nouns.indexOf(matches[1]) === -1) { | |
110 | this.nouns.push(matches[1]) | |
111 | } | |
112 | } | |
113 | }, this); | |
114 | ||
115 | resolve(); | |
116 | }.bind(this)); | |
117 | }, | |
118 | ||
119 | _getVerb : function _getVerb() { | |
120 | var index; | |
121 | index = Math.floor(Math.random()*this.verbs.length); | |
122 | return Promise.resolve(this.verbs[index]); | |
123 | }, | |
124 | ||
125 | _conjugateVerb : function _conjugateVerb(verb) { | |
126 | return new Promise(function (resolve, reject) { | |
127 | var tries; | |
128 | ||
129 | tries = 0; | |
130 | ||
131 | var attemptConjugation = function attemptConjugation(verb, tries) { | |
132 | if (tries > this._maxTries) { | |
133 | return reject(new Error("Couldn't find a proper verb")); | |
134 | }; | |
135 | ||
136 | this._conjugator.conjugate(verb) | |
137 | .then(function (conjugatedVerb) { | |
138 | resolve(conjugatedVerb); | |
139 | }) | |
140 | .catch(function (err) { | |
141 | this._getVerb().then(function (verb) { | |
142 | attemptConjugation.bind(this)(verb, tries + 1); | |
143 | }.bind(this)) | |
144 | }.bind(this)); | |
145 | }; | |
146 | ||
147 | setTimeout(attemptConjugation.bind(this, verb, 0), 0); | |
148 | }.bind(this)); | |
149 | }, | |
150 | ||
151 | _getNoun : function _getNoun() { | |
f66f0639 | 152 | var index, noun; |
1a652aac | 153 | index = Math.floor(Math.random()*this.nouns.length); |
f66f0639 | 154 | return Promise.resolve(this.nouns[index].toLowerCase()); |
1a652aac BB |
155 | }, |
156 | ||
157 | _generateInsult : function _generateInsult(verb, noun) { | |
158 | return Promise.resolve(verb + this._pluralize(noun)); | |
159 | }, | |
160 | ||
161 | // Super dumb pluralizer | |
162 | _pluralize : function _pluralize(noun) { | |
24e8faba BB |
163 | var lastLetter; |
164 | ||
165 | lastLetter = noun[noun.length - 1]; | |
166 | ||
167 | if (lastLetter === "s") { | |
168 | return noun; | |
169 | } | |
170 | ||
171 | if (["a","e","i","o","u"].indexOf(lastLetter) >= 0) { | |
1a652aac BB |
172 | return noun + "s" |
173 | } | |
174 | ||
175 | return noun + "es"; | |
176 | } | |
177 | } | |
178 | }); | |
179 | ||
180 | module.exports = InsultGenerator; |