forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/word_sentence.go

346 lines
6.9 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"bytes"
"errors"
"math/rand"
"strings"
"unicode"
)
type paragrapOptions struct {
paragraphCount int
sentenceCount int
wordCount int
separator string
2024-04-26 19:30:35 +00:00
}
const bytesPerWordEstimation = 6
type sentenceGenerator func(r *rand.Rand, wordCount int) string
2024-04-26 19:30:35 +00:00
type wordGenerator func(r *rand.Rand) string
// Sentence will generate a random sentence
2024-04-26 19:30:35 +00:00
func Sentence(wordCount int) string { return sentence(globalFaker.Rand, wordCount) }
// Sentence will generate a random sentence
2024-04-26 19:30:35 +00:00
func (f *Faker) Sentence(wordCount int) string { return sentence(f.Rand, wordCount) }
func sentence(r *rand.Rand, wordCount int) string {
2024-04-26 19:30:35 +00:00
return sentenceGen(r, wordCount, word)
2024-04-26 19:30:35 +00:00
}
// Paragraph will generate a random paragraphGenerator
2024-04-26 19:30:35 +00:00
func Paragraph(paragraphCount int, sentenceCount int, wordCount int, separator string) string {
2024-04-26 19:30:35 +00:00
return paragraph(globalFaker.Rand, paragraphCount, sentenceCount, wordCount, separator)
2024-04-26 19:30:35 +00:00
}
// Paragraph will generate a random paragraphGenerator
2024-04-26 19:30:35 +00:00
func (f *Faker) Paragraph(paragraphCount int, sentenceCount int, wordCount int, separator string) string {
2024-04-26 19:30:35 +00:00
return paragraph(f.Rand, paragraphCount, sentenceCount, wordCount, separator)
2024-04-26 19:30:35 +00:00
}
func paragraph(r *rand.Rand, paragraphCount int, sentenceCount int, wordCount int, separator string) string {
2024-04-26 19:30:35 +00:00
return paragraphGen(r, paragrapOptions{paragraphCount, sentenceCount, wordCount, separator}, sentence)
2024-04-26 19:30:35 +00:00
}
func sentenceGen(r *rand.Rand, wordCount int, word wordGenerator) string {
2024-04-26 19:30:35 +00:00
if wordCount <= 0 {
2024-04-26 19:30:35 +00:00
return ""
2024-04-26 19:30:35 +00:00
}
wordSeparator := ' '
2024-04-26 19:30:35 +00:00
sentence := bytes.Buffer{}
2024-04-26 19:30:35 +00:00
sentence.Grow(wordCount * bytesPerWordEstimation)
for i := 0; i < wordCount; i++ {
2024-04-26 19:30:35 +00:00
word := word(r)
2024-04-26 19:30:35 +00:00
if i == 0 {
2024-04-26 19:30:35 +00:00
runes := []rune(word)
2024-04-26 19:30:35 +00:00
runes[0] = unicode.ToTitle(runes[0])
2024-04-26 19:30:35 +00:00
word = string(runes)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
sentence.WriteString(word)
2024-04-26 19:30:35 +00:00
if i < wordCount-1 {
2024-04-26 19:30:35 +00:00
sentence.WriteRune(wordSeparator)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
sentence.WriteRune('.')
2024-04-26 19:30:35 +00:00
return sentence.String()
2024-04-26 19:30:35 +00:00
}
func paragraphGen(r *rand.Rand, opts paragrapOptions, sentecer sentenceGenerator) string {
2024-04-26 19:30:35 +00:00
if opts.paragraphCount <= 0 || opts.sentenceCount <= 0 || opts.wordCount <= 0 {
2024-04-26 19:30:35 +00:00
return ""
2024-04-26 19:30:35 +00:00
}
//to avoid making Go 1.10 dependency, we cannot use strings.Builder
2024-04-26 19:30:35 +00:00
paragraphs := bytes.Buffer{}
2024-04-26 19:30:35 +00:00
//we presume the length
2024-04-26 19:30:35 +00:00
paragraphs.Grow(opts.paragraphCount * opts.sentenceCount * opts.wordCount * bytesPerWordEstimation)
2024-04-26 19:30:35 +00:00
wordSeparator := ' '
for i := 0; i < opts.paragraphCount; i++ {
2024-04-26 19:30:35 +00:00
for e := 0; e < opts.sentenceCount; e++ {
2024-04-26 19:30:35 +00:00
paragraphs.WriteString(sentecer(r, opts.wordCount))
2024-04-26 19:30:35 +00:00
if e < opts.sentenceCount-1 {
2024-04-26 19:30:35 +00:00
paragraphs.WriteRune(wordSeparator)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
if i < opts.paragraphCount-1 {
2024-04-26 19:30:35 +00:00
paragraphs.WriteString(opts.separator)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
return paragraphs.String()
2024-04-26 19:30:35 +00:00
}
// Question will return a random question
2024-04-26 19:30:35 +00:00
func Question() string {
2024-04-26 19:30:35 +00:00
return question(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
// Question will return a random question
2024-04-26 19:30:35 +00:00
func (f *Faker) Question() string {
2024-04-26 19:30:35 +00:00
return question(f.Rand)
2024-04-26 19:30:35 +00:00
}
func question(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return strings.Replace(hipsterSentence(r, number(r, 3, 10)), ".", "?", 1)
2024-04-26 19:30:35 +00:00
}
// Quote will return a random quote from a random person
2024-04-26 19:30:35 +00:00
func Quote() string { return quote(globalFaker.Rand) }
// Quote will return a random quote from a random person
2024-04-26 19:30:35 +00:00
func (f *Faker) Quote() string { return quote(f.Rand) }
func quote(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return `"` + hipsterSentence(r, number(r, 3, 10)) + `" - ` + firstName(r) + " " + lastName(r)
2024-04-26 19:30:35 +00:00
}
func addWordSentenceLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("sentence", Info{
Display: "Sentence",
Category: "word",
2024-04-26 19:30:35 +00:00
Description: "Set of words expressing a statement, question, exclamation, or command",
Example: "Interpret context record river mind.",
Output: "string",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{Field: "wordcount", Display: "Word Count", Type: "int", Default: "5", Description: "Number of words in a sentence"},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
wordCount, err := info.GetInt(m, "wordcount")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if wordCount <= 0 || wordCount > 50 {
2024-04-26 19:30:35 +00:00
return nil, errors.New("invalid word count, must be greater than 0, less than 50")
2024-04-26 19:30:35 +00:00
}
return sentence(r, wordCount), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("paragraph", Info{
Display: "Paragraph",
Category: "word",
2024-04-26 19:30:35 +00:00
Description: "Distinct section of writing covering a single theme, composed of multiple sentences",
Example: "Interpret context record river mind press self should compare property outcome divide. Combine approach sustain consult discover explanation direct address church husband seek army. Begin own act welfare replace press suspect stay link place manchester specialist. Arrive price satisfy sign force application hair train provide basis right pay. Close mark teacher strengthen information attempt head touch aim iron tv take.",
Output: "string",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{Field: "paragraphcount", Display: "Paragraph Count", Type: "int", Default: "2", Description: "Number of paragraphs"},
2024-04-26 19:30:35 +00:00
{Field: "sentencecount", Display: "Sentence Count", Type: "int", Default: "2", Description: "Number of sentences in a paragraph"},
2024-04-26 19:30:35 +00:00
{Field: "wordcount", Display: "Word Count", Type: "int", Default: "5", Description: "Number of words in a sentence"},
2024-04-26 19:30:35 +00:00
{Field: "paragraphseparator", Display: "Paragraph Separator", Type: "string", Default: "<br />", Description: "String value to add between paragraphs"},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
paragraphCount, err := info.GetInt(m, "paragraphcount")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if paragraphCount <= 0 || paragraphCount > 20 {
2024-04-26 19:30:35 +00:00
return nil, errors.New("invalid paragraph count, must be greater than 0, less than 20")
2024-04-26 19:30:35 +00:00
}
sentenceCount, err := info.GetInt(m, "sentencecount")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if sentenceCount <= 0 || sentenceCount > 20 {
2024-04-26 19:30:35 +00:00
return nil, errors.New("invalid sentence count, must be greater than 0, less than 20")
2024-04-26 19:30:35 +00:00
}
wordCount, err := info.GetInt(m, "wordcount")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if wordCount <= 0 || wordCount > 50 {
2024-04-26 19:30:35 +00:00
return nil, errors.New("invalid word count, must be greater than 0, less than 50")
2024-04-26 19:30:35 +00:00
}
paragraphSeparator, err := info.GetString(m, "paragraphseparator")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
return paragraph(r, paragraphCount, sentenceCount, wordCount, paragraphSeparator), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("question", Info{
Display: "Question",
Category: "word",
2024-04-26 19:30:35 +00:00
Description: "Statement formulated to inquire or seek clarification",
Example: "Roof chia echo?",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return question(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("quote", Info{
Display: "Quote",
Category: "word",
2024-04-26 19:30:35 +00:00
Description: "Direct repetition of someone else's words",
Example: `"Roof chia echo." - Lura Lockman`,
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return quote(r), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}