Advertisement
Guest User

Untitled

a guest
Aug 27th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns dialog-app.core
  2.   (:gen-class))
  3.  
  4.  
  5. (defn switch-speaker [speaker w]
  6.   (swap! w assoc :who speaker))
  7.  
  8. (defn switch-to-student [w]
  9.   (switch-speaker :student w))
  10.  
  11. (defn switch-to-teacher [w]
  12.   (switch-speaker :teacher w))
  13.  
  14. (defn set-already-met [w]
  15.   (swap! w assoc :already-met true))
  16.  
  17. (defn set-teacher-continue [w]
  18.   (swap! w assoc :teacher-continue true))
  19.  
  20. (defn set-start-teaching [w]
  21.   (swap! w assoc :start-teaching true))
  22.  
  23. (defn unset-teacher-continue [w]
  24.   (swap! w assoc :teacher-continue false))
  25.  
  26. (def dialog-base
  27.   [{:who :teacher :context :shop :phrase "Hello, I'm Lingo!" :already-met false :update-funcs [set-teacher-continue]}
  28.    {: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]}
  29.    {:who :teacher :context :shop :phrase "Now I'll teach you how to buy things." :teacher-continue true}])
  30.  
  31.  
  32. (def world-state (atom {:who :teacher :context :shop :already-met false :start-teaching false}))
  33.  
  34. (defn score-attr [[name value] row]
  35.   (if (contains? row name) 1 0))
  36.  
  37. (defn score [q row]
  38.   (apply + (for [attr (map vector (keys q) (vals q))]
  39.              (score-attr attr row))))
  40.  
  41. (defn attr-match-or-empty? [[name value] row]
  42.   (or
  43.    (nil? (name row))
  44.    (= (name row) value)))
  45.  
  46. (defn all-attrs-match-or-empty [q row]
  47.   (for [attr (map vector (keys q) (vals q))]
  48.     (attr-match-or-empty? attr row)))
  49.  
  50.  
  51. (defn drop-other-attrs [q row]
  52.   (apply dissoc row (concat (keys q) [:phrase :update-funcs])))
  53.  
  54. (defn match-func [q row]
  55.   (and
  56.    (every? true? (all-attrs-match-or-empty q row))
  57.    (empty? (drop-other-attrs q row))))
  58.  
  59. (defn search-base [q]
  60.   (sort-by (comp - first)
  61.            (map
  62.             (fn [row] [((partial score q) row) row])
  63.             (filter (partial match-func q) dialog-base))))
  64.  
  65.  
  66. (defn candidates [world-state]
  67.   (let [search-results (search-base @world-state)]
  68.     (take-while
  69.      #(= (ffirst search-results) (first %))
  70.      search-results)))
  71.  
  72.  
  73. (defn dialog []
  74.   (let [candidates (candidates world-state)]
  75.     (if (empty? candidates)
  76.       (println "Nothing to say :(")
  77.       (do
  78.         (let [best (second (rand-nth candidates))]
  79.           (println (:phrase best))
  80.           (run! #(% world-state) (:update-funcs best)))))))
  81.  
  82.  
  83.  
  84. (defn -main
  85.   "I don't do a whole lot ... yet."
  86.   [& args]
  87.   (println "Hello, World!"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement