#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') # In[2]: from tf.app import use # # Cluster display in Old Babylonian # # We show some details of the display logic by following an example: cluster nodes in the Old Babylonian corpus. # # Clusters are difficult, because # # * they do not necessarily respect proper embedding # * material can be part of several clusters # # We show how we deal with the second part and prevent multiple display of members of multiple clusters. # As an illustration, we'll show the effect of an earlier bug and indicate the fix. # # We start with loading the corpus. # In[3]: A = use("oldbabylonian:clone", checkout="clone", hoist=globals()) # In[4]: A.reuse() # # An example line # # Here is a line with some nested clusters. # In fact, it is the first line of the corpus. # # The node number is stored in the variable `ln`. # # We show the raw ATF source of the line, and the text according to several text formats. # In[4]: ln = F.otype.s("line")[0] ln # In[5]: F.srcLn.v(ln) # In[6]: T.text(ln) # In[7]: T.text(ln, fmt="text-orig-rich") # In[8]: T.text(ln, fmt="text-orig-unicode") # N.B: These are the right unicodes but not the right signs, we need another font for that. # # We can get the right signs by using `plain`: # In[9]: A.plain(ln, fmt="text-orig-unicode") # even better, we translate the effect of clusters into layout: # In[10]: A.plain(ln, fmt="layout-orig-unicode") # Click on the passage link in order to go to the page for this tablet on CDLI, where you can read off the # exact source: # # ``` # 1. [a-na] _{d}suen_-i-[din-nam] # ``` # ## The clusters # # By means of the # [`L` API](https://annotation.github.io/text-fabric/tf/core/locality.html) # the clusters of this line can be found. # # They are returned as a tuple of nodes. # In[11]: cls = L.d(ln, otype="cluster") cls # We'll give each cluster its own highlight color: # In[12]: colors = """ cyan yellow lightsalmon lightgreen goldenrod cornflowerblue forestgreen burlywood orange indianred """.strip().split() highlights = dict(zip(cls, colors)) highlights # In[13]: A.plain(ln, highlights=highlights) # In this corpus, `pretty` displays unfold until the word level, by default. # # But first we want it to unfold to the very end, to the sign level. # In[14]: A.pretty(ln, highlights=highlights, baseTypes="sign") # Let's see some more examples. # We have written a function to quickly execute examples. # The first example is the index of the line in the list of all lines produces by `F.otype.s('line')`. # In[15]: def example(nLine, extraHighlights={}, **options): ln = F.otype.s("line")[nLine] print(ln) print(F.srcLn.v(ln)) print(T.text(ln)) A.plain(ln, fmt="layout-orig-unicode") cls = L.d(ln, otype="cluster") highlights = dict(zip(cls, colors[0 : len(cls)])) print(highlights) A.plain(ln, highlights={**highlights, **extraHighlights}, **options) A.pretty( ln, highlights={**highlights, **extraHighlights}, baseTypes="sign", **options, explain=True ) A.pretty(ln, highlights={**highlights, **extraHighlights}, **options) # In[16]: example(0, withNodes=True) # In[17]: example(22, withNodes=True, extraHighlights={258252: "lightsalmon"}) # # More examples # # We finish off with some more examples. # Something peculiar is going on. # In order to talk about it, we add node numbers. # In[18]: example(2553, withNodes=True, extraHighlights={265903: "lightsalmon"}) # # More cases # In[19]: results = ( (258201, 112), (258404, 591), ) # In[20]: n = results[0][0] A.pretty(n, highlights=set(results[0]), baseTypes="sign", withNodes=True, explain=False) # In[21]: n = results[1][0] A.pretty(n, highlights=set(results[1]), baseTypes="sign", withNodes=True, explain=False) # In[22]: w = 260817 A.pretty(w, baseTypes="sign", explain=False) # In[23]: ln = 231650 print(T.text(ln)) print(F.srcLn.v(ln)) # In[24]: A.plain( ln, withNodes=False, baseTypes="sign", explain=False, highlights={ 204104: "lightsalmon", 204105: "yellow", 204106: "lightgreen", 204107: "lightblue", }, ) A.pretty( ln, withNodes=False, baseTypes="sign", explain=False, highlights={ 204104: "lightsalmon", 204105: "yellow", 204106: "lightgreen", 204107: "lightblue", }, ) # In[25]: A.pretty(ln) # # Developer's cells # # # Use `A.reuse()` if you have changed the `config.yaml` of this corpus and want to reapply the settings. # # Inspect the result of the new settings by means of `A.showContext()`. # In[ ]: A.reuse() # In[26]: A.showContext()