                     (: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

            :backwardtable
                (update-table (:backwardtable state) (reverse words))
            :n-grams
                (update-ngrams (:n-grams state) words))))
(defn log-sentence! [message]
    (with-open [out (writer corpus-file :append true)]
        (.write out (str message "\n"))))

(def empty-state {
    :forwardtable {}
    :backwardtable {}
    :n-grams (sorted-set)
})


