111 lines
4.3 KiB
JavaScript
111 lines
4.3 KiB
JavaScript
/*
|
|
file: letter.js
|
|
desc: simple script that generates a (skeleton) letter
|
|
based on Christopher Stratchey's Love Letter as
|
|
analysed by Noah Wardrip-Fruin in "Digital Media Archeology"
|
|
author: gauthier
|
|
date: 11/11/16
|
|
*/
|
|
|
|
// import chance (http://chancejs.com)
|
|
var chance = require('chance').Chance(); // npm install --save chance
|
|
|
|
// import word-wrap (https://www.npmjs.com/package/word-wrap)
|
|
var wrap = require('word-wrap'); // npm install --save word-wrap
|
|
|
|
// Stratchey's Love letter vocabulary database
|
|
// based on Noah Wardrip-Fruin's "Digital Media Archeology" Table 14.1
|
|
const first = ['DARLING', 'DEAR', 'HONEY', 'JEWEL'];
|
|
|
|
const second = ['DUCK', 'LOVE', 'MOPPET', 'SWEETHEART'];
|
|
|
|
const adjectives = ['ADORABLE', 'AFFECTIONATE', 'AMOROUS', 'ANXIOUS', 'ARDENT', 'AVID', 'BREATHLESS', 'BURNING', 'COVETOUS', 'CRAVING', 'CURIOUS', 'DARLING', 'DEAR', 'DEVOTED', 'EAGER', 'EROTIC', 'FERVENT', 'FOND', 'IMPATIENT', 'KEEN', 'LITTLE', 'LOVEABLE', 'LOVESICK', 'LOVING', 'PASSIONATE', 'PRECIOUS', 'SWEET', 'SYMPATHETIC', 'TENDER', 'UNSATISFIED', 'WISTFUL'];
|
|
|
|
const nouns = ['ADORATION', 'AFFECTION', 'AMBITION', 'APPETITE', 'ARDOUR', 'CHARM', 'DESIRE', 'DEVOTION', 'EAGERNESS', 'ENCHANTMENT', 'ENTHUSIASM', 'FANCY', 'FELLOW FEELING', 'FERVOUR', 'FONDNESS', 'HEART', 'HUNGER', 'INFATUATION', 'LIKING', 'LONGING', 'LOVE', 'LUST', 'PASSION', 'RAPTURE', 'SYMPATHY', 'TENDERNESS', 'THIRST', 'WISH', 'YEARNING'];
|
|
|
|
const adverbs = ['AFFECTIONATELY', 'ANXIOUSLY', 'ARDENTLY', 'AVIDLY', 'BEAUTIFULLY', 'BREATHLESSLY', 'BURNINGLY', 'COVETOUSLY', 'CURIOUSLY', 'DEVOTEDLY', 'EAGERLY', 'FERVENTLY', 'FONDLY', 'IMPATIENTLY', 'KEENLY', 'LOVINGLY', 'PASSIONATELY', 'SEDUCTIVELY', 'TENDERLY', 'WINNINGLY', 'WISTFULLY'];
|
|
|
|
const verbs = ['ADORES', 'ATTRACTS', 'CARES FOR', 'CHERISHES', 'CLINGS TO', 'DESIRES','HOLDS DEAR', 'HOPES FOR', 'HUNGERS FOR', 'IS WEDDED TO', 'LIKES', 'LONGS FOR', 'LOVES', 'LUSTS AFTER', 'PANTS FOR', 'PINES FOR', 'PRIZES', 'SIGHS FOR', 'TEMPTS', 'THIRSTS FOR', 'TREASURES', 'WANTS', 'WISHES', 'WOOS', 'YEARNS FOR'];
|
|
|
|
|
|
/**
|
|
* Picks a random element from an array
|
|
* @param {Array} array
|
|
* @return {Object} choice
|
|
*/
|
|
function choice(array) {
|
|
var index = chance.natural({'min': 0, 'max': array.length - 1}); // **** NOTE: 'max': array.length - 1
|
|
return array[index];
|
|
}
|
|
|
|
/**
|
|
* Randomly picks or not a random element from an array
|
|
* @param {Array} array
|
|
* @return {Object} choice
|
|
* @return {String} empty string
|
|
*/
|
|
function maybe(array) {
|
|
if( chance.bool() ) {
|
|
return choice(array);
|
|
} else {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generates a short phrase consisting of a randomly chosen
|
|
* adjective and noun
|
|
* @return {String} phrase
|
|
*/
|
|
function short() {
|
|
return choice(adjectives) + ' ' + choice(nouns);
|
|
}
|
|
|
|
/**
|
|
* Generates a sentence composed of randomly chosen
|
|
* adjective, noun, adverb, verb, adjective and noun
|
|
* @return {String} sentence
|
|
*/
|
|
function long() {
|
|
return 'MY ' + maybe(adjectives) + ' ' + choice(nouns) + ' ' +
|
|
maybe(adverbs) + ' ' + choice(verbs) + ' ' +
|
|
'YOUR ' + maybe(adjectives) + ' ' + choice(nouns) + '. ';
|
|
}
|
|
|
|
// format the output (header)
|
|
console.log('\n\n\n\n'); // 4 x newline ('\n')
|
|
|
|
|
|
/*
|
|
Group 02: During the class I did not have time to add the word-wrapping
|
|
to the example we coded in class. It is here used to format the output of the script.
|
|
|
|
What is different from what you may have, is that I construct a single string called 'text'
|
|
which holds all generated sentences in the loop below. Thus, in the loop, rather than
|
|
outputting the sentence (console.log(long())), I simply concatenate the new sentence with
|
|
'text' (text += long()), which, in turn, holds all 5 sentences. The I use word-wrap to
|
|
format the 'text' depending on the width (in characters) I want the resulting letter to
|
|
be (here 65 characters).
|
|
|
|
for more information https://www.npmjs.com/package/word-wrap
|
|
*/
|
|
|
|
|
|
// in order to use word-wrap, I need to construct a single string
|
|
// (text) containing all generated sentences
|
|
var text = '';
|
|
|
|
// loop generating 5 sentences
|
|
for(var i = 0; i < 5; i++) {
|
|
|
|
// concatenate the new sentence generated by long() with the text string
|
|
text += long(); // (+=) text = text + long()
|
|
}
|
|
|
|
// wrap the text with a width of 65 characters and log (output) it to the console
|
|
console.log(wrap(text, {'width': 65}));
|
|
|
|
// format for the output (footer)
|
|
console.log('\n\n\n\n'); // 4 x newline ('\n')
|
|
|