Advertisement
mjunolainen

go-reloaded

Sep 29th, 2021
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.92 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io/ioutil"
  6.     "os"
  7.     "regexp"
  8.     "strconv"
  9.     "strings"
  10. )
  11.  
  12. func main() {
  13.     file, _ := ioutil.ReadFile("sample.txt")
  14.     data := string(file)
  15.     word := strings.Split(data, " ")
  16.  
  17.     for i := 0; i < len(word); i++ {
  18.         switch word[i] {
  19.         case "(cap)":
  20.             word[i-1] = strings.Title(word[i-1])
  21.         case "(up)":
  22.             word[i-1] = strings.ToUpper(word[i-1])
  23.         case "(low)":
  24.             word[i-1] = strings.ToLower(word[i-1])
  25.         case "(hex)":
  26.             dec, _ := strconv.ParseInt(word[i-1], 16, 64)
  27.             word[i-1] = strconv.Itoa(int(dec))
  28.         case "(bin)":
  29.             dec, _ := strconv.ParseInt(word[i-1], 2, 64)
  30.             word[i-1] = strconv.Itoa(int(dec))
  31.         case "(up,", "(low,", "(cap,":
  32.             num, _ := strconv.ParseInt(word[i+1][:1], 0, 64)
  33.             for j := int(num); j > 0; j-- {
  34.                 if word[i] == "(cap," {
  35.                     word[i-j] = strings.Title(word[i-j])
  36.                 }
  37.                 if word[i] == "(up," {
  38.                     word[i-j] = strings.ToUpper(word[i-j])
  39.                 }
  40.                 if word[i] == "(low," {
  41.                     word[i-j] = strings.ToLower(word[i-j])
  42.                 }
  43.             }
  44.         case "a", "A":
  45.             if word[i] == "a" || word[i] == "e" || word[i] == "i" || word[i] == "o" || word[i] == "u" || word[i] == "h" {
  46.                 word[i] = word[i] + "n"
  47.             }
  48.             if word[i] == "A" || word[i] == "E" || word[i] == "I" || word[i] == "O" || word[i] == "U" || word[i] == "H" {
  49.                 word[i] = word[i] + "n"
  50.             }
  51.         }
  52.     }
  53.  
  54.     regex := regexp.MustCompile(`\(up\)|\(low\)|\(cap\)|\(hex\)|\(bin\)|\(up,|\(low,|\(cap,|\d+\)`)
  55.     str := strings.Join(word, " ")
  56.     del := regex.ReplaceAllString(str, " ")
  57.  
  58.     r := regexp.MustCompile(`  `)
  59.     com := r.ReplaceAllString(del, "")
  60.  
  61.     com = strings.ReplaceAll(com, ".", " . ")
  62.     com = strings.ReplaceAll(com, ",", " , ")
  63.     com = strings.ReplaceAll(com, "!", " ! ")
  64.     com = strings.ReplaceAll(com, "?", " ? ")
  65.     com = strings.ReplaceAll(com, ":", " : ")
  66.     com = strings.ReplaceAll(com, ";", " ; ")
  67.  
  68.     com = strings.Join(strings.Fields(com), " ")
  69.  
  70.     com = strings.ReplaceAll(com, " . ", ". ")
  71.     com = strings.ReplaceAll(com, " ! ", "! ")
  72.     com = strings.ReplaceAll(com, " ? ", "? ")
  73.     com = strings.ReplaceAll(com, " : ", ": ")
  74.     com = strings.ReplaceAll(com, " ; ", "; ")
  75.     com = strings.ReplaceAll(com, " , ", ", ")
  76.  
  77.     com = strings.ReplaceAll(com, " .", ".")
  78.     com = strings.ReplaceAll(com, " ,", ",")
  79.     com = strings.ReplaceAll(com, " !", "!")
  80.     com = strings.ReplaceAll(com, " ?", "?")
  81.     com = strings.ReplaceAll(com, " :", ":")
  82.     com = strings.ReplaceAll(com, " ;", ";")
  83.  
  84.     com = strings.Join(strings.Fields(com), " ")
  85.     com = strings.ReplaceAll(com, "‘", "'")
  86.     com = strings.ReplaceAll(com, "’", "'")
  87.     counter := 0
  88.     for i := 0; i < len(com)-1; i++ {
  89.         if string(com[i]) == "'" && (string(com[i-1]) == " " || string(com[i+1]) == " ") {
  90.             if counter%2 == 0 {
  91.                 if string(com[i+1]) == " " {
  92.                     com = com[:i] + "‘" + com[i+2:] + " "
  93.                 }
  94.                 counter++
  95.             } else {
  96.                 if string(com[i-1]) == " " {
  97.                     com = com[:i-1] + "’" + com[i+1:] + " "
  98.                 }
  99.                 counter++
  100.             }
  101.         }
  102.     }
  103.     com = strings.ReplaceAll(com, "'", "‘")
  104.     fmt.Println(com)
  105.     os.WriteFile("result.txt", []byte(com), 0o0666)
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement