Skip to content

Commit

Permalink
Merge pull request #1 from roycald245/dev
Browse files Browse the repository at this point in the history
add statistical fact ability
  • Loading branch information
roycald245 authored Jul 23, 2024
2 parents 742dd60 + 05162b0 commit 36324f3
Show file tree
Hide file tree
Showing 7 changed files with 3,567 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/roycald245/homebrew-yogev

go 1.22.2

require github.com/spf13/cobra v1.8.1

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
153 changes: 153 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package main

import (
"bufio"
"fmt"
"github.com/spf13/cobra"
"math/rand"
"os"
"strings"
)

// Define a list of templates
//var templates = []string{
// "Every %a %n %v a %a %n in a %per %a %n.",
// "%a %n %v %n with %per %a %n.",
// "A %n only has %num %ns.",
// "In the %n, the %n %v %per.",
// "About %per of the %ns %vs %per of the time.",
// "%a %n %v %per of the %ns.",
// "A %n %v with %per %a %n.",
//}

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()
},
}

// Add the fact command to the root command
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 := os.Open(fmt.Sprintf("./resources/%s/%s.txt", wordType, "english"))
if err != nil {
panic("Got an error. Very weird...")
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
panic("Got an error. Very weird...")
}
}(file)

lines, err := readLines(file)
if err != nil {
fmt.Println("Error reading lines:", err)
panic("Got an error. Very weird...")
}
return lines[rand.Intn(len(lines))]
}

func readLines(file *os.File) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(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
}
}
Loading

0 comments on commit 36324f3

Please sign in to comment.