-
Notifications
You must be signed in to change notification settings - Fork 1
/
commonrules.clj
274 lines (235 loc) · 8.08 KB
/
commonrules.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
(ns com.nytimes.querqy.commonrules
"CommonRules based rewriter"
(:refer-clojure :exclude [filter])
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[com.nytimes.querqy.model :as model]
[com.nytimes.querqy.parser :as parser])
(:import
(java.io
Reader)
(java.net
URL)
(java.util
List
UUID)
(querqy.model
Input
Input$BooleanInput
Input$SimpleInput)
(querqy.parser
QuerqyParser)
(querqy.rewrite
RewriterFactory)
(querqy.rewrite.commonrules
CommonRulesRewriter
LineParser
QuerqyParserFactory
SimpleCommonRulesParser
WhiteSpaceQuerqyParserFactory)
(querqy.rewrite.commonrules.model
BoostInstruction
BoostInstruction$BoostDirection
BoostInstruction$BoostMethod
DeleteInstruction
FilterInstruction
InstructionDescription
Instructions
SynonymInstruction
TrieMapRulesCollectionBuilder)
(querqy.rewrite.commonrules.select
SelectionStrategyFactory)
(querqy.rewrite.commonrules.select.booleaninput
BooleanInputParser)
(querqy.rewrite.commonrules.select.booleaninput.model
BooleanInputElement
BooleanInputElement$Type
BooleanInputLiteral)))
(set! *warn-on-reflection* true)
(defprotocol CommonRulesRewriterBuilder
(common-rules-rewriter* [this]))
(extend-protocol CommonRulesRewriterBuilder
nil
(common-rules-rewriter* [_]
(throw (IllegalArgumentException. "Must provide rules to rules-rewriter"))))
(defn rules-rewriter
"Create a CommonRulesRewriter.
You may pass in a resource pointing to a Querqy CommonRulesRewriter file or
use the Clojure-DSL."
[& args]
(if (and (= 1 (count args)) (instance? URL (first args)))
(common-rules-rewriter* (first args))
(common-rules-rewriter* args)))
(defn rewriter-factory
[rules]
(proxy [RewriterFactory] [(str (UUID/randomUUID))]
(createRewriter [_ _]
(CommonRulesRewriter.
rules
SelectionStrategyFactory/DEFAULT_SELECTION_STRATEGY))
(getCacheableGenerableTerms [] #{})))
;; ----------------------------------------------------------------------
;; Resource
(defn parse-rules
([stream]
(parse-rules stream nil))
([^Reader stream {:keys [boolean-input, ignore-case, parser]
:or {boolean-input true
ignore-case true
parser (WhiteSpaceQuerqyParserFactory.)}}]
(let [rules-parser (SimpleCommonRulesParser.
^Reader stream
^boolean boolean-input
^QuerqyParserFactory parser
^boolean ignore-case
BoostInstruction$BoostMethod/ADDITIVE)]
(.parse rules-parser))))
(extend-protocol CommonRulesRewriterBuilder
URL
(common-rules-rewriter* [url]
(rewriter-factory (parse-rules (io/reader url)))))
;; ----------------------------------------------------------------------
;; DSL
(def ^:dynamic ^QuerqyParser *query-parser* parser/whitespace-parser)
(defrecord Rule [input instructions])
(defn- description
^InstructionDescription
[& {:keys [^String type, param, ^String value]}]
(cond-> (InstructionDescription/builder)
(some? type) (.typeName type)
(some? param) (.param param)
(some? value) (.value value)
:finally (.build)))
(defn match*
"Create a "
[head & tail]
(->Rule head (vec tail)))
(defmacro match
"Create a rewriter rule from matching text input or boolean input followed by
any number of query transformations.
```clojure
;; Inject bar as a synonym to foo in any query.
(match \"foo\"
(synonym \"bar\"))
```"
[head & tail]
`(match* '~head ~@tail))
(defn- parse-string
[string]
(mapv #(LineParser/parseTerm %) (str/split string #"\s+")))
(defn- parse-query
[query]
(cond
(string? query)
(.parse *query-parser* query)
(map? query)
(model/rawq {:query query})))
(defn delete?
[obj]
(instance? DeleteInstruction obj))
(defn delete
"Delete text from a matched query. This is useful for removing unhelpful
search terms from the query."
[string]
(DeleteInstruction.
(parse-string string)
(description {:type "delete", :value string})))
(defn synonym?
[obj]
(instance? SynonymInstruction obj))
(defn synonym
"Create a synonym for the matched text.
```clojure
(match \"chickpeas\"
(synonym \"garbanzo beans\"))
```"
([string]
(synonym 1.0 string))
([boost string]
(SynonymInstruction.
(parse-string string)
boost
(description {:type "synonym", :param boost, :value string}))))
(defn boost
"Boost a matching term or query.
```clojure
;; Boost recipes which take less than 30 minutes when queries
;; match quick and recipe or recipes.
(match (and \"quick\" (or \"recipe\" \"recipes\")
(boost 10 {:range {:minutes {:lte 30}}}))
```"
[boost query]
(when (zero? boost)
(throw (IllegalArgumentException. "Cannot boost by 0")))
(let [UP BoostInstruction$BoostDirection/UP
DOWN BoostInstruction$BoostDirection/DOWN]
(BoostInstruction.
(parse-query query)
(if (>= boost 0) UP DOWN)
BoostInstruction$BoostMethod/ADDITIVE
(abs boost)
(description {:type "boost", :param boost, :value (pr-str query)}))))
(defn filter
"Add a filter to the query. Filters require a match on documents.
```clojure
(match \"by paul krugman\"
(filter {:term {:author \"Paul Krugman\"}}))
```"
[query]
(FilterInstruction.
(parse-query query)
(description {:type "filter", :value (pr-str query)})))
;;; match impl
(defmulti parse-boolean-input (fn [form] (type form)))
(defmethod parse-boolean-input String
[string]
(for [term (str/split string #"\s+")]
(BooleanInputElement. term BooleanInputElement$Type/TERM)))
(defmethod parse-boolean-input List
[[operator & terms :as input]]
(let [OR (BooleanInputElement. "OR" BooleanInputElement$Type/OR)
AND (BooleanInputElement. "AND" BooleanInputElement$Type/AND)
NOT (BooleanInputElement. "NOT" BooleanInputElement$Type/NOT)
LEFTP (BooleanInputElement. "(" BooleanInputElement$Type/LEFT_PARENTHESIS)
RIGHTP (BooleanInputElement. ")" BooleanInputElement$Type/RIGHT_PARENTHESIS)
wrap (fn [xs] (flatten (concat (list LEFTP) xs (list RIGHTP))))]
(case operator
or (wrap (interpose OR (parse-boolean-input terms)))
and (wrap (interpose AND (parse-boolean-input terms)))
not (wrap (cons NOT (parse-boolean-input terms)))
(map parse-boolean-input input))))
(defn parse-simple-input
^Input$SimpleInput
[string]
(Input/parseSimpleInput string))
(extend-protocol CommonRulesRewriterBuilder
List
(common-rules-rewriter* [rules]
(let [rules (flatten rules)
counter (atom 0)
rules-builder (TrieMapRulesCollectionBuilder. true)
boolean-input-parser (BooleanInputParser.)]
(doseq [{:keys [input instructions]} rules]
(let [ord (swap! counter inc)
id (str input "#" ord)
instructions (Instructions. ord id instructions)]
(cond
(string? input)
(.addRule rules-builder (parse-simple-input input) instructions)
(list? input)
(do
(when (some delete? instructions)
(throw (IllegalArgumentException. "Cannot use a delete instruction with boolean input")))
(when (some synonym? instructions)
(throw (IllegalArgumentException. "Cannot use a synonym instruction with boolean input")))
(let [bool-input (Input$BooleanInput. (parse-boolean-input input) boolean-input-parser (pr-str input))]
;; inputPattern.applyInstructions(instructions, builder);
(.applyInstructions bool-input instructions rules-builder))))))
;; Add boolean literals at the end
(doseq [^BooleanInputLiteral literal (.values (.getLiteralRegister boolean-input-parser))]
(let [string (str/join \space (.getTerms literal))
input (parse-simple-input string)]
(.addRule rules-builder input literal)))
;;
(rewriter-factory (.build rules-builder)))))