Package Exports
- sentence-engine
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sentence-engine) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Sentence Engine
An easy-to-use sentence generator running on Node.js. It takes a template and vocabulary freely defined by the user.
Features
- Unbound vocabulary; define use case-specific categories or adhere to conventional keywords
- Object-oriented
- Lightweight
Usage
Sentence
const Sentence = require('sentence-engine');
const defaultSentence = Sentence(); // instantiates Sentence object
console.log(defaultSentence.get()); // outputs "hello, world"Calling Sentence() creates an object of the Sentence class which stores a template, vocabulary, and a set of options. In the case above we pass no arguments and the object is simply given some default values and resolves to "hello, world". See Sentence.js for the class implementation.
Templates, Vocabulary
By passing a template and/or vocabulary, the engine becomes much more powerful. See examples below.
const template = 'This is {a-adjective} template.'
const vocabulary = {
adjective: ['awesome', 'bad', 'great', 'repulsive']
};
console.log(Sentence(template, vocabulary).get()); // <-- might output "This is an awesome template"In the above example, we pass a single template. We can also pass several templates for the engine to choose between.
const templates = [
'This is {a-adjective} template.',
'This template is {adjective}.'
];
console.log(Sentence(templates, vocabulary).get());Options
There are also a set of options that may be used to configure how the sentence generation behaves.
Allow Duplicates
Sentence(template, vocabulary, { allowDuplicates: true }); Allow duplicate templates and vocabulary words/phrases to be stored, thereby increasing the chances of it being chosen randomly. In the future the sentence-engine will support weight templates and vocabularies.
Capitalize
Sentence(template, vocabulary, { capitalize: true }); Will capitalize inserted words if they appear at the start of a string, or after a full-stop.
Force New Sentence
Sentence(template, vocabulary, { forceNewSentence: true });Will force Sentence.generate() to refresh the stored sentence until it is different from the previous sentence, if possible.
Placeholder Notation
Sentence(template, vocabulary, { placeholderNotation: '{ }' });May be set to change the notation used to detect placeholders to be changed by the templating engine. Start { and end } are separated by a single space.
Preserve Placeholder Notation
Sentence(template, vocabulary, { preservePlaceholderNotation: true });If set to true, the output generated by Sentence.generate() will now keep the chosen placeholders around generated words/phrases.
Note that you may call Sentence.generate() to randomly recreate the sentence with the parameters given when initially instantiating the Sentence object. This way it's possible to easily maintain several sentences that are randomly generated and regenerated with different rules for each of them.
Defaults
Defaults for templates, vocabulary, and options are found here.
Development
Early stages and likely to see fundamental changes.
Contributing
Sure!
Background
The package is inspired by the TV show Better Off Ted's episode S2E8 "The Impertence of Communicationizing", and started off as a proof-of-concept for the insult formula introduced in the episode.