words.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package gofakeit
  2. import (
  3. "bytes"
  4. "strings"
  5. "unicode"
  6. )
  7. type paragrapOptions struct {
  8. paragraphCount int
  9. sentenceCount int
  10. wordCount int
  11. separator string
  12. }
  13. const bytesPerWordEstimation = 6
  14. type sentenceGenerator func(wordCount int) string
  15. type wordGenerator func() string
  16. // Word will generate a random word
  17. func Word() string {
  18. return getRandValue([]string{"lorem", "word"})
  19. }
  20. // Sentence will generate a random sentence
  21. func Sentence(wordCount int) string {
  22. return sentence(wordCount, Word)
  23. }
  24. // Paragraph will generate a random paragraphGenerator
  25. // Set Paragraph Count
  26. // Set Sentence Count
  27. // Set Word Count
  28. // Set Paragraph Separator
  29. func Paragraph(paragraphCount int, sentenceCount int, wordCount int, separator string) string {
  30. return paragraphGenerator(paragrapOptions{paragraphCount, sentenceCount, wordCount, separator}, Sentence)
  31. }
  32. func sentence(wordCount int, word wordGenerator) string {
  33. if wordCount <= 0 {
  34. return ""
  35. }
  36. wordSeparator := ' '
  37. sentence := bytes.Buffer{}
  38. sentence.Grow(wordCount * bytesPerWordEstimation)
  39. for i := 0; i < wordCount; i++ {
  40. word := word()
  41. if i == 0 {
  42. runes := []rune(word)
  43. runes[0] = unicode.ToTitle(runes[0])
  44. word = string(runes)
  45. }
  46. sentence.WriteString(word)
  47. if i < wordCount-1 {
  48. sentence.WriteRune(wordSeparator)
  49. }
  50. }
  51. sentence.WriteRune('.')
  52. return sentence.String()
  53. }
  54. func paragraphGenerator(opts paragrapOptions, sentecer sentenceGenerator) string {
  55. if opts.paragraphCount <= 0 || opts.sentenceCount <= 0 || opts.wordCount <= 0 {
  56. return ""
  57. }
  58. //to avoid making Go 1.10 dependency, we cannot use strings.Builder
  59. paragraphs := bytes.Buffer{}
  60. //we presume the length
  61. paragraphs.Grow(opts.paragraphCount * opts.sentenceCount * opts.wordCount * bytesPerWordEstimation)
  62. wordSeparator := ' '
  63. for i := 0; i < opts.paragraphCount; i++ {
  64. for e := 0; e < opts.sentenceCount; e++ {
  65. paragraphs.WriteString(sentecer(opts.wordCount))
  66. if e < opts.sentenceCount-1 {
  67. paragraphs.WriteRune(wordSeparator)
  68. }
  69. }
  70. if i < opts.paragraphCount-1 {
  71. paragraphs.WriteString(opts.separator)
  72. }
  73. }
  74. return paragraphs.String()
  75. }
  76. // Question will return a random question
  77. func Question() string {
  78. return strings.Replace(HipsterSentence(Number(3, 10)), ".", "?", 1)
  79. }
  80. // Quote will return a random quote from a random person
  81. func Quote() string {
  82. return `"` + HipsterSentence(Number(3, 10)) + `" - ` + FirstName() + " " + LastName()
  83. }