Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns dialog-app.core
- (:gen-class))
- (defn switch-speaker [speaker w]
- (swap! w assoc :who speaker))
- (defn switch-to-student [w]
- (switch-speaker :student w))
- (defn switch-to-teacher [w]
- (switch-speaker :teacher w))
- (defn set-already-met [w]
- (swap! w assoc :already-met true))
- (defn set-teacher-continue [w]
- (swap! w assoc :teacher-continue true))
- (defn set-start-teaching [w]
- (swap! w assoc :start-teaching true))
- (defn unset-teacher-continue [w]
- (swap! w assoc :teacher-continue false))
- (def dialog-base
- [{:who :teacher :context :shop :phrase "Hello, I'm Lingo!" :already-met false :update-funcs [set-teacher-continue]}
- {:who :teacher :context :shop :phrase "I will guide you through this!" :teacher-continue true :already-met false :update-funcs [set-already-met set-start-teaching]}
- {:who :teacher :context :shop :phrase "Now I'll teach you how to buy things." :teacher-continue true}])
- (def world-state (atom {:who :teacher :context :shop :already-met false :start-teaching false}))
- (defn score-attr [[name value] row]
- (if (contains? row name) 1 0))
- (defn score [q row]
- (apply + (for [attr (map vector (keys q) (vals q))]
- (score-attr attr row))))
- (defn attr-match-or-empty? [[name value] row]
- (or
- (nil? (name row))
- (= (name row) value)))
- (defn all-attrs-match-or-empty [q row]
- (for [attr (map vector (keys q) (vals q))]
- (attr-match-or-empty? attr row)))
- (defn drop-other-attrs [q row]
- (apply dissoc row (concat (keys q) [:phrase :update-funcs])))
- (defn match-func [q row]
- (and
- (every? true? (all-attrs-match-or-empty q row))
- (empty? (drop-other-attrs q row))))
- (defn search-base [q]
- (sort-by (comp - first)
- (map
- (fn [row] [((partial score q) row) row])
- (filter (partial match-func q) dialog-base))))
- (defn candidates [world-state]
- (let [search-results (search-base @world-state)]
- (take-while
- #(= (ffirst search-results) (first %))
- search-results)))
- (defn dialog []
- (let [candidates (candidates world-state)]
- (if (empty? candidates)
- (println "Nothing to say :(")
- (do
- (let [best (second (rand-nth candidates))]
- (println (:phrase best))
- (run! #(% world-state) (:update-funcs best)))))))
- (defn -main
- "I don't do a whole lot ... yet."
- [& args]
- (println "Hello, World!"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement