Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Clojure Markov Chain Example
(ns markov-chain
(:require [clojure.string :as string]))
(defn simple-weighted-rand-nth [m]
(let [ks (keys m)
vs (map m ks) ; Ensure map value ordering
ranges (reductions + vs)
x (rand-int (last ranges))
i (count (take-while #(<= % x) ranges))
k (nth ks i)]
[k (m k)]))
(defn gen-probability [data]
(let [note-occurence (fn [m c] (update-in m c #(inc (or % 0))))
d (concat [:start] data)]
(reduce note-occurence {} (partition 2 1 d))))
(defn gen-markov-chain-seq
([training-data] (gen-markov-chain-seq nil (gen-probability training-data)))
([c prob-map]
(let [[newc p] (simple-weighted-rand-nth (prob-map (or c :start)))]
(when newc
(cons newc (lazy-seq (gen-markov-chain-seq newc prob-map)))))))
;; Letter Chaining
(->> "at noon you can"
gen-markov-chain-seq
(take 6)
(apply str)) ;; "at nou"
;; Word Chaining
(def data "ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. et harum quidem rerum facilis est et expedita distinctio. nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. ")
(as-> data x
(string/split x #"\b")
(filter not-empty x)
(gen-markov-chain-seq x)
(take 15 x)
(apply str x)) ;; "ut tenetur in in esse, qui minus aut"
;; Function Chaining
(defn f1 [a] (assoc a :a 1))
(defn f2 [a] (assoc a :b 1))
(defn f3 [a] "f3")
(defn f4 [a] (inc a))
(defn f5 [a] a)
(defn f6 [a] 1)
(defn f7 [a] 5)
(defn f8 [a] {:a a})
(defn f9 [a] (str a))
(->> [f1 f6 f4 f7 f8 f2 f1 f1 f2 f9 f5 f6 f3 f8 f2 f6]
gen-markov-chain-seq
(take 7)
(reduce #(%2 %1) nil)) ;; {:a "f3"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment