-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_tts_substitute.go
More file actions
68 lines (61 loc) · 1.28 KB
/
Copy pathchat_tts_substitute.go
File metadata and controls
68 lines (61 loc) · 1.28 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
package main
import (
_ "embed"
"os"
"path/filepath"
"strings"
"sync"
)
//go:embed data/tts_substitute.txt
var defaultTTSSubstitute []byte
var (
ttsSubs map[string]string
ttsSubsMu sync.RWMutex
)
const ttsSubstituteFile = "tts_substitute.txt"
func init() {
loadTTSSubstitutions()
}
func loadTTSSubstitutions() {
path := filepath.Join(dataDirPath, ttsSubstituteFile)
var b []byte
if isWASM {
b = append([]byte(nil), defaultTTSSubstitute...)
} else {
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.WriteFile(path, defaultTTSSubstitute, 0o644)
}
var err error
b, err = os.ReadFile(path)
if err != nil {
logError("read tts_substitute: %v", err)
return
}
}
m := make(map[string]string)
lines := strings.Split(string(b), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
if idx := strings.Index(line, "="); idx >= 0 {
from := strings.TrimSpace(line[:idx])
to := strings.TrimSpace(line[idx+1:])
if from != "" {
m[from] = to
}
}
}
ttsSubsMu.Lock()
ttsSubs = m
ttsSubsMu.Unlock()
}
func substituteTTS(text string) string {
ttsSubsMu.RLock()
for from, to := range ttsSubs {
text = strings.ReplaceAll(text, from, to)
}
ttsSubsMu.RUnlock()
return text
}