Skip to content

Instantly share code, notes, and snippets.

Created July 30, 2014 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/10dc0db756867c4662bf to your computer and use it in GitHub Desktop.
Save anonymous/10dc0db756867c4662bf to your computer and use it in GitHub Desktop.
(ns jk.core)
(defn get-text []
(slurp "book.txt"))
(defn end-word? [word]
(boolean
(re-find #"\w+[.!?]" word)))
(defn get-words [text]
(re-seq #"[\w',.!?]+" text))
(defn build-chain [words]
(apply merge-with
concat
(map (fn [word next-word] {word [next-word]})
words
(next words))))
(defn build-words [chain]
(let [[word next-words] (rand-nth (seq chain))
next-word (rand-nth next-words)]
(loop [words [word next-word]]
(let [next-words (get chain (last words))]
(if (nil? next-words)
words
(let [next-word (rand-nth next-words)
words (conj words next-word)]
(if (end-word? next-word)
words
(recur words))))))))
(defn build-sentence [words]
(str
(clojure.string/capitalize (first words))
(clojure.string/join " " (rest words))))
(defn main []
(println
(->> (get-text)
(get-words)
(build-chain)
(build-words)
(build-sentence))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment