Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
38 lines (30 sloc) 1009 Bytes
(ns markov.core
(:require clojure.string))
;; I/O with the DOM
(defn input []
(.-value (.getElementById js/document "text")))
(defn output [s]
(set! (.-innerHTML (.getElementById js/document "output")) s))
;; Markov chain generation
(defn markov-data [text]
(let [maps
(for [line (clojure.string/split text #"\.")
m (let [l (str line ".")
(cons :start (clojure.string/split l #"\s+"))]
(for [p (partition 2 1 (remove #(= "" %) words))]
{(first p) [(second p)]}))]
(apply merge-with concat maps)))
(defn sentence [data]
(loop [ws (data :start)
acc []]
(let [w (rand-nth ws)
nws (data w)
nacc (concat acc [w])]
(if (= \. (last w))
(clojure.string/join " " nacc)
(recur nws nacc)))))
(defn generate []
(let [d (markov-data (input))]
(output (clojure.string/join " " (take 6 (repeatedly #(sentence d)))))))