-
Notifications
You must be signed in to change notification settings - Fork 21
/
search.py
542 lines (431 loc) · 19.5 KB
/
search.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"""
# Search (top-level)
"""
from ..core.helpers import console, wrapMessages
from .searchexe import SearchExe
from ..core.timestamp import SILENT_D, AUTO, silentConvert
class Search:
""" """
def __init__(self, api, silent=SILENT_D):
silent = silentConvert(silent)
self.api = api
self.silent = silent
self.exe = None
perfDefaults = SearchExe.perfDefaults
self.perfParams = {}
self.perfParams.update(perfDefaults)
def tweakPerformance(self, silent=SILENT_D, **kwargs):
"""Tweak parameters that influence the search process.
!!! explanation "Theory"
Before the search engine retrieves result tuples of nodes,
there is a process to narrow down the search space.
See `tf.about.searchdesign` and remember that we use the term *yarn* for
the sets of candidate nodes from which we stitch our results together.
*Edge spinning* is the process of
transferring constraints on one node via edges to constraints on
another node. The one node lives in a yarn, i.e. a set of candidate nodes,
and the node at the other side of the edge lives in a yarn.
If the first yarn is small then we might be able to reduce the second yarn
by computing the counterparts of the nodes in the small yarn in the second
yarn. We can leave out all other nodes from the second yarn.
A big reduction!
The success of edge spinning depends mainly on two factors:
!!! info "Size difference"
Edge spinning works best if there is a big difference in size
between the candidate
sets for the nodes at both sides of an edge.
!!! info "Spread"
The spread of a relation is the number of different edges
that can start from the same node or end at the same node.
For example, the spread of the `equality` operator is just 1, but
the spread of the `inequality` operator is virtually as big
as the relevant yarn.
If there are constraints on a node in one yarn, and if there is an edge
between that yarn and another one, and if the spread is big,
not much of the constraint can be transferred to the other yarn.
!!! example "Example"
Suppose both yarns are words, the first yarn has been constrained
to verbs, and the equality relation holds must hold between the yarns.
Then in all results the node from the second yarn is also a verb.
So we can constrain the second yarn to verbs too.
But if the relation is inequality, we cannot impose any additional
restriction on nodes in the second yarn. All nodes in the second
yarn are unequal to at least one verb.
!!! info "Estimating the spread"
We estimate the spreads of edges over and over again, in a series
of iterations where we reduce yarns.
An exhaustive computation would be too expensive, so we take
a sample of a limited amount of relation computations.
If you do not pass a parameter, its value will not be changed.
If you pass `None` for a parameter, its value will be reset to the default value.
Here are the parameters that you can tweak:
Parameters
----------
yarnRatio: float
The `yarnRatio` is the minimal factor between the sizes of
the smallest and the biggest set of candidates of the nodes at both ends of
the edge. And that divided by the spread of the relation as estimated
by a sample.
!!! example "Example"
Suppose we set the `yarnRatio` to 1.5.
Then, if we have yarns of 100,000 and 10,000 members,
with a relation between them with spread 5,
then 100,000 / 10,000 / 5 = 2.
This is higher than the `yarnRatio` of 1.5,
so the search engine decides that edge spinning is worth it.
The reasoning is that the 10,000 nodes in the smallest yarn are expected
to reach only 10,000 * 5 nodes in the other yarn by the relation,
and so we can achieve a significant reduction.
If you have a very slow query, and you think that a bit more edge spinning
helps, decrease the `yarnRatio` towards 0.
If you find that a lot of queries spend too much time in edge spinning,
increase the `yarnRatio`.
tryLimitFrom: integer
In order to determine the spreads of the relations, TF takes
random samples and extrapolates the results. We grab some nodes
from the set at the *from* side of an edge, and some nodes at the
*to* side of the same edge, Then we compute in how many cases the relation
holds. That is a measure for the spread.
The parameters `tryLimitFrom` and `tryLimitTo` dictate how big these
samples are. The bigger, the better the estimation of the spread.
But also the more work it is.
If you find that your queries take consistently a tad too much time,
consider lowering these parameters to 10.
If you find that the times your queries take varies a lot,
increase these values to 10000.
tryLimitTo: integer
See `tryLimitFrom`
"""
silent = silentConvert(silent)
api = self.api
TF = api.TF
error = TF.error
info = TF.info
isSilent = TF.isSilent
setSilent = TF.setSilent
defaults = SearchExe.perfDefaults
wasSilent = isSilent()
setSilent(silent)
for (k, v) in kwargs.items():
if k not in defaults:
error(f'No such performance parameter: "{k}"', tm=False)
continue
if v is None:
v = defaults[k]
elif type(v) is not int and k != "yarnRatio":
error(
f'Performance parameter "{k}" must be set to an integer, not to "{v}"',
tm=False,
)
continue
self.perfParams[k] = v
info("Performance parameters, current values:", tm=False)
for (k, v) in sorted(self.perfParams.items()):
info(f"\t{k:<20} = {v:>7}", tm=False)
SearchExe.setPerfParams(self.perfParams)
setSilent(wasSilent)
def search(
self,
searchTemplate,
limit=None,
sets=None,
shallow=False,
silent=SILENT_D,
here=True,
_msgCache=False,
):
"""Searches for combinations of nodes that together match a search template.
If you can, you should use `tf.advanced.search.search` instead.
Parameters
----------
searchTemplate: string
A string that conforms to the rules described in `tf.about.searchusage`.
shallow: set | tuple
If `True` or `1`, the result is a set of things that match the
top-level element of the `query`.
If `2` or a bigger number `n`, return the set of truncated result tuples:
only the first `n` members of each tuple is retained.
If `False` or `0`, a sorted list of all result tuples will be returned.
sets: dict
If not `None`, it should be a dictionary of sets, keyed by a names.
limit: integer, optional None
If `limit` is a positive number, it will fetch only that many results.
If it is negative, 0, None, or absent, it will fetch arbitrary many results.
!!! caution "there is an upper *fail limit* for safety reasons.
The limit is a factor times the max node in your corpus.
See `tf.parameters.SEARCH_FAIL_FACTOR`.
If this *fail limit* is exceeded in cases where no positive `limit`
has been passed, you get a warning message.
Returns
-------
generator | tuple
Each result is a tuple of nodes, where each node corresponds to an
*atom*-line in your search template (see `tf.about.searchusage`).
If `limit` is not `None`, a *generator* is returned,
which yields the results one by one.
Otherwise, the results will be fetched up till `limit`
and delivered as a tuple.
Notes
-----
!!! hint "More info on the search plan"
Searching is complex. The search template must be parsed, interpreted,
and translated into a search plan. See `tf.search.search.Search.study`.
"""
exe = SearchExe(
self.api,
searchTemplate,
outerTemplate=searchTemplate,
quKind=None,
offset=0,
sets=sets,
shallow=shallow,
silent=silent,
_msgCache=_msgCache,
setInfo={},
)
if here:
self.exe = exe
queryResults = exe.search(limit=limit)
if type(_msgCache) is list:
(status, messages) = wrapMessages(_msgCache)
self._msgCache = _msgCache
return (
(queryResults, status, messages)
if here
else (queryResults, status, messages, exe)
)
return queryResults
def study(
self,
searchTemplate,
strategy=None,
sets=None,
shallow=False,
here=True,
silent=SILENT_D,
):
"""Studies a template to prepare for searching with it.
The search space will be narrowed down and a plan for retrieving the results
will be set up.
If the search template query has quantifiers, the associated search templates
will be constructed and executed. These searches will be reported clearly.
The resulting plan can be viewed by `tf.search.search.Search.showPlan`.
Parameters
----------
searchTemplate: string
A string that conforms to the rules described in `tf.about.searchusage`.
strategy: string
In order to tame the performance of search, the strategy by which results
are fetched matters a lot. The search strategy is an implementation detail,
but we bring it to the surface nevertheless.
To see the names of the available strategies, just call
`S.study('', strategy='x')` and you will get a list of options reported to
choose from.
Feel free to experiment. To see what the strategies do, see the
code in `tf.search.stitch`.
shallow: set | tuple
If `True` or `1`, the result is a set of things that match the
top-level element of the search template.
If `2` or a bigger number `n`, return the set of truncated result tuples:
only the first `n` members of each tuple is retained.
If `False` or `0`, a sorted list of all result tuples will be returned.
sets: dict
If not `None`, it should be a dictionary of sets, keyed by a names.
In the search template you can refer to those names to invoke those sets.
silent: string, optional tf.core.timestamp.SILENT_D
See `tf.core.timestamp.Timestamp`
See Also
--------
tf.about.searchusage: Search guide
"""
if silent is False:
silent = AUTO
exe = SearchExe(
self.api,
searchTemplate,
outerTemplate=searchTemplate,
quKind=None,
offset=0,
sets=sets,
shallow=shallow,
silent=SILENT_D,
showQuantifiers=True,
setInfo={},
)
if here:
self.exe = exe
return exe.study(strategy=strategy)
def fetch(self, limit=None, _msgCache=False):
"""Retrieves query results, up to a limit.
Must be called after a previous `tf.search.search.Search.search()` or
`tf.search.search.Search.study()`.
Parameters
----------
limit: integer, optional None
If `limit` is a positive number, it will fetch only that many results.
If it is negative, 0, None, or absent, it will fetch arbitrary many results.
!!! caution "there is an upper *fail limit* for safety reasons.
The limit is a factor times the max node in your corpus.
See `tf.parameters.SEARCH_FAIL_FACTOR`.
If this *fail limit* is exceeded in cases where no positive `limit`
has been passed, you get a warning message.
Returns
-------
generator | tuple
Each result is a tuple of nodes, where each node corresponds to an
*atom*-line in your search template (see `tf.about.searchusage`).
If `limit` is not `None`, a *generator* is returned,
which yields the results one by one.
Otherwise, the results will be fetched up till `limit`
and delivered as a tuple.
Notes
-----
!!! example "Iterating over the `fetch()` generator"
You typically fetch results by saying:
i = 0
for tup in S.results():
do_something(tup[0])
do_something_else(tup[1])
Alternatively, you can set the `limit` parameter, to ask for just so many
results. They will be fetched, and when they are all collected,
returned as a tuple.
!!! example "Fetching a limited amount of results"
This
S.fetch(limit=10)
gives you the first 10 results without further ado.
"""
exe = self.exe
TF = self.api.TF
if exe is None:
error = TF.error
error('Cannot fetch if there is no previous "study()"')
else:
queryResults = exe.fetch(limit=limit)
if type(_msgCache) is list:
messages = TF.cache(_asString=True)
return (queryResults, messages)
return queryResults
def count(self, progress=None, limit=None):
"""Counts the results, with progress messages, optionally up to a limit.
Must be called after a previous `tf.search.search.Search.search()` or
`tf.search.search.Search.study()`.
Parameters
----------
progress: integer, optional, default `100`
Every once for every `progress` results a progress message is shown
when fetching results.
limit: integer, optional None
If `limit` is a positive number, it will fetch only that many results.
If it is negative, 0, None, or absent, it will fetch arbitrary many results.
!!! caution "there is an upper *fail limit* for safety reasons.
The limit is a factor times the max node in your corpus.
See `tf.parameters.SEARCH_FAIL_FACTOR`.
If this *fail limit* is exceeded in cases where no positive `limit`
has been passed, you get a warning message.
!!! note "why needed"
You typically need this in cases where result fetching turns out to
be (very) slow.
!!! caution "generator versus list"
`len(S.results())` does not work in general, because `S.results()` is
usually a generator that delivers its results as they come.
Returns
-------
None
The point of this function is to show the counting of the results
on the screen in a series of timed messages.
"""
exe = self.exe
if exe is None:
error = self.api.TF.error
error('Cannot count if there is no previous "study()"')
else:
exe.count(progress=progress, limit=limit)
def showPlan(self, details=False):
"""Show the result of the latest study of a template.
Search results are tuples of nodes and the plan shows which part of the tuple
corresponds to which part of the search template.
Only meaningful after a previous `tf.search.search.Search.study`.
Parameters
----------
details: boolean, optional False
If `True`, more information will be provided:
an overview of the search space and a description of how the results
will be retrieved.
!!! note "after S.study()"
This function is only meaningful after a call to `S.study()`.
"""
exe = self.exe
if exe is None:
error = self.api.TF.error
error('Cannot show plan if there is no previous "study()"')
else:
exe.showPlan(details=details)
def relationsLegend(self):
"""Dynamic info about the basic relations that can be used in templates.
It includes the edge features that are available in your dataset.
Returns
-------
None
The legend will be shown in the output.
"""
exe = self.exe
if exe is None:
exe = SearchExe(self.api, "")
console(exe.relationLegend)
def glean(self, tup):
"""Renders a single result into something human readable.
A search result is just a tuple of nodes that correspond to your template, as
indicated by `showPlan()`. Nodes give you access to all information that the
corpus has about it.
This function is meant to just give you a quick first impression.
Parameters
----------
tup: tuple of int
The tuple of nodes in question.
Returns
-------
string
The result indicates where the tuple occurs in terms of sections,
and what text is associated with the tuple.
Notes
-----
!!! example "Inspecting results"
This
for result in S.fetch(limit=10):
TF.info(S.glean(result))
is a handy way to get an impression of the first bunch of results.
!!! hint "Universal"
This function works on all tuples of nodes, whether they have been
obtained by search or not.
!!! hint "More ways of showing results"
The advanced API offers much better ways of showing results.
See `tf.advanced.display.show` and `tf.advanced.display.table`.
"""
T = self.api.T
F = self.api.F
E = self.api.E
fOtype = F.otype.v
slotType = F.otype.slotType
maxSlot = F.otype.maxSlot
eoslots = E.oslots.data
lR = len(tup)
if lR == 0:
return ""
fields = []
for (i, n) in enumerate(tup):
otype = fOtype(n)
words = [n] if otype == slotType else eoslots[n - maxSlot - 1]
if otype == T.sectionTypes[2]:
field = "{} {}:{}".format(*T.sectionFromNode(n))
elif otype == slotType:
field = T.text(words)
elif otype in T.sectionTypes[0:2]:
field = ""
else:
field = "{}[{}{}]".format(
otype,
T.text(words[0:5]),
"..." if len(words) > 5 else "",
)
fields.append(field)
return " ".join(fields)