Skip to content

Instantly share code, notes, and snippets.

@qwtel
Last active November 6, 2015 09:58
Show Gist options
  • Save qwtel/d19a622ffd734a934c03 to your computer and use it in GitHub Desktop.
Save qwtel/d19a622ffd734a934c03 to your computer and use it in GitHub Desktop.
(defn generate-markov-nodes
[words]
(->>
words
(map str/lower-case)
(str/join " ")
(partition 3 1)
(map #(list (take 2 %1) (nth %1 2)))
(reduce
(fn [acc [l next-l]] (update-in acc [l next-l] (fnil inc 0)))
{}
)
)
)
(defn generate-usrname [nodes]
(loop [node (nodes (list \space \space))
acc []]
(let [probabilities (vec (vals node))
index (wrand probabilities)
letter (nth (keys node) index)
next-node-key (list (or (last acc) \space) letter)
next-node (nodes next-node-key)]
(if (= 6 (count acc))
(clojure.string/join acc)
(if (= 1 (count node))
(clojure.string/join acc)
(if (= letter \space)
(recur node acc)
(recur next-node (conj acc letter))
)
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment