-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
46 lines (38 loc) · 829 Bytes
/
generator.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
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"time"
"github.com/julien-roland/go-randtext/randtext"
)
var number = flag.Uint("number", 1, "number of sentences")
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "generator: missing sample text")
os.Exit(1)
}
file, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "counter: %s\n", err)
os.Exit(1)
}
defer file.Close()
model := randtext.NewMarkovModel(file)
b := bufio.NewWriterSize(os.Stdout, 4096)
rand.Seed(time.Now().UnixNano())
for i := uint(0); i < *number; i++ {
fmt.Fprintln(b, model.Sentence())
}
b.Flush()
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: generator [options] sample")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
os.Exit(2)
}