You might want to consider the start of this tutorial.

In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
from tf.app import use
In [3]:
VERSION = '2017'
In [4]:
A = use('bhsa', hoist=globals(), version=VERSION)
	connecting to online GitHub repo annotation/app-bhsa ... connected
Using TF-app in /Users/dirk/text-fabric-data/annotation/app-bhsa/code:
	#d3cf8f0c2ab5d690a0fda14ea31c33da5c5c8483 (latest commit)
	connecting to online GitHub repo etcbc/bhsa ... connected
Using data in /Users/dirk/text-fabric-data/etcbc/bhsa/tf/2017:
	rv1.6 (latest release)
	connecting to online GitHub repo etcbc/phono ... connected
Using data in /Users/dirk/text-fabric-data/etcbc/phono/tf/2017:
	r1.2=#1ac68e976ee4a7f23eb6bb4c6f401a033d0ec169 (latest release)
	connecting to online GitHub repo etcbc/parallels ... connected
Using data in /Users/dirk/text-fabric-data/etcbc/parallels/tf/2017:
	r1.2=#395dfe2cb69c261862fab9f0289e594a52121d5c (latest release)

Rough edges

It might be helpful to peek under the hood, especially when exploring searches that go slow.

If you went through the previous parts of the tutorial you have encountered cases where things come to a grinding halt.

Yet we can get a hunch of what is going on, even in those cases. For that, we use the lower-level search api S of Text-Fabric, and not the wrappers that the corpus specific A api provides.

The main difference is, that S.search() returns a generator of the results, whereas A.search() returns a list of the results. In fact, A.search() calls the generator function delivered by S.search() as often as needed.

For some queries, the fetching of results is quite costly, so costly that we do not want to fetch all results up-front. Rather we want to fetch a few, to see how it goes. In these cases, directly using S.search() is preferred over A.search().

In [5]:
query = '''
book
  chapter
    verse
      phrase det=und
        word lex=>LHJM/
'''

Study

First we call S.study(query).

The syntax will be checked, features loaded, the search space will be set up, narrowed down, and the fetching of results will be prepared, but not yet executed.

In order to make the query a bit more interesting, we lift the constraint that the results must be in Genesis 1-2.

In [6]:
S.study(query)
   |     0.00s Feature overview: 111 for nodes; 8 for edges; 2 configs; 8 computed
  0.00s Checking search template ...
  0.00s Setting up search space for 5 objects ...
  0.64s Constraining search space with 4 relations ...
  0.65s 	4 edges thinned
  0.65s Setting up retrieval plan ...
  0.72s Ready to deliver results from 2735 nodes
Iterate over S.fetch() to get the results
See S.showPlan() to interpret the results

Before we rush to the results, lets have a look at the plan.

In [7]:
S.showPlan()
  3.58s The results are connected to the original search template as follows:
 0     
 1 R0  book
 2 R1    chapter
 3 R2      verse
 4 R3        phrase det=und
 5 R4          word lex=>LHJM/
 6     

Here you see already what your results will look like. Each result r is a tuple of nodes:

(R0, R1, R2, R3, R4)

that instantiate the objects in your template.

In case you are curious, you can get details about the search space as well:

In [8]:
S.showPlan(details=True)
Search with 5 objects and 4 relations
Results are instantiations of the following objects:
node  0-book                              (    29   choices)
node  1-chapter                           (   329   choices)
node  2-verse                             (   754   choices)
node  3-phrase                            (   805   choices)
node  4-word                              (   818   choices)
Performance parameters:
	yarnRatio            =     1.2
	tryLimitFrom         =     100
	tryLimitTo           =     100
Instantiations are computed along the following relations:
node                      0-book          (    29   choices)
edge  0-book          [[  1-chapter       (    11.3 choices (thinned))
edge  1-chapter       [[  2-verse         (     2.0 choices (thinned))
edge  2-verse         [[  3-phrase        (     1.1 choices (thinned))
edge  3-phrase        [[  4-word          (     1.0 choices (thinned))
  4.58s The results are connected to the original search template as follows:
 0     
 1 R0  book
 2 R1    chapter
 3 R2      verse
 4 R3        phrase det=und
 5 R4          word lex=>LHJM/
 6     

The part about the nodes shows you how many possible instantiations for each object in your template has been found. These are not results yet, because only combinations of instantiations that satisfy all constraints are results.

The constraints come from the relations between the objects that you specified. In this case, there is only an implicit relation: embedding [[. Later on we'll examine all spatial relations.

The part about the edges shows you the constraints, and in what order they will be computed when stitching results together. In this case the order is exactly the order by which the relations appear in the template, but that will not always be the case. Text-Fabric spends some time and ingenuity to find out an optimal stitch plan. Fetching results is like selecting a node, stitching it to another node with an edge, and so on, until a full stitch of nodes intersects with all the node sets from which they must be chosen (the yarns).

Fetching results may take time.

For some queries, it can take a large amount of time to walk through all results. Even worse, it may happen that it takes a large amount of time before getting the first result. During stitching, many stitchings will be tried and fail before they can be completed.

This has to do with search strategies on the one hand, and the very likely possibility to encounter pathological search patterns, which have billions of results, mostly unintended. For example, a simple query that asks for 5 words in the Hebrew Bible without further constraints, will have 425,000 to the power of 5 results. That is 10-e28 (a one with 28 zeros), roughly the number of molecules in a few hundred liters of air. That may not sound much, but it is 10,000 times the amount of bytes that can be currently stored on the whole Internet.

Text-Fabric search is not yet done with finding optimal search strategies, and I hope to refine its arsenal of methods in the future, depending on what you report.

Counting results

It is always a good idea to get a feel for the amount of results, before you dive into them head-on.

In [9]:
S.count(progress=1, limit=5)
  0.00s Counting results per 1 up to 5 ...
   |     0.01s 1
   |     0.01s 2
   |     0.01s 3
   |     0.01s 4
   |     0.01s 5
  0.01s Done: 5 results

We asked for 5 results in total, with a progress message for every one. That was a bit conservative.

In [10]:
S.count(progress=100, limit=500)
  0.00s Counting results per 100 up to 500 ...
   |     0.01s 100
   |     0.02s 200
   |     0.04s 300
   |     0.06s 400
   |     0.08s 500
  0.08s Done: 500 results

Still pretty quick, now we want to count all results.

In [11]:
S.count(progress=200, limit=-1)
  0.00s Counting results per 200 up to  the end of the results ...
   |     0.02s 200
   |     0.05s 400
   |     0.08s 600
   |     0.10s 800
  0.10s Done: 818 results

Fetching results

It is time to see something of those results.

In [12]:
S.fetch(limit=10)
Out[12]:
((426585, 426624, 1414190, 651505, 4),
 (426585, 426624, 1414191, 651515, 26),
 (426585, 426624, 1414192, 651520, 34),
 (426585, 426624, 1414193, 651528, 42),
 (426585, 426624, 1414193, 651534, 50),
 (426585, 426624, 1414194, 651538, 60),
 (426585, 426624, 1414195, 651554, 81),
 (426585, 426624, 1414196, 651564, 97),
 (426585, 426624, 1414197, 651578, 127),
 (426585, 426624, 1414198, 651590, 142))

Not very informative. Just a quick observation: look at the last column. These are the result nodes for the word part in the query, indicated as R7 by showPlan() before. And indeed, they are all below 425,000, the number of words in the Hebrew Bible.

Nevertheless, we want to glean a bit more information off them.

In [13]:
for r in S.fetch(limit=10):
    print(S.glean(r))
  Genesis 1:1 phrase[אֱלֹהִ֑ים ] אֱלֹהִ֑ים 
  Genesis 1:2 phrase[ר֣וּחַ אֱלֹהִ֔ים ] אֱלֹהִ֔ים 
  Genesis 1:3 phrase[אֱלֹהִ֖ים ] אֱלֹהִ֖ים 
  Genesis 1:4 phrase[אֱלֹהִ֛ים ] אֱלֹהִ֛ים 
  Genesis 1:4 phrase[אֱלֹהִ֔ים ] אֱלֹהִ֔ים 
  Genesis 1:5 phrase[אֱלֹהִ֤ים׀ ] אֱלֹהִ֤ים׀ 
  Genesis 1:6 phrase[אֱלֹהִ֔ים ] אֱלֹהִ֔ים 
  Genesis 1:7 phrase[אֱלֹהִים֮ ] אֱלֹהִים֮ 
  Genesis 1:8 phrase[אֱלֹהִ֛ים ] אֱלֹהִ֛ים 
  Genesis 1:9 phrase[אֱלֹהִ֗ים ] אֱלֹהִ֗ים 
Caution

It is not possible to do len(S.fetch()). Because fetch() is a generator, not a list. It will deliver a result every time it is being asked and for as long as there are results, but it does not know in advance how many there will be.

Fetching a result can be costly, because due to the constraints, a lot of possibilities may have to be tried and rejected before a the next result is found.

That is why you often see results coming in at varying speeds when counting them.

We can also use A.table() to make a list of results. This function is part of the Bhsa API, not of the generic Text-Fabric machinery, as opposed to S.glean().

So, you can use S.glean() for every Text-Fabric corpus, but the output is still not very nice. A.table() gives much nicer output, but works only for the BHSA corpus.

We put hyperlinks to SHEBANQ under column 3.

In [14]:
A.table(S.fetch(limit=10), linked=3)
np bookchapterversephraseword
1 Genesis 1:1בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ׃ אֱלֹהִ֑ים אֱלֹהִ֑ים
2 Genesis 1:2וְהָאָ֗רֶץ הָיְתָ֥ה תֹ֨הוּ֙ וָבֹ֔הוּ וְחֹ֖שֶׁךְ עַל־פְּנֵ֣י תְהֹ֑ום וְר֣וּחַ אֱלֹהִ֔ים מְרַחֶ֖פֶת עַל־פְּנֵ֥י הַמָּֽיִם׃ ר֣וּחַ אֱלֹהִ֔ים אֱלֹהִ֔ים
3 Genesis 1:3וַיֹּ֥אמֶר אֱלֹהִ֖ים יְהִ֣י אֹ֑ור וַֽיְהִי־אֹֽור׃ אֱלֹהִ֖ים אֱלֹהִ֖ים
4 Genesis 1:4וַיַּ֧רְא אֱלֹהִ֛ים אֶת־הָאֹ֖ור כִּי־טֹ֑וב וַיַּבְדֵּ֣ל אֱלֹהִ֔ים בֵּ֥ין הָאֹ֖ור וּבֵ֥ין הַחֹֽשֶׁךְ׃ אֱלֹהִ֛ים אֱלֹהִ֛ים
5 Genesis 1:4וַיַּ֧רְא אֱלֹהִ֛ים אֶת־הָאֹ֖ור כִּי־טֹ֑וב וַיַּבְדֵּ֣ל אֱלֹהִ֔ים בֵּ֥ין הָאֹ֖ור וּבֵ֥ין הַחֹֽשֶׁךְ׃ אֱלֹהִ֔ים אֱלֹהִ֔ים
6 Genesis 1:5וַיִּקְרָ֨א אֱלֹהִ֤ים׀ לָאֹור֙ יֹ֔ום וְלַחֹ֖שֶׁךְ קָ֣רָא לָ֑יְלָה וַֽיְהִי־עֶ֥רֶב וַֽיְהִי־בֹ֖קֶר יֹ֥ום אֶחָֽד׃ פ אֱלֹהִ֤ים׀ אֱלֹהִ֤ים׀
7 Genesis 1:6וַיֹּ֣אמֶר אֱלֹהִ֔ים יְהִ֥י רָקִ֖יעַ בְּתֹ֣וךְ הַמָּ֑יִם וִיהִ֣י מַבְדִּ֔יל בֵּ֥ין מַ֖יִם לָמָֽיִם׃ אֱלֹהִ֔ים אֱלֹהִ֔ים
8 Genesis 1:7וַיַּ֣עַשׂ אֱלֹהִים֮ אֶת־הָרָקִיעַ֒ וַיַּבְדֵּ֗ל בֵּ֤ין הַמַּ֨יִם֙ אֲשֶׁר֙ מִתַּ֣חַת לָרָקִ֔יעַ וּבֵ֣ין הַמַּ֔יִם אֲשֶׁ֖ר מֵעַ֣ל לָרָקִ֑יעַ וַֽיְהִי־כֵֽן׃ אֱלֹהִים֮ אֱלֹהִים֮
9 Genesis 1:8וַיִּקְרָ֧א אֱלֹהִ֛ים לָֽרָקִ֖יעַ שָׁמָ֑יִם וַֽיְהִי־עֶ֥רֶב וַֽיְהִי־בֹ֖קֶר יֹ֥ום שֵׁנִֽי׃ פ אֱלֹהִ֛ים אֱלֹהִ֛ים
10 Genesis 1:9וַיֹּ֣אמֶר אֱלֹהִ֗ים יִקָּו֨וּ הַמַּ֜יִם מִתַּ֤חַת הַשָּׁמַ֨יִם֙ אֶל־מָקֹ֣ום אֶחָ֔ד וְתֵרָאֶ֖ה הַיַּבָּשָׁ֑ה וַֽיְהִי־כֵֽן׃ אֱלֹהִ֗ים אֱלֹהִ֗ים

Slow queries

The search template above has some pretty tight constraints on one of its objects, so the amount of data to deal with is pretty limited.

If the constraints are weak, search may become slow.

For example, here is a query that looks for pairs of phrases in the same clause in such a way that one is engulfed by the other.

In [15]:
query = '''
% test
% verse book=Genesis chapter=2 verse=25
verse
  clause
                                 
    p1:phrase
      w1:word
      w3:word
      w1 < w3

    p2:phrase
      w2:word
      w1 < w2 
      w3 > w2
    
    p1 < p2   
'''

A couple of remarks you may have encountered before.

  • some objects have got a name
  • there are additional relations specified between named objects
  • < means: comes before, and >: comes after in the canonical order for nodes, which for words means: comes textually before/after, but for other nodes the meaning is explained here
  • later on we describe those relations in more detail

Note on order Look at the words w1 and w3 below phrase p1. Although in the template w1 comes before w3, this is not translated in a search constraint of the same nature.

Order between objects in a template is never significant, only embedding is.

Because order is not significant, you have to specify order yourself, using relations.

It turns out that this is better than the other way around. In MQL order is significant, and it is very difficult to search for w1 and w2 in any order. Especially if your are looking for more than 2 complex objects with lots of feature conditions, your search template would explode if you had to spell out all possible permutations. See the example of Reinoud Oosting below.

Note on gaps Look at the phrases p1 and p2. We do not specify an order here, only that they are different. In order to prevent duplicated searches with p1 and p2 interchanged, we even stipulate that p1 < p2. There are many spatial relationships possible between different objects. In many cases, neither the one comes before the other, nor vice versa. They can overlap, one can occur in a gap of the other, they can be completely disjoint and interleaved, etc.

In [16]:
# ignore this
# S.tweakPerformance(yarnRatio=2)
In [17]:
S.study(query)
   |     0.00s Feature overview: 111 for nodes; 8 for edges; 2 configs; 8 computed
  0.00s Checking search template ...
  0.00s Setting up search space for 7 objects ...
  0.28s Constraining search space with 10 relations ...
  2.76s 	6 edges thinned
  2.76s Setting up retrieval plan ...
  2.80s Ready to deliver results from 1894418 nodes
Iterate over S.fetch() to get the results
See S.showPlan() to interpret the results

Text-Fabric knows that narrowing down the search space in this case would take ages, without resulting in a significantly shrunken space. So it skips doing so for most constraints.

Let us see the plan, with details.

In [18]:
S.showPlan(details=True)
Search with 7 objects and 10 relations
Results are instantiations of the following objects:
node  0-verse                             ( 23207   choices)
node  1-clause                            ( 88051   choices)
node  2-phrase                            (252982   choices)
node  3-word                              (425732   choices)
node  4-word                              (425732   choices)
node  5-phrase                            (252982   choices)
node  6-word                              (425732   choices)
Performance parameters:
	yarnRatio            =     1.2
	tryLimitFrom         =     100
	tryLimitTo           =     100
Instantiations are computed along the following relations:
node                      0-verse         ( 23207   choices)
edge  0-verse         [[  1-clause        (     4.0 choices (thinned))
edge  1-clause        [[  2-phrase        (     2.6 choices (thinned))
edge  2-phrase        [[  3-word          (     1.7 choices (thinned))
edge  2-phrase        [[  4-word          (     1.7 choices (thinned))
edge  3-word          <   4-word          (212866.0 choices)
edge  1-clause        [[  5-phrase        (     2.9 choices (thinned))
edge  2-phrase        <   5-phrase        (126491.0 choices)
edge  5-phrase        [[  6-word          (     1.7 choices (thinned))
edge  6-word          >   3-word          (212866.0 choices)
edge  4-word          >   6-word          (212866.0 choices)
  7.51s The results are connected to the original search template as follows:
 0     
 1     % test
 2     % verse book=Genesis chapter=2 verse=25
 3 R0  verse
 4 R1    clause
 5                                      
 6 R2      p1:phrase
 7 R3          w1:word
 8 R4          w3:word
 9             w1 < w3
10     
11 R5      p2:phrase
12 R6          w2:word
13             w1 < w2 
14             w3 > w2
15         
16         p1 < p2   
17     

As you see, we have a hefty search space here. Let us play with the count() function.

In [19]:
S.count(progress=10, limit=100)
  0.00s Counting results per 10 up to 100 ...
   |     0.10s 10
   |     0.10s 20
   |     0.10s 30
   |     0.12s 40
   |     0.12s 50
   |     0.12s 60
   |     0.14s 70
   |     0.14s 80
   |     0.14s 90
   |     0.14s 100
  0.14s Done: 100 results

We can be bolder than this!

In [20]:
S.count(progress=100, limit=1000)
  0.00s Counting results per 100 up to 1000 ...
   |     0.13s 100
   |     0.15s 200
   |     0.15s 300
   |     0.27s 400
   |     0.31s 500
   |     0.31s 600
   |     0.36s 700
   |     0.46s 800
   |     0.47s 900
   |     0.61s 1000
  0.61s Done: 1000 results

OK, not too bad, but note that it takes a big fraction of a second to get just 100 results.

Now let us go for all of them by the thousand.

In [21]:
S.count(progress=1000, limit=-1)
  0.00s Counting results per 1000 up to  the end of the results ...
   |     0.58s 1000
   |     1.03s 2000
   |     1.47s 3000
   |     1.91s 4000
   |     2.29s 5000
   |     3.05s 6000
   |     4.59s 7000
  5.67s Done: 7618 results

See? This is substantial work.

In [22]:
A.table(S.fetch(limit=10))
np verseclausephrasewordwordphraseword
1 Genesis 2:25וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו וְלֹ֖א יִתְבֹּשָֽׁשׁוּ׃ וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ הָֽעֲרוּמִּ֔ים עֲרוּמִּ֔ים
2 Genesis 2:25וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו וְלֹ֖א יִתְבֹּשָֽׁשׁוּ׃ וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ אָדָ֖ם עֲרוּמִּ֔ים עֲרוּמִּ֔ים
3 Genesis 2:25וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו וְלֹ֖א יִתְבֹּשָֽׁשׁוּ׃ וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ וְעֲרוּמִּ֔ים עֲרוּמִּ֔ים
4 Genesis 2:25וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו וְלֹ֖א יִתְבֹּשָֽׁשׁוּ׃ וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו שְׁנֵיהֶם֙ אִשְׁתֹּ֑ו עֲרוּמִּ֔ים עֲרוּמִּ֔ים
5 Genesis 4:4וְהֶ֨בֶל הֵבִ֥יא גַם־ה֛וּא מִבְּכֹרֹ֥ות צֹאנֹ֖ו וּמֵֽחֶלְבֵהֶ֑ן וַיִּ֣שַׁע יְהוָ֔ה אֶל־הֶ֖בֶל וְאֶל־מִנְחָתֹֽו׃ וְהֶ֨בֶל הֵבִ֥יא גַם־ה֛וּא מִבְּכֹרֹ֥ות צֹאנֹ֖ו וּמֵֽחֶלְבֵהֶ֑ן הֶ֨בֶל גַם־ה֛וּא הֶ֨בֶל גַם־הֵבִ֥יא הֵבִ֥יא
6 Genesis 4:4וְהֶ֨בֶל הֵבִ֥יא גַם־ה֛וּא מִבְּכֹרֹ֥ות צֹאנֹ֖ו וּמֵֽחֶלְבֵהֶ֑ן וַיִּ֣שַׁע יְהוָ֔ה אֶל־הֶ֖בֶל וְאֶל־מִנְחָתֹֽו׃ וְהֶ֨בֶל הֵבִ֥יא גַם־ה֛וּא מִבְּכֹרֹ֥ות צֹאנֹ֖ו וּמֵֽחֶלְבֵהֶ֑ן הֶ֨בֶל גַם־ה֛וּא הֶ֨בֶל ה֛וּא הֵבִ֥יא הֵבִ֥יא
7 Genesis 10:21וּלְשֵׁ֥ם יֻלַּ֖ד גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־אֲחִ֖י אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר עֵ֔בֶר
8 Genesis 10:21וּלְשֵׁ֥ם יֻלַּ֖ד גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־אֲחִ֖י אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲבִי֙
9 Genesis 10:21וּלְשֵׁ֥ם יֻלַּ֖ד גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־אֲחִ֖י אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר כָּל־
10 Genesis 10:21וּלְשֵׁ֥ם יֻלַּ֖ד גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־ה֑וּא אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ גַּם־אֲחִ֖י אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר בְּנֵי־

Hand-coding

As a check, here is some code that looks for basically the same phenomenon: a phrase within the gap of another phrase. It does not use search, and it gets a bit more focused results, in half the time compared to the search with the template.

Hint If you are comfortable with programming, and what you look for is fairly generic, you may be better off without search, provided you can translate your insight in the data into an effective procedure within Text-Fabric. But wait till we are completely done with this example!

In [23]:
indent(reset=True)
info('Getting gapped phrases')
results = []
for v in F.otype.s('verse'):
    for c in L.d(v, otype='clause'):
        ps = L.d(c, otype='phrase')
        first = {}
        last = {}
        slots = {}
        # make index of phrase boundaries
        for p in ps:
            words = L.d(p, otype='word')
            first[p] = words[0]
            last[p] = words[-1]
            slots[p] = set(words)
        for p1 in ps:
            for p2 in ps:
                if p2 < p1: continue
                if len(slots[p1] & slots[p2]) != 0: continue
                if first[p1] < first[p2] and last[p2] < last[p1]:
                    results.append((v, c, p1, p2, first[p1], first[p2], last[p2], last[p1]))
info('{} results'.format(len(results)))
  0.00s Getting gapped phrases
  2.14s 368 results

Pretty printing

We can use the pretty printing of A.table() and A.show() here as well, even though we have not used search!

Not that you can show the node numbers. In this case it helps to see where the gaps are.

In [24]:
A.table(results, withNodes=True, end=10)
A.show(results, start=1, end=1)
np verseclausephrasephrasewordwordwordword
1 Genesis 2:25וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו וְלֹ֖א יִתְבֹּשָֽׁשׁוּ׃ 1414245 וַיִּֽהְי֤וּ שְׁנֵיהֶם֙ עֲרוּמִּ֔ים הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו 427767 שְׁנֵיהֶם֙ הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו 652147 עֲרוּמִּ֔ים 652148 שְׁנֵיהֶם֙ 1159 עֲרוּמִּ֔ים 1160 עֲרוּמִּ֔ים 1160 אִשְׁתֹּ֑ו 1164
2 Genesis 4:4וְהֶ֨בֶל הֵבִ֥יא גַם־ה֛וּא מִבְּכֹרֹ֥ות צֹאנֹ֖ו וּמֵֽחֶלְבֵהֶ֑ן וַיִּ֣שַׁע יְהוָ֔ה אֶל־הֶ֖בֶל וְאֶל־מִנְחָתֹֽו׃ 1414273 וְהֶ֨בֶל הֵבִ֥יא גַם־ה֛וּא מִבְּכֹרֹ֥ות צֹאנֹ֖ו וּמֵֽחֶלְבֵהֶ֑ן 427889 הֶ֨בֶל גַם־ה֛וּא 652504 הֵבִ֥יא 652505 הֶ֨בֶל 1720 הֵבִ֥יא 1721 הֵבִ֥יא 1721 ה֛וּא 1723
3 Genesis 10:21וּלְשֵׁ֥ם יֻלַּ֖ד גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ 1414445 גַּם־ה֑וּא אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ 428386 גַּם־ה֑וּא אֲחִ֖י יֶ֥פֶת הַגָּדֹֽול׃ 654102 אֲבִי֙ כָּל־בְּנֵי־עֵ֔בֶר 654103 גַּם־ 4819 אֲבִי֙ 4821 עֵ֔בֶר 4824 גָּדֹֽול׃ 4828
4 Genesis 12:17וַיְנַגַּ֨ע יְהוָ֧ה׀ אֶת־פַּרְעֹ֛ה נְגָעִ֥ים גְּדֹלִ֖ים וְאֶת־בֵּיתֹ֑ו עַל־דְּבַ֥ר שָׂרַ֖י אֵ֥שֶׁת אַבְרָֽם׃ 1414505 וַיְנַגַּ֨ע יְהוָ֧ה׀ אֶת־פַּרְעֹ֛ה נְגָעִ֥ים גְּדֹלִ֖ים וְאֶת־בֵּיתֹ֑ו עַל־דְּבַ֥ר שָׂרַ֖י אֵ֥שֶׁת אַבְרָֽם׃ 428569 אֶת־פַּרְעֹ֛ה וְאֶת־בֵּיתֹ֑ו 654678 נְגָעִ֥ים גְּדֹלִ֖ים 654679 אֶת־ 5803 נְגָעִ֥ים 5805 גְּדֹלִ֖ים 5806 בֵּיתֹ֑ו 5809
5 Genesis 13:1וַיַּעַל֩ אַבְרָ֨ם מִמִּצְרַ֜יִם ה֠וּא וְאִשְׁתֹּ֧ו וְכָל־אֲשֶׁר־לֹ֛ו וְלֹ֥וט עִמֹּ֖ו הַנֶּֽגְבָּה׃ 1414509 וַיַּעַל֩ אַבְרָ֨ם מִמִּצְרַ֜יִם ה֠וּא וְאִשְׁתֹּ֧ו וְכָל־הַנֶּֽגְבָּה׃ 428585 אַבְרָ֨ם ה֠וּא וְאִשְׁתֹּ֧ו וְכָל־ 654725 מִמִּצְרַ֜יִם 654726 אַבְרָ֨ם 5868 מִ 5869 מִּצְרַ֜יִם 5870 כָל־ 5875
6 Genesis 14:16וַיָּ֕שֶׁב אֵ֖ת כָּל־הָרְכֻ֑שׁ וְגַם֩ אֶת־לֹ֨וט אָחִ֤יו וּרְכֻשֹׁו֙ הֵשִׁ֔יב וְגַ֥ם אֶת־הַנָּשִׁ֖ים וְאֶת־הָעָֽם׃ 1414542 וְגַם֩ אֶת־לֹ֨וט אָחִ֤יו וּרְכֻשֹׁו֙ הֵשִׁ֔יב וְגַ֥ם אֶת־הַנָּשִׁ֖ים וְאֶת־הָעָֽם׃ 428692 גַם֩ אֶת־לֹ֨וט אָחִ֤יו וּרְכֻשֹׁו֙ וְגַ֥ם אֶת־הַנָּשִׁ֖ים וְאֶת־הָעָֽם׃ 655061 הֵשִׁ֔יב 655062 גַם֩ 6515 הֵשִׁ֔יב 6521 הֵשִׁ֔יב 6521 עָֽם׃ 6530
7 Genesis 17:7וַהֲקִמֹתִ֨י אֶת־בְּרִיתִ֜י בֵּינִ֣י וּבֵינֶ֗ךָ וּבֵ֨ין זַרְעֲךָ֧ אַחֲרֶ֛יךָ לְדֹרֹתָ֖ם לִבְרִ֣ית עֹולָ֑ם לִהְיֹ֤ות לְךָ֙ לֵֽאלֹהִ֔ים וּֽלְזַרְעֲךָ֖ אַחֲרֶֽיךָ׃ 1414594 לִהְיֹ֤ות לְךָ֙ לֵֽאלֹהִ֔ים וּֽלְזַרְעֲךָ֖ אַחֲרֶֽיךָ׃ 428886 לְךָ֙ וּֽלְזַרְעֲךָ֖ אַחֲרֶֽיךָ׃ 655642 לֵֽאלֹהִ֔ים 655643 לְךָ֙ 7431 לֵֽ 7432 אלֹהִ֔ים 7433 אַחֲרֶֽיךָ׃ 7437
8 Genesis 19:4טֶרֶם֮ יִשְׁכָּבוּ֒ וְאַנְשֵׁ֨י הָעִ֜יר אַנְשֵׁ֤י סְדֹם֙ נָסַ֣בּוּ עַל־הַבַּ֔יִת מִנַּ֖עַר וְעַד־זָקֵ֑ן כָּל־הָעָ֖ם מִקָּצֶֽה׃ 1414651 וְאַנְשֵׁ֨י הָעִ֜יר אַנְשֵׁ֤י סְדֹם֙ נָסַ֣בּוּ עַל־הַבַּ֔יִת מִנַּ֖עַר וְעַד־זָקֵ֑ן כָּל־הָעָ֖ם מִקָּצֶֽה׃ 429128 אַנְשֵׁ֨י הָעִ֜יר אַנְשֵׁ֤י סְדֹם֙ מִנַּ֖עַר וְעַד־זָקֵ֑ן כָּל־הָעָ֖ם מִקָּצֶֽה׃ 656353 נָסַ֣בּוּ 656354 אַנְשֵׁ֨י 8502 נָסַ֣בּוּ 8507 נָסַ֣בּוּ 8507 קָּצֶֽה׃ 8520
9 Genesis 19:4טֶרֶם֮ יִשְׁכָּבוּ֒ וְאַנְשֵׁ֨י הָעִ֜יר אַנְשֵׁ֤י סְדֹם֙ נָסַ֣בּוּ עַל־הַבַּ֔יִת מִנַּ֖עַר וְעַד־זָקֵ֑ן כָּל־הָעָ֖ם מִקָּצֶֽה׃ 1414651 וְאַנְשֵׁ֨י הָעִ֜יר אַנְשֵׁ֤י סְדֹם֙ נָסַ֣בּוּ עַל־הַבַּ֔יִת מִנַּ֖עַר וְעַד־זָקֵ֑ן כָּל־הָעָ֖ם מִקָּצֶֽה׃ 429128 אַנְשֵׁ֨י הָעִ֜יר אַנְשֵׁ֤י סְדֹם֙ מִנַּ֖עַר וְעַד־זָקֵ֑ן כָּל־הָעָ֖ם מִקָּצֶֽה׃ 656353 עַל־הַבַּ֔יִת 656355 אַנְשֵׁ֨י 8502 עַל־ 8508 בַּ֔יִת 8510 קָּצֶֽה׃ 8520
10 Genesis 22:3וַיַּשְׁכֵּ֨ם אַבְרָהָ֜ם בַּבֹּ֗קֶר וַֽיַּחֲבֹשׁ֙ אֶת־חֲמֹרֹ֔ו וַיִּקַּ֞ח אֶת־שְׁנֵ֤י נְעָרָיו֙ אִתֹּ֔ו וְאֵ֖ת יִצְחָ֣ק בְּנֹ֑ו וַיְבַקַּע֙ עֲצֵ֣י עֹלָ֔ה וַיָּ֣קָם וַיֵּ֔לֶךְ אֶל־הַמָּקֹ֖ום אֲשֶׁר־אָֽמַר־לֹ֥ו הָאֱלֹהִֽים׃ 1414740 וַיִּקַּ֞ח אֶת־שְׁנֵ֤י נְעָרָיו֙ אִתֹּ֔ו וְאֵ֖ת יִצְחָ֣ק בְּנֹ֑ו 429497 אֶת־שְׁנֵ֤י נְעָרָיו֙ וְאֵ֖ת יִצְחָ֣ק בְּנֹ֑ו 657505 אִתֹּ֔ו 657506 אֶת־ 10284 אִתֹּ֔ו 10287 אִתֹּ֔ו 10287 בְּנֹ֑ו 10291

result 1

sentence 59|151
clause WayX
sentence 60|152
clause WxY0
phrase Conj CP
conj and
phrase Nega NegP
phrase Pred VP

NB Gaps are a tricky phenomenon. In gaps we will deal with them cruelly.

Performance tuning

Here is an example by Yanniek van der Schans (2018-09-21).

In [27]:
query = '''
c:clause
  PreGap:phrase_atom
  LastPhrase:phrase_atom
  :=

Gap:clause_atom
  :: word

PreGap < Gap
Gap < LastPhrase
c || Gap
'''
In [26]:
S.study(query)
S.showPlan(details=True)
   |     0.00s Feature overview: 111 for nodes; 8 for edges; 2 configs; 8 computed
  0.00s Checking search template ...
  0.00s Setting up search space for 5 objects ...
  0.16s Constraining search space with 8 relations ...
  0.79s 	2 edges thinned
  0.79s Setting up retrieval plan ...
  0.81s Ready to deliver results from 454123 nodes
Iterate over S.fetch() to get the results
See S.showPlan() to interpret the results
Search with 5 objects and 8 relations
Results are instantiations of the following objects:
node  0-clause                            ( 88101   choices)
node  1-phrase_atom                       (267519   choices)
node  2-phrase_atom                       ( 88101   choices)
node  3-clause_atom                       (  5201   choices)
node  4-word                              (  5201   choices)
Performance parameters:
	yarnRatio            =     1.2
	tryLimitFrom         =     100
	tryLimitTo           =     100
Instantiations are computed along the following relations:
node                      3-clause_atom   (  5201   choices)
edge  3-clause_atom   ::  4-word          (     1.0 choices (thinned))
edge  4-word          ]]  3-clause_atom   (     1.0 choices)
edge  3-clause_atom   <   2-phrase_atom   ( 44050.5 choices)
edge  2-phrase_atom   :=  0-clause        (     1.0 choices (thinned))
edge  0-clause        [[  2-phrase_atom   (     1.0 choices)
edge  0-clause        ||  3-clause_atom   (  4680.9 choices)
edge  0-clause        [[  1-phrase_atom   (     3.1 choices)
edge  1-phrase_atom   <   3-clause_atom   (  2600.5 choices)
  0.82s The results are connected to the original search template as follows:
 0     
 1 R0  c:clause
 2 R1   PreGap:phrase_atom
 3 R2   LastPhrase:phrase_atom
 4      :=
 5     
 6 R3  Gap:clause_atom
 7 R4   :: word
 8     
 9     PreGap < Gap
10     Gap < LastPhrase
11     c || Gap
12     
In [28]:
S.count(progress=1, limit=4)
  0.00s Counting results per 1 up to 4 ...
   |     1.84s 1
   |     8.54s 2
   |       29s 3
   |       32s 4
    32s Done: 4 results

Can we do better?

The performance parameter yarnRatio can be used to increase the amount of preprocessing, and we can increase to number of random samples that we make by tryLimitFrom and tryLimitTo.

We start with increasing the amount of up-front edge-spinning.

In [33]:
S.tweakPerformance(yarnRatio=0.2, tryLimitFrom=10000, tryLimitTo=10000)
Performance parameters, current values:
	tryLimitFrom         =   10000
	tryLimitTo           =   10000
	yarnRatio            =     0.2
In [34]:
S.study(query)
S.showPlan(details=True)
   |     0.00s Feature overview: 111 for nodes; 8 for edges; 2 configs; 8 computed
  0.00s Checking search template ...
  0.00s Setting up search space for 5 objects ...
  0.15s Constraining search space with 8 relations ...
  1.03s 	2 edges thinned
  1.03s Setting up retrieval plan ...
  1.25s Ready to deliver results from 454123 nodes
Iterate over S.fetch() to get the results
See S.showPlan() to interpret the results
Search with 5 objects and 8 relations
Results are instantiations of the following objects:
node  0-clause                            ( 88101   choices)
node  1-phrase_atom                       (267519   choices)
node  2-phrase_atom                       ( 88101   choices)
node  3-clause_atom                       (  5201   choices)
node  4-word                              (  5201   choices)
Performance parameters:
	yarnRatio            =     0.2
	tryLimitFrom         =   10000
	tryLimitTo           =   10000
Instantiations are computed along the following relations:
node                      3-clause_atom   (  5201   choices)
edge  3-clause_atom   ::  4-word          (     1.0 choices (thinned))
edge  4-word          ]]  3-clause_atom   (     1.0 choices)
edge  3-clause_atom   <   2-phrase_atom   ( 44050.5 choices)
edge  2-phrase_atom   :=  0-clause        (     1.0 choices (thinned))
edge  0-clause        [[  2-phrase_atom   (     1.0 choices)
edge  0-clause        ||  3-clause_atom   (  4680.9 choices)
edge  0-clause        [[  1-phrase_atom   (     3.0 choices)
edge  1-phrase_atom   <   3-clause_atom   (  2600.5 choices)
  1.26s The results are connected to the original search template as follows:
 0     
 1 R0  c:clause
 2 R1   PreGap:phrase_atom
 3 R2   LastPhrase:phrase_atom
 4      :=
 5     
 6 R3  Gap:clause_atom
 7 R4   :: word
 8     
 9     PreGap < Gap
10     Gap < LastPhrase
11     c || Gap
12     

It seems to be the same plan. No improvement.

What if we decrease the amount of edge spinning?

In [35]:
S.tweakPerformance(yarnRatio=5, tryLimitFrom=10000, tryLimitTo=10000)
Performance parameters, current values:
	tryLimitFrom         =   10000
	tryLimitTo           =   10000
	yarnRatio            =       5
In [36]:
S.study(query)
S.showPlan(details=True)
   |     0.00s Feature overview: 111 for nodes; 8 for edges; 2 configs; 8 computed
  0.00s Checking search template ...
  0.00s Setting up search space for 5 objects ...
  0.16s Constraining search space with 8 relations ...
  0.60s 	2 edges thinned
  0.60s Setting up retrieval plan ...
  0.82s Ready to deliver results from 454123 nodes
Iterate over S.fetch() to get the results
See S.showPlan() to interpret the results
Search with 5 objects and 8 relations
Results are instantiations of the following objects:
node  0-clause                            ( 88101   choices)
node  1-phrase_atom                       (267519   choices)
node  2-phrase_atom                       ( 88101   choices)
node  3-clause_atom                       (  5201   choices)
node  4-word                              (  5201   choices)
Performance parameters:
	yarnRatio            =       5
	tryLimitFrom         =   10000
	tryLimitTo           =   10000
Instantiations are computed along the following relations:
node                      3-clause_atom   (  5201   choices)
edge  3-clause_atom   ::  4-word          (     1.0 choices (thinned))
edge  4-word          ]]  3-clause_atom   (     1.0 choices)
edge  3-clause_atom   <   2-phrase_atom   ( 44050.5 choices)
edge  2-phrase_atom   :=  0-clause        (     1.0 choices (thinned))
edge  0-clause        [[  2-phrase_atom   (     1.0 choices)
edge  0-clause        ||  3-clause_atom   (  4680.9 choices)
edge  0-clause        [[  1-phrase_atom   (     3.0 choices)
edge  1-phrase_atom   <   3-clause_atom   (  2600.5 choices)
  0.83s The results are connected to the original search template as follows:
 0     
 1 R0  c:clause
 2 R1   PreGap:phrase_atom
 3 R2   LastPhrase:phrase_atom
 4      :=
 5     
 6 R3  Gap:clause_atom
 7 R4   :: word
 8     
 9     PreGap < Gap
10     Gap < LastPhrase
11     c || Gap
12     

No change either.

We'll look for queries where the parameters matter more in the future.

Next

You have seen cases where the implementation is to blame.

Now I want to point to gaps in your understanding: gaps


basic advanced sets relations quantifiers rough gaps