-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
88 lines (59 loc) · 1.5 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"io/ioutil"
"regexp"
// "time"
"math/rand"
"strings"
)
// func randomPos(min, max int) (int) {
// result := rand.Intn(max - min) + min
// return result
// }
func main() {
// TODO: input and output paths should be command line arguments
inputFilePath := "in.txt"
// seed with non-predictable value
// rand.Seed(time.Now().UTC().UnixNano())
contentBytes, err := ioutil.ReadFile(inputFilePath)
if err != nil {
panic(err)
}
content := string(contentBytes)
result := shuffleSentenceParts(content)
//result := shuffleWords(content)
fmt.Println(result)
}
type wordFormatter func(string) string
func formatSentencePart(part string) string {
firstLetter := strings.ToUpper(part[:1])
result := firstLetter + part[1:] + "\n"
return result
}
func formatWordPart(part string) string {
result := part + " "
return result
}
func shuffleSentenceParts(input string) string {
regexSplit := ", |\\. | and | but | or | which |\\n"
result := shuffleParts(input, regexSplit, formatSentencePart)
return result
}
func shuffleWords(input string) string {
regexSplit := "\\s"
result := shuffleParts(input, regexSplit, formatWordPart)
return result
}
func shuffleParts(input, regexSplit string, formatter wordFormatter) string {
result := ""
regex := regexp.MustCompile(regexSplit)
parts := regex.Split(input, -1)
indexes := rand.Perm(len(parts))
for _, index := range indexes {
if len(parts[index]) > 0 {
result += formatter(parts[index])
}
}
return result
}