-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsum.sql
More file actions
307 lines (281 loc) · 9.23 KB
/
Copy pathsum.sql
File metadata and controls
307 lines (281 loc) · 9.23 KB
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
-- Each summary is initially an array of 29 with s_rank, o_fill, o1, p1, sc1, o2, p2, sc2
-- and so on. After so many entries, more are not added.
create procedure s_sum_init (inout env any)
{
env := make_array (30, 'any');
}
;
create procedure s_sum_acc (inout env any, in s_rank double precision, in p iri_id, in o any, in sc int)
{
declare fill int;
fill := env[1];
if (fill = 0)
env[0] := s_rank;
else if (fill >= 25)
return;
env[2] := env[2] + sc;
env[fill + 3] := o;
env[fill + 4] := p;
env[fill + 5] := sc;
env[1] := fill + 3;
}
;
create procedure s_sum_fin (inout env any)
{
return env;
}
;
create aggregate DB.DBA.S_SUM (in s_rank double precision, in p iri_id, in o any, in sc int) returns any from
s_sum_init, s_sum_acc, s_sum_fin;
create procedure sum_rank (inout arr any)
{
if (not isvector (arr) or length (arr) < 3)
return 0;
return rnk_scale (arr[0]) + cast (arr[2] as real) / (arr[1] / 3);
}
;
create procedure sum_o_p_score (inout o any, inout p any)
{
declare p_weight any;
declare lng_m, pm int;
declare lng_pref any;
lng_pref := connection_get ('lang');
p_weight := connection_get ('p_weight');
if (lng_pref is not null and is_rdf_box (o) and rdf_box_lang (o) = lng_pref)
lng_m := 3;
else
lng_m := 0;
if (p_weight is null)
return lng_m;
pm := dict_get (p_weight, p);
if (pm)
return lng_m + pm;
return lng_m;
}
;
create procedure sum_result (inout final any,
inout res any,
inout text_exp any,
inout s varchar,
inout start_inx int,
inout end_inx int,
inout s_rank real,
inout lbl any,
inout g any)
{
declare sorted, inx, tot, exc, elt, tsum any;
tsum := 0;
tot := '';
sorted := subseq (res, start_inx, end_inx);
for (inx := 0; inx < length (sorted); inx := inx + 3)
{
tsum := tsum + sorted[inx + 2];
sorted[inx + 2] := sum_o_p_score (sorted[inx], sorted[inx + 1]);
}
--dbg_obj_print ('sorted = ', sorted);
gvector_sort (sorted, 3, 2, 0);
for (inx := 0; inx < length (sorted); inx := inx + 3)
tot := tot || cast (rdf_box_data (sorted[inx]) as varchar);
-- tot := __bft (tot, 2); XXX: disabled until fixed excerpt to make ∞ loop
s := charset_recode (s, 'UTF-8', '_WIDE_');
exc := fct_bold_tags (search_excerpt (text_exp, tot));
-- dbg_obj_print (' summaries of ', tot, ' ', lbl, ' ', exc);
elt := xmlelement ('row',
xmlelement ('column', xmlattributes ('trank' as "datatype"), cast (cast (tsum as real) / ((end_inx - start_inx) / 3) as varchar)),
xmlelement ('column', xmlattributes ('erank' as "datatype"), cast (s_rank as varchar)),
xmlelement ('column', xmlattributes ('url' as "datatype", fct_short_form (s) as "shortform"), s),
xmlelement ('column', lbl),
xmlelement ('column', xmlattributes ('url' as "datatype", fct_short_form (g) as "shortform"), g),
xmlelement ('column', exc)
);
xte_nodebld_xmlagg_acc (final, elt);
}
;
create procedure sum_final (inout x any)
{
return xml_tree_doc (xte_nodebld_final_root (x));
}
;
create procedure s_sum_page_s (in rows any, in text_exp varchar)
{
-- dbg_obj_print (rows);
/* fill the os and translate the iris and make sums */
declare inx, s, g, prev_s, prev_fill, fill, inx2, n, s_rank, lbl any;
declare dp, os, res, final any;
declare lng_pref any;
lng_pref := connection_get ('langs');
xte_nodebld_init (final);
n := 0;
for (inx := 0; inx < length (rows); inx := inx + 1)
{
os := aref (rows, inx, 1);
for (inx2 := 3; inx2 < os[1] + 3; inx2 := inx2 + 3)
n := n + 1;
}
n := 3 * n;
--dbg_obj_print ('result length ', n);
res := make_array (n, 'any');
fill := 0;
for (inx := 0; inx < length (rows); inx := inx + 1)
{
os := aref (rows, inx, 1);
s_rank := rnk_scale (os[0]);
s := ID_TO_IRI (rows[inx][0]);
lbl := FCT_LABEL_S (rows[inx][0], 0, 'facets', lng_pref);
g := ID_TO_IRI (rows[inx][2]);
prev_fill := fill;
for (inx2 := 3; inx2 < os[1] + 3; inx2 := inx2 + 3)
{
res[fill] := __RO2SQ (os[inx2]);
res[fill + 1] := os[inx2 + 1];
res[fill + 2] := os[inx2 + 2];
fill := fill + 3;
}
sum_result (final, res, text_exp, s, prev_fill, fill, s_rank, lbl, g);
}
return sum_final (final);
}
;
create procedure s_sum_page_c (in rows any, in text_exp varchar)
{
/* fill the os and translate the iris and make sums */
declare inx, s, g, prev_s, prev_fill, fill, inx2, n, s_rank, lbl any;
declare dp, os, so, res, final any;
declare lng_pref any;
lng_pref := connection_get ('langs');
dp := dpipe (1, 'ID_TO_IRI', '__RO2SQ', 'FCT_LABEL_L', 'ID_TO_IRI');
xte_nodebld_init (final);
for (inx := 0; inx < length (rows); inx := inx + 1)
{
os := aref (rows, inx, 1);
for (inx2 := 3; inx2 < os[1] + 3; inx2 := inx2 + 3)
dpipe_input (dp, aref (rows, inx, 0),os[inx2], vector (aref (rows, inx, 0), 0, 'facets', lng_pref), aref (rows, inx, 2));
}
n := 3 * dpipe_count (dp);
--dbg_obj_print ('result length ', n);
res := make_array (n, 'any');
fill := 0;
for (inx := 0; inx < length (rows); inx := inx + 1)
{
os := aref (rows, inx, 1);
s_rank := rnk_scale (os[0]);
prev_fill := fill;
for (inx2 := 3; inx2 < os[1] + 3; inx2 := inx2 + 3)
{
so := dpipe_next (dp, 0);
--dbg_obj_print ('res ', fill, so);
s := so[0];
g := so[3];
res[fill] := so[1];
res[fill + 1] := os[inx2 + 1];
res[fill + 2] := os[inx2 + 2];
lbl := so[2];
fill := fill + 3;
}
sum_result (final, res, text_exp, s, prev_fill, fill, s_rank, lbl, g);
}
dpipe_next (dp, 1);
--sum_result (final, res, text_exp, s, prev_fill, fill, s_rank);
return sum_final (final);
}
;
create procedure s_sum_page (in rows any, in text_exp varchar)
{
declare i int;
if (__tag (text_exp) = 193)
foreach (any v in text_exp) do
{
if (iswidestring (v))
{
v := charset_recode (v, '_WIDE_', 'UTF-8');
text_exp [i] := v;
}
i := i + 1;
}
if (sys_stat ('cl_run_local_only'))
return s_sum_page_s (rows, text_exp);
else
return s_sum_page_c (rows, text_exp);
}
;
--create procedure sum_tst (in text_exp varchar, in text_words varchar := null)
--{
--declare res any;
--if (text_words is null)
--text_words := vector (text_exp);
--res := (sparql select (<sql:vector_agg> (<bif:vector> (?c1, ?sm))) as ?res
--where {
--{ select (<SHORT_OR_LONG::>(?s1)) as ?c1,
--(<sql:S_SUM> (
--<SHORT_OR_LONG::IRI_RANK> (?s1),
--<SHORT_OR_LONG::>(?s1textp),
--<SHORT_OR_LONG::>(?o1),
--?sc ) ) as ?sm
--where { ?s1 ?s1textp ?o1 . ?o1 bif:contains "NEW AND YORK" option (score ?sc) . }
--order by desc (<sql:sum_rank> ((<sql:S_SUM> (
--<SHORT_OR_LONG::IRI_RANK> (?s1),
--<SHORT_OR_LONG::>(?s1textp),
--<SHORT_OR_LONG::>(?o1),
--?sc ) ) ) ) limit 20 } } );
----dbg_obj_print (res);
--res := s_sum_page (res, text_words);
--return res;
--}
--;
--create procedure sum_tst_1 (in text_exp varchar, in text_words varchar := null)
--{
--declare res any;
--if (text_words is null)
--text_words := vector (text_exp);
--res := (select vector_agg (vector ("c1", "sm")) from (
--sparql
--select (<SHORT_OR_LONG::>(?s1)) as ?c1, (<sql:S_SUM> (
--<SHORT_OR_LONG::IRI_RANK> (?s1),
--<SHORT_OR_LONG::>(?s1textp),
--<SHORT_OR_LONG::>(?o1),
--?sc ) ) as ?sm
--where { ?s1 ?s1textp ?o1 . ?o1 bif:contains "NEW AND YORK" option (score ?sc) }
--order by desc (<sql:sum_rank> ((<sql:S_SUM> (
--<SHORT_OR_LONG::IRI_RANK> (?s1),
--<SHORT_OR_LONG::>(?s1textp),
--<SHORT_OR_LONG::>(?o1),
--?sc ) ) ) )
--limit 20 ) s option (quietcast)
--);
----dbg_obj_print (res);
--res := s_sum_page (res, text_words);
--return res;
--}
--;
--create procedure sum_tst_2 (in text_exp varchar, in text_words varchar := null)
--{
-- declare res any;
-- if (text_words is null)
-- text_words := vector (text_exp);
-- res := (select vector_agg (vector (s, sm)) from (
-- select top 20 s, s_sum (iri_rank (s), p, o, score) as sm
-- from rdf_obj, rdf_ft, rdf_quad q1
-- where contains (ro_flags, text_exp) and rf_id = ro_id and q1.o = rf_o group by s
-- order by sum_rank (sm) option (quietcast) ) s option (quietcast)
--);
--dbg_obj_print (res);
-- res := s_sum_page (res, text_words);
-- return res;
--}
--;
-- sum_tst ('oori');
--
-- sparql
-- select (<SHORT_OR_LONG::>(?s1)) as ?c1, (<sql:S_SUM> (
-- <SHORT_OR_LONG::IRI_RANK> (?s1),
-- <SHORT_OR_LONG::>(?s1textp),
-- <SHORT_OR_LONG::>(?o1),
-- ?sc ) ) as ?sm
-- where { ?s1 ?s1textp ?o1 . ?o1 bif:contains "NEW AND YORK" option (score ?sc) . }
-- order by desc (<sql:sum_rank> ((<sql:S_SUM> (
-- <SHORT_OR_LONG::IRI_RANK> (?s1),
-- <SHORT_OR_LONG::>(?s1textp),
-- <SHORT_OR_LONG::>(?o1),
-- ?sc ) ) ) )
-- limit 20;
-- explain ('sparql select ?s1 as ?c1, (<SHORT_OR_LONG::s_sum> (<SHORT_OR_LONG::IRI_RANK> (?s1), ?s1textp, ?o1, ?sc)) as ?sm where { ?s1 ?s1textp ?o1 . ?o1 bif:contains ''NEW AND YORK'' option (score ?sc) . } group by ?s1 order by desc (<SHORT_OR_LONG::sum_rank> (<SHORT_OR_LONG::s_sum> (<SHORT_OR_LONG::IRI_RANK> (?s1), ?s1textp, ?o1, ?sc))) limit 20');