 (defn run-test
   []
   (let [text (slurp "./src/cljc/algorithms_clj/markov-chain-input.edn")
-        markov (read-transitions (split-words text) 1)]
+        markov (transition->markov-chain (read-transitions (split-words text) 1))]
     (str/join " "
       (take 100 (random-map-walk markov)))
     ))

        (if (< choice w) val (recur more))))))


;; Construct the transitions
;; TODO - have a specific transducer for this

(defn read-transitions

      (partition 2 1 (partition memory 1 token-seq)))))


;; Transformation into a markov chain

(defn transition->markov-chain
  [transitions]
  (letfn [(weight-key [[curr nexts]] [curr (count nexts)])]
    {:weighted-keys (into {} (map weight-key) transitions)
     :transitions transitions}))


;; Random generation based on initial values

(defn random-jump
  [{:keys [weighted-keys transitions]} curr]
  (let [possibles (or (get transitions curr) weighted-keys)]
    (weighted-rand possibles)))

(defn random-walk-from
  [markov-chain curr]
  (letfn [(go [curr]
            (cons (first curr)
              (lazy-seq
                (go (random-jump markov-chain curr)))))]
    (go curr)))

(defn random-map-walk
  [markov-chain]
  (random-walk-from markov-chain
    (weighted-rand (:weighted-keys markov-chain))))


;; Test case
