%load_ext autoreload
%autoreload 2
# Loading the Text-Fabric code
# Note: it is assumed Text-Fabric is installed in your environment
from tf.fabric import Fabric
from tf.app import use
# load the N1904 app and data
N1904 = use ("tonyjurg/Nestle1904GBI", version="0.4", hoist=globals())
Locating corpus resources ...
Name | # of nodes | # slots/node | % coverage |
---|---|---|---|
book | 27 | 5102.93 | 100 |
chapter | 260 | 529.92 | 100 |
sentence | 5720 | 24.09 | 100 |
verse | 7943 | 17.35 | 100 |
clause | 16124 | 8.54 | 100 |
phrase | 72674 | 1.90 | 100 |
word | 137779 | 1.00 | 100 |
# The following will push the Text-Fabric stylesheet to this notebook (to facilitate proper display with notebook viewer)
N1904.dh(N1904.getCss())
Each query templace can be inspected by use of S.study()
. This is particulary helpfull in case the query is complicated.
ComplicatedQuery = '''
clause
/where/
phrase phrasefunction=S
/have/
/without/
word sp#conj
/-/
/-/
phrase phrasefunction=O
'''
S.study(ComplicatedQuery)
0.00s Checking search template ... 0.00s Setting up search space for 2 objects ... | 0.00s "Quantifier on "parent:clause" | | /where/ | | parent:clause | | phrase phrasefunction=S | | 0.08s 11391 matching nodes | | /have/ | | parent:clause | | phrase phrasefunction=S | | /without/ | | word sp#conj | | /-/ | | /- | | | /without/ | | | parent:phrase phrasefunction=S | | | word sp#conj | | | /- | | | 0.21s 10976 nodes to exclude | | 0.23s reduction from 11391 to 415 nodes | | 0.23s 415 matching nodes | | 0.24s 8783 match antecedent but not consequent | 0.41s reduction from 16124 to 7341 nodes 0.26s Constraining search space with 1 relations ... 0.27s 1 edges thinned 0.27s Setting up retrieval plan with strategy small_choice_multi ... 0.27s Ready to deliver results from 8849 nodes Iterate over S.fetch() to get the results See S.showPlan() to interpret the results
S.showPlan()
4.00s The results are connected to the original search template as follows: 0 1 R0 clause 2 /where 3 phrase phrasefunction=S 4 /have 5 /without 6 word sp#conj 7 /- 8 /- 9 R1 phrase phrasefunction=O 10
for result in S.fetch(limit=10):
TF.info(S.glean(result))
6.12s clause[πολλαπλασίονα λήμψεται καὶ ζωὴν αἰώνιον ...] phrase[πολλαπλασίονα ] 6.12s clause[πολλαπλασίονα λήμψεται καὶ ζωὴν αἰώνιον ...] phrase[ζωὴν αἰώνιον ] 6.12s clause[συμφωνήσας δὲ μετὰ τῶν ἐργατῶν ...] phrase[αὐτοὺς ] 6.13s clause[καὶ ἐξελθὼν περὶ τρίτην ὥραν ...] phrase[ἄλλους ] 6.13s clause[Τί οὖν ἐροῦμεν; ] phrase[Τί οὖν ] 6.13s clause[διὰ τοῦ ἀγαθοῦ μοι κατεργαζομένη ...] phrase[θάνατον, ] 6.13s clause[καὶ ἴσους αὐτοὺς ἡμῖν ἐποίησας ...] phrase[αὐτοὺς ] 6.13s clause[καὶ ἴσους αὐτοὺς ἡμῖν ἐποίησας ...] phrase[τὸ βάρος τῆς ἡμέρας καὶ ...] 6.13s clause[οὐκ ἀδικῶ σε· οὐχὶ δηναρίου ...] phrase[σε· οὐχὶ ] 6.13s clause[ἆρον τὸ σὸν καὶ ὕπαγε· ...] phrase[τὸ σὸν καὶ ]
Using python standard functions, it apears to be easy to verify if the result of two queries are the same or different. However, it is good to have a closer look at the matter since there are a few pitfalls that could lead to false results.
# define query template 1
SomeQuery ='''
phrase phrasefunction=V
word lemma=λέγω
'''
# now create two lists with identical query results and compare result lists
SomeResult1=N1904.search(SomeQuery)
SomeResult2=N1904.search(SomeQuery)
print(f'Same result? {SomeResult1 == SomeResult2}')
0.16s 2216 results 0.15s 2216 results Same result? True
This is exactly what we would expect. But comparing lists can be tricky. Consider the following two queries.
# define query template 1
Query1 ='''
phrase
a:word sp=prep
b:word sp=adj
c:word sp=noun
'''
# define query template 2
Query2 ='''
phrase
a:word sp=prep
b:word sp=noun
c:word sp=adj
'''
# create and compare result lists
ResultQuery1=N1904.search(Query1)
ResultQuery2=N1904.search(Query2)
print(f'Same result? {ResultQuery1 == ResultQuery2}')
0.34s 3851 results 0.35s 3851 results Same result? False
This method of comparing the lists results identifies a difference between the two lists. However, upon closer examination, that may or may not be the case, depending on what is understood as difference. The 'problem' here is that both ResultQuery1 and ResultQuery2 are lists of ordered tuples. The swapping of the feature conditions 'sp=adj' and 'sp=noun' did not result in a different set of results; only the presentation of rhe result differed.
# create 2 result lists
ResultQuery3=N1904.search(Query1,sort=True)
ResultQuery4=N1904.search(Query1,sort=False)
# compare unsorted lists
print(f'Unsorted lists: Same result ? {ResultQuery3 == ResultQuery4}')
# sort both lists on the first tuple
SortedResultQuery3 = sorted(ResultQuery3, key=lambda x: x[0])
SortedResultQuery4 = sorted(ResultQuery4, key=lambda x: x[0])
# compare sorted lists
print(f'Sorted lists: Same result ? {SortedResultQuery3 == SortedResultQuery4}')
0.34s 3851 results 0.33s 3851 results Unsorted lists: Same result ? False Sorted lists: Same result ? False
Unexpectedly the python list compare still viewed the two lists as different. But why? Python does report that the two lists are different because the comparison of lists (SortedResultQuery3 == SortedResultQuery4) checks for the equality of the list objects, not their contents.
The search() function in Text-Fabric returns a list of nodes or tuples representing search results. Even if the search criteria and the data are the same, the two lists, ResultQuery3 and ResultQuery4, are distinct list objects. Hence, when comparing them directly using the == operator, Python considers them as different objects, resulting in False.
To compare the content of the lists, it is advices to first onvert them to sets and compare those sets. See following example:
# Convert tuples to sets
set1 = set(tuple(item) for item in ResultQuery3)
set2 = set(tuple(item) for item in ResultQuery4)
# Compare the sets
if set1 == set2:
print("Lists ResultQuery3 and ResultQuery4 are equal.")
else:
print("Lists ResultQuery3 and ResultQuery4 are not equal.")
Lists ResultQuery3 and ResultQuery4 are equal.
Now, let's compare ResultQuery1 and ResultQuery2 again by first converting them to sets.
# Convert tuples to sets
set1 = set(tuple(item) for item in ResultQuery1)
set2 = set(tuple(item) for item in ResultQuery2)
# Compare the sets
if set1 == set2:
print("Lists ResultQuery1 and ResultQuery2 are equal.")
else:
print("Lists ResultQuery1 and ResultQuery2 are not equal.")
Lists ResultQuery1 and ResultQuery2 are not equal.
This is indeed the result we expected (see earlier mentioned reasons).
The scripts in this notebook require (beside text-fabric
) the following Python libraries to be installed in the environment:
{none}
You can install any missing library from within Jupyter Notebook using eitherpip
or pip3
.