-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaiku.js
409 lines (390 loc) · 11.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* Three methods of haiku generation:
(1) Basic: Selecting random words from a dictionary
(2) Markov chain 1: Uses Markov chain to generate likely strings of words; train on any text
(3) Markov chain 2: Uses a backwards Markov chain to build the haiku backwards; train on other haikus
*/
var fs = require("fs");
var cmudictFile = readCmudictFile('./cmudict.txt');
//SHARED FUNCTIONS
function readCmudictFile(file){
return fs.readFileSync(file).toString();
}
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//FUNCTIONS FOR BASIC METHOD
/*Generates a random word structure fitting the syllabic demands
(e.g. [5,7,5] -> [[2,3],[3,3,1],[5]])
Selects random words of the proper syllable length to fill out structure*/
function haikuBasic(structure){
//Generate syllablesArr(data structure containing CMU dict parsed by syllables)
var syllablesArr=formatData(cmudictFile);
//Generate word structure (complexStructure)
var complexStructure=[];
for(var i=0;i<structure.length;i++){
var availableSylls=structure[i];
var lineStructure=[];
while(availableSylls>0){
var syllsInWord=getRandomIntInclusive(1,availableSylls);
while(syllablesArr[syllsInWord]==undefined){
syllsInWord=getRandomIntInclusive(1,availableSylls);
}
availableSylls-=syllsInWord;
lineStructure.push(syllsInWord);
}
complexStructure.push(lineStructure);
}
//Choose random words from syllableArr to fill out word structure
var poem="\n";
for(var i=0;i<complexStructure.length;i++){
for(var j=0;j<complexStructure[i].length;j++){
var numSylls=complexStructure[i][j];
var wordIndex=getRandomIntInclusive(0,syllablesArr[numSylls].length-1);
var word=syllablesArr[numSylls][wordIndex].replace(/[^a-zA-Z!']+/g, "");
poem=poem+word+" "
}
poem+="\n";
}
console.log(poem);
}
/*Parses the CMU dictionary; returns a syllable array; each index contains an array of words
from the CMU dict consisting of (index) syllables.*/
function formatData(data){
var syllablesArr=[];
var lines = data.toString().split("\n"),
lineSplit
lines.forEach(function(line){
lineSplit = line.split(" ");
var word=lineSplit[0];
var syllCount=0; //
if(lineSplit[1]!=undefined){
var phonemes=lineSplit[1].split(" ");
for(var i=0;i<phonemes.length;i++){
if(phonemes[i].match(/\d/)){
syllCount++;
}
}
if(syllablesArr[syllCount]==undefined){
syllablesArr[syllCount]=[word];
}
else{
syllablesArr[syllCount].push(word);
}
}
});
return syllablesArr;
}
//FUNCTIONS FOR MARKOV CHAIN 1
/*Parses a text into a frequency table
that maps words to the counts of words appearing after them;
builds chains of words, in the proper syllabic structure,
based on these frequencies*/
function haikuMarkov(structure,text){
var syllDict=formatData2(cmudictFile);
var text=processText(text);
var freqTable=generateFrequencyTable(text);
var keys=Object.keys(freqTable);
var wordsUsed={};
var str="";
for(var i=0;i<structure.length;i++){
var nextLine=getNextLine(structure[i],keys,freqTable,syllDict,wordsUsed);
var arr=nextLine.split(" ");
for(var j=0;j<arr.length;j++){
wordsUsed[arr[j]]=true;
}
str=str+nextLine+"\n";
}
console.log(str);
}
/*Parses the CMU dictionary; returns a dictionary mapping each word
to its syllable count*/
function formatData2(data){
var syllDict={};
var lines = data.toString().split("\n"),
lineSplit
lines.forEach(function(line){
lineSplit = line.split(" ");
if(lineSplit[1]!=undefined){
var phonemes=lineSplit[1].split(" ");
var word=lineSplit[0];
var syllCount=0; //
for(var i=0;i<phonemes.length;i++){
if(phonemes[i].match(/\d/)){
syllCount++;
}
}
}
syllDict[word]=syllCount;
});
return syllDict;
}
/*Transforms training text to uppercase and removes extraneous characters*/
function processText(file){
return fs.readFileSync(file).toString().replace(/[^a-zA-Z!' ]+/g, "").toUpperCase();
}
/*Generates a frequency table for a text, mapping each word to an object that
maps following words to their counts
E.g. "The dog walked, the dog sat. ->
{"the":{"dog":2},"dog":{"walked":1,"sat":1},"sat":{},"walked":{"the":1}}*/
function generateFrequencyTable(text){
freqTable={};
var arr=text.split(" ");
for(var i=0;i<(arr.length-1);i++){
if(freqTable[arr[i]]===undefined){
var nextWord=arr[i+1];
freqTable[arr[i]]={};
}
if(freqTable[arr[i]][arr[i+1]]===undefined){
freqTable[arr[i]][arr[i+1]]=1;
}
else{
freqTable[arr[i]][arr[i+1]]+=1;
}
}
return freqTable;
}
/*Generates chains of words, with probabilities based on the frequency table,
until the correct syllable account is achieved*/
function getNextLine(syllables,keys,freqTable,syllDict,wordsUsed){
while(true){
syllablesLeft=syllables;
str="";
var firstIndex=getRandomIntInclusive(0,keys.length-1)
var current=keys[firstIndex];
while(syllablesLeft>0){
var nextWord=selectNextWord(current,freqTable,wordsUsed,keys);
if(syllDict[nextWord]===undefined){
var nextIndex=getRandomIntInclusive(0,keys.length-1)
nextWord=keys[nextIndex];
}
var syllablesUsed=syllDict[nextWord];
str=str+nextWord+" "
syllablesLeft-=syllablesUsed;
current=nextWord;
if(syllablesLeft==0&&checkEnd(str)){
return str;
}
}
}
}
/*Used by both Markov chain approaches; uses the frequency table to generate probabilities
for a word to follow the current word;
selects a new word based on these probabilities*/
function selectNextWord(current,freqTable,wordsUsed,keys){
if(freqTable[current]===undefined){
console.log("DEBUG",current); //DO SOMETHING
} //DEBUGGING
var successors=Object.keys(freqTable[current]);
var probRanges=[];
var sum=0;
for(var i=0;i<successors.length;i++){
sum+=freqTable[current][successors[i]];
}
for(var i=0;i<successors.length;i++){
if(i==0){
probRanges.push(freqTable[current][successors[i]]/sum);
}
else{
probRanges.push(probRanges[i-1]+(freqTable[current][successors[i]]/sum));
}
}
var randNum=Math.random();
var upperLim=probRanges[0];
var counter=0;
while(randNum>upperLim){
if(randNum<probRanges[counter+1]){
counter++;
upperLim=probRanges[counter];
}
else{
break;
}
}
var nextWord=successors[counter];
if(wordsUsed[nextWord]==true){ //Helps avoid overusing words
if(Math.random()>.25){
if(successors.length!=1){
var newIndex=getRandomIntInclusive(0,successors.length-1)
return successors[newIndex];
}
}
}
return nextWord;
}
/*Used by both Markov chain methods to prevent prepositions etc. from being
the last word in a haiku*/
function checkEnd(str){
var arr=str.split(" ");
var forbidden=["THE","A","OF","ON","BY","FROM","FOR","AND","OR","YET","TO","MY","SHE","WITH","ITS","INTO","BUT","IS"];
for(var i=0;i<forbidden.length;i++){
if(arr[arr.length-2]==forbidden[i]){
//console.log("caught",str);
return false;
}
}
return true;
}
//MARKOV CHAIN 2
/*Designed specifically to train on other haikus;
uses word chains to build haiku backwards from
the last word*/
function haikuMarkovNew(structure,text){
var syllDict=formatData2(cmudictFile);
var text=processTextNew(text);
var info=generateFrequencyTableNew(text);
var lastWords=info[0];
var freqTable=info[1];
var keys=Object.keys(freqTable);
var wordsUsed={};
var str="";
var index=getRandomIntInclusive(0,lastWords.length-1);
var lastWord=lastWords[index];
var seed=null;
for(var i=structure.length-1;i>=0;i--){
var nextLine=getNextLineNew(structure[i],keys,freqTable,syllDict,wordsUsed,lastWord,seed);
var arr=nextLine.split(" ");
for(var j=0;j<arr.length;j++){
wordsUsed[arr[j]]=true;
}
str=nextLine+"\n"+str;
seed=nextLine.split(" ")[0];
if(seed==undefined){
seed=lastWords[getRandomIntInclusive(0,lastWords.length-1)];
}
}
console.log(str);
}
/*Returns a frequency table tracking mapping a word to the words that appear BEFORE it and their counts;
also returns a list of appropriate last words in the haiku from the training text*/
function generateFrequencyTableNew(text){
freqTable={};
var arr=text.split("\n\n");
var lastWords=[];
for(var i=0;i<(arr.length-1);i++){
//COLLECT LAST WORD
var haiku=arr[i].split(" ");
var lastWord=haiku[haiku.length-1];
var index=haiku.length-1;
//console.log(index);
var changed=false;
while(lastWord==""&&index>0){
lastWord=haiku[index];
index--;
changed=true;
}
lastWords.push(lastWord);
//GENERATE BACKWARDS FREQUENCY TABLE
var startIndex=index;
if(changed==true){
startIndex++;
}
var chainBack=[];
for(var j=startIndex;j>=0;j--){
var word=haiku[j];
if(word!=""){
var words=word.split('\n');
for(var k=words.length-1;k>=0;k--){
if(words[k]!=""){
chainBack.push(words[k]);
}
}
}
}
for(var j=0;j<chainBack.length;j++){
if(freqTable[chainBack[j]]===undefined){
freqTable[chainBack[j]]={};
}
if(freqTable[chainBack[j]][chainBack[j+1]]===undefined){
freqTable[chainBack[j]][chainBack[j+1]]=1;
}
else{
freqTable[chainBack[j]][chainBack[j+1]]+=1;
}
}
}
var toReturn=[lastWords,freqTable];
return toReturn;
}
/*Converts the text to uppercase and strips most characters*/
function processTextNew(file){
return fs.readFileSync(file).toString().replace(/[^a-zA-Z!' \n]+/g,"").toUpperCase();
}
/*Generates chains of words, with probabilities based on the frequency table,
until the correct syllable account is achieved*/
function getNextLineNew(syllables,keys,freqTable,syllDict,wordsUsed,lastWord,seed){
var calls=0;
while(true){
if(calls>50){ //Lazy way of escaping bugs - should neevr actually get called
return getNextLineNew(syllables,keys,freqTable,syllDict,wordsUsed,keys[getRandomIntInclusive(0,keys.length-1)],null);
}
calls++;
var current=lastWord;
if(seed!=null){
current=selectNextWord(seed,freqTable,wordsUsed,keys);
}
while((current==undefined)||(syllDict[current]==undefined)){
current=keys[getRandomIntInclusive(0,keys.length-1)];
}
var syllablesUsed=syllDict[current];
var syllablesLeft=syllables-syllablesUsed;
while(syllablesLeft<0){
current=keys[getRandomIntInclusive(0,keys.length-1)];
while(syllDict[current]==undefined){
current=keys[getRandomIntInclusive(0,keys.length-1)];
}
syllablesUsed=syllDict[current];
syllablesLeft=syllables-syllablesUsed;
}
var str=current;
if(syllablesLeft==0){
return str;
}
var attempts=0;
var attemptsAllowed=syllables*5;
while(syllablesLeft>0){
var nextWord=selectNextWord(current,freqTable,wordsUsed,keys);
while(syllDict[nextWord]==undefined){
nextWord=keys[getRandomIntInclusive(0,keys.length-1)];
}
if(attempts>=attemptsAllowed){
str="";
syllablesLeft=syllables;
nextWord=keys[getRandomIntInclusive(0,keys.length-1)];
while(syllDict[nextWord]==undefined){
nextWord=keys[getRandomIntInclusive(0,keys.length-1)];
}
attempts=0;
}
syllablesUsed=syllDict[nextWord];
str=nextWord+" "+str;
syllablesLeft-=syllablesUsed;
current=nextWord;
attempts++;
if(syllablesLeft==0&&checkEnd(str)){
return str;
}
if(syllablesLeft<=0){
str="";
syllablesLeft=syllables;
current=keys[getRandomIntInclusive(0,keys.length-1)];
while(syllDict[current]==undefined){
current=keys[getRandomIntInclusive(0,keys.length-1)];
}
attempts=0;
}
}
}
}
function createHaiku(structure){
console.log("\n[basic method]");
haikuBasic(structure);
console.log("[markov chain 1]\n");
haikuMarkov(structure,'./wasteland.txt');
console.log("[markov chain 2]\n");
for(var i=0;i<3;i++){
haikuMarkovNew(structure,'./basho.txt');
}
}
module.exports = {
createHaiku: createHaiku,
};