Skip to content

Instantly share code, notes, and snippets.

@steos
Created December 8, 2012 20:18

Revisions

  1. steos revised this gist Dec 9, 2012. 1 changed file with 50 additions and 29 deletions.
    79 changes: 50 additions & 29 deletions conflux.clj
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,60 @@
    ;; minimal "confluxer"
    ;; inspired by http://generators.christopherpound.com/
    ;;;; minimal "confluxer"
    ;;;; inspired by http://generators.christopherpound.com/

    (defn words [text]
    (clojure.string/split text #"\s+"))
    (require '(clojure [string :as str]))

    (defn words [text] (str/split text #"\s+"))

    (defn- word-triplets [s]
    (partition 3 1 (str/capitalize (str 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))]
    (apply concat (map word-triplets (words s))))

    (defn group-with [kf vf coll]
    (reduce (fn [acc x]
    (update-in acc
    [(kf x)]
    (fnil conj [])
    (vf x)))
    {}
    coll))

    (defn follow-map [s]
    (group-with #(apply str (butlast %))
    #(last %)
    (triplets s)))

    (defn- make-word
    ([fm pair]
    (make-word fm pair pair))
    ([fm pair buf]
    (let [next (rand-nth (fm pair))]
    (if (= \space next)
    (str buffer)
    (str buf)
    (recur fm
    (str (second last-pair) next)
    (str buffer next))))))
    (str (second pair) next)
    (str buf next))))))

    (defn cap-first? [str] (Character/isUpperCase (first str)))

    (defn gen-words
    ([fm n]
    (let [starts (filter cap-first? (keys fm))]
    (repeatedly n #(make-word fm (rand-nth starts))))))

    (defn gen-word [m] (first (gen-words m 1)))

    ;; for the repl
    (comment (do
    (def s (slurp "jap.txt"))
    (def m (follow-map s))
    (def f (partial gen-words m))
    (defn do'em [n] (doall (f n)))
    (defn time'em [n] (do (time (do'em n)) nil)))
    )


    (comment
    (repeatedly 50 (partial gen-word (follow-map (slurp "./jap.txt"))))
    )



  2. steos revised this gist Dec 8, 2012. 2 changed files with 17 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion conflux.clj
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@


    (comment
    (repeatedly 50 (partial gen-word (follow-map (slurp "/home/stefan/jap.txt"))))
    (repeatedly 50 (partial gen-word (follow-map (slurp "./jap.txt"))))
    )


    16 changes: 16 additions & 0 deletions jap.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    agatamori akimitsu akira arinori azumabito bakin benkei buntaro
    chikafusa chikayo chomei chuemon dosan emishi emon fuhito fujifusa
    fujitaka fususaki gekkai gennai gidayu gongoro hakatoko hamanari
    haruhisa hideharu hideo hidetanda hideyoshi hirohito hirotsugu
    hitomaru iemitsu ienobu ieyasu ieyoshi imoko issai iwao iwazumi jikkyo
    jozen junkei jussai kageharu kagemasa kagemusha kahei kanemitsu
    katsumi katsuyori kazan kazunori keisuke kintaro kiyomori kiyosuke
    kmako komaro koremasa koreyasu kuronushi kyuso mabuchi maro masahide
    masamitsu michifusa mitsukane miyamoto mochiyo morinaga munetaka
    murashige nagafusa nagate nakahira nambo naoshige narihiro oguromaro
    okimoto okura omaro otondo razan rikyu rokuemon ryokai sadakata
    sanehira sanetomo sanzo saru shigenobu shigeuji shingen shoetsu shozen
    sukemasa tadabumi tadashiro takatoshi tameyori taneo taneyoshi tensui
    togama tomomasa toshifusa toyonari tsunayoshi tsunetane uchimaro
    ujihiro umakai watamaro yakamochi yasumori yoriie yoritomo yoshiie
    yoshisune yoshitane yoshizumi yukihira zuiken
  3. steos created this gist Dec 8, 2012.
    39 changes: 39 additions & 0 deletions conflux.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    ;; 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"))))
    )