-
Notifications
You must be signed in to change notification settings - Fork 1
/
vocx.go
192 lines (148 loc) · 4.18 KB
/
vocx.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
package vocx
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// Transcriber handles transcribing text.
type Transcriber struct {
rules *Rules
}
// Transcribe transcribes text using the current rules.
func (t *Transcriber) Transcribe(text string) string {
text = strings.ReplaceAll(text, "\n", " ")
isNumber := func(word string) bool {
word = strings.ReplaceAll(strings.ReplaceAll(word, ",", ""), ".", "")
_, err := strconv.ParseFloat(word, 64)
return err == nil
}
parseNumber := func(word string) (int64, int64) {
parts := strings.Split(strings.ReplaceAll(word, ".", ""), ",")
if len(parts) == 1 {
whole, _ := strconv.ParseInt(parts[0], 10, 64)
return whole, 0
}
if len(parts) == 2 {
whole, _ := strconv.ParseInt(parts[0], 10, 64)
fraction, _ := strconv.ParseInt(parts[1], 10, 64)
return whole, fraction
}
return 0, 0
}
words := transform(strings.Split(text, " "), func(word string) string {
word = strings.TrimSpace(word)
if word == "" {
return word
}
if isNumber(word) {
return t.transcribeNumber(parseNumber(word))
}
if override := t.rules.findOverride(word); override != "" {
return override
}
letters := strings.Split(word, "")
for i, letter := range letters {
l, ok := t.rules.Letters[strings.ToLower(letter)]
if !ok {
continue
}
letters[i] = l
}
word = strings.Join(letters, "")
for _, fragment := range t.rules.Fragments {
r := regexp.MustCompile(fragment.Match)
word = r.ReplaceAllString(word, fragment.Replace)
}
return word
})
return strings.Join(words, " ")
}
// LoadRules loads a new set of rules from the JSON string.
func (t *Transcriber) LoadRules(json string) error {
rules, err := loadRules(json)
if err != nil {
return err
}
t.rules = rules
return nil
}
func (t *Transcriber) transcribeNumber(whole int64, fraction int64) string {
result := t.transcribeNumberPart(whole)
if fraction != 0 && fraction < 100 {
return fmt.Sprintf("%s, komo %s", result, t.transcribeNumberPart(fraction))
}
return result
}
func (t *Transcriber) transcribeNumberPart(number int64) string {
getNumber := func(number int64) string {
return t.rules.Numbers[strconv.FormatInt(number, 10)]
}
transcribe := func(one, ten, hundred int64, includeOne bool) string {
result := ""
if hundred > 0 {
if hundred == 1 {
result += fmt.Sprintf(" %s", getNumber(100))
} else {
result += fmt.Sprintf(" %s %s", getNumber(hundred), getNumber(100))
}
}
if ten > 0 {
if ten == 1 {
result += fmt.Sprintf(" %s", getNumber(10))
} else {
result += fmt.Sprintf(" %s %s", getNumber(ten), getNumber(10))
}
}
if one == 1 {
if includeOne {
result += fmt.Sprintf(" %s", getNumber(1))
}
} else if one != 0 {
result += fmt.Sprintf(" %s", getNumber(one))
}
return strings.TrimSpace(result)
}
ones := number % 10
tens := (number / 10) % 10
hundreds := (number / 100) % 10
thousands := (number / 1000) % 10
tenThousands := (number / 10000) % 10
hundredThousands := (number / 100000) % 10
millions := (number / 1000000) % 10
tenMillions := (number / 10000000) % 10
hundredMillions := (number / 100000000) % 10
first := transcribe(ones, tens, hundreds, true)
second := transcribe(thousands, tenThousands, hundredThousands, false)
third := transcribe(millions, tenMillions, hundredMillions, false)
hasValues := func(n1, n2, n3 int64) bool {
return n1 != 0 || n2 != 0 || n3 != 0
}
result := ""
if hasValues(millions, tenMillions, hundredMillions) {
result += fmt.Sprintf("%s %s,", third, getNumber(1000000))
}
if hasValues(thousands, tenThousands, hundredThousands) {
result += fmt.Sprintf(" %s %s,", second, getNumber(1000))
}
if hasValues(ones, tens, hundreds) {
result += fmt.Sprintf(" %s", first)
}
return strings.TrimRight(strings.TrimSpace(result), ",")
}
// NewTranscriber returns a transcriber with default rules.
func NewTranscriber() *Transcriber {
t := &Transcriber{}
_ = t.LoadRules(defaultRules)
return t
}
func transform(items []string, fn func(item string) string) []string {
transformed := make([]string, 0)
for _, item := range items {
str := fn(item)
if str != "" {
transformed = append(transformed, str)
}
}
return transformed
}