gibberish-sentence-generator
Simply import the SentenceGenerator
class, add syllable types and letter classes!
Letter classes are one-letter-named objects of letters and their corresponding weights.
For example, if you have two classes, named C
and V
(consonants and vowels respectively), they may look like this:
const classes = {
C: {
'b': 10,
'c': 20,
'd': 5,
'g': 50
},
V: {
'a': 2,
'o': 1,
'u': 3,
'i': 0
}
}
Here we can see that the letter g
will be the most frequent consonant,
10 times as frequent as d
and 5 times as frequent as b
, for example.
In vowel department, u
will be the most popular vowel, followed by a
and then o
. i
will not be generated at all, since it has a weight of 0.
Syllable types are combinations of letter classes, they are also weighted.
To continue the above example, we will create two syllable types: open (consonant + vowel) and closed (consonant + vowel + consonant):
const syllableTypes = {
CV: 100,
CVC: 33
}
CV will be the most frequent syllable type (e.g. ba
or du
), while CVC (e.g. bad
, guc
) will be
three times as rare.
In order to add your letter classes and syllable types, simply add them into the constructor parameter, like so:
const sg = new SentenceGenerator({
classes: {
C: {
'b': 10,
'c': 20,
'd': 5,
'g': 50
},
V: {
'a': 2,
'o': 1,
'u': 3,
'i': 0
}
},
syllableTypes: {
CV: 100,
CVC: 33
}
});
Then you can generate separate syllables, words and even sentences (as the package name implies):
console.log(sg.generateSyllable()) // prints "gub", CVC
console.log(sg.generateSyllable()) // prints "go", CV
console.log(sg.generateSyllable()) // prints "du", CV
console.log(sg.generateWord()) // prints "gagu", CV+CV
console.log(sg.generateWord()) // prints "cu", CV
console.log(sg.generateWord()) // prints "gugbucug", CVC+CV+CVC
console.log(sg.generateSentence()) // prints "Gogo cubgugo bugu, ca dubgu gucagu gaggugu ga gugog, gu bucuba co gobug, guc gagucgu"
console.log(sg.generateSentence()) // prints "Cacu gugogo dagbaggub coga gugo"
console.log(sg.generateSentence()) // prints "Gu gabuc gogu, guc gug gabugu"
Also, you can modify settings such as word length and sentence length:
const sg = new SentenceGenerator({
wordOptions: {
minSyllables: 5, // minimum random word length (in syllables)
maxSyllables: 15 // maximum random word length (in syllables)
},
sentenceOptions: {
minWords: 1, // minimum random sentence length (in words)
maxWords: 15, // maximum random sentence length (in words)
minWordsBeforeComma: 3, // minimum random amount of words after which to put comma
maxWordsBeforeComma: 7 // maximum random amount of words after which to put comma
},
// put your classes and syllableTypes here
});