      :counted counted}))
 
 (defn corpus-chain
+  "Takes an array of requests, returns a lazy seq of the chain"
   [corpus]
   (-> corpus
       preprocess-corpus

            [cheshire.core :as json])
  (:import java.util.TreeMap))

(defn compile-transitions
  "Compile a system's transitions into a format suitable for efficient markov chain generation. Internally this creates a lookup table of URLs with treemaps used to map probabilities onto numerical ranges."
  [transitions]
  (doall
   (reduce
    (fn [out [url-id edges]]
      (assoc out
        url-id
        (let [m (TreeMap.)]
          (reduce
           (fn [offset [edge-url-id weight]]
             (let [off-ceil (+ offset weight)]
               (.put m off-ceil edge-url-id)
               off-ceil))
           0.0
           edges)
          m)))
    {}
    transitions)))

(defn chain
  "Returns a lazy sequence of requests. Not guaranteed to terminate."
  ([compiled]
     (chain compiled :rand))
  ([{:keys [transitions request-keys requests] :as compiled} request]
     (let [r (request-keys (rand-int (count request-keys)))]
       (if-let [edges (transitions r)]
         (let [e (.ceilingEntry edges (rand))]
           (if-let [e-req (.getValue e)]
             (lazy-seq (cons (first (requests e-req)) (chain compiled e-req)))
             (throw (Exception.  "This should never happen. The morkov chain is compromised!"))))
         (throw (Exception. (str "Could not find edge for: " request)))))))

(defn build-requests
  [parsed]

(ns engulf.formulas.markov-requests
  "Support for generating markov chains of requests."
  ;; TODO: This file's a bit of a messy in terms of organization and naming
  (:use clojure.pprint
        [clojure.string :only [lower-case]]
        [clojure.walk :only [keywordize-keys]])
