-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (122 loc) · 2.99 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bufio"
"embed"
"fmt"
"github.com/spf13/cobra"
"math/rand"
"os"
"strings"
)
//go:embed resources
var f embed.FS
func main() {
var rootCmd = &cobra.Command{
Use: "yogev",
Short: "Yogev is a basic command line tool for education and facts",
}
// Define a command to fetch a fact
var factCmd = &cobra.Command{
Use: "fact",
Short: "Get a random fact",
Run: func(cmd *cobra.Command, args []string) {
generateFact()
},
}
rootCmd.AddCommand(factCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func generateFact() {
template := randomLine("templates")
words := strings.Fields(template)
// Replace placeholders with random words
for i, word := range words {
trimmedWord := strings.Trim(word, ".,;:!?)}")
switch trimmedWord {
case "%n":
words[i] = randomLine("nouns")
case "%ns":
words[i] = pluralizeNoun(randomLine("nouns"))
case "%vs":
words[i] = randomLine("verbs")
case "%v":
words[i] = thridPersoniseVerb(randomLine("verbs"))
case "%a":
words[i] = randomLine("adjectives")
case "%num":
words[i] = fmt.Sprintf("%d", rand.Intn(1000000))
case "%per":
words[i] = fmt.Sprintf("%d%%", rand.Intn(100))
}
}
fmt.Println(strings.Join(words, " "))
}
func randomLine(wordType string) string {
file, err := f.ReadFile(fmt.Sprintf("resources/%s/%s.txt", wordType, "english"))
if err != nil {
panic("Got an error. Very weird..." + err.Error())
}
lines, err := readLines(file)
if err != nil {
panic("Got an error. Very weird..." + err.Error())
}
return lines[rand.Intn(len(lines))]
}
func readLines(file []byte) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(strings.NewReader(string(file)))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func pluralizeNoun(noun string) string {
if strings.HasSuffix(noun, "s") || strings.HasSuffix(noun, "sh") || strings.HasSuffix(noun, "ch") || strings.HasSuffix(noun, "x") || strings.HasSuffix(noun, "z") {
return noun + "es"
}
if strings.HasSuffix(noun, "y") && !isVowelBeforeY(noun) {
return noun[:len(noun)-1] + "ies"
}
if strings.HasSuffix(noun, "f") {
return noun[:len(noun)-1] + "ves"
}
if strings.HasSuffix(noun, "fe") {
return noun[:len(noun)-2] + "ves"
}
switch noun {
case "child":
return "children"
case "mouse":
return "mice"
case "sheep":
return "sheep"
default:
return noun + "s"
}
}
func thridPersoniseVerb(verb string) string {
if strings.HasSuffix(verb, "s") || strings.HasSuffix(verb, "sh") || strings.HasSuffix(verb, "ch") || strings.HasSuffix(verb, "x") || strings.HasSuffix(verb, "z") {
return verb + "es"
}
if strings.HasSuffix(verb, "y") && !isVowelBeforeY(verb) {
return verb[:len(verb)-1] + "ies"
}
return verb + "s"
}
func isVowelBeforeY(noun string) bool {
if len(noun) < 2 {
return false
}
switch noun[len(noun)-2] {
case 'a', 'e', 'i', 'o', 'u':
return true
default:
return false
}
}