#!/usr/bin/env python # coding: utf-8 # # Structure ins and outs # # See also the [docs](https://annotation.github.io/text-fabric/Api/Text/#structure) # In[1]: get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') # In[2]: import os from tf.fabric import Fabric # In[3]: GH_BASE = os.path.expanduser('~/github') ORG = 'annotation' REPO = 'banks' FOLDER = 'tf' TF_DIR = f'{GH_BASE}/{ORG}/{REPO}/{FOLDER}' VERSION = '0.2' TF_PATH = f'{TF_DIR}/{VERSION}' TF = Fabric(locations=TF_PATH) # We ask for a list of all features: # In[4]: allFeatures = TF.explore(silent=True, show=True) loadableFeatures = allFeatures['nodes'] + allFeatures['edges'] loadableFeatures # We load all features: # In[5]: api = TF.load(loadableFeatures, silent=False) T = api.T F = api.F # Look at the structure definition in the `otext` feature: # In[6]: TF.features['otext'].metaData # The fields `structureTypes` and `structureFeatures` define the node types that correspond to structural elements # and their heading features. # # But we do not have to ask for the this raw configuration, we can just interrogate the `T` API: # In[7]: T.structureInfo() # Print the top-level nodes: # In[8]: T.top() # Print the heading of the top-level node: # In[9]: top = T.top()[0] T.headingFromNode(top) # Get the node from the heading: # In[10]: T.nodeFromHeading((('book', 'Consider Phlebas'),)) # Go a level down: # In[11]: level2 = T.down(top) level2 # and print their headings: # In[12]: for l2 in level2: print(T.headingFromNode(l2)) # Go a level up: # In[13]: for l2 in level2: print(T.up(l2)) # The complete structure of the corpus as a tuple: # In[14]: T.structure() # The structure of the first chapter as a tuple: # In[15]: T.structure(node=101) # Pretty-print the structure of the first chapter: # In[16]: print(T.structurePretty(node=101)) # Pretty-print the complete structure: # In[17]: print(T.structurePretty()) # Pretty-print the complete structure with full headings: # In[18]: print(T.structurePretty(fullHeading=True)) # In[ ]: