Exemple #1
0
func loadChain(c *markovianomatic.Chain, dbc *mgo.Collection) {
	iter := dbc.Find(bson.M{}).Iter()
	var node model.Node
	for iter.Next(&node) {
		c.Set(node.Key, node.Choices)
	}
	if err := iter.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "Error iterating the collection: %s\n", err.Error())
		os.Exit(1)
	}
}
Exemple #2
0
func main() {
	rand.Seed(time.Now().UnixNano())
	var numWords int
	var prefixLen int
	var file string
	var verbose bool

	app := cli.NewApp()
	app.Name = "Markovianomatic"
	app.Usage = "Build a random text with Markov-ish rules"

	app.Flags = []cli.Flag{
		cli.IntFlag{
			Name:        "words",
			Value:       100,
			Usage:       "maximum number of words to print",
			Destination: &numWords,
		},
		cli.IntFlag{
			Name:        "prefix",
			Value:       2,
			Usage:       "prefix Length in words",
			Destination: &prefixLen,
		},
		cli.StringFlag{
			Name:        "file",
			Value:       "",
			Usage:       "text file to use as seed",
			Destination: &file,
		},
		cli.BoolFlag{
			Name:        "verbose",
			Usage:       "I wanna read useless stuff",
			Destination: &verbose,
		},
	}

	app.Action = func(cc *cli.Context) {
		var c *markovianomatic.Chain
		cns, _ := model.Collections(model.Database())

		var ok bool
		var what = "append"
		if len(cns) == 0 {
			fmt.Fprintf(os.Stderr, "There are no available PrefixBase. Start from scratch\n")
			c = newEmptyChain(prefixLen, verbose)
		} else {
			fmt.Fprintf(os.Stdout, "Want to [u]se, [a]ppend, [d]elete an existing DB? [n]o(new) ")
			ok, what = askForConfirmation()
			if ok {
				i := chooseCollection(cns)
				c = load(prefixLen, verbose, cns[i])
			} else {
				c = newEmptyChain(prefixLen, verbose)
			}
		}

		if what == "append" {
			if len(file) == 0 {
				c.Build(os.Stdin)
			} else {
				c.Load(file)
			}
			if len(c.Keys()) > 0 {
				if verbose {
					c.Pretty()
				}
				c.Save()
			} else {
				fmt.Fprintf(os.Stdout, "Empty text map. Cannot generate text\n")
				os.Exit(1)
			}
		}

		b := new(bytes.Buffer)
		c.Generate(b, numWords)
		fmt.Println(b.String())
	}

	app.Run(os.Args)
}