-
Notifications
You must be signed in to change notification settings - Fork 1
/
technobabble.js
73 lines (54 loc) · 1.78 KB
/
technobabble.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var technobabble = (function(){
var dictionary = require('./dictionary.json');
/**
* Generate a specified number of sentences.
* @param {number} num - Number of sentences to generate
* @public
*/
function generateSentence(num) {
var sentenceResult = '',
sentencePattern = getRandom(dictionary.template);
// Loop through template and grab respective random word
for (var i = 0, max = sentencePattern.length; i < max; i ++) {
sentenceResult += getRandom(dictionary[sentencePattern[i]]);
if (i === max - 1) {
sentenceResult += '.';
}
}
return lintSentence(sentenceResult);
}
/**
* Generate a specified number of paragraphs.
* @param {number} num - Number of paragraphs to generate
* @public
*/
function generateParagraph(num) {
}
/**
* Randomly grab a word from a collection.
* @param {array} arr - Collection of words to randomly extract from
* @private
*/
function getRandom(arr) {
return arr[~~(Math.random()*arr.length)];
}
/**
* Take generated sentence and lint/fix grammatical issues
* @param {string} sentence - Sentence to be linted and fixed
* @returns {string}
* @private
*/
function lintSentence(sentence) {
return sentence.toLowerCase()
.replace(/a(?= [aeiouAEIOU])/g, 'an') //change appropriate a's to an's
.replace(/^./, function(char) { return char.toUpperCase() }); //capitalize the first letter of every sentence
}
/**
* Return public methods to technobabble
*/
return {
sentence: generateSentence,
paragraph: generateParagraph
};
})();
module.exports = technobabble;