forked from facebookarchive/duckling_old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.clj
30 lines (27 loc) · 948 Bytes
/
helpers.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns duckling.helpers
"This namespace contains the common helpers used in rules")
(defmacro fn& [dim & args-body]
(let [meta-map (when (-> args-body first map?)
(first args-body))
args-body (if meta-map
(rest args-body)
args-body)]
(merge meta-map
`{:dim ~(keyword dim)
:pred (fn ~@args-body)})))
(defn dim
"Returns a func checking dim of a token and additional preds"
[dim-val & predicates]
(fn [token]
(and (= dim-val (:dim token))
(every? #(% token) predicates))))
(defn integer
"Return a func (duckling pattern) checking that dim=number and integer=true,
optional range (inclusive), and additional preds"
[& [min max & predicates]]
(fn [token]
(and (= :number (:dim token))
(:integer token)
(or (nil? min) (<= min (:val token)))
(or (nil? max) (<= (:val token) max))
(every? #(% token) predicates))))