Last active
November 6, 2015 09:58
-
-
Save qwtel/d19a622ffd734a934c03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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