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)) text_slice := SplitBySpace(string(input_text)) // fmt.Println(text_slice) text_slice = FindHexBinArticles(text_slice) // convert nr systems text := RemoveDoubleSpaces(strings.Join(text_slice, " ")) // empty slice units removed // fmt.Println(text) text = FixPunctuation(text) // first punctuation check next_slice := SplitBySpace(text) next_slice = FindTriggers(next_slice) // upper lower capitalize output_text := RemoveDoubleSpaces(strings.Join(next_slice, " ")) // slice back to single string output_text = FixPunctuation(output_text) // final punctuation control output_text = FixApostrophes(output_text) // apostrophes being checked // fmt.Println(string(output_text)) os.WriteFile(string(os.Args[2]), []byte(string(output_text)), os.ModePerm) } 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 runes := []rune(text) mark1 := '‘' // rune('\'') mark2 := '’' // rune('\'') for i, letter := range runes { if letter == mark2 { // fmt.Println("mark2 detected") if i > 0 && i < len(runes)-1 && runes[i-1] != 32 && runes[i+1] != 32 { // fmt.Println("check: mark 2 in text") continue } } if letter == mark1 { // separate if for troubleshooting only; will be joined with previous if // fmt.Println("mark1 detected") if i > 0 && i < len(runes)-1 && runes[i-1] != 32 && runes[i+1] != 32 { // fmt.Println("check: mark 1 in text") continue } } // runes = append(runes[:i], runes[i+2:]...) // runes = append(runes[:i-1], runes[i:]...) if letter == mark1 || letter == mark2 { if pair_count { if i < len(runes)-2 && runes[i+1] == rune(32) { runes = append(runes[:i], runes[i+2:]...) // suspected reason for malfunction pair_count = false // fmt.Println("first_...") continue } else { pair_count = false // fmt.Println("first...") continue } } if !pair_count { if i > 1 && runes[i-1] == rune(32) { runes = append(runes[:i-1], runes[i:]...) // suspected reason for malfunction pair_count = true // fmt.Println("..._second") continue } else { pair_count = true // fmt.Println("...second") continue } } } } return text /* for i := 0; i < len(text); i++ { //initial apostrophe check for " ' " markings (functional) 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 } } } */ } func FindHexBinArticles(slice []string) []string { var x int for i := range slice { if strings.Contains(string(slice[i]), "(bin)") || strings.Contains(string(slice[i]), "(hex)") { x = Converter(strings.ToUpper(slice[i-1]), slice[i][:5]) slice[i-1] = strconv.Itoa(x) slice[i] = strings.TrimPrefix(slice[i], "(bin)") slice[i] = strings.TrimPrefix(slice[i], "(hex)") } 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 FindTriggers(slice []string) []string { // all of those add extra space when completed (fix implemented) var x int // how many words to modify for i := range slice { 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,") { x = int(slice[i][5]) - 48 for a := i - x; 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,") { x = int(slice[i][6]) - 48 for b := i - x; 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,") { x = int(slice[i][6]) - 48 for c := i - x; c < i; c++ { slice[c] = strings.ToUpper(string(slice[c][0])) + slice[c][1:] } slice[i] = slice[i][8:] } } return slice } func SplitBySpace(text string) []string { separator := " " str_temp := "" // temporary string text_slice := make([]string, 0) for i, char := range text { if string(char) != separator { str_temp += string(char) } else if str_temp == "(low," || str_temp == "(up," || str_temp == "(cap," { str_temp += string(char) 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 }