Added variable "paths" to templates, like {person.age}.

This commit is contained in:
John Wiseman 2020-02-05 21:24:33 -08:00
parent dd2c646e3f
commit 567cf8a4f9
3 changed files with 58 additions and 41 deletions

View File

@ -2,7 +2,10 @@
(:require
[clojure.math.combinatorics :as combo]
[clojure.string :as string]
[instaparse.core :as insta]))
[instaparse.core :as insta]
[lemondronor.circlebot.logging :as logging]))
(logging/deflog "generation" logger)
(def ^:private %parse-template
@ -47,14 +50,6 @@
(defmulti expand% (fn [template data] (first template)))
;; (defmethod expand% :varref [template data]
;; (let [var (keyword (second template))
;; var-path (string/split var #"\.")
;; val (get-in data var-path)]
;; (if (or (nil? val) (= val ""))
;; '()
;; (list {:varrefs [var] :text (str (data var))}))))
(def vowels #{"a" "e" "i" "o" "u"})
(defn is-consonant? [c]
@ -80,7 +75,9 @@
(let [filter (filters (keyword filter-name))]
(when-not filter
(throw (js/Error. (str "Unknown filter: " filter-name))))
(filter s))))
(let [fv (filter s)]
;;(log-info "Applied filter %s to %s: %s" filter-name s fv)
fv))))
(defn parse-varref [s]
@ -88,13 +85,25 @@
{:var (keyword var) :filter filter}))
;; (defmethod expand% :varref [template data]
;; (let [varref (second template)
;; {:keys [var filter]} (parse-varref varref)
;; val (data var)]
;; (if (or (nil? val) (= val ""))
;; '()
;; (list {:varrefs [var] :text (apply-filter (str (data var)) filter)}))))
(defmethod expand% :varref [template data]
(let [varref (second template)
{:keys [var filter]} (parse-varref varref)
val (data var)]
var-path (map keyword (string/split (name var) #"\."))
val (get-in data var-path)
;;_ (println "WOO template:" template "var:" var "var-path:" var-path "data:" data "val:" val)
]
(if (or (nil? val) (= val ""))
'()
(list {:varrefs [var] :text (apply-filter (str (data var)) filter)}))))
(list {:varrefs [var] :text (apply-filter (str val) filter)}))))
(defmethod expand% :text [template data]
(list {:varrefs [] :text (second template)}))

View File

@ -95,12 +95,13 @@
)))
;; (deftest var-paths
;; (is (= "Hello, John"
;; (generation/generate
;; [(generation/parse-template "Hello, {person.name}")]
;; {:person {:name "John"}})))
;; (is (= 9
;; (generation/generate
;; [(generation/parse-template "Hello, {person.name}")]
;; {:person {:age 49}}))))
(deftest var-paths
(testing "var-paths"
(is (= "Hello, John"
(generation/generate
[(generation/parse-template "Hello, {person.name}")]
{:person {:name "John"}})))
(is (= "I am 49"
(generation/generate
[(generation/parse-template "I am {person.age}")]
{:person {:age 49}})))))

View File

