                     (:backwardtable state)
                     (:forwardtable state)))))))
         (let [filtered-grams
-                (remove
-                    (fn [s] (empty? (remove
-                        (fn [k] (empty? (filter #(.contains % k) (remove keyword? s))))
-                        keywords)))
-                    (:n-grams state))]
+                (apply concat
+                    (map
+                        (fn [k]
+                            (subseq (:n-grams state)
+                                >= (extend-vec [k] "" prefix-length)
+                                < (extend-vec [(append-null k)] "" prefix-length)))
+                        keywords))]
             (if (empty? filtered-grams)
                 (generate-sentence state [])
                 (generate-sentence

(def empty-state {
    :forwardtable {}
    :backwardtable {}
    :n-grams []
})



            (reverse (take 500 (stream-of-shite (reverse seed) backward))))
        (take 500 (stream-of-shite seed forward))))

(defn extend-vec [v e n] (vec (concat v (take (- n (count v)) (repeat e)))))
(defn append-null [s] (join (concat (seq s) [(char 0)])))

(defn generate-sentence [state keywords]
    {
        :pre [(or (empty? keywords) (sorted? (:n-grams state)))]
    }
    (if (empty? keywords)
        (remove keyword? (map first
            (apply max-key #(count (filter true? (map second %)))
