 (def corpus-file "corpus.txt")
 (def word-pattern #"\S+")
 (def generated-sentence-length 10)
+(def prefix-length 3)
 
 (def table (ref {}))
 (def starters (ref []))

        (if (< i weight)
            word
            (recur (rest c) (- i weight)))))
(defn pick-next-word [a b t]
    (when-let [candidates (t [a b])]
        (select candidates
            (rand-int (reduce + 0 (vals candidates))))))
(defn further-stream-of-shite [a b t]
    (lazy-seq
        (when-let [c (pick-next-word a b t)]
            (cons c (further-stream-of-shite b c t)))))
(defn stream-of-shite [a b t]
    (concat [a b] (further-stream-of-shite a b t)))
(defn generate-sentence []
    (let [[a b] (nth @starters (rand-int (count @starters)))]
        (take generated-sentence-length (stream-of-shite a b @table))))

(defn value-or-default [value default]
    (if (nil? value) default value))

    (assoc m
        c
        (inc (value-or-default (m c) 0))))
(defn update-transition [prefix c t]
    (assoc t
        prefix
        (update-count
            (value-or-default (t prefix) {})
            c)))
(defn update-table [table words]
    (if (> (count words) prefix-length)
        (let [[prefix [c & _]] (split-at prefix-length words)]
            (recur (update-transition prefix c table) (rest words)))
        table))
(defn update-message! [message]
    (dosync
        (let [words (split-sentence-to-words message)]
            (ref-set table (update-table @table words))
            (if (>= (count words) prefix-length)
                (ref-set starters (conj @starters (take prefix-length words)))))))
(defn log-sentence! [message]
    (with-open [out (writer corpus-file :append true)]
        (.write out (str message "\n"))
