-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (135 loc) · 3.42 KB
/
main.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
package main
import (
"os"
"strconv"
"strings"
)
func main() {
processedContent := processText(readFullFile("./sample.txt"))
println(processedContent)
os.WriteFile("./result.txt", []byte(processedContent), 0644)
}
func processText(text string) string {
words := strings.Fields(text)
quoteCount := 0
for i, word := range words {
switch word {
case "A", "a":
if strings.Contains("aeiouhAEIOUH", string(words[i+1][0])) {
words[i] += "n"
}
case "'":
quoteCount++
if quoteCount%2 == 1 {
words[i+1] = word + words[i+1]
} else if strings.Contains("...,!?:;", words[i-1]) {
words[i-2] += word
} else {
words[i-1] += word
}
case "(bin)":
words[i-1] = strconv.Itoa(Bin2Dec(words[i-1]))
case "(hex)":
words[i-1] = strconv.Itoa(Hex2Dec(words[i-1]))
case "(cap)":
Cap(&words[i-1])
case "(low)":
Lower(&words[i-1])
case "(up)":
Upper(&words[i-1])
case "(cap,":
digit := extractDigit(words[i+1])
applyFunc2Many(Cap, digit, i-1, words)
case "(low,":
digit := extractDigit(words[i+1])
applyFunc2Many(Lower, digit, i-1, words)
case "(up,":
digit := extractDigit(words[i+1])
applyFunc2Many(Upper, digit, i-1, words)
default:
if strings.Contains("...,!?:;", string(word[0])) {
if string(words[i-1][1]) == ")" {
words[i-3] += string(word[0])
} else {
words[i-1] += string(word[0])
}
words[i] = word[1:]
}
}
}
return strings.Join(removeCommands(words), " ")
}
func extractDigit(word string) int {
myint, _ := strconv.Atoi(word[:len(word)-1])
return myint
}
// assuming start is i-1 and d is number extracted from 3) for example.
// apply function f to the last d words starting from start
func applyFunc2Many(f func(*string), d int, start int, words []string) {
for i := start; i > start-d; i-- {
f(&words[i])
}
}
func removeCommands(words []string) []string {
result := []string{}
removeNext := false
for _, word := range words {
if word == "(cap)" || word == "(low)" || word == "(up)" || word == "(bin)" || word == "(hex)" {
continue
}
if word == "(cap)," || word == "(low)," || word == "(up)," || word == "(bin)," || word == "(hex)," {
result[len(result)-1] += ","
continue
}
if word == "(cap," || word == "(low," || word == "(up," {
removeNext = true
continue
}
if removeNext {
removeNext = false
continue
}
if len(word) > 1 && OnlyPunctuation(word) {
result[len(result)-1] += word
continue
}
if strings.Contains(".,!?:;'", word) { // remove punctuation
continue
}
result = append(result, word)
}
return result
}
func OnlyPunctuation(word string) bool {
for _, c := range word {
if !strings.Contains(".,!?:;'", string(c)) {
return false
}
}
return true
}
// convert binary number to digital
func Bin2Dec(binary string) int {
result, _ := strconv.ParseInt(binary, 2, 64)
return int(result)
}
// convert hex number to digital
func Hex2Dec(hex string) int {
result, _ := strconv.ParseInt(hex, 16, 64)
return int(result)
}
func Cap(str *string) {
if len(*str) > 0 {
*str = strings.ToUpper((*str)[:1]) + (*str)[1:]
}
}
func Lower(str *string) {
*str = strings.ToLower(*str)
}
func Upper(str *string) {
*str = strings.ToUpper(*str)
}
func readFullFile(filename string) string {
content, _ := os.ReadFile(filename)
return string(content)
}