app.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "unicode"
  6. )
  7. func encodeWord(word []rune) []rune {
  8. wordLength := len(word)
  9. if wordLength < 4 {
  10. return word
  11. }
  12. if wordLength == 4 {
  13. word[1], word[2] = word[2], word[1]
  14. return word
  15. }
  16. toShuffle := word[1 : wordLength-1]
  17. toShuffleLength := wordLength - 2
  18. rand.Shuffle(toShuffleLength, func(i, j int) {
  19. toShuffle[i], toShuffle[j] = toShuffle[j], toShuffle[i]
  20. })
  21. return word
  22. }
  23. func processText(text []rune) string {
  24. var currentWord []rune
  25. var newString []rune
  26. for _, item := range text {
  27. if unicode.IsPunct(item) || unicode.IsSpace(item) {
  28. currentWord = encodeWord(currentWord)
  29. for _, letter := range currentWord {
  30. newString = append(newString, letter)
  31. }
  32. currentWord = []rune{}
  33. newString = append(newString, item)
  34. continue
  35. }
  36. currentWord = append(currentWord, item)
  37. }
  38. return string(newString)
  39. }
  40. func main() {
  41. test := `This is a long looong test sentence,
  42. with some big (biiiiig) words!`
  43. fmt.Println(processText([]rune(test)))
  44. }