Skip to content

Instantly share code, notes, and snippets.

@steos
Created December 8, 2012 20:18
Show Gist options
  • Save steos/4241752 to your computer and use it in GitHub Desktop.
Save steos/4241752 to your computer and use it in GitHub Desktop.
minimal "language confluxer"
;;;; minimal "confluxer"
;;;; inspired by http://generators.christopherpound.com/
(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]
(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 buf)
(recur fm
(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)))
)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment