56 lines
1.1 KiB
Go
Executable File
56 lines
1.1 KiB
Go
Executable File
package core
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// source on www.socketloop.com
|
|
func removeCharacters(input string, characters string) string {
|
|
filter := func(r rune) rune {
|
|
if strings.IndexRune(characters, r) < 0 {
|
|
return r
|
|
}
|
|
return -1
|
|
}
|
|
return strings.Map(filter, input)
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/34839659/how-can-i-easily-get-a-substring-in-go-while-guarding-against-slice-bounds-out
|
|
// get a substring of size "max" from a string "s"
|
|
func maxString(s string, max int) string {
|
|
if len(s) > max {
|
|
r := 0
|
|
for i := range s {
|
|
r++
|
|
if r > max {
|
|
return s[:i]
|
|
}
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// this only strip custom emoji, not default unicode ones
|
|
func stripEmoji(msg string) string {
|
|
re := regexp.MustCompile(`:\w+:`)
|
|
match := re.FindAll([]byte(msg), -1)
|
|
if len(match) > 0 {
|
|
s := msg
|
|
for i := range match {
|
|
s = strings.Replace(s, string(match[i]), "", -1)
|
|
}
|
|
return s
|
|
} else {
|
|
return msg
|
|
}
|
|
}
|
|
|
|
// sanitize received message for text-to-speech
|
|
func prepareTTSMessage(msg string) string {
|
|
t1 := maxString(msg, 300)
|
|
t2 := stripEmoji(t1)
|
|
t3 := removeCharacters(t2, "-\"'`$();:.\\")
|
|
return strings.Trim(t3, " ")
|
|
}
|