Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Created December 23, 2020 18:31
Show Gist options
  • Save SethVandebrooke/dc0e96c95567968cb6e0bc6b602bde9d to your computer and use it in GitHub Desktop.
Save SethVandebrooke/dc0e96c95567968cb6e0bc6b602bde9d to your computer and use it in GitHub Desktop.
Generate a writing prompt
function PromptGenerator() {
var self = this;
function pos(a) {
if (Array.isArray(a)) {
if (!('d' in pos)) pos.d = {};
for (var i = 0, p = null; i < a.length; i++) {
(pos.d[p] ? pos.d[p] : pos.d[p] = []).push(p = a[i]);
}
} else if ("number" == typeof a) {
for (var i = 0, p = null, o = []; i < a; i++) {
if (i > 0 && p == null) {
o.pop();
break;
}
o.push(p = pos.d[p][Math.floor(Math.random() * pos.d[p].length)]);
}
if (o[o.length - 1] == null) o.pop();
return o;
}
};
self.words = {
// Nouns
n: ['hacker','assasin','warrior','lieutenant','vilan','perp','monster','demi god'],
// Proper Nouns
N: ['Molly','Jim','Henry','Harry','John','Tim','Sally','Susan','Ted','James'],
// adjectives
a: ['blue','beautiful','green','lovely','red','terrible','fantastic','stupid','dumb','big','small'],
// verbs
v: ['died','killed','yelled','cried','spoke','laughed','collapsed','lost','hated','loved','shot','slashed','cut'],
// adverbs
av: ['quickly','sharply','awkwardly','quietly','slowly','terribly','perfectly'],
// articles
t: ['the','a'],
};
// n: noun, N: proper noun, v: verb, a: adjective
pos(['t', 'n',null]); // articles can precede nouns
pos(['t', 'n','v']); // verbs can follow nouns
pos(['N', 'v',null]); // verbs can also follow proper nouns
pos(['a', 'n',null]); // adjectives can precede nouns
pos(['t', 'a','n',null]); // articles can precede adjectives
pos(['n', 'v','av',null]); // verbs can be followed by adverbs
pos(['t', 'n', 'v']); // articles can precede nouns
pos(['t', 'n', 'v']); // articles can precede nouns
pos(['t', 'n', 'v']); // verbs can follow nouns
pos(['t', 'n', 'v']); // verbs can follow nouns
pos(['t', 'n', 'v', 'av', null]); // verbs can be followed by adverbs
pos(['N', 'v']); // verbs can also follow proper nouns
pos(['N', 'v']); // verbs can also follow proper nouns
self.randomWord = function (type) {
if (type in self.words) {
return self.words[type][Math.floor(Math.random() * self.words[type].length)];
} else {
throw new Error('That word type does not exist:', type);
}
};
self.generatePrompt = function(length) {
let sentence = pos(length);
for (let i = 0; i < sentence.length; i++) {
sentence[i] = self.randomWord(sentence[i]);
}
return sentence.join(' ');
}
}
// var pg = new PromptGenerator();
// pg.generatePrompt(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment