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.
 
 

233 lines
5.5 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)
}
// fmt.Println(string(input_text))
input_text = []byte(FixPunctuation(string(input_text))) // initial punctuation edits
text_slice := SplitBySpace(string(input_text)) // string to slice of strings
text_slice = FindTriggers(text_slice) // find and correct formatting/converting
output_text := RemoveDoubleSpaces(strings.Join(text_slice, " ")) // slice back to single string
output_text = FixApostrophes(output_text) // function dealing with apostrophes
output_text = FixPunctuation(output_text) // final punctuation control
os.WriteFile(string(os.Args[2]), []byte(string(output_text)), os.ModePerm)
// fmt.Println(string(output_text))
}
func RemoveDoubleSpaces(text string) string {
for x := 0; x < len(text)-1; x++ {
if x < len(text)-1 && rune(text[x]) == 32 && rune(text[x+1]) == 32 {
text = text[:x] + text[x+1:]
x = 0
}
}
return text
}
func FixPunctuation(text string) string {
text = RemoveDoubleSpaces(text)
for i := 0; i < len(text); i++ {
if string(text[i]) == "." || string(text[i]) == "," || string(text[i]) == "!" ||
string(text[i]) == "?" || string(text[i]) == ":" || string(text[i]) == ";" {
if i > 0 && string(text[i-1]) == " " {
text = text[:(i-1)] + text[i:]
}
} else if string(text[i]) != " " {
if i > 0 && (string(text[i-1]) == "." || string(text[i-1]) == "," || string(text[i-1]) == "!" ||
string(text[i-1]) == "?" || string(text[i-1]) == ":" || string(text[i-1]) == ";") {
text = text[:i] + " " + text[i:]
}
}
}
return text
}
func FixApostrophes(text string) string {
pair_count := true // after finding first apostrophe will turn false, after second back to true
for i := 0; i < len(text); i++ {
if string(text[i]) == "'" {
if i > 0 && i < len(text)-1 && string(text[i-1]) != " " && string(text[i+1]) != " " {
continue
}
if i < len(text)-2 && string(text[i+1]) == " " && pair_count {
text = text[:i+1] + text[i+2:]
pair_count = false
continue
}
if pair_count {
pair_count = false
continue
}
if i > 1 && string(text[i-1]) == " " && !pair_count {
text = text[:i-1] + text[i:]
pair_count = true
}
if !pair_count {
pair_count = true
}
}
}
// if string(text[i]) == "'" &&
/*
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 { // all of those add extra space when completed (fix implemented)
var x int
var z int // how many words to modify
for i := range slice {
if slice[i] == "(bin)" || slice[i] == "(hex)" {
x = Converter(slice[i-1], slice[i])
slice[i-1] = strconv.Itoa(x)
slice[i] = ""
}
if strings.Contains(string(slice[i]), "(up)") {
slice[i-1] = strings.ToUpper(slice[i-1])
slice[i] = strings.TrimPrefix(slice[i], "(up)")
}
if strings.Contains(string(slice[i]), "(up,") {
z = int(slice[i][5]) - 48
for a := i - z; a < i; a++ {
slice[a] = strings.ToUpper(slice[a])
}
slice[i] = slice[i][7:]
}
if strings.Contains(string(slice[i]), "(low)") {
slice[i-1] = strings.ToLower(slice[i-1])
slice[i] = strings.TrimPrefix(slice[i], "(low)")
}
if strings.Contains(string(slice[i]), "(low,") {
z = int(slice[i][6]) - 48
for b := i - z; b < i; b++ {
slice[b] = strings.ToLower(slice[b])
}
slice[i] = slice[i][8:]
}
if strings.Contains(string(slice[i]), "(cap)") {
slice[i-1] = strings.ToUpper(string(slice[i-1][0])) + slice[i-1][1:]
slice[i] = strings.TrimPrefix(slice[i], "(cap)")
}
if strings.Contains(string(slice[i]), "(cap,") {
z = int(slice[i][6]) - 48
for c := i - z; c < i; c++ {
slice[c] = strings.ToUpper(string(slice[c][0])) + slice[c][1:]
}
slice[i] = slice[i][8:]
}
if i+1 != len(slice) && (slice[i] == "a" || slice[i] == "A") {
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," {
str_temp += string(text[i])
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 { // converting ok (input check missing)
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
}