Created
December 8, 2012 20:18
-
-
Save steos/4241752 to your computer and use it in GitHub Desktop.
minimal "language confluxer"
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
;; minimal "confluxer" | |
;; inspired by http://generators.christopherpound.com/ | |
(defn words [text] | |
(clojure.string/split text #"\s+")) | |
(defn triplets [s] | |
(partition 3 1 (clojure.string/capitalize (str s " ")))) | |
(defn conj-triplet [map trip] | |
(let [[fst snd trd] trip] | |
(update-in map [(str fst snd)] conj trd))) | |
(defn follow-map [str] | |
(reduce conj-triplet {} | |
(apply concat | |
(map triplets (words str))))) | |
(defn gen-word | |
([fm] | |
(let [start (rand-nth (filter | |
#(Character/isUpperCase (first %)) | |
(keys fm)))] | |
(gen-word fm start start))) | |
([fm last-pair buffer] | |
(let [next (rand-nth (fm last-pair))] | |
(if (= \space next) | |
(str buffer) | |
(recur fm | |
(str (second last-pair) next) | |
(str buffer next)))))) | |
(comment | |
(repeatedly 50 (partial gen-word (follow-map (slurp "/home/stefan/jap.txt")))) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment