-
Notifications
You must be signed in to change notification settings - Fork 3
/
haiku.js
115 lines (96 loc) · 3.1 KB
/
haiku.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Generates haikus, just because.
* It can generate pure random haikus or others that are hopefully more logical/related
*
* --all_random uses all random unrelated words. Default behaviour.
* !not-implemented --random uses random start word, but haiku must relate to the random word
* !not-implemented --mandatory words that must appear in the haiku
* --theme words to create a haiku from, the final haiku could be "related" but not necessarily using the words
* !not-implemented --opposite haiku must be contrary or using antonyms for these words
* !not-implemented --smart use learned knowledge from previous liked haikus
* !not-implemented --sayit say it!
*/
var Stdio = require('stdio');
var RandomWord = require('random-words');
var Syllable = require('syllable');
var Request = require('rest-request');
var RestAPI = new Request('https://api.datamuse.com');
/**
* Gets options from command line
*/
var commands = Stdio.getopt({
'all_random': { key: 'a', description: 'Use all random unrelated words for the whole poem'},
'theme': { key: 't', description: 'words to create a haiku from, the result could be related to the theme but not use the exact word'}
});
var createRandomLine = function (syllableCount) {
if (syllableCount === void 0 || syllableCount !== parseInt(syllableCount, 10)) {
syllableCount = 5;
}
var line = '';
do {
line = RandomWord() + ' ' + RandomWord() + ' ' + RandomWord();
} while (Syllable(line) != syllableCount);
return line;
};
/**
* Create one line of haiku
*/
var createLine = function (syllableCount, wordPool) {
if (syllableCount === void 0 || syllableCount !== parseInt(syllableCount, 10)) {
syllableCount = 5;
}
var line = '';
var totalSyllableCount = 0;
do {
line += ' ' + wordPool[Math.floor(Math.random() * wordPool.length)].word;
totalSyllableCount = Syllable(line);
if (totalSyllableCount > syllableCount) {
line = '';
totalSyllableCount = 0;
}
} while (totalSyllableCount < syllableCount);
return line.trim();
};
var randomHaiku = function () {
var haiku = [
createRandomLine(5),
createRandomLine(7),
createRandomLine(5)
];
console.log(haiku);
};
var haiku = function (mainWords) {
/**
* Make the call to the API
*/
RestAPI.get('/words', {rel_jja: mainWords, max: 100})
.then(
function (words) {
// TODO: this can come back null, handle the error
var wordPool = words;
/**
*
*/
var haiku = [
createLine(5, wordPool),
createLine(7, wordPool),
createLine(5, wordPool)
];
console.log(haiku);
},
function () {
console.log('There\'s an error with something?');
}
);
};
/* Main command */
if (commands.all_random) {
randomHaiku();
} else {
// if there's no words requested, the haiku will be all random words
if (!commands.theme) {
randomHaiku();
} else {
haiku(commands.args);
}
}