-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
115 lines (102 loc) · 2.07 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package serendipity
import (
"bytes"
"log"
"strings"
)
func (r *Serendipity) Char(from string) string {
return string(from[r.Intn(len(from))])
}
func (r *Serendipity) Letter() string {
return r.Char("abcdefghijklmnopqrstuvwxyz")
}
func (r *Serendipity) Vowel() string {
return r.Char("aeoui")
}
func (r *Serendipity) Consonant() string {
return r.Char("bcdfghjklmnpqrstvwxyz")
}
func (r *Serendipity) Syllable() string {
length := r.N(2, 3)
text := make([]string, length)
b := r.Bool()
for i := 0; i < length; i++ {
if (i%2 == 0) == b {
text[i] = r.Vowel()
} else {
text[i] = r.Consonant()
}
}
return strings.Join(text, "")
}
func (r *Serendipity) FakeWord() string {
length := r.N(1, 3)
text := make([]string, length)
for i := 0; i < length; i++ {
text[i] = r.Syllable()
}
return strings.Join(text, "")
}
func (r *Serendipity) Word() string {
if r.Bool() {
return r.Adjective()
}
return r.Noun()
}
func (r *Serendipity) Adjective() string {
obj, err := r.loadStrings("/adjective.txt")
if err != nil {
log.Println(err)
return ""
}
count := len(*obj)
if count == 0 {
return ""
}
i := r.N(0, count-1)
return (*obj)[i]
}
func (r *Serendipity) Noun() string {
obj, err := r.loadStrings("/noun.txt")
if err != nil {
log.Println(err)
return ""
}
count := len(*obj)
if count == 0 {
return ""
}
i := r.N(0, count-1)
return (*obj)[i]
}
func (r *Serendipity) Sentence(punctuation ...bool) string {
p := false
if len(punctuation) > 0 {
p = punctuation[0]
} else {
p = r.Bool()
}
count := r.N(12, 18)
words := make([]string, count)
for i := 0; i < count; i++ {
words[i] = r.FakeWord()
}
text := strings.Join(words, " ")
text = string(bytes.Join([][]byte{bytes.ToUpper([]byte{text[0]}), []byte(text)[1:]}, nil))
if p {
text += r.Punctuation()
}
return text
}
func (r *Serendipity) Paragraph() string {
count := r.N(3, 7)
words := make([]string, count)
for i := 0; i < count; i++ {
words[i] = r.Sentence(true)
}
text := strings.Join(words, " ")
return text
}
func (r *Serendipity) Punctuation() string {
return r.Char(".?;!:")
}