From 582568e92803522a195950e02cb1091730c6f60e Mon Sep 17 00:00:00 2001 From: sndnvaps Date: Sat, 30 Nov 2019 18:57:41 +0800 Subject: [PATCH] Add golang version of BullshitGenerator --- BullshitGenerator.go | 113 +++++++++++++++++++++++++++++++++++++++++++ rand_linux.go | 22 +++++++++ rand_windows.go | 19 ++++++++ readJSON.go | 33 +++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 BullshitGenerator.go create mode 100644 rand_linux.go create mode 100644 rand_windows.go create mode 100644 readJSON.go diff --git a/BullshitGenerator.go b/BullshitGenerator.go new file mode 100644 index 000000000..f27bf221e --- /dev/null +++ b/BullshitGenerator.go @@ -0,0 +1,113 @@ +package main + +import ( + "fmt" + "log" + "os" + "regexp" + "time" + + "github.com/urfave/cli" +) + +//随便取一句 从列表当中 +func PickUpOneSentence(list []string) string { + index := RandInt(0, len(list)) + tmp := list[index] + return tmp +} + +//来点名人名言 +func (this BoshJson) PickUpOneFamous() string { + tmp := PickUpOneSentence(this.Famous) + reg_a := regexp.MustCompile(`a`) + reg_b := regexp.MustCompile(`b`) + tmp = reg_a.ReplaceAllString(tmp, PickUpOneSentence(this.After)) + tmp = reg_b.ReplaceAllString(tmp, PickUpOneSentence(this.Before)) + return tmp +} + +//来点废话,MainTheme是需要更换的主题 +func (this BoshJson) PickupOneBosh(MainTheme string) string { + tmp := PickUpOneSentence(this.Bosh) + reg_x := regexp.MustCompile(`x`) + tmp = reg_x.ReplaceAllString(tmp, MainTheme) + return tmp +} + +//增加段落 +func AddNewPhrase(Content string) string { + Content = Content + "。 " + Content = Content + "\r\n" + Content = Content + " " + return Content +} + +func BullshitGenerator(c *cli.Context) error { + var Article string = "" //文章 + var Content string = "" //章节 + var ContentLength int = 0 //章节长度 + + //var MainTheme string = "一天掉多少根头发" //主题 + MainTheme := c.String("bush") //根据命令行传入的参数来定义主题 + + var Sentence string = "" //句子 + bosh := ReadJson("data.json") + Article = MainTheme //文章开头为主题 + Article = AddNewPhrase(Article) //主题后,需要新建一段 + for { + index := RandInt(0, 100) //随机取一个数,用做段落数 + if index < 5 && ContentLength > 200 { + Content = AddNewPhrase(Content) + Article = Article + Content + Content = "" + } else if index < 20 { + Sentence = bosh.PickUpOneFamous() + ContentLength = ContentLength + len(Sentence) + Content = Content + Sentence + + } else { + Sentence = bosh.PickupOneBosh(MainTheme) + ContentLength = ContentLength + len(Sentence) + Content = Content + Sentence + } + if ContentLength > 6000 { //当文章长度超过6000字时退出 + break + } + + } + Content = AddNewPhrase(Content) + Article = Article + Content + Article = Article + "\n" + + fmt.Println(Article) + return nil +} + +func main() { + app := cli.NewApp() + app.Name = "golang BullshitGenerator" + app.Compiled = time.Now() + app.Version = "1.0.0" + app.Authors = []cli.Author{ + cli.Author{ + Name: "Jimes Yang", + Email: "sndnvaps@gmail.com", + }, + } + app.Copyright = "(c) 2019 Jimes Yang" + app.Usage = "只用来生成废话文章。仅供用做娱乐,不可乱用!" + app.Action = BullshitGenerator + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "bush,b", + Value: "一个人每天掉多少头发", //设置默认主题,当没有输入 自定义主题的时候使用 + Usage: "定义文章的主题", + }, + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} diff --git a/rand_linux.go b/rand_linux.go new file mode 100644 index 000000000..c1156c43e --- /dev/null +++ b/rand_linux.go @@ -0,0 +1,22 @@ +// +build linux + +package main + +import ( + "math/rand" + "time" +) + +//get the random numer in [min, max] +func RandInt(min, max int) int { + if min >= max || max == 0 { + return max + } + rand.Seed(time.Now().UnixNano()) + + //x := r.Intn(max-min) + min + x := rand.Intn(max-min) + min + + //fmt.Println("RandInt: = ",x) + return x +} diff --git a/rand_windows.go b/rand_windows.go new file mode 100644 index 000000000..2361caf00 --- /dev/null +++ b/rand_windows.go @@ -0,0 +1,19 @@ +// +build windows + +package main + +import ( + "math/rand" +) + +//get the random numer in [min, max] +func RandInt(min, max int) int { + if min >= max || max == 0 { + return max + } + + x := rand.Intn(max-min) + min + + //fmt.Println("RandInt: = ",x) + return x +} diff --git a/readJSON.go b/readJSON.go new file mode 100644 index 000000000..e20f32607 --- /dev/null +++ b/readJSON.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "os" +) + +type BoshJson struct { + Title string `json:"title,omitempty"` + Famous []string `json:"famous,omitempty"` + Bosh []string `json:"bosh,omitempty"` + After []string `json:"after,omitempty"` + Before []string `json:"before,omitempty"` +} + +func ReadJson(filename string) BoshJson { + var BoshJsons BoshJson + file, err := os.Open(filename) + checkErr(err) + b, err := ioutil.ReadAll(file) + checkErr(err) + + err = json.Unmarshal(b, &BoshJsons) + checkErr(err) + return BoshJsons +} + +func checkErr(err error) { + if err != nil { + panic(err) + } +}