To get started: consult start
In a notebook, you can perform searches and view them in a tabular display and zoom in on items with pretty displays.
But there are times that you want to take your results outside Text-Fabric, outside a notebook, outside Python, and just work with them in other programs, such as Excel.
You want to do that not only with query results, but with all kinds of lists of tuples of nodes.
There is a function for that, A.export()
, and here we show what it can do.
%load_ext autoreload
%autoreload 2
import os
from tf.app import use
A = use("q-ran/quran", hoist=globals())
# A = use('q-ran/quran', hoist=globals())
This is Text-Fabric 9.2.3 Api reference : https://annotation.github.io/text-fabric/tf/cheatsheet.html 40 features found and 0 ignored
We write a function that can peek into file on your system, and show the first few lines. We'll use it to inspect the exported files that we are going to produce.
EXPORT_FILE = os.path.expanduser("~/Downloads/results.tsv")
UPTO = 10
def checkout():
with open(EXPORT_FILE, encoding="utf_16") as fh:
for (i, line) in enumerate(fh):
if i >= UPTO:
break
print(line)
Our exported .tsv
files open in Excel without hassle, even if they contain non-latin characters.
That is because TF writes such files in an
encoding that works well with Excel: utf_16_le
.
You can just open them in Excel, there is no need for conversion before or after opening these files.
Should you want to process these files by means of a (Python) program,
take care to read them with encoding utf_16
.
We first run a query in order to export the results.
query = """
aya
word pos=verb
<: word pos=noun posx=proper root=Alh
<: word
"""
results = A.search(query)
0.30s 529 results
You can export the table of results to Excel.
The following command writes a tab-separated file results.tsv
to your downloads directory.
You can specify arguments toDir=directory
and toFile=file name
to write to a different file.
If the directory does not exist, it will be created.
We stick to the default, however.
A.export(results)
Check out the contents:
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 NODE2 TYPE2 TEXT2 pos2 NODE3 TYPE3 TEXT3 pos3 posx3 root3 NODE4 TYPE4 TEXT4 1 2 7 128233 aya خَتَمَ ٱللَّهُ عَلَىٰ قُلُوبِهِمْ وَعَلَىٰ سَمْعِهِمْ وَعَلَىٰٓ أَبْصَٰرِهِمْ غِشَٰوَةٌ وَلَهُمْ عَذَابٌ عَظِيمٌ 131 word خَتَمَ verb 132 word ٱللَّهُ noun proper Alh 133 word عَلَىٰ 2 2 17 128243 aya مَثَلُهُمْ كَمَثَلِ ٱلَّذِى ٱسْتَوْقَدَ نَارًا فَلَمَّآ أَضَآءَتْ مَا حَوْلَهُۥ ذَهَبَ ٱللَّهُ بِنُورِهِمْ وَتَرَكَهُمْ فِى ظُلُمَٰتٍ لَّا يُبْصِرُونَ 341 word ذَهَبَ verb 342 word ٱللَّهُ noun proper Alh 343 word بِ 3 2 20 128246 aya يَكَادُ ٱلْبَرْقُ يَخْطَفُ أَبْصَٰرَهُمْ كُلَّمَآ أَضَآءَ لَهُم مَّشَوْا۟ فِيهِ وَإِذَآ أَظْلَمَ عَلَيْهِمْ قَامُوا۟ وَلَوْ شَآءَ ٱللَّهُ لَذَهَبَ بِسَمْعِهِمْ وَأَبْصَٰرِهِمْ إِنَّ ٱللَّهَ عَلَىٰ كُلِّ شَىْءٍ قَدِيرٌ 417 word شَآءَ verb 418 word ٱللَّهُ noun proper Alh 419 word لَ 4 2 26 128252 aya إِنَّ ٱللَّهَ لَا يَسْتَحْىِۦٓ أَن يَضْرِبَ مَثَلًا مَّا بَعُوضَةً فَمَا فَوْقَهَا فَأَمَّا ٱلَّذِينَ ءَامَنُوا۟ فَيَعْلَمُونَ أَنَّهُ ٱلْحَقُّ مِن رَّبِّهِمْ وَأَمَّا ٱلَّذِينَ كَفَرُوا۟ فَيَقُولُونَ مَاذَآ أَرَادَ ٱللَّهُ بِهَٰذَا مَثَلًا يُضِلُّ بِهِۦ كَثِيرًا وَيَهْدِى بِهِۦ كَثِيرًا وَمَا يُضِلُّ بِهِۦٓ إِلَّا ٱلْفَٰسِقِينَ 641 word أَرَادَ verb 642 word ٱللَّهُ noun proper Alh 643 word بِ 5 2 27 128253 aya ٱلَّذِينَ يَنقُضُونَ عَهْدَ ٱللَّهِ مِنۢ بَعْدِ مِيثَٰقِهِۦ وَيَقْطَعُونَ مَآ أَمَرَ ٱللَّهُ بِهِۦٓ أَن يُوصَلَ وَيُفْسِدُونَ فِى ٱلْأَرْضِ أُو۟لَٰٓئِكَ هُمُ ٱلْخَٰسِرُونَ 676 word أَمَرَ verb 677 word ٱللَّهُ noun proper Alh 678 word بِ 6 2 55 128281 aya وَإِذْ قُلْتُمْ يَٰمُوسَىٰ لَن نُّؤْمِنَ لَكَ حَتَّىٰ نَرَى ٱللَّهَ جَهْرَةً فَأَخَذَتْكُمُ ٱلصَّٰعِقَةُ وَأَنتُمْ تَنظُرُونَ 1393 word نَرَى verb 1394 word ٱللَّهَ noun proper Alh 1395 word جَهْرَةً 7 2 70 128296 aya قَالُوا۟ ٱدْعُ لَنَا رَبَّكَ يُبَيِّن لَّنَا مَا هِىَ إِنَّ ٱلْبَقَرَ تَشَٰبَهَ عَلَيْنَا وَإِنَّآ إِن شَآءَ ٱللَّهُ لَمُهْتَدُونَ 1922 word شَآءَ verb 1923 word ٱللَّهُ noun proper Alh 1924 word لَ 8 2 73 128299 aya فَقُلْنَا ٱضْرِبُوهُ بِبَعْضِهَا كَذَٰلِكَ يُحْىِ ٱللَّهُ ٱلْمَوْتَىٰ وَيُرِيكُمْ ءَايَٰتِهِۦ لَعَلَّكُمْ تَعْقِلُونَ 1996 word يُحْىِ verb 1997 word ٱللَّهُ noun proper Alh 1998 word ٱلْ 9 2 76 128302 aya وَإِذَا لَقُوا۟ ٱلَّذِينَ ءَامَنُوا۟ قَالُوٓا۟ ءَامَنَّا وَإِذَا خَلَا بَعْضُهُمْ إِلَىٰ بَعْضٍ قَالُوٓا۟ أَتُحَدِّثُونَهُم بِمَا فَتَحَ ٱللَّهُ عَلَيْكُمْ لِيُحَآجُّوكُم بِهِۦ عِندَ رَبِّكُمْ أَفَلَا تَعْقِلُونَ 2127 word فَتَحَ verb 2128 word ٱللَّهُ noun proper Alh 2129 word عَلَيْ
You see the following columns:
R
the sequence number of the result tuple in the result listS1 S2
the section as sura and aya in separate columnsNODEi TYPEi
the node and its type, for each node i
in the result tupleTEXTi
the full text of node i
, if the node type admits a concise text representationpos2
the value of feature pos
on node 2, since our query mentions the feature pos
on node 2pos3
, posx3
, root3
If we want to see the feature posx
and the word gender (feature gn
) on the last word (4), we must mention them
in the query.
We can do so as follows:
query = """
aya
word pos=verb
<: word pos=noun posx=proper root=Alh
<: word posx* gn*
"""
results = A.search(query)
0.49s 529 results
The same number of results as before.
The *
is a trivial condition, it is always true.
We do the export again and peek at the results.
A.export(results)
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 NODE2 TYPE2 TEXT2 pos2 NODE3 TYPE3 TEXT3 pos3 posx3 root3 NODE4 TYPE4 TEXT4 gn4 posx4 1 2 7 128233 aya خَتَمَ ٱللَّهُ عَلَىٰ قُلُوبِهِمْ وَعَلَىٰ سَمْعِهِمْ وَعَلَىٰٓ أَبْصَٰرِهِمْ غِشَٰوَةٌ وَلَهُمْ عَذَابٌ عَظِيمٌ 131 word خَتَمَ verb 132 word ٱللَّهُ noun proper Alh 133 word عَلَىٰ 2 2 17 128243 aya مَثَلُهُمْ كَمَثَلِ ٱلَّذِى ٱسْتَوْقَدَ نَارًا فَلَمَّآ أَضَآءَتْ مَا حَوْلَهُۥ ذَهَبَ ٱللَّهُ بِنُورِهِمْ وَتَرَكَهُمْ فِى ظُلُمَٰتٍ لَّا يُبْصِرُونَ 341 word ذَهَبَ verb 342 word ٱللَّهُ noun proper Alh 343 word بِ 3 2 20 128246 aya يَكَادُ ٱلْبَرْقُ يَخْطَفُ أَبْصَٰرَهُمْ كُلَّمَآ أَضَآءَ لَهُم مَّشَوْا۟ فِيهِ وَإِذَآ أَظْلَمَ عَلَيْهِمْ قَامُوا۟ وَلَوْ شَآءَ ٱللَّهُ لَذَهَبَ بِسَمْعِهِمْ وَأَبْصَٰرِهِمْ إِنَّ ٱللَّهَ عَلَىٰ كُلِّ شَىْءٍ قَدِيرٌ 417 word شَآءَ verb 418 word ٱللَّهُ noun proper Alh 419 word لَ emphatic 4 2 26 128252 aya إِنَّ ٱللَّهَ لَا يَسْتَحْىِۦٓ أَن يَضْرِبَ مَثَلًا مَّا بَعُوضَةً فَمَا فَوْقَهَا فَأَمَّا ٱلَّذِينَ ءَامَنُوا۟ فَيَعْلَمُونَ أَنَّهُ ٱلْحَقُّ مِن رَّبِّهِمْ وَأَمَّا ٱلَّذِينَ كَفَرُوا۟ فَيَقُولُونَ مَاذَآ أَرَادَ ٱللَّهُ بِهَٰذَا مَثَلًا يُضِلُّ بِهِۦ كَثِيرًا وَيَهْدِى بِهِۦ كَثِيرًا وَمَا يُضِلُّ بِهِۦٓ إِلَّا ٱلْفَٰسِقِينَ 641 word أَرَادَ verb 642 word ٱللَّهُ noun proper Alh 643 word بِ 5 2 27 128253 aya ٱلَّذِينَ يَنقُضُونَ عَهْدَ ٱللَّهِ مِنۢ بَعْدِ مِيثَٰقِهِۦ وَيَقْطَعُونَ مَآ أَمَرَ ٱللَّهُ بِهِۦٓ أَن يُوصَلَ وَيُفْسِدُونَ فِى ٱلْأَرْضِ أُو۟لَٰٓئِكَ هُمُ ٱلْخَٰسِرُونَ 676 word أَمَرَ verb 677 word ٱللَّهُ noun proper Alh 678 word بِ 6 2 55 128281 aya وَإِذْ قُلْتُمْ يَٰمُوسَىٰ لَن نُّؤْمِنَ لَكَ حَتَّىٰ نَرَى ٱللَّهَ جَهْرَةً فَأَخَذَتْكُمُ ٱلصَّٰعِقَةُ وَأَنتُمْ تَنظُرُونَ 1393 word نَرَى verb 1394 word ٱللَّهَ noun proper Alh 1395 word جَهْرَةً f 7 2 70 128296 aya قَالُوا۟ ٱدْعُ لَنَا رَبَّكَ يُبَيِّن لَّنَا مَا هِىَ إِنَّ ٱلْبَقَرَ تَشَٰبَهَ عَلَيْنَا وَإِنَّآ إِن شَآءَ ٱللَّهُ لَمُهْتَدُونَ 1922 word شَآءَ verb 1923 word ٱللَّهُ noun proper Alh 1924 word لَ emphatic 8 2 73 128299 aya فَقُلْنَا ٱضْرِبُوهُ بِبَعْضِهَا كَذَٰلِكَ يُحْىِ ٱللَّهُ ٱلْمَوْتَىٰ وَيُرِيكُمْ ءَايَٰتِهِۦ لَعَلَّكُمْ تَعْقِلُونَ 1996 word يُحْىِ verb 1997 word ٱللَّهُ noun proper Alh 1998 word ٱلْ 9 2 76 128302 aya وَإِذَا لَقُوا۟ ٱلَّذِينَ ءَامَنُوا۟ قَالُوٓا۟ ءَامَنَّا وَإِذَا خَلَا بَعْضُهُمْ إِلَىٰ بَعْضٍ قَالُوٓا۟ أَتُحَدِّثُونَهُم بِمَا فَتَحَ ٱللَّهُ عَلَيْكُمْ لِيُحَآجُّوكُم بِهِۦ عِندَ رَبِّكُمْ أَفَلَا تَعْقِلُونَ 2127 word فَتَحَ verb 2128 word ٱللَّهُ noun proper Alh 2129 word عَلَيْ
As you see, you have extra columns gn4
and posx4
.
This gives you a lot of control over the generation of spreadsheets.
You can also export lists of node tuples that are not obtained by a query:
tuples = (
tuple(results[0][1:3]),
tuple(results[1][1:3]),
)
tuples
((131, 132), (341, 342))
Two rows, each row has a clause node and a word node.
Let's do a bare export:
A.export(tuples)
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 NODE2 TYPE2 TEXT2 pos2 1 2 7 131 word خَتَمَ 132 word ٱللَّهُ noun 2 2 17 341 word ذَهَبَ 342 word ٱللَّهُ noun
Wait a minute: why is the pos2
there?
It is because we have run a query before where we asked for pos
.
If we do not want to be influenced by previous things we've run, we need to reset the display:
A.displayReset("tupleFeatures")
Again:
A.export(tuples)
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 NODE2 TYPE2 TEXT2 1 2 7 131 word خَتَمَ 132 word ٱللَّهُ 2 2 17 341 word ذَهَبَ 342 word ٱللَّهُ
We can get richer exports by means of
A.displaySetup()
, using the parameter tupleFeatures
:
A.displaySetup(
tupleFeatures=(
(0, "pos posx"),
(1, "pos root"),
)
)
We assign extra features per member of the tuple.
In the above case:
0
) member (the first word node), gets features pos
and posx
;1
) member (the second word node), gets features pos
and root
.A.export(tuples)
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 pos1 posx1 NODE2 TYPE2 TEXT2 pos2 root2 1 2 7 131 word خَتَمَ verb 132 word ٱللَّهُ noun Alh 2 2 17 341 word ذَهَبَ verb 342 word ٱللَّهُ noun Alh
Talking about display setup: other parameters also have effect, e.g. the text format.
Let's change it to the ASCII transcription.
A.export(tuples, fmt="text-trans-full")
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 pos1 posx1 NODE2 TYPE2 TEXT2 pos2 root2 1 2 7 131 word xatama verb 132 word {ll~ahu noun Alh 2 2 17 341 word *ahaba verb 342 word {ll~ahu noun Alh
You can chain queries like this:
results = (
A.search(
"""
aya
word pos=verb
<: word pos=noun posx=proper root=Alh
<: word pos=verb
"""
)[0:2]
+ A.search(
"""
aya
word pos=verb
<: word pos=noun posx=proper root=Alh
<: word pos=noun
"""
)[0:2]
)
0.30s 24 results 0.30s 101 results
In such cases, it is better to setup the features yourself:
A.displaySetup(
tupleFeatures=(
(1, "root formation tense"),
(3, "pos gn nu ps mood"),
),
fmt="text-orig-full",
)
Now we can do a fine export:
A.export(results)
checkout()
R S1 S2 NODE1 TYPE1 TEXT1 NODE2 TYPE2 TEXT2 root2 formation2 tense2 NODE3 TYPE3 TEXT3 NODE4 TYPE4 TEXT4 pos4 gn4 nu4 ps4 mood4 1 2 91 128317 aya وَإِذَا قِيلَ لَهُمْ ءَامِنُوا۟ بِمَآ أَنزَلَ ٱللَّهُ قَالُوا۟ نُؤْمِنُ بِمَآ أُنزِلَ عَلَيْنَا وَيَكْفُرُونَ بِمَا وَرَآءَهُۥ وَهُوَ ٱلْحَقُّ مُصَدِّقًا لِّمَا مَعَهُمْ قُلْ فَلِمَ تَقْتُلُونَ أَنۢبِيَآءَ ٱللَّهِ مِن قَبْلُ إِن كُنتُم مُّؤْمِنِينَ 2645 word أَنزَلَ nzl IV perfect 2646 word ٱللَّهُ 2647 word قَالُ verb m p 3 2 2 170 128396 aya وَإِذَا قِيلَ لَهُمُ ٱتَّبِعُوا۟ مَآ أَنزَلَ ٱللَّهُ قَالُوا۟ بَلْ نَتَّبِعُ مَآ أَلْفَيْنَا عَلَيْهِ ءَابَآءَنَآ أَوَلَوْ كَانَ ءَابَآؤُهُمْ لَا يَعْقِلُونَ شَيْـًٔا وَلَا يَهْتَدُونَ 5164 word أَنزَلَ nzl IV perfect 5165 word ٱللَّهُ 5166 word قَالُ verb m p 3 3 2 55 128281 aya وَإِذْ قُلْتُمْ يَٰمُوسَىٰ لَن نُّؤْمِنَ لَكَ حَتَّىٰ نَرَى ٱللَّهَ جَهْرَةً فَأَخَذَتْكُمُ ٱلصَّٰعِقَةُ وَأَنتُمْ تَنظُرُونَ 1393 word نَرَى rAy imperfect 1394 word ٱللَّهَ 1395 word جَهْرَةً noun f 4 2 80 128306 aya وَقَالُوا۟ لَن تَمَسَّنَا ٱلنَّارُ إِلَّآ أَيَّامًا مَّعْدُودَةً قُلْ أَتَّخَذْتُمْ عِندَ ٱللَّهِ عَهْدًا فَلَن يُخْلِفَ ٱللَّهُ عَهْدَهُۥٓ أَمْ تَقُولُونَ عَلَى ٱللَّهِ مَا لَا تَعْلَمُونَ 2239 word يُخْلِفَ xlf IV imperfect 2240 word ٱللَّهُ 2241 word عَهْدَ noun m
All chapters:
CC-BY Dirk Roorda