├── README.md ├── main.go └── markov.go /README.md: -------------------------------------------------------------------------------- 1 | ## Marc 2 | 3 | The markov-chain-junk-text-generator. Feed it a filepath to a text file, and it'll generate pseudo-real text, based upon the corpus that it was provided. Good fun. An example below. 4 | 5 | > "Jonathan Ive has reluctantly accepted the services of a chimpanzee, and a half billion things designed by Ive." 6 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | fpath := flag.String("f", "", "The filepath of the text file") 11 | length := flag.Int("length", 10, "The length of sentence in words") 12 | flag.Parse() 13 | 14 | if *fpath == "" { 15 | fmt.Println("[Error] I need a valid filepath in order to be able to spew junk.") 16 | return 17 | } 18 | 19 | m := NewMarkov() 20 | err := m.Seed(*fpath) 21 | if err != nil { 22 | fmt.Println("[Error] Could not open file, check your filepath is all good.") 23 | } 24 | 25 | for { 26 | fmt.Println(m.GenerateText(*length)) 27 | time.Sleep(5 * time.Second) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /markov.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io/ioutil" 5 | "math/rand" 6 | "strings" 7 | "time" 8 | ) 9 | 10 | type Markov struct { 11 | words []string 12 | cache map[string][]string 13 | } 14 | 15 | // Returns a pointer to a new Markov instance. 16 | func NewMarkov() *Markov { 17 | return &Markov{ 18 | words: make([]string, 0), 19 | cache: make(map[string][]string), 20 | } 21 | } 22 | 23 | // Takes a slice of slice of words and seeds internal word list to choose from. 24 | func (m *Markov) Seed(filename string) error { 25 | dat, err := ioutil.ReadFile(filename) 26 | if err != nil { 27 | return err 28 | } 29 | m.words = strings.Split(string(dat), " ") 30 | 31 | for i := range m.words { 32 | triple := m.words[i : i+3] 33 | if i >= len(m.words)-3 { 34 | return nil 35 | } 36 | 37 | one := triple[0] 38 | two := triple[1] 39 | key := one + two 40 | _, ok := m.cache[key] 41 | if !ok { 42 | m.cache[key] = []string{} 43 | } 44 | 45 | m.cache[key] = append(m.cache[key], triple[2]) 46 | } 47 | 48 | return nil 49 | } 50 | 51 | // Generate random text from our cache. 52 | func (m *Markov) GenerateText(size int) string { 53 | rand.Seed(time.Now().Unix()) 54 | seed := rand.Intn(len(m.words) - 3) 55 | 56 | // Select words randomly. 57 | genWords := []string{} 58 | w1 := m.words[seed] 59 | w2 := m.words[seed+1] 60 | 61 | for i := 0; i < size; i++ { 62 | w1 = strings.TrimSpace(w1) 63 | genWords = append(genWords, w1) 64 | key := w1 + w2 65 | res, ok := m.cache[key] 66 | if !ok { 67 | continue 68 | } 69 | tempw2 := res[rand.Intn(len(res))] 70 | w1 = w2 71 | w2 = tempw2 72 | } 73 | genWords = append(genWords, w2) 74 | return strings.Join(genWords, " ") 75 | } 76 | --------------------------------------------------------------------------------