Skip to content

Instantly share code, notes, and snippets.

@Gonzih
Last active March 9, 2017 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Gonzih/37e7353bcf300b626c5afd9ec16e404b to your computer and use it in GitHub Desktop.
Save Gonzih/37e7353bcf300b626c5afd9ec16e404b to your computer and use it in GitHub Desktop.
markov-chain-meetup-dojo
(ns markov-chain.core
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(defn read-input []
(-> "input"
io/resource
slurp
(string/split #"[\n\r\s]+")))
(defn generate-model [words]
(reduce (fn [m [f s]] (update m f conj s))
{}
(partition 2 1 words)))
(defn generate-sentence [start-word model]
(loop [output ""
next-word start-word]
(if (= \. (last next-word))
(str output " " next-word)
(recur (format "%s %s" output next-word)
(rand-nth (get model next-word))))))
(->> (read-input)
(map #(string/replace % #"[()\"]*" ""))
(filter #(> (count %) 0 ))
generate-model
(generate-sentence "Alice")
string/triml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment