diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-09-01 19:13:48 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-09-01 20:20:56 -0400 |
| commit | 3a18f19dffeff93ddd2d3fb0ae8c8cd8b877241a (patch) | |
| tree | 562e35d10ef91480210a525e1221228641bbad2a /markov.go | |
| parent | b60931fceea5fa0661ff18da13d324c0136897f7 (diff) | |
| download | markov-3a18f19dffeff93ddd2d3fb0ae8c8cd8b877241a.zip | |
add 'max number of output words' command line arg
Diffstat (limited to 'markov.go')
| -rw-r--r-- | markov.go | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -2,13 +2,14 @@ // prose based on the input text. It works by breaking each phrase into two parts: // a multi-word prefix, and a single word suffix. It generates output by randomly // choosing a suffix that follows the prefix. -// +// // The program is based on Chapter 3 of "The Practice of Programming" by Brian Kernighan // and Rob Pike. package main import ( "bufio" + "flag" "fmt" "io" "math/rand" @@ -16,8 +17,8 @@ import ( ) const ( - nPref = 2 // number of prefix words - maxGen = 10000 // maximum number of words to generate + nPref = 2 // number of prefix words + maxGenDefault = 10000 // maximum number of words to generate ) type Prefix struct { @@ -42,8 +43,11 @@ func (p *Prefix) push(word string) { type StateMap map[Prefix][]string func main() { + maxGen := flag.Int("w", maxGenDefault, "maximum number of words to generate") + flag.Parse() + states := build(os.Stdin) - generate(states, maxGen) + generate(states, *maxGen) } // build populates a StateMap with words read from `in'. @@ -64,10 +68,10 @@ func build(in io.Reader) StateMap { return states } -// generate produces at most `nWords' of output, one word per line. -func generate(states StateMap, nWords int) { +// generate produces at most `maxWords' of output, one word per line. +func generate(states StateMap, maxWords int) { prefix := Prefix{} - for i := 0; i < maxGen; i++ { + for i := 0; i < maxWords; i++ { suffixes := states[prefix] suffix := suffixes[rand.Intn(len(suffixes))] if suffix == "" { // ending state |