txtgen
Lightweight util for generating random sentences, paragraphs and articles in English. Inspired by Sentencer and metaphorpsum.com.
Demo
See how it works?
Setup
Usage
var txtgen = require('txtgen');
let sentence = txtgen.sentence();
console.log(sentence);
let paragraph = txtgen.paragraph();
console.log(paragraph);
let article = txtgen.article();
console.log(article);
APIs
- .sentence()
- .paragraph([Number totalSentences])
- .article([Number totalParagraphs])
- .addNouns(Array nouns)
- .addAdjectives(Array adjectives)
- .addTemplates(Array sentenceTemplates)
- .setNouns(Array nouns)
- .setAdjectives(Array adjectives)
- .setTemplates(Array sentenceTemplates)
- .getNouns()
- .getAdjectives()
- .getTemplates()
As their name means, we have 4 groups of methods:
sentence()
, paragraph()
, article()
: generate text by given grammatical unitaddNouns()
, addAdjectives()
, addTemplates()
: add more samples to current sample setsetNouns()
, setAdjectives()
, setTemplates()
: replace current sample set with new onesgetNouns()
, getAdjectives()
, getTemplates()
: get current sample set
The set*
and get*
methods were added since v2.2.3 to help you customize your sample data.
Template
If you want to add more kinds of sentence, just use .addTemplates()
method. It expects a list of sentence templates.
Each of sentence template is an English sentence, with the placeholders that can be replaced with any alternative word.
For example:
import {
addTemplates
} from 'txtgen';
let templates = [
'{{a_noun}} is {{a_noun}} from the right perspective',
'the {{noun}} of {{a_noun}} becomes {{an_adjective}} {{noun}}'
];
addTemplates(templates);
Here are the available placeholders:
- noun
- nouns
- a_noun
- adjective
- an_adjective
Test
git clone https:
cd txtgen
npm install
npm test
License
The MIT License (MIT)