-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.go
More file actions
164 lines (140 loc) · 3.31 KB
/
Copy pathprocessor.go
File metadata and controls
164 lines (140 loc) · 3.31 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
package main
import (
"strconv"
"strings"
)
func normalizeText(s string) string {
replacer := strings.NewReplacer(
",", " , ",
".", " . ",
"!", " ! ",
"?", " ? ",
":", " : ",
";", " ; ",
"'", " ' ",
)
// temporarily protect commands
s = strings.ReplaceAll(s, "(up,", "UPCOMMAND")
s = strings.ReplaceAll(s, "(low,", "LOWCOMMAND")
s = strings.ReplaceAll(s, "(cap,", "CAPCOMMAND")
s = replacer.Replace(s)
// restore commands
s = strings.ReplaceAll(s, "UPCOMMAND", "(up,")
s = strings.ReplaceAll(s, "LOWCOMMAND", "(low,")
s = strings.ReplaceAll(s, "CAPCOMMAND", "(cap,")
return s
}
func handleAll(input string) string {
input = normalizeText(input)
word := strings.Fields(input)
quoteOpen := false
for i := 0; i < len(word); i++ {
// HEX / BIN
if i > 0 && (word[i] == "(hex)" || word[i] == "(bin)") {
var base int
if word[i] == "(hex)" {
base = 16
} else {
base = 2
}
prev, err := strconv.ParseInt(word[i-1], base, 64)
if err == nil {
word[i-1] = strconv.FormatInt(prev, 10)
}
word = append(word[:i], word[i+1:]...)
i--
continue
}
// UP
if i > 0 && word[i] == "(up)" {
word[i-1] = strings.ToUpper(word[i-1])
word = append(word[:i], word[i+1:]...)
i--
continue
}
// LOW
if i > 0 && word[i] == "(low)" {
word[i-1] = strings.ToLower(word[i-1])
word = append(word[:i], word[i+1:]...)
i--
continue
}
// CAP
if i > 0 && word[i] == "(cap)" {
if len(word[i-1]) > 0 {
word[i-1] = strings.ToUpper(word[i-1][:1]) + strings.ToLower(word[i-1][1:])
}
word = append(word[:i], word[i+1:]...)
i--
continue
}
// CASE WITH NUMBER (up, 2) (low, 3) (cap, 4)
if i > 0 && (strings.HasPrefix(word[i], "(up,") || strings.HasPrefix(word[i], "(low,") || strings.HasPrefix(word[i], "(cap,")) {
if i+1 >= len(word) {
continue
}
command := strings.TrimPrefix(word[i], "(")
command = strings.TrimSuffix(command, ",")
numberPart := strings.TrimSuffix(word[i+1], ")")
count, err := strconv.Atoi(numberPart)
if err != nil {
continue
}
for j := 1; j <= count && i-j >= 0; j++ {
switch command {
case "up":
word[i-j] = strings.ToUpper(word[i-j])
case "low":
word[i-j] = strings.ToLower(word[i-j])
case "cap":
if len(word[i-j]) > 0 {
word[i-j] = strings.ToUpper(word[i-j][:1]) + strings.ToLower(word[i-j][1:])
}
}
}
word = append(word[:i], word[i+2:]...)
i--
continue
}
// PUNCTUATION FIX
puncts := ".,!?;:"
if i > 0 && allPunct(word[i], puncts) {
word[i-1] += word[i]
word = append(word[:i], word[i+1:]...)
i--
continue
}
// QUOTES
if word[i] == "'" {
if !quoteOpen && i+1 < len(word) {
word[i+1] = "'" + word[i+1]
} else if quoteOpen && i > 0 {
word[i-1] = word[i-1] + "'"
}
quoteOpen = !quoteOpen
word = append(word[:i], word[i+1:]...)
i--
continue
}
// A -> AN RULE
if i < len(word)-1 && (word[i] == "a" || word[i] == "A") && len(word[i+1]) > 0 {
vowel := strings.ToLower(word[i+1][:1])
if strings.Contains("aeiouh", vowel) {
if word[i] == "A" {
word[i] = "An"
} else {
word[i] = "an"
}
}
}
}
return strings.Join(word, " ")
}
func allPunct(s, puncts string) bool {
for _, r := range s {
if !strings.ContainsRune(puncts, r) {
return false
}
}
return true
}