Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "io/ioutil"
- "os"
- "regexp"
- "strconv"
- "strings"
- )
- func main() {
- file, _ := ioutil.ReadFile("sample.txt")
- data := string(file)
- word := strings.Split(data, " ")
- for i := 0; i < len(word); i++ {
- switch word[i] {
- case "(cap)":
- word[i-1] = strings.Title(word[i-1])
- case "(up)":
- word[i-1] = strings.ToUpper(word[i-1])
- case "(low)":
- word[i-1] = strings.ToLower(word[i-1])
- case "(hex)":
- dec, _ := strconv.ParseInt(word[i-1], 16, 64)
- word[i-1] = strconv.Itoa(int(dec))
- case "(bin)":
- dec, _ := strconv.ParseInt(word[i-1], 2, 64)
- word[i-1] = strconv.Itoa(int(dec))
- case "(up,", "(low,", "(cap,":
- num, _ := strconv.ParseInt(word[i+1][:1], 0, 64)
- for j := int(num); j > 0; j-- {
- if word[i] == "(cap," {
- word[i-j] = strings.Title(word[i-j])
- }
- if word[i] == "(up," {
- word[i-j] = strings.ToUpper(word[i-j])
- }
- if word[i] == "(low," {
- word[i-j] = strings.ToLower(word[i-j])
- }
- }
- case "a", "A":
- if word[i] == "a" || word[i] == "e" || word[i] == "i" || word[i] == "o" || word[i] == "u" || word[i] == "h" {
- word[i] = word[i] + "n"
- }
- if word[i] == "A" || word[i] == "E" || word[i] == "I" || word[i] == "O" || word[i] == "U" || word[i] == "H" {
- word[i] = word[i] + "n"
- }
- }
- }
- regex := regexp.MustCompile(`\(up\)|\(low\)|\(cap\)|\(hex\)|\(bin\)|\(up,|\(low,|\(cap,|\d+\)`)
- str := strings.Join(word, " ")
- del := regex.ReplaceAllString(str, " ")
- r := regexp.MustCompile(` `)
- com := r.ReplaceAllString(del, "")
- com = strings.ReplaceAll(com, ".", " . ")
- com = strings.ReplaceAll(com, ",", " , ")
- com = strings.ReplaceAll(com, "!", " ! ")
- com = strings.ReplaceAll(com, "?", " ? ")
- com = strings.ReplaceAll(com, ":", " : ")
- com = strings.ReplaceAll(com, ";", " ; ")
- com = strings.Join(strings.Fields(com), " ")
- com = strings.ReplaceAll(com, " . ", ". ")
- com = strings.ReplaceAll(com, " ! ", "! ")
- com = strings.ReplaceAll(com, " ? ", "? ")
- com = strings.ReplaceAll(com, " : ", ": ")
- com = strings.ReplaceAll(com, " ; ", "; ")
- com = strings.ReplaceAll(com, " , ", ", ")
- com = strings.ReplaceAll(com, " .", ".")
- com = strings.ReplaceAll(com, " ,", ",")
- com = strings.ReplaceAll(com, " !", "!")
- com = strings.ReplaceAll(com, " ?", "?")
- com = strings.ReplaceAll(com, " :", ":")
- com = strings.ReplaceAll(com, " ;", ";")
- com = strings.Join(strings.Fields(com), " ")
- com = strings.ReplaceAll(com, "‘", "'")
- com = strings.ReplaceAll(com, "’", "'")
- counter := 0
- for i := 0; i < len(com)-1; i++ {
- if string(com[i]) == "'" && (string(com[i-1]) == " " || string(com[i+1]) == " ") {
- if counter%2 == 0 {
- if string(com[i+1]) == " " {
- com = com[:i] + "‘" + com[i+2:] + " "
- }
- counter++
- } else {
- if string(com[i-1]) == " " {
- com = com[:i-1] + "’" + com[i+1:] + " "
- }
- counter++
- }
- }
- }
- com = strings.ReplaceAll(com, "'", "‘")
- fmt.Println(com)
- os.WriteFile("result.txt", []byte(com), 0o0666)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement