-
Notifications
You must be signed in to change notification settings - Fork 12
/
qobom.red
339 lines (325 loc) · 6.23 KB
/
qobom.red
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
Red[
Title: "QOBOM - Query over block of maps"
Author: "Boleslav Březovský"
Usage: {
```
keep [ <key> or * ] where
<key> is <value>
<key> [ = < > <= >= ] <value>
<key> contains <value>
<key> matches <parse rule>
```
<value> can be `paren!` and then is evaluated first
<value> can be `block!` and then is interpred as list of values that can match
Support for expressions in count - see following example:
>> qobom messages [keep ['author 'text] as map where 'sent > (now - 6:0:0) count by 'author (length? text)]
== #(
"pekr" 1999
"9214" 116
"BeardPower" 69
)
NOTE: expression must return number to be counted (probably should add some checks)
}
]
qobom!: context [
time: none
select-deep: func [
series
value
][
either word? value [
select series value
][
; path
foreach elem value [
series: select series elem
]
]
]
sort-by: func [
"Sort block of maps"
data
match-key
; keep-key ; TODO: support * for keeping everything
; TODO: sorting order
/local result value
][
; NOTE: How expensive is map!->block!->map! ? Is there other way?
result: clear #()
foreach item data [
value: item/:match-key
result/:value: either result/:value [
result/:value + 1
][
1
]
]
to map! sort/skip/compare/reverse to block! result 2 2
]
do-conditions: func [
data conditions selector type
/local value
][
type: equal? map! type
collect [
foreach item data [
if any conditions [
case [
equal? '* selector [keep/only either type [item][values-of item]]
block? selector [
value: to map! collect [foreach s selector [keep reduce [s select-key item to lit-word! s]]]
keep/only either type [value][values-of value]
]
'default [
value: select-key item selector
keep either type [to map! reduce [selector value]][value]
]
]
]
]
]
]
select-key: func [item selector][
switch type?/word selector [
none! [item]
lit-word! lit-path! [select-deep item to path! selector]
block! [
collect [
foreach key selector [keep select-deep item to path! key]
]
]
]
]
count-values: func [
"Count occurences of each value in DATA. Return map! with values as keys and count as values"
data
/key
name "Key to match"
action
; TODO: support some refinement to return block! instead (or make it default?)
/local result act-result
][
result: copy #()
foreach value data [
either key [
; NOTE: I'm doing some black magic here to simplify the dialect
; It's certainly not the fastest way and should be redone
act-result: do bind as block! action make object! to block! value
key-name: value/:name
result/:key-name: either result/:key-name [result/:key-name + act-result][act-result]
][
result/:value: either result/:value [result/:value + 1][1]
]
]
to map! sort/skip/compare/reverse to block! result 2 2
]
select-key: func [
"Deep select key"
value
key
/local item elem
][
item: either lit-path? key [
item: value
foreach elem key [item: select item elem]
item
][select value key]
]
clean-word: func [
"Remove punctuation from a word"
word
][
; TODO clean punctuation only if it's last letter?
parse word [
some [
change #"." ""
| change #"," ""
| change #"?" ""
| change #"." ""
| skip
]
]
word
]
count-frequency: func [
"Count frequency of keys or words in keys"
type "BY for counting keys, IN for counting words in keys"
key
/local result
][
result: #()
switch type [
by [
foreach value data-block [
item: select-key value key
result/:item: either result/:item [
result/:item + 1
][1]
]
]
in [
foreach value data-block [
set 'v value
item: select-key value key
foreach word split item space [
word: clean-word word
result/:word: either result/:word [
result/:word + 1
][1]
]
]
]
]
result: make map! sort/skip/compare/reverse to block! result 2 2
]
add-condition: func [
condition
][
append group condition
]
lits: [lit-word! | lit-path!]
value-rule: [
set value skip (
if paren? value [value: compose value]
)
]
reflector-rule: [
(reflector: none)
set value skip 'in (
reflector: value
)
]
col-rule: [
opt reflector-rule
set key lits
[
'is 'from set value block! (
add-condition compose/deep [
find [(value)] select-deep item (key)
]
)
| ['is | '=] value-rule (
either reflector [
add-condition compose [
equal? (to paren! compose [t: select-deep item (key)]) (value)
]
][
add-condition compose [
equal? select-deep item (key) (value)
]
]
)
| set symbol ['< | '> | '<= | '>=] value-rule (
add-condition compose [
(to paren! reduce ['select-deep 'item key]) (symbol) (value)
]
)
]
]
find-rule: [
set key lits
'contains
value-rule (
add-condition compose [
find select-deep item (key) (value)
]
)
]
match-rule: [
set key lits
'matches
value-rule (
append value [to end]
add-condition compose/deep [
parse select-deep item (key) [(value)]
]
)
]
keep-rule: [
(keep-type: block!)
'keep
set selector ['* | block! | lit-word! | lit-path!]
opt ['as 'map (keep-type: map!)]
'where
]
sort-rule: [
'sort 'by set value skip (
sort-by result value
)
]
count-rule: [
'count (count-by?: no)
opt [
'by (count-by?: yes)
set key lit-word!
set value paren!
]
(
result: either count-by? [
count-values/key result key value
][
count-values result
]
)
]
conditions-rule: [col-rule | find-rule | match-rule]
do-cond-rule: [(
repend conditions ['all group]
result: do-conditions data-block conditions selector keep-type
)]
basic-rule: [
keep-rule
conditions-rule
any [
['and conditions-rule]
| [
'or (
repend conditions ['all group]
group: copy []
)
conditions-rule
]
]
do-cond-rule
opt count-rule
]
frequency-rule: [
'frequency [
set type ['by | 'in]
set key lits
]
(count-frequency type key)
]
main-rule: [
frequency-rule
| basic-rule
]
conditions: []
group: none
data-block: none
result: none
value: none
key: none
type: none
reflector: none
t: none
set 'qobom func [
"Simple query dialect for filtering messages"
data
dialect
/local
selector
keep-type count-by?
t
][
t: now/time/precise
data-block: data
clear conditions
value: result: none
group: copy []
parse dialect main-rule
time: now/time/precise - t
result
]
; -- end of context
]