-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttifier.go
More file actions
210 lines (178 loc) · 5.2 KB
/
buttifier.go
File metadata and controls
210 lines (178 loc) · 5.2 KB
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
package buttifier
import (
"math/rand/v2"
"slices"
"strings"
"unicode"
"github.com/speedata/hyphenation"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
/*
implements rand.Source
replaced in unit tests to pass a custom random seed
*/
type DefaultRandSource struct{}
func (DefaultRandSource) Uint64() uint64 {
return rand.Uint64()
}
type Buttifier struct {
hyphenator *hyphenation.Lang
ButtWord string
ButtificationProbability float64
ButtificationRate float64
RandSource rand.Source
}
type syllable struct {
Letters string
IdxStart int
IdxEnd int
}
type hyphenatedWord struct {
Word string
Breakpoints []int
Syllables []*syllable
}
type SupportedLang int
const (
English SupportedLang = iota
Portuguese
)
var langToHyphenatorDataKey = map[SupportedLang]string{
English: "en",
Portuguese: "pt",
}
func New(lang SupportedLang) (*Buttifier, error) {
key, _ := langToHyphenatorDataKey[lang]
data, _ := HyphenatorData[key]
hyph, err := hyphenation.New(strings.NewReader(data))
if err != nil {
return nil, err
}
return &Buttifier{
ButtWord: "butt",
hyphenator: hyph,
ButtificationProbability: 0.05,
ButtificationRate: 0.3,
RandSource: DefaultRandSource{},
}, nil
}
// replace random syllables with buttWord
// returns the buttified word and the number of buttified syllables
func (b *Buttifier) ButtifyWord(word string) (string, int) {
if word == "" {
return "", 0
}
var wordBuffer strings.Builder
buttCount := 0
for _, hyphenatedSyllable := range b.HyphenateWord(word).Syllables {
// random float between 0 and 1
rn := rand.New(b.RandSource).Float64()
if rn < b.ButtificationRate {
// normalize buttWord's case to match currentSyllable's case
buttifiedSyllable := normalizeCase(hyphenatedSyllable.Letters, b.ButtWord)
wordBuffer.WriteString(buttifiedSyllable)
buttCount++
} else {
wordBuffer.WriteString(hyphenatedSyllable.Letters)
}
}
return wordBuffer.String(), buttCount
}
func (b *Buttifier) HyphenateWord(word string) *hyphenatedWord {
word = normalizeDiacritics(word)
breakpoints := b.hyphenator.Hyphenate(word)
if len(breakpoints) == 0 {
// some words like "partne" return an empty slice, so we need to add a breakpoint
breakpoints = []int{len(word)}
} else if len(breakpoints) == 1 && breakpoints[0] == len(word)-1 {
// words like "asd" return []int{2}, resulting in "as" instead of "asd"
breakpoints[0] += 1
} else if breakpoints[len(breakpoints)-1] != len(word) {
// words with a single breakpoint like "partner" return []int{4}, resulting in "part" instead of "partner"
breakpoints = append(breakpoints, len(word))
}
var syllables []*syllable
idxStart := 0
for _, breakpoint := range breakpoints {
syllables = append(syllables, &syllable{
Letters: word[idxStart:breakpoint],
IdxStart: idxStart,
IdxEnd: breakpoint,
})
idxStart = breakpoint
}
return &hyphenatedWord{
Word: word,
Breakpoints: breakpoints,
Syllables: syllables,
}
}
func (b *Buttifier) HyphenateSentence(sentence string) []*hyphenatedWord {
words := strings.Split(sentence, " ")
var result []*hyphenatedWord
for _, word := range words {
result = append(result, b.HyphenateWord(word))
}
return result
}
// replace random syllables from each word with buttWord
// returns the buttified word and true if the word was changed
func (b *Buttifier) ButtifySentence(sentence string) string {
hyphenatedSentence := b.HyphenateSentence(sentence)
buttifiedSyllables := 0
totalSyllables := func() int {
count := 0
for _, hyphenatedWord := range hyphenatedSentence {
count += len(hyphenatedWord.Breakpoints)
}
return count
}()
reachedButtificationRate := func() bool {
return (float64(buttifiedSyllables) / float64(totalSyllables)) >= b.ButtificationRate
}
unbuttifiedWords := hyphenatedSentence
for !reachedButtificationRate() && len(unbuttifiedWords) > 1 {
randomWordIdx := rand.New(b.RandSource).Int() % len(unbuttifiedWords)
buttifiedWord, buttCount := b.ButtifyWord(unbuttifiedWords[randomWordIdx].Word)
if buttCount > 0 {
unbuttifiedWords[randomWordIdx].Word = buttifiedWord
buttifiedSyllables += buttCount
// remove the word we just buttified from the slice
unbuttifiedWords = slices.Delete(unbuttifiedWords, randomWordIdx, randomWordIdx)
}
}
result := []string{}
for _, hyphenatedWord := range hyphenatedSentence {
result = append(result, hyphenatedWord.Word)
}
return strings.Join(result, " ")
}
func (b *Buttifier) ToButtOrNotToButt() bool {
rn := rand.New(b.RandSource).Float64()
return rn < b.ButtificationProbability
}
func isUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) {
return false
}
}
return true
}
// return uppercase buttWord if the whole syllable is uppercase
func normalizeCase(currentSyllable string, buttWord string) string {
if isUpper(currentSyllable) {
return strings.ToUpper(buttWord)
}
return buttWord
}
func normalizeDiacritics(s string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, err := transform.String(t, s)
if err != nil {
return s
}
return result
}