-
Notifications
You must be signed in to change notification settings - Fork 2
/
superspin.go
219 lines (181 loc) · 4.01 KB
/
superspin.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package superspin
import (
"math/rand"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// Block is a type for parsing text
type Block struct {
id string
deepLevel int
ref string
token rune
typ string
}
// SuperSpin is the main type for spun text
type SuperSpin struct {
openToken rune
closeToken rune
orToken rune
text string
result string
seed int64
useSeed bool
blocks []struct {
id string
value []string
}
}
// Seed use as default seed for SuperSpin
func (sp *SuperSpin) Seed(seed int64) {
sp.seed = seed
}
func (sp *SuperSpin) parse() {
tokens := []Block{}
deepLevel := 0
id := generateID()
ids := [][]string{}
ids = append(ids, []string{})
ids[deepLevel] = append(ids[deepLevel], id)
runes := []rune(sp.text)
for i := 0; i < len(sp.text); i++ {
char := runes[i]
if char == sp.openToken {
id := generateID()
block := Block{
id: ids[deepLevel][len(ids[deepLevel])-1],
ref: id,
typ: "block",
}
deepLevel++
ids = append(ids[:deepLevel], []string{})
ids[deepLevel] = append(ids[deepLevel], id)
tokens = append(tokens, block)
continue
}
if char == sp.closeToken {
deepLevel--
continue
}
block := Block{
id: ids[deepLevel][len(ids[deepLevel])-1],
token: char,
deepLevel: deepLevel,
typ: "char",
}
tokens = append(tokens, block)
}
blockGroup := map[string][]Block{}
for i := 0; i < len(tokens); i++ {
blockGroup[tokens[i].id] = append(blockGroup[tokens[i].id], tokens[i])
}
for key, val := range blockGroup {
var block struct {
id string
value []string
}
var s []string
for i := 0; i < len(val); i++ {
if val[i].typ == "char" {
s = append(s, string(val[i].token))
}
if val[i].typ == "block" {
s = append(s, "${"+val[i].ref+"}")
}
}
block.id = key
value := strings.Join(s, "")
block.value = strings.Split(value, string(sp.orToken))
sp.blocks = append(sp.blocks, block)
}
}
var reBlock = regexp.MustCompile(`\$\{([\w\-\_]+)\}`)
// Spin create spin version of text
func (sp *SuperSpin) Spin(text string) string {
sp.text = text
sp.parse()
// let seed = this._seed;
// seed := sp.seed
var spinnedBlocks []struct {
id string
text string
}
for i := 0; i < len(sp.blocks); i++ {
block := sp.blocks[i]
id := block.id
value := block.value
randSeed, _ := strconv.ParseInt(id, 32, 64)
if sp.seed > 0 {
rand.Seed(sp.seed)
} else {
rand.Seed(int64(randSeed) + time.Now().UnixNano())
}
index := rand.Perm(len(value))
text := value[index[0]]
var spinnedBlock struct{ id, text string }
spinnedBlock.id = id
spinnedBlock.text = text
spinnedBlocks = append(spinnedBlocks, spinnedBlock)
}
blockMap := make(map[string]string)
for i := 0; i < len(spinnedBlocks); i++ {
blockMap[spinnedBlocks[i].id] = spinnedBlocks[i].text
}
for {
prevBlockLen := len(blockMap)
for key, val := range blockMap {
matched := reBlock.FindStringSubmatch(val)
if len(matched) > 0 {
rawID := matched[0]
id := matched[1]
blockMap[key] = strings.Replace(blockMap[key], rawID, blockMap[id], -1)
delete(blockMap, id)
}
}
if prevBlockLen == len(blockMap) {
break
}
}
lengthBlockMapVal := make(map[int]string)
for key, val := range blockMap {
lengthBlockMapVal[len(val)] = key
}
var keys []int
for k := range lengthBlockMapVal {
keys = append(keys, k)
}
sort.Ints(keys)
keyName := lengthBlockMapVal[keys[len(keys)-1]]
sp.result = blockMap[keyName]
return sp.result
}
func (sp *SuperSpin) String() (s string) {
return sp.text
}
func generateID() string {
rand.Seed(time.Now().UnixNano())
numericID := rand.Int63()
id := strconv.FormatInt(numericID, 32)
return id
}
// New create new SuperSpin structure.
func New(params ...rune) SuperSpin {
sp := SuperSpin{
openToken: '{',
closeToken: '}',
orToken: '|',
}
if len(params) == 1 {
sp.openToken = params[0]
}
if len(params) == 2 {
sp.closeToken = params[1]
}
if len(params) == 3 {
sp.orToken = params[2]
}
return sp
}