@ -2,11 +2,18 @@
(:require [cljs.test :refer (deftest is testing)]
[lemondronor.circlebot :as circlebot]))
(def epsilon 0.0000001)
(defn a= [a b]
(< (Math/abs (- a b)) epsilon))
(defn strmatch [pattern text]
(if (string? text)
(re-find pattern text)
false))
;; (deftest bearing->angle
;; (is (angles= (cooleradar/bearing->angle 0) (/ Math/PI 2)))
;; (is (angles= (cooleradar/bearing->angle (/ Math/PI 2)) 0))
@ -96,14 +103,14 @@
(deftest expand-template
(let [data {:locality "Palmdale", :continent "North America", :military? true, :alt 3850, :speed "209", :normalized-curviness 14.768651250300287, :accuracy "centroid", :country_a "USA", :continent_gid "whosonfirst:continent:102191575", :name "Palmdale", :squawk "5330", :icao "AE1482", :county_a "LO", :county "Los Angeles County", :source "whosonfirst", :gid "whosonfirst:locality:85923493", :curviness 1269.8089810739468, :locality_gid "whosonfirst:locality:85923493", :region "California", :militaryicao "AE1482", :region_a "CA", :nearbydistance 8.167, :callsign "RAIDR49", :layer "locality", :mlat? false, :country_gid "whosonfirst:country:85633793", :label "Palmdale, CA, USA", :id "85923493", :lon -118.00375, :region_gid "whosonfirst:region:85688637", :lat 34.661074, :militaryregistration "166765", :county_gid "whosonfirst:county:102086957", :started-circling-time 1576266715691, :distance 6.855, :source_id "85923493", :registration "166765", :confidence 0.5, :country "United States", :postime 1576266689756, :nearbylandmark "Living Faith Foursquare Church"}]
(is (re-find #"military" (-> (circlebot/expand-template data) :text))))
(is (strmatch #"military" (-> (circlebot/expand-template data) :text))))
(let [data {:locality "Palmdale", :continent "North America", :military? true, :alt 3200, :speed "161", :normalized-curviness 15.783422690487765, :accuracy "centroid", :country_a "USA", :continent_gid "whosonfirst:continent:102191575", :name "Palmdale", :squawk "5330", :icao "AE1482", :county_a "LO", :county "Los Angeles County", :source "whosonfirst", :gid "whosonfirst:locality:85923493", :curviness 1098.803548060181, :locality_gid "whosonfirst:locality:85923493", :region "California", :militaryicao "AE1482", :region_a "CA", :nearbydistance 7.828, :callsign "RAIDR49", :layer "locality", :mlat? false, :country_gid "whosonfirst:country:85633793", :label "Palmdale, CA, USA", :id "85923493", :lon -118.049183, :region_gid "whosonfirst:region:85688637", :lat 34.649808, :militaryregistration "166765", :county_gid "whosonfirst:county:102086957", :started-circling-time 1576267564959, :distance 6.336, :source_id "85923493", :registration "166765", :confidence 0.5, :country "United States", :postime 1576267555709, :nearbylandmark "Living Faith Foursquare Church"}]
(is (re-find #"military" (-> (circlebot/expand-template data) :text))))
(is (strmatch #"military" (-> (circlebot/expand-template data) :text))))
(testing "a vs. an for type"
(let [data {:registration "TEST" :icao "123" :type "Airbus 380" :locality "Test City"}]
(is (re-find #"an Airbus" (:text (circlebot/expand-template data)))))
(is (strmatch #"an Airbus" (:text (circlebot/expand-template data)))))
(let [data {:registration "TEST" :icao "123" :type "Yoyo 380" :locality "Test City"}]
(is (re-find #"a Yoyo" (:text (circlebot/expand-template data)))))))
(is (strmatch #"a Yoyo" (:text (circlebot/expand-template data)))))))
(deftest merge-adsbx-sqb
@ -127,10 +134,10 @@
reverse {:properties {:neighbourhood "Silver Lake" :locality "Los Angeles"}}
nearby nil
desc (circlebot/generate-description ac sqb reverse nearby)]
(is (re-find #"NBADB0Y" desc))
(is (re-find #"a B52" desc))
(is (re-find #"Silver Lake.*Los Angeles" desc))
(is (re-find #"#NBADB0Y" desc))))
(is (strmatch #"NBADB0Y" desc))
(is (strmatch #"a B52" desc))
(is (strmatch #"Silver Lake.*Los Angeles" desc))
(is (strmatch #"#NBADB0Y" desc))))
(testing "Missing ADSBX registration"
(let [ac {:icao "B00B00"}
sqb {:registration "NGOODB0Y"
@ -138,10 +145,10 @@
reverse {:properties {:neighbourhood "Silver Lake" :locality "Los Angeles"}}
nearby {:name "Disneyland" :distance 2}
desc (circlebot/generate-description ac sqb reverse nearby)]
(is (re-find #"NGOODB0Y" desc))
(is (re-find #"a B52" desc))
(is (re-find #"Silver Lake.*Los Angeles" desc))
(is (re-find #"#NGOODB0Y" desc))))
(is (strmatch #"NGOODB0Y" desc))
(is (strmatch #"a B52" desc))
(is (strmatch #"Silver Lake.*Los Angeles" desc))
(is (strmatch #"#NGOODB0Y" desc))))
(testing "foo"
(let [ac {:military? false :alt 1300 :speed 72.1 :squawk "1200"
:icao "AAE0C2" :type nil, :callsign "N80NT", :registration nil}
@ -149,14 +156,14 @@
reverse {:properties {:neighbourhood "Silver Lake" :locality "Los Angeles"}}
nearby {:name "Disneyland" :distance 2}
desc (circlebot/generate-description ac sqb reverse nearby)]
(is (re-find #"N80NT" desc))
(is (re-find #"a Eurocopter Squirrel AS 350 B2" desc))
(is (re-find #"callsign N80NT" desc))
(is (re-find #"Silver Lake.*Los Angeles" desc))
(is (re-find #"1300 feet" desc))
(is (re-find #"speed 83 MPH" desc))
(is (re-find #"squawking 1200" desc))
(is (re-find #"#N80NT" desc)))))
(is (strmatch #"N80NT" desc))
(is (strmatch #"a Eurocopter Squirrel AS 350 B2" desc))
(is (strmatch #"callsign N80NT" desc))
(is (strmatch #"Silver Lake.*Los Angeles" desc))
(is (strmatch #"1300 feet" desc))
(is (strmatch #"speed 83 MPH" desc))
(is (strmatch #"squawking 1200" desc))
(is (strmatch #"#N80NT" desc)))))
(deftest filter-landmarks