%load_ext autoreload
%autoreload 2
from tf.fabric import Fabric
from tf.extra.bhsa import Bhsa
VERSION = '2017'
DATABASE = '~/github/etcbc'
BHSA = f'bhsa/tf/{VERSION}'
PARA = f'parallels/tf/{VERSION}'
TF = Fabric(locations=[DATABASE], modules=[BHSA, PARA], silent=True )
api = TF.load('', silent=True)
api.makeAvailableIn(globals())
B = Bhsa(api, 'search', version=VERSION)
Documentation: BHSA Feature docs BHSA API Text-Fabric API 5.0.4 Search Reference
Searches often do not deliver the results you expect. Besides typos, lack of familiarity with the template formalism and bugs in the system, there is another cause: difficult semantics of the data.
Most users reason about phrases, clauses and sentences as if they are consecutive blocks of words. But in the BHSA this is not the case: each of these objects may have gaps.
Most of the time, verse boundaries coincide with the boundaries of sentences, clauses, and phrases. But not always, there are verse spanning sentences.
These phenomena may wreak havoc with your intuitive reasoning about what search templates should deliver.
We are going to show these issues in depth.
Search has no direct primitives to deal with gaps. Nodes correspond to textual objects such as word, phrases, clauses, verses, book. Usually these are consecutive sequences of words, but in theory they can be arbitrary sets of slots.
And, as far as the BHSA corpus is concerned, in practice too. If we look at phrases, then the overwhelming majority is consecutive, so without gaps, but gaps in phrases do occur and are not even exceptional.
People that are familiar with MQL (see fromMQL may remember that in MQL you can search for a gap. The MQL query
SELECT ALL OBJECTS WHERE
[phrase FOCUS
[word lex='L']
[gap]
]
looks for a phrase with a gap in it (i.e. one or more consecutive words between the start and the end of the phrase that do not belong to the phrase). The query then asks additionally for those gap-containing phrases that have a certain word in front of the gap.
We want this too!
Note
The fact that sentences, clauses, and phrases may not be coherent chunks wreaks havoc with your basic intuitions about textual objects. Our query templates do not require the objects to be consecutive and still they make sense. But that might not be your sense, unless you Mind the gap!
We start with a query that aims to get the same results as the MQL query above.
A phrase p
is gapped, if there is a word between two of its words that does not belong to it.
In our template, we require that there is a word wPreGrap
in the phrase that is just before the gap,
a word wGap
that comes right after, so it is in the gap, and hence does not belong to the phrase.
But this all must happen before the last word wLast
of the phrase.
query = '''
verse
p:phrase
wPreGap:word lex=L
wLast:word
:=
wGap:word
wPreGap <: wGap
wGap < wLast
p || wGap
'''
results = B.search(query)
1.13s 13 results
Nice and quick. Let's see the results.
B.table(results)
n | verse | phrase | word | word | word |
---|---|---|---|---|---|
1 | Genesis 17:7 | לְךָ֙ וּֽלְזַרְעֲךָ֖ אַחֲרֶֽיךָ׃ | לְךָ֙ | אַחֲרֶֽיךָ׃ | לֵֽ |
2 | Genesis 28:4 | לְךָ֙ לְךָ֖ וּלְזַרְעֲךָ֣ אִתָּ֑ךְ | לְךָ֙ | אִתָּ֑ךְ | אֶת־ |
3 | Genesis 31:16 | לָ֥נוּ וּלְבָנֵ֑ינוּ | לָ֥נוּ | בָנֵ֑ינוּ | ה֖וּא |
4 | Exodus 30:21 | לָהֶ֧ם לֹ֥ו וּלְזַרְעֹ֖ו | לָהֶ֧ם | זַרְעֹ֖ו | חָק־ |
5 | Leviticus 25:6 | לָכֶם֙ לְךָ֖ וּלְעַבְדְּךָ֣ וְלַאֲמָתֶ֑ךָ וְלִשְׂכִֽירְךָ֙ וּלְתֹושָׁ֣בְךָ֔ | לָכֶם֙ | תֹושָׁ֣בְךָ֔ | לְ |
6 | Numbers 20:15 | לָ֛נוּ וְלַאֲבֹתֵֽינוּ׃ | לָ֛נוּ | אֲבֹתֵֽינוּ׃ | מִצְרַ֖יִם |
7 | Numbers 32:33 | לָהֶ֣ם׀ לִבְנֵי־גָד֩ וְלִבְנֵ֨י רְאוּבֵ֜ן וְלַחֲצִ֣י׀ שֵׁ֣בֶט׀ מְנַשֶּׁ֣ה בֶן־יֹוסֵ֗ף | לָהֶ֣ם׀ | יֹוסֵ֗ף | מֹשֶׁ֡ה |
8 | Deuteronomy 1:36 | לֹֽו־וּלְבָנָ֑יו | לֹֽו־ | בָנָ֑יו | אֶתֵּ֧ן |
9 | Deuteronomy 26:11 | לְךָ֛ וּלְבֵיתֶ֑ךָ | לְךָ֛ | בֵיתֶ֑ךָ | יְהוָ֥ה |
10 | 1_Samuel 25:31 | לְךָ֡ לַאדֹנִ֗י | לְךָ֡ | אדֹנִ֗י | לְ |
11 | 2_Kings 25:24 | לָהֶ֤ם וּלְאַנְשֵׁיהֶ֔ם | לָהֶ֤ם | אַנְשֵׁיהֶ֔ם | גְּדַלְיָ֨הוּ֙ |
12 | Jeremiah 40:9 | לָהֶ֜ם וּלְאַנְשֵׁיהֶ֣ם | לָהֶ֜ם | אַנְשֵׁיהֶ֣ם | גְּדַלְיָ֨הוּ |
13 | Daniel 9:8 | לָ֚נוּ לִמְלָכֵ֥ינוּ לְשָׂרֵ֖ינוּ וְלַאֲבֹתֵ֑ינוּ | לָ֚נוּ | אֲבֹתֵ֑ינוּ | בֹּ֣שֶׁת |
Let's color the word in the gap differently.
B.show(results, end=3, condensed=False, colorMap={2: 'lightyellow', 3: 'yellow', 5: 'magenta'})
These were particular gaps. Now we want to get all gapped phrases.
We can just lift the special requirement that
the preGapWord
has to satisfy a special lexical condition.
query = '''
p:phrase
wPreGap:word
wLast:word
:=
wGap:word
wPreGap <: wGap
wGap < wLast
p || wGap
'''
results = B.search(query)
3.55s 715 results
Not too bad! We could wait for it. Here are some results.
B.table(results, end=10)
n | phrase | word | word | word |
---|---|---|---|---|
1 | בֵּ֤ין הַמַּ֨יִם֙ וּבֵ֣ין הַמַּ֔יִם | מַּ֨יִם֙ | מַּ֔יִם | אֲשֶׁר֙ |
2 | דֶּ֔שֶׁא עֵ֚שֶׂב עֵ֣ץ פְּרִ֞י | עֵ֚שֶׂב | פְּרִ֞י | מַזְרִ֣יעַ |
3 | דֶּ֠שֶׁא עֵ֣שֶׂב וְעֵ֧ץ | עֵ֣שֶׂב | עֵ֧ץ | מַזְרִ֤יעַ |
4 | אֶת־כָּל־עֵ֣שֶׂב׀ וְאֶת־כָּל־הָעֵ֛ץ | עֵ֣שֶׂב׀ | עֵ֛ץ | זֹרֵ֣עַ |
5 | שְׁנֵיהֶם֙ הָֽאָדָ֖ם וְאִשְׁתֹּ֑ו | שְׁנֵיהֶם֙ | אִשְׁתֹּ֑ו | עֲרוּמִּ֔ים |
6 | הֶ֨בֶל גַם־ה֛וּא | הֶ֨בֶל | ה֛וּא | הֵבִ֥יא |
7 | מִן־הַבְּהֵמָה֙ הַטְּהֹורָ֔ה וּמִן־הַ֨בְּהֵמָ֔ה וּמִ֨ן־הָעֹ֔וף וְכֹ֥ל | בְּהֵמָ֔ה | כֹ֥ל | אֲשֶׁ֥ר |
8 | הֵ֜מָּה וְכָל־הַֽחַיָּ֣ה לְמִינָ֗הּ וְכָל־הַבְּהֵמָה֙ לְמִינָ֔הּ וְכָל־הָרֶ֛מֶשׂ לְמִינֵ֑הוּ וְכָל־הָעֹ֣וף לְמִינֵ֔הוּ כֹּ֖ל צִפֹּ֥ור כָּל־כָּנָֽף׃ | רֶ֛מֶשׂ | כָּנָֽף׃ | הָ |
9 | כָּל־בָּשָׂ֣ר׀ בָּעֹ֤וף וּבַבְּהֵמָה֙ וּבַ֣חַיָּ֔ה וּבְכָל־הַשֶּׁ֖רֶץ וְכֹ֖ל הָאָדָֽם׃ | בָּשָׂ֣ר׀ | אָדָֽם׃ | הָ |
10 | כָּל־בָּשָׂ֣ר׀ בָּעֹ֤וף וּבַבְּהֵמָה֙ וּבַ֣חַיָּ֔ה וּבְכָל־הַשֶּׁ֖רֶץ וְכֹ֖ל הָאָדָֽם׃ | שֶּׁ֖רֶץ | אָדָֽם׃ | הַ |
If a phrase has multiple gaps, we encounter it multiple times in our results.
We show the two condensed results in Genesis 7:21.
B.show(results, condensed=True, start=9, end=10, colorMap={1: 'lightyellow', 2: 'yellow', 4: 'magenta'})
If we want just the phrases, and only once, we can run the query in shallow mode, see advanced:
gapQueryResults = B.search(query, shallow=True)
3.49s 671 results
We can make an equivalent query to get the gaps.
query = '''
p:phrase
=: wFirst:word
wLast:word
:=
wGap:word
wFirst < wGap
wLast > wGap
p || wGap
'''
Experience has shown that this is a slow query, so we handle it with care.
S.study(query)
S.count(progress=1, limit=8)
| 0.00s Feature overview: 109 for nodes; 8 for edges; 1 configs; 7 computed 0.00s Checking search template ... 0.00s Setting up search space for 4 objects ... 0.38s Constraining search space with 7 relations ... 0.40s Setting up retrieval plan ... 0.45s Ready to deliver results from 1532939 nodes Iterate over S.fetch() to get the results See S.showPlan() to interpret the results 0.00s Counting results per 1 up to 8 ... | 43s 1 | 43s 2 | 43s 3 | 43s 4 | 43s 5 | 43s 6 | 1m 16s 7 | 1m 16s 8 1m 16s Done: 8 results
This is a good example of a query that is slow to deliver even its first result. And that is bad, because it is such a straight-forward query.
Why is this one so slow, while the previous one went so smoothly?
The crucial thing is the wGap
word. In the latter template, wGap
is not embedded in anything.
It is constrained by wFirst < wGap
and wGap < wLast
.
However, the way the search strategy works is by examining all possibilities for wFirst < wGap
and only then checking whether wGap < wLast
.
The algorithm cannot check both conditions at the same time.
With embedding relations, things are better. Text-Fabric is heavily optimized to deal with embedding relationships.
In the former template, we see that the wGap
is required to be adjacent
to wPreGap
, and this one
is embedded in the phrase. Hence there are few cases to consider for wPreGap
, and per instance
there is only one wGap
.
Lesson
Try to prevent the use of free floating nodes in your template that become constrained by other spatial relationships than embedding.
The former template had it right. Can we rescue the latter template?
We can assume that the phrase and the gap both contain a word that belongs to the same verse. Note that phrase and gap may belong to different clauses and sentences. We assume that a phrase cannot belong to more than two verses, so either the first or the last word of the phrase is in the same verse as a word in the gap.
query = '''
p:phrase
=: wFirst:word
wLast:word
:=
wGap:word
wFirst < wGap
wLast > wGap
p || wGap
v:verse
v [[ wFirst
v [[ wGap
'''
S.study(query)
S.count(progress=100, limit=3000)
| 0.00s Feature overview: 109 for nodes; 8 for edges; 1 configs; 7 computed 0.00s Checking search template ... 0.00s Setting up search space for 5 objects ... 0.36s Constraining search space with 9 relations ... 0.38s Setting up retrieval plan ... 0.45s Ready to deliver results from 1556152 nodes Iterate over S.fetch() to get the results See S.showPlan() to interpret the results 0.00s Counting results per 100 up to 3000 ... | 0.50s 100 | 1.08s 200 | 1.48s 300 | 1.67s 400 | 1.81s 500 | 2.17s 600 | 2.25s 700 | 2.67s 800 | 2.81s 900 | 3.03s 1000 | 3.29s 1100 | 3.48s 1200 | 3.78s 1300 | 4.70s 1400 | 5.44s 1500 | 5.89s 1600 | 6.43s 1700 | 6.68s 1800 | 7.15s 1900 | 7.68s 2000 | 8.13s 2100 | 8.44s 2200 | 9.20s 2300 | 11s 2400 | 12s 2500 | 12s 2600 | 13s 2700 13s Done: 2707 results
We are going to run this query in shallow
mode.
results = B.search(query, shallow=True)
14s 671 results
Shallow mode tends to be quicker, but that does not always materialize. The number of results agrees with the first query. Yet we have been lucky, because we required the word in the gap to be in the same verse as the first word in the phrase. What if we require if it is the last word in the phrase?
query = '''
p:phrase
=: wFirst:word
wLast:word
:=
wGap:word
wFirst < wGap
wLast > wGap
p || wGap
v:verse
v [[ wLast
v [[ wGap
'''
results = B.search(query, shallow=True)
42s 660 results
Then we would not have found all results.
So, this road, although doable, is much less comfortable, performance-wise and logic-wise.
In this misty landscape of gaps we need some corroboration that we found the right results.
gapQueryResults
a phrase?gapQueryResults
have a gap?gapQueryResults
?We check all this by hand coding.
Here is a function that checks whether a phrase has a gap. If the distance between its end points is greater than the number of words it contains, it must have a gap.
def hasGap(p):
words = L.d(p, otype='word')
return words[-1] - words[0] + 1 > len(words)
Now we can perform the checks.
otypesGood = True
haveGaps = True
for p in gapQueryResults:
otype = F.otype.v(p)
if otype != 'phrase':
print(f'Non phrase detected: {p}) is a {otype}')
otypesGood = False
break
if not hasGap(p):
print(f'Phrase without a gap: {p}')
B.pretty(p)
haveGaps = False
break
print(f'{len(gapQueryResults)} nodes in query result')
if otypesGood:
print('1. all nodes are phrases')
if haveGaps:
print('2. all nodes have gaps')
inResults = True
for p in F.otype.s('phrase'):
if hasGap(p):
if p not in gapQueryResults:
print(f'Gapped phrase outside query results: {p}')
B.pretty(p)
inResults = False
break
if inResults:
print('3. all gapped phrases are contained in the results')
671 nodes in query result 1. all nodes are phrases 2. all nodes have gaps 3. all gapped phrases are contained in the results
Note that by hand coding we can get the gapped phrases much more quickly and securely!
We have obtained a set with all gapped phrases, and we have paid a price:
It would be nice if we could kick-start our queries using this set as a given. And that is exactly what we are going to do now.
We make to custom sets and give them a name, one for gapped phrases and one for non-gapped phrases.
customSets = dict(
gapphrase=gapQueryResults,
conphrase=set(F.otype.s('phrase')) - gapQueryResults,
)
Suppose we want all verbs that occur in a gapped phrase.
query = '''
gapphrase
word sp=verb
'''
Note that we have used the foreign name gapphrase
in our search template, instead of phrase
.
But we can still run search()
, provided we tell it what we mean by gapphrase
.
We do that by passing the sets
parameter to search()
, which should be a dictionary of sets.
Search will look up gapphrase
in this dictionary, and will use its value, which should be a node set.
That way, it understands that the expression gapphrase
stands for the nodes in the given node set.
Here we go:
results = B.search(query, sets=customSets)
1.03s 93 results
B.show(results, start=1, end=3)
That looks good.
We can also apply feature conditions to gapphrase
:
query = '''
gapphrase function=Subj
'''
results = B.search(query, sets=customSets)
B.show(results, start=1, end=3)
We can find the gaps, but do our minds always reckon with gaps? Gaps cause unexpected semantics. Here is a little puzzle.
Suppose we want to count the clauses consisting of exactly two phrases.
Here follows a little journey. We use a query to find the clauses, check the result with hand-coding, scratch our heads, refine the query, the hand-coding and our question until we are satisfied.
The following template should do it: a clause, starting with a phrase, followed by an adjacent phrase, which terminates the clause.
query = '''
clause
=: phrase
<: phrase
:=
'''
results = B.search(query)
B.table(results, end=10)
1.11s 23483 results
n | clause | phrase | phrase |
---|---|---|---|
1 | יְהִ֣י אֹ֑ור | יְהִ֣י | אֹ֑ור |
2 | כִּי־טֹ֑וב | כִּי־ | טֹ֑וב |
3 | אֲשֶׁר֙ מִתַּ֣חַת לָרָקִ֔יעַ | אֲשֶׁר֙ | מִתַּ֣חַת לָרָקִ֔יעַ |
4 | אֲשֶׁ֖ר מֵעַ֣ל לָרָקִ֑יעַ | אֲשֶׁ֖ר | מֵעַ֣ל לָרָקִ֑יעַ |
5 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
6 | מַזְרִ֣יעַ זֶ֔רַע | מַזְרִ֣יעַ | זֶ֔רַע |
7 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
8 | לְהַבְדִּ֕יל בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה | לְהַבְדִּ֕יל | בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה |
9 | לְהָאִ֖יר עַל־הָאָ֑רֶץ | לְהָאִ֖יר | עַל־הָאָ֑רֶץ |
10 | לְהָאִ֖יר עַל־הָאָֽרֶץ׃ | לְהָאִ֖יר | עַל־הָאָֽרֶץ׃ |
If we want to have the clauses only, we run it in shallow mode:
clausesByQuery = sorted(B.search(query, shallow=True))
1.06s 23483 results
Let us check this with a piece of hand-written code. We want clauses that consist of exactly two phrases.
indent(reset=True)
info('counting ...')
clausesByHand = []
for clause in F.otype.s('clause'):
phrases = L.d(clause, otype='phrase')
if len(phrases) == 2:
clausesByHand.append(clause)
clausesByHand = sorted(clausesByHand)
info(f'Done: found {len(clausesByHand)}')
0.00s counting ... 0.82s Done: found 23862
Strange, we end up with too many cases. What is happening? Let us compare the results. We look at the first result where both methods diverge.
We put the difference finding in a little function.
def showDiff(queryResults, handResults):
diff = [x for x in zip(queryResults, handResults) if x[0] != x[1]]
if not diff:
print(f'''
{len(queryResults):>6} queryResults
are identical with
{len(handResults):>6} handResults
''')
return
(rQuery, rHand) = diff[0]
if rQuery < rHand:
print(f'clause {rQuery} is a query result but not found by hand')
toShow = rQuery
else:
print(f'clause {rHand} is not a query result but has been found by hand')
toShow = rHand
colors = ['aqua', 'aquamarine', 'khaki', 'lavender', 'yellow']
highlights = {}
for (i, phrase) in enumerate(L.d(toShow, otype='phrase')):
highlights[phrase] = colors[i % len(colors)]
# for atom in L.d(phrase, otype='phrase_atom'):
# highlights[atom] = colors[i % len(colors)]
B.pretty(toShow, withNodes=True, suppress={'lex', 'sp', 'vt', 'vs'}, highlights=highlights)
showDiff(clausesByQuery, clausesByHand)
Lo and behold:
indent(reset=True)
info('counting ...')
clausesByHand2 = []
for clause in F.otype.s('clause'):
phrases = L.d(clause, otype='phrase')
if len(phrases) == 2:
if L.d(phrases[0], otype='word')[-1] + 1 == L.d(phrases[1], otype='word')[0]:
clausesByHand2.append(clause)
clausesByHand2 = sorted(clausesByHand2)
info(f'Done: found {len(clausesByHand2)}')
0.00s counting ... 1.00s Done: found 23399
Now we have too few cases. What is going on?
showDiff(clausesByQuery, clausesByHand2)
clause 428692 is a query result but not found by hand
Observe:
This clause has three phrases, but the third one lies inside the second one.
Can we adjust the pattern to exclude cases like this? Yes, with custom sets, see advanced.
Instead of looking through all phrases, we can just consider non gapped phrases only.
Earlier in this notebook we have constructed the set of non-gapped phrases
and put it under the name conphrase
in the custom sets.
query = '''
clause
=: conphrase
<: conphrase
:=
'''
clausesByQuery2 = sorted(B.search(query, sets=customSets, shallow=True))
1.32s 23327 results
There is still a difference.
showDiff(clausesByQuery2, clausesByHand2)
clause 428374 is not a query result but has been found by hand
Observe:
This clause has two phrases, the second one has a gap, which coincides with a gap in the clause.
indent(reset=True)
info('counting ...')
clausesByHand3 = []
for clause in F.otype.s('clause'):
if hasGap(clause):
continue
phrases = L.d(clause, otype='phrase')
if len(phrases) == 2:
if L.d(phrases[0], otype='word')[-1] + 1 == L.d(phrases[1], otype='word')[0]:
clausesByHand3.append(clause)
clausesByHand3 = sorted(clausesByHand3)
info(f'Done: found {len(clausesByHand3)}')
0.00s counting ... 1.35s Done: found 23327
Now the number of results agree. But are they really the same?
showDiff(clausesByQuery2, clausesByHand3)
23327 queryResults are identical with 23327 handResults
It took four attempts to arrive at the final concept of things that we were looking for.
Sometimes the search template had to be modified, sometimes the hand-written code.
The interplay and systematic comparison between the attempts helped to spot all relevant configurations of phrases within clauses.
Here is another cause of wrong query results: there are sentences that span multiple verses. Such sentences are not contained in any verse. That makes that they are easily missed out in queries.
We describe a scenario where that happens.
A clause and its mother do not have to be in the same verse. We are going to fetch are the cases where they are in different verses.
But first we fetch all pairs of clauses connected by a mother edge.
query = '''
clause
-mother> clause
'''
allMotherPairs = B.search(query)
B.table(results, end=10)
0.96s 13907 results
n | clause | phrase | phrase |
---|---|---|---|
1 | יְהִ֣י אֹ֑ור | יְהִ֣י | אֹ֑ור |
2 | כִּי־טֹ֑וב | כִּי־ | טֹ֑וב |
3 | אֲשֶׁר֙ מִתַּ֣חַת לָרָקִ֔יעַ | אֲשֶׁר֙ | מִתַּ֣חַת לָרָקִ֔יעַ |
4 | אֲשֶׁ֖ר מֵעַ֣ל לָרָקִ֑יעַ | אֲשֶׁ֖ר | מֵעַ֣ל לָרָקִ֑יעַ |
5 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
6 | מַזְרִ֣יעַ זֶ֔רַע | מַזְרִ֣יעַ | זֶ֔רַע |
7 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
8 | לְהַבְדִּ֕יל בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה | לְהַבְדִּ֕יל | בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה |
9 | לְהָאִ֖יר עַל־הָאָ֑רֶץ | לְהָאִ֖יר | עַל־הָאָ֑רֶץ |
10 | לְהָאִ֖יר עַל־הָאָֽרֶץ׃ | לְהָאִ֖יר | עַל־הָאָֽרֶץ׃ |
Now we modify the query to the effect that mother and daughter must sit in distinct verses.
query = '''
cm:clause
-mother> cd:clause
v1:verse
v2:verse
v1 # v2
cm ]] v1
cd ]] v2
'''
diffMotherPairs = B.search(query)
B.table(results, end=10)
0.33s 721 results
n | clause | phrase | phrase |
---|---|---|---|
1 | יְהִ֣י אֹ֑ור | יְהִ֣י | אֹ֑ור |
2 | כִּי־טֹ֑וב | כִּי־ | טֹ֑וב |
3 | אֲשֶׁר֙ מִתַּ֣חַת לָרָקִ֔יעַ | אֲשֶׁר֙ | מִתַּ֣חַת לָרָקִ֔יעַ |
4 | אֲשֶׁ֖ר מֵעַ֣ל לָרָקִ֑יעַ | אֲשֶׁ֖ר | מֵעַ֣ל לָרָקִ֑יעַ |
5 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
6 | מַזְרִ֣יעַ זֶ֔רַע | מַזְרִ֣יעַ | זֶ֔רַע |
7 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
8 | לְהַבְדִּ֕יל בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה | לְהַבְדִּ֕יל | בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה |
9 | לְהָאִ֖יר עַל־הָאָ֑רֶץ | לְהָאִ֖יר | עַל־הָאָ֑רֶץ |
10 | לְהָאִ֖יר עַל־הָאָֽרֶץ׃ | לְהָאִ֖יר | עַל־הָאָֽרֶץ׃ |
As a check,
we modify the latter query and require v1
and v2
to be the same verse, to get the
mother pairs of which both members are in the same verse.
query = '''
cm:clause
-mother> cd:clause
v1:verse
v2:verse
v1 = v2
cm ]] v1
cd ]] v2
'''
sameMotherPairs = B.search(query)
B.table(results, end=10)
0.51s 13160 results
n | clause | phrase | phrase |
---|---|---|---|
1 | יְהִ֣י אֹ֑ור | יְהִ֣י | אֹ֑ור |
2 | כִּי־טֹ֑וב | כִּי־ | טֹ֑וב |
3 | אֲשֶׁר֙ מִתַּ֣חַת לָרָקִ֔יעַ | אֲשֶׁר֙ | מִתַּ֣חַת לָרָקִ֔יעַ |
4 | אֲשֶׁ֖ר מֵעַ֣ל לָרָקִ֑יעַ | אֲשֶׁ֖ר | מֵעַ֣ל לָרָקִ֑יעַ |
5 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
6 | מַזְרִ֣יעַ זֶ֔רַע | מַזְרִ֣יעַ | זֶ֔רַע |
7 | כִּי־טֹֽוב׃ | כִּי־ | טֹֽוב׃ |
8 | לְהַבְדִּ֕יל בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה | לְהַבְדִּ֕יל | בֵּ֥ין הַיֹּ֖ום וּבֵ֣ין הַלָּ֑יְלָה |
9 | לְהָאִ֖יר עַל־הָאָ֑רֶץ | לְהָאִ֖יר | עַל־הָאָ֑רֶץ |
10 | לְהָאִ֖יר עַל־הָאָֽרֶץ׃ | לְהָאִ֖יר | עַל־הָאָֽרֶץ׃ |
Let's check if the numbers add up:
Then the results of the second and third query combined should equal the results of the first query.
That makes sense.
Still, let's check:
discrepancy = len(allMotherPairs) - len(diffMotherPairs) - len(sameMotherPairs)
print(discrepancy)
26
The numbers do not add up. We are missing cases. Why?
Clauses may cross verse boundaries. In that case they are not part of a verse, and hence our latter two queries do not detect them. Let's count how many verse boundary crossing clauses there are.
query = '''
clause
=: first:word
last:word
:=
v1:verse
w1:word
v2:verse
w2:word
first = w1
last = w2
v1 # v2
'''
results = B.search(query)
3.50s 50 results
Some of these verse spanning clauses do not have mothers or are not mothers. Let's count the cases where two clauses are in a mother relation and at least one of them spans a verse.
We need two queries for that. These queries are almost similar. One retrieves the clause pairs where the mother crosses verse boundaries, and the other where the daughter does so.
But we are programmers. We do not have to repeat ourselves:
queryCommon = '''
c1:clause
-mother> c2:clause
c3:clause
=: first:word
last:word
:=
v1:verse
w1:word
v2:verse
w2:word
first = w1
last = w2
v1 # v2
'''
query1 = f'''
{queryCommon}
c1 = c3
'''
query2 = f'''
{queryCommon}
c2 = c3
'''
results1 = B.search(query1, silent=True)
results2 = B.search(query2, silent=True)
spannersByQuery = {(r[0], r[1]) for r in results1 + results2}
print(f'{len(spannersByQuery):>3} spanners are missing')
print(f'{discrepancy:>3} missing cases were detected before')
print(f'{discrepancy - len(spannersByQuery):>3} is the resulting disagreement')
26 spanners are missing 26 missing cases were detected before 0 is the resulting disagreement
We may find the mother clause pairs in which it least one member is verse spanning by hand-coding in an easier way:
Starting with the set of all mother pairs, we filter out any pair that has a verse spanner.
spannersByHand = set()
for (c1, c2) in allMotherPairs:
if not (
L.u(c1, otype='verse')
and
L.u(c2, otype='verse')
):
spannersByHand.add((c1, c2))
len(spannersByHand)
26
And, to be completely sure:
spannersByHand == spannersByQuery
True
If we are content with the clauses that do not span verses,
we can put them in a set, and modify the queries by replacing clause
by conclause
and bind the right set to it.
Here we go. In one cell we run the queries to get all pairs, the mother-daughter-in-separate-verses pairs, and the mother-daughter-in-same-verses pair and we do the math of checking.
conClauses = {c for c in F.otype.s('clause') if L.u(c, otype='verse')}
customSets = dict(conclause=conClauses)
print('All pairs')
allPairs = B.search('''
conclause
-mother> conclause
''',
sets=customSets,
)
print('Different verse pairs')
diffPairs = B.search('''
cm:conclause
-mother> cd:conclause
v1:verse
v2:verse
v1 # v2
cm ]] v1
cd ]] v2
''',
sets=customSets,
)
print('Same verse pairs')
samePairs = B.search('''
cm:conclause
-mother> cd:conclause
v1:verse
v2:verse
v1 = v2
cm ]] v1
cd ]] v2
''',
sets=customSets,
)
allPairSet = set(allPairs)
diffPairSet = {(r[0], r[1]) for r in diffPairs}
samePairSet = {(r[0], r[1]) for r in samePairs}
print(f'Intersection same-verse/different-verse pairs: {samePairSet & diffPairSet}')
print(f'All pairs is union of same-verse/different-verse pairs: {allPairSet == (samePairSet | diffPairSet)}')
All pairs 0.46s 13881 results Different verse pairs 0.56s 721 results Same verse pairs 0.45s 13160 results Intersection same-verse/different-verse pairs: set() All pairs is union of same-verse/different-verse pairs: True