Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created November 21, 2013 02:20
Show Gist options
  • Save skatenerd/7575047 to your computer and use it in GitHub Desktop.
Save skatenerd/7575047 to your computer and use it in GitHub Desktop.
make gibberish
(defn update-the-submap [m word]
(update-in m [word] (fnil inc 0)))
(defn update-the-map [m triplet]
(let [thekey (take 2 triplet)
theval (last triplet)]
(update-in m [thekey] (fnil #(update-the-submap % theval) {}))))
(def thetext '("hello" "world" "dogs" "cats" "computer" "mouse" "hello" "world" "cats" "cats" "dogs" "cats" "food" "hello" "world" "dogs"))
(def offsettext (conj thetext nil))
(def doubleoffsettext (conj offsettext nil))
(def zipped (map list doubleoffsettext offsettext thetext))
(def triplets (rest (rest zipped)))
(def counts-map (reduce update-the-map {} triplets))
(defn random-selection [weight-map]
(let [sublists (map (fn [[word times]] (repeat times word)) weight-map)
to-choose-from (reduce concat sublists)]
(rand-nth to-choose-from)))
(defn word-for-pair [m pair]
(random-selection (get m pair (rand-nth (vals m)))))
(def gibberish (iterate #(conj % (word-for-pair counts-map (take 2 %))) '("world" "hello")))
(prn (reverse (nth gibberish 10)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment