This repository has been archived by the owner on May 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
corpus.clj
180 lines (160 loc) · 6.59 KB
/
corpus.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(ns duckling.corpus
(:use [clojure.tools.logging]
[plumbing.core :except [millis]])
(:require [duckling.time.obj :as time]
[duckling.util :as util]))
; Checker functions return *nil* when OK, or [expected actual] when not OK
(defn- vec->date-and-map
"Turns a vector of args into a date and a map of extra fields"
[args]
(let [[date-fields other-keys-and-values] (split-with integer? args)
token-fields (into {} (map vec (partition 2 other-keys-and-values)))
date (-> (apply time/t -2 date-fields)
(?> (:grain token-fields) (assoc :grain (:grain token-fields)))
(?> (:timezone token-fields) (assoc :timezone (:timezone token-fields))))]
[date token-fields]))
(defn datetime
"Creates a datetime checker function to check if the token is valid"
[& args]
(let [[date token-fields] (vec->date-and-map args)]
(fn [context token]
(when-not
(and
(= :time (:dim token))
(util/hash-match (select-keys token-fields [:direction :precision])
token)
(= (-> token :value) date))
[date (:value token)]))))
(defn datetime-interval
"Creates a datetime interval checker function"
[from to]
(let [[start start-fields] (vec->date-and-map from)
[end end-fields] (vec->date-and-map to)
date (time/interval start end)]
(fn [context {:keys [value dim] :as token}]
(when-not
(and
(= :time dim)
(= value date))
[date value]))))
(defn number
"check if the token is a number equal to value.
If value is integer, it also checks :integer true"
[value]
(fn [_ token] (when-not
(and
(= :number (:dim token))
(or (not (integer? value)) (:integer token))
(= (:value token) value))
[value (:value token)])))
(defn ordinal
[value]
(fn [_ token] (when-not
(and
(= :ordinal (:dim token))
(= (:value token) value))
[value (:value token)])))
(defn temperature
"Create a temp condition"
[value' & [unit' precision']]
(fn [_ {:keys [dim value unit precision] :as token}]
(not (and
(= :temperature dim)
(= value' value)
(= unit' unit)
(= precision' precision)))))
(defn distance
"Create a distance condition"
[value' & [unit' normalized' precision']]
(fn [_ {:keys [dim value unit normalized precision] :as token}]
(not (and
(= :distance dim)
(= value' value)
(= unit' unit)
(= normalized' normalized)
(= precision' precision)))))
(defn money
"Create a amount-of-money condition"
[value' & [unit' precision']]
(fn [_ {:keys [dim value unit precision] :as token}]
(not (and
(= :amount-of-money dim)
(= value' value)
(= unit' unit)
(= precision' precision)))))
(defn place
"Create a place checker"
[pnl n]
(fn [token context] (and
(= :pnl (:dim token))
(= n (:n token))
(= pnl (:pnl token)))))
(defn metric
"Create a metric checker"
[cat val]
(fn [token context] (and
(= :unit (:dim token))
(= val (:val token))
(= cat (:cat token)))))
(defn quantity
"Create a quantity condition"
[value unit & [product]]
(fn [token _] (and
(= :quantity (:dim token))
(= value (-> token :value :value))
(= unit (-> token :value :unit))
(= product (-> token :value :product)))))
(defn volume
"Create a volume condition"
[value unit & [normalized]]
(fn [token _] (and
(= :volume (:dim token))
(= value (-> token :value :value))
(= unit (-> token :value :unit))
(= normalized (-> token :value :normalized)))))
(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 (:value token)))
(or (nil? max) (<= (:value token) max))
(every? #(% token) predicates))))
(defn corpus
"Parse corpus" ;; TODO should be able to load several files, like rules
[forms]
(-> (fn [state [head & more :as forms] context tests]
(if head
(case state
:init (cond (map? head) (recur :test-strings more
head
(conj tests {:text [], :checks []}))
:else (throw (Exception. (str "Invalid form at init state. A map is expected for context:" (prn-str head)))))
:test-strings (cond (string? head) (recur :test-strings more
context
(assoc-in tests
[(dec (count tests)) :text (count (:text (peek tests)))]
head))
(fn? head) (recur :test-checks forms
context
tests)
:else (throw (Exception. (str "Invalid form at test-strings state: " (prn-str head)))))
:test-checks (cond (fn? head) (recur :test-checks more
context
(assoc-in tests
[(dec (count tests)) :checks (count (:checks (peek tests)))]
head))
(string? head) (recur :test-strings forms
context
(conj tests {:text [], :checks []}))
:else (throw (Exception. (str "Invalid form at test-checks stats:" (prn-str head))))))
{:context context, :tests tests}))
(apply [:init forms [] []])))
(defmacro this-ns "Total hack to get ns of this file at compile time" [] *ns*)
(defn read-corpus
"Read a list of symbol and return a Corpus map {:context {}, :tests []}"
[new-file]
(let [symbols (read-string (slurp new-file))]
(corpus (map #(binding [*ns* (this-ns)] (eval %)) symbols))))