Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加Go语言版本 BushitGenerator #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions BullshitGenerator.go
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
},
}
app.Copyright = "(c) 2019 Jimes Yang<[email protected]>"
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)
}
}
22 changes: 22 additions & 0 deletions rand_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions rand_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions readJSON.go
Original file line number Diff line number Diff line change
@@ -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)
}
}