lorem.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package lorem
  2. import (
  3. "rand"
  4. "time"
  5. "strings"
  6. )
  7. var src = rand.New(rand.NewSource(time.Nanoseconds()))
  8. // Generate a natural word len.
  9. func genWordLen() int {
  10. f := src.Float32() * 100
  11. // a table of word lengths and their frequencies.
  12. switch {
  13. case f < 1.939:
  14. return 1
  15. case f < 19.01:
  16. return 2
  17. case f < 38.00:
  18. return 3
  19. case f < 50.41:
  20. return 4
  21. case f < 61.00:
  22. return 5
  23. case f < 70.09:
  24. return 6
  25. case f < 78.97:
  26. return 7
  27. case f < 85.65:
  28. return 8
  29. case f < 90.87:
  30. return 9
  31. case f < 95.05:
  32. return 10
  33. case f < 97.27:
  34. return 11
  35. case f < 98.67:
  36. return 12
  37. case f < 100.0:
  38. return 13
  39. }
  40. return 2 // shouldn't get here
  41. }
  42. func intRange(min, max int) int {
  43. n := src.Int() % (max - min)
  44. return n + min
  45. }
  46. func word(wordLen int) string {
  47. if wordLen < 1 {
  48. wordLen = 1
  49. }
  50. if wordLen > 13 {
  51. wordLen = 13
  52. }
  53. n := src.Int() % len(wordlist)
  54. for {
  55. if n >= len(wordlist)-1 {
  56. n = 0
  57. }
  58. if len(wordlist[n]) == wordLen {
  59. return wordlist[n]
  60. }
  61. n++
  62. }
  63. return ""
  64. }
  65. // Generate a word in a specfied range of letters.
  66. func Word(min, max int) string {
  67. n := intRange(min, max)
  68. return word(n)
  69. }
  70. // Generate a sentence with a specified range of words.
  71. func Sentence(min, max int) string {
  72. n := intRange(min, max)
  73. // grab some words
  74. ws := []string{}
  75. maxcommas := 2
  76. numcomma := 0
  77. for i := 0; i < n; i++ {
  78. // maybe insert a comma, if there's less than two and
  79. // the current word is not the last or first
  80. if (src.Int()%n == 0) && numcomma < maxcommas && i < n-1 && i > 2 {
  81. ws[len(ws)-1] += ","
  82. numcomma += 1
  83. }
  84. ws = append(ws, (word(genWordLen())))
  85. }
  86. sentence := strings.Join(ws, " ") + "."
  87. sentence = strings.ToUpper(sentence[:1]) + sentence[1:]
  88. return sentence
  89. }
  90. // Generate a paragraph with a specified range of sentenences.
  91. const (
  92. minwords = 5
  93. maxwords = 22
  94. )
  95. func Paragraph(min, max int) string {
  96. n := intRange(min, max)
  97. p := []string{}
  98. for i := 0; i < n; i++ {
  99. p = append(p, Sentence(minwords, maxwords))
  100. }
  101. return strings.Join(p, " ")
  102. }
  103. // Generate a random URL
  104. func Url() string {
  105. n := intRange(0, 3)
  106. base := `http://www.` + Host()
  107. switch n {
  108. case 0: break
  109. case 1: base += "/" + Word(2,8)
  110. case 2: base += "/" + Word(2,8) + "/" + Word(2,8) + ".html"
  111. }
  112. return base
  113. }
  114. // Host
  115. func Host() string {
  116. n := intRange(0, 3)
  117. tld := ""
  118. switch n {
  119. case 0: tld = ".com"
  120. case 1: tld = ".net"
  121. case 2: tld = ".org"
  122. }
  123. parts := []string{Word(2,8), Word(2,8), tld}
  124. return strings.Join(parts, ``)
  125. }
  126. // Email
  127. func Email() string {
  128. return Word(4,10) + `@` + Host()
  129. }