You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
3.1 KiB
156 lines
3.1 KiB
package main |
|
|
|
import ( |
|
"fmt" |
|
"io/ioutil" |
|
"os" |
|
"strconv" |
|
"strings" |
|
) |
|
|
|
func main() { |
|
file_name := string(os.Args[1]) |
|
input_text, err_01 := ioutil.ReadFile(file_name) |
|
|
|
if err_01 != nil { |
|
fmt.Println("ebaõnnestus") |
|
os.Exit(1) |
|
} |
|
|
|
text_slice := SplitBySpace(string(input_text)) |
|
|
|
text_slice = FindTriggers(text_slice) |
|
|
|
output_text := strings.Join(text_slice, " ") |
|
|
|
output_text = FixPunctuation(output_text) |
|
|
|
// os.WriteFile(os.Args[2], output_text, os.ModePerm) |
|
|
|
fmt.Println(string(output_text)) |
|
} |
|
|
|
func FixPunctuation(text string) string { |
|
apostrophe_pair := true |
|
|
|
for i := 0; i < len(text); i++ { |
|
|
|
if strings.ContainsAny(string(text[i]), ".,!?:;") && i > 0 && i > len(text)-1 { |
|
fmt.Println("found sth") |
|
text = strings.TrimPrefix(text[i-1:i+1], " ") // removes single space before punctuation |
|
|
|
if strings.ContainsAny(string(text[i+1]), ".,!?:;") { |
|
continue |
|
} |
|
if string(text[i+1]) != " " { |
|
text = text[:i+1] + " " + text[i+1:] |
|
} |
|
} |
|
|
|
if string(text[i]) == "'" && apostrophe_pair && i > len(text)-1 { |
|
fmt.Println("found first apostrophe") |
|
text = strings.TrimSuffix(text[i:i+2], " ") |
|
apostrophe_pair = false |
|
} |
|
if string(text[i]) == "'" && !apostrophe_pair { |
|
fmt.Println("found second apostrophe") |
|
text = strings.TrimPrefix(text[i-1:i+1], " ") |
|
apostrophe_pair = true |
|
} |
|
} |
|
return text |
|
} |
|
|
|
func FindTriggers(slice []string) []string { |
|
var x int |
|
for i := range slice { |
|
|
|
if slice[i] == "(bin)" || slice[i] == "(hex)" { // ok, except one additional space added |
|
x = Converter(slice[i-1], slice[i]) |
|
slice[i-1] = strconv.Itoa(x) |
|
slice[i] = "" |
|
} |
|
if slice[i] == "(up)" { |
|
slice[i-1] = strings.ToUpper(slice[i-1]) |
|
slice[i] = "" |
|
} |
|
if slice[i] == "(low)" { |
|
slice[i-1] = strings.ToLower(slice[i-1]) |
|
slice[i] = "" |
|
} |
|
if slice[i] == "(cap)" { |
|
slice[i-1] = strings.ToUpper(string(slice[i-1][0])) + slice[i-1][1:] |
|
slice[i] = "" |
|
} |
|
if i+1 != len(slice) && (slice[i] == "a" || slice[i] == "A") { // ok |
|
if strings.ContainsAny(string(slice[i+1][0]), "aeiouh") { |
|
slice[i] += "n" |
|
} |
|
} |
|
|
|
} |
|
return slice |
|
} |
|
|
|
func SplitBySpace(text string) []string { |
|
separator := " " |
|
str_temp := "" // temporary string |
|
text_slice := make([]string, 0) |
|
|
|
for i := 0; i < len(text); i++ { |
|
|
|
if string(text[i]) != separator { |
|
str_temp += string(text[i]) |
|
} else if str_temp == "(low," || str_temp == "(up," || str_temp == "(cap," { |
|
continue |
|
} else { |
|
text_slice = append(text_slice, str_temp) |
|
str_temp = "" |
|
} |
|
if i == len(text)-1 { |
|
text_slice = append(text_slice, str_temp) |
|
} |
|
} |
|
return text_slice |
|
} |
|
|
|
func Converter(str, system string) int { |
|
var x int |
|
var base string |
|
|
|
if system == "(hex)" { |
|
base = "0123456789ABCDEF" |
|
} |
|
if system == "(bin)" { |
|
base = "01" |
|
} |
|
|
|
temp_result := 0 |
|
result := 0 |
|
|
|
for id, digit := range str { |
|
|
|
for base_id, base_nr := range base { |
|
if digit == base_nr { |
|
x = base_id |
|
} |
|
} |
|
z := len(str) - 1 - id |
|
y := len(base) |
|
yVar := y |
|
if z == 0 { |
|
temp_result = x |
|
} else if z == 1 { |
|
temp_result = x * y |
|
} else { |
|
for i := 1; i < z; i++ { |
|
yVar *= y |
|
} |
|
temp_result = x * yVar |
|
} |
|
result += temp_result |
|
y = 0 |
|
z = 0 |
|
} |
|
return result |
|
}
|
|
|