Version: 0.5 (July 25, 2023 - major updates; adding textual critics details, unaccented and transliterated text)
The source data for the conversion are the LowFat XML trees files representing the macula-greek version of the Nestle 1904 Greek New Testment (British Foreign Bible Society, 1904). The starting dataset is formatted according to Syntax diagram markup by the Global Bible Initiative (GBI). The most recent source data can be found on github https://github.com/Clear-Bible/macula-greek/tree/main/Nestle1904/lowfat.
Attribution: "MACULA Greek Linguistic Datasets, available at https://github.com/Clear-Bible/macula-greek/".
The production of the Text-Fabric files consist of two phases. First one is the creation of piclke files (section 2). The second phase is the the actual Text-Fabric creation process (section 3). The process can be depicted as follows:
This script harvests all information from the LowFat tree data (XML nodes), puts it into a Panda DataFrame and stores the result per book in a pickle file. Note: pickling (in Python) is serialising an object into a disk file (or buffer). See also the Python3 documentation.
Within the context of this script, the term 'Leaf' refers to nodes that contain the Greek word as data. These nodes are also referred to as 'terminal nodes' since they do not have any children, similar to leaves on a tree. Additionally, Parent1 represents the parent of the leaf, Parent2 represents the parent of Parent1, and so on. For a visual representation, please refer to the following diagram.
For a full description of the source data see document MACULA Greek Treebank for the Nestle 1904 Greek New Testament.pdf
import pandas as pd
import sys
import os
import time
import pickle
import re #regular expressions
from os import listdir
from os.path import isfile, join
import xml.etree.ElementTree as ET
The following global data initializes the script, gathering the XML data to store it into the pickle files.
IMPORTANT: To ensure proper creation of the Text-Fabric files on your system, it is crucial to adjust the values of BaseDir, InputDir, and OutputDir to match the location of the data and the operating system you are using. In this Jupyter Notebook, Windows is the operating system employed.
BaseDir = 'D:\\'
XmlDir = BaseDir+'xml\\'
PklDir = BaseDir+'pkl\\'
XlsxDir = BaseDir+'xlsx\\'
# note: create output directory prior running this part
# key: filename, [0]=book_long, [1]=book_num, [3]=book_short
bo2book = {'01-matthew': ['Matthew', '1', 'Matt'],
'02-mark': ['Mark', '2', 'Mark'],
'03-luke': ['Luke', '3', 'Luke'],
'04-john': ['John', '4', 'John'],
'05-acts': ['Acts', '5', 'Acts'],
'06-romans': ['Romans', '6', 'Rom'],
'07-1corinthians': ['I_Corinthians', '7', '1Cor'],
'08-2corinthians': ['II_Corinthians', '8', '2Cor'],
'09-galatians': ['Galatians', '9', 'Gal'],
'10-ephesians': ['Ephesians', '10', 'Eph'],
'11-philippians': ['Philippians', '11', 'Phil'],
'12-colossians': ['Colossians', '12', 'Col'],
'13-1thessalonians':['I_Thessalonians', '13', '1Thess'],
'14-2thessalonians':['II_Thessalonians','14', '2Thess'],
'15-1timothy': ['I_Timothy', '15', '1Tim'],
'16-2timothy': ['II_Timothy', '16', '2Tim'],
'17-titus': ['Titus', '17', 'Titus'],
'18-philemon': ['Philemon', '18', 'Phlm'],
'19-hebrews': ['Hebrews', '19', 'Heb'],
'20-james': ['James', '20', 'Jas'],
'21-1peter': ['I_Peter', '21', '1Pet'],
'22-2peter': ['II_Peter', '22', '2Pet'],
'23-1john': ['I_John', '23', '1John'],
'24-2john': ['II_John', '24', '2John'],
'25-3john': ['III_John', '25', '3John'],
'26-jude': ['Jude', '26', 'Jude'],
'27-revelation': ['Revelation', '27', 'Rev']}
In order to be able to traverse from the 'leafs' upto the root of the tree, it is required to add information to each node pointing to the parent of each node. The terminating nodes of an XML tree are called "leaf nodes" or "leaves." These nodes do not have any child elements and are located at the end of a branch in the XML tree. Leaf nodes contain the actual data or content within an XML document. In contrast, non-leaf nodes are called "internal nodes," which have one or more child elements.
(Attribution: the concept of following functions is taken from https://stackoverflow.com/questions/2170610/access-elementtree-node-parent-node)
def addParentInfo(et):
for child in et:
child.attrib['parent'] = et
addParentInfo(child)
def getParent(et):
if 'parent' in et.attrib:
return et.attrib['parent']
else:
return None
This code processes books in the correct order. Firstly, it parses the XML and adds parent information to each node. Then, it loops through the nodes and checks if it is a 'leaf' node, meaning it contains only one word. If it is a 'leaf' node, the following steps are performed:
Note that this script takes a long time to execute (due to the large number of itterations). However, once the XML data is converted to PKL, there is no need to rerun (unless the source XML data is updated).
# set some globals
WordOrder=1 # stores the word order as it is found in the XML files (unique number for each word in the full corpus)
CollectedItems= 0
# process books in order
for bo, bookinfo in bo2book.items():
CollectedItems=0
SentenceNumber=0
WordGroupNumber=0
full_df=pd.DataFrame({})
book_long=bookinfo[0]
booknum=bookinfo[1]
book_short=bookinfo[2]
InputFile = os.path.join(XmlDir, f'{bo}.xml')
OutputFile = os.path.join(PklDir, f'{bo}.pkl')
print(f'Processing {book_long} at {InputFile}')
DataFrameList = []
# Send XML document to parsing process
tree = ET.parse(InputFile)
# Now add all the parent info to the nodes in the xtree [important!]
addParentInfo(tree.getroot())
start_time = time.time()
# walk over all the XML data
for elem in tree.iter():
if elem.tag == 'sentence':
# add running number to 'sentence' tags
SentenceNumber+=1
elem.set('SN', SentenceNumber)
if elem.tag == 'wg':
# add running number to 'wg' tags
WordGroupNumber+=1
elem.set('WGN', WordGroupNumber)
if elem.tag == 'w':
# all nodes containing words are tagged with 'w'
# show progress on screen
CollectedItems+=1
if (CollectedItems%100==0): print (".",end='')
#Leafref will contain list with book, chapter verse and wordnumber
Leafref = re.sub(r'[!: ]'," ", elem.attrib.get('ref')).split()
#push value for word_order to element tree
elem.set('word_order', WordOrder)
WordOrder+=1
# add some important computed data to the leaf
elem.set('LeafName', elem.tag)
elem.set('word', elem.text)
elem.set('book_long', book_long)
elem.set('booknum', int(booknum))
elem.set('book_short', book_short)
elem.set('chapter', int(Leafref[1]))
elem.set('verse', int(Leafref[2]))
# folling code will trace down parents upto the tree and store found attributes
parentnode=getParent(elem)
index=0
while (parentnode):
index+=1
elem.set('Parent{}Name'.format(index), parentnode.tag)
elem.set('Parent{}Type'.format(index), parentnode.attrib.get('type'))
elem.set('Parent{}Appos'.format(index), parentnode.attrib.get('appositioncontainer'))
elem.set('Parent{}Class'.format(index), parentnode.attrib.get('class'))
elem.set('Parent{}Rule'.format(index), parentnode.attrib.get('rule'))
elem.set('Parent{}Role'.format(index), parentnode.attrib.get('role'))
elem.set('Parent{}Cltype'.format(index), parentnode.attrib.get('cltype'))
elem.set('Parent{}Unit'.format(index), parentnode.attrib.get('unit'))
elem.set('Parent{}Junction'.format(index), parentnode.attrib.get('junction'))
elem.set('Parent{}SN'.format(index), parentnode.attrib.get('SN'))
elem.set('Parent{}WGN'.format(index), parentnode.attrib.get('WGN'))
currentnode=parentnode
parentnode=getParent(currentnode)
elem.set('parents', int(index))
#this will add all elements found in the tree to a list of dataframes
DataFrameChunk=pd.DataFrame(elem.attrib, index={'word_order'})
DataFrameList.append(DataFrameChunk)
#store the resulting DataFrame per book into a pickle file for further processing
full_df = pd.concat([df for df in DataFrameList])
output = open(r"{}".format(OutputFile), 'wb')
pickle.dump(full_df, output)
output.close()
print("\nFound ",CollectedItems, " items in %s seconds\n" % (time.time() - start_time))
Processing Matthew at D:\xml\01-matthew.xml
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [8], in <cell line: 13>() 27 tree = ET.parse(InputFile) 28 # Now add all the parent info to the nodes in the xtree [important!] ---> 29 addParentInfo(tree.getroot()) 30 start_time = time.time() 32 # walk over all the XML data NameError: name 'addParentInfo' is not defined
This script creates the Text-Fabric files by recursive calling the TF walker function. API info: https://annotation.github.io/text-fabric/tf/convert/walker.html
The pickle files created by the script in section 2.4 are stored on Github location /resources/pickle.
The following global data initializes the Text-Fabric conversion script.
IMPORTANT: To ensure the proper creation of the Text-Fabric files on your system, it is crucial to adjust the values of BaseDir, PklDir, etc., to match the location of the data and the operating system you are using. This Jupyter Notebook employs the Windows operating system.
import pandas as pd
import os
import re
import gc
from tf.fabric import Fabric
from tf.convert.walker import CV
from tf.parameters import VERSION
from datetime import date
import pickle
from unidecode import unidecode
import unicodedata
BaseDir = 'D:\\'
XmlDir = BaseDir+'xml\\'
PklDir = BaseDir+'pkl\\'
XlsxDir = BaseDir+'xlsx\\'
# key: filename, [0]=book_long, [1]=book_num, [3]=book_short
bo2book = {'01-matthew': ['Matthew', '1', 'Matt'],
'02-mark': ['Mark', '2', 'Mark'],
'03-luke': ['Luke', '3', 'Luke'],
'04-john': ['John', '4', 'John'],
'05-acts': ['Acts', '5', 'Acts'],
'06-romans': ['Romans', '6', 'Rom'],
'07-1corinthians': ['I_Corinthians', '7', '1Cor'],
'08-2corinthians': ['II_Corinthians', '8', '2Cor'],
'09-galatians': ['Galatians', '9', 'Gal'],
'10-ephesians': ['Ephesians', '10', 'Eph'],
'11-philippians': ['Philippians', '11', 'Phil'],
'12-colossians': ['Colossians', '12', 'Col'],
'13-1thessalonians':['I_Thessalonians', '13', '1Thess'],
'14-2thessalonians':['II_Thessalonians','14', '2Thess'],
'15-1timothy': ['I_Timothy', '15', '1Tim'],
'16-2timothy': ['II_Timothy', '16', '2Tim'],
'17-titus': ['Titus', '17', 'Titus'],
'18-philemon': ['Philemon', '18', 'Phlm'],
'19-hebrews': ['Hebrews', '19', 'Heb'],
'20-james': ['James', '20', 'Jas'],
'21-1peter': ['I_Peter', '21', '1Pet'],
'22-2peter': ['II_Peter', '22', '2Pet'],
'23-1john': ['I_John', '23', '1John'],
'24-2john': ['II_John', '24', '2John'],
'25-3john': ['III_John', '25', '3John'],
'26-jude': ['Jude', '26', 'Jude'],
'27-revelation': ['Revelation', '27', 'Rev']}
This step is optional. It will allow for manual examining the input data to the Text-Fabric conversion script.
# test: sorting the data
import openpyxl
import pickle
#if True:
for bo in bo2book:
'''
load all data into a dataframe
process books in order (bookinfo is a list!)
'''
InputFile = os.path.join(PklDir, f'{bo}.pkl')
print(f'\tloading {InputFile}...')
pkl_file = open(InputFile, 'rb')
df = pickle.load(pkl_file)
pkl_file.close()
df.to_excel(os.path.join(XlsxDir, f'{bo}.xlsx'), index=False)
loading D:\pkl\01-matthew.pkl... loading D:\pkl\02-mark.pkl... loading D:\pkl\03-luke.pkl... loading D:\pkl\04-john.pkl... loading D:\pkl\05-acts.pkl... loading D:\pkl\06-romans.pkl... loading D:\pkl\07-1corinthians.pkl... loading D:\pkl\08-2corinthians.pkl... loading D:\pkl\09-galatians.pkl... loading D:\pkl\10-ephesians.pkl... loading D:\pkl\11-philippians.pkl... loading D:\pkl\12-colossians.pkl... loading D:\pkl\13-1thessalonians.pkl... loading D:\pkl\14-2thessalonians.pkl... loading D:\pkl\15-1timothy.pkl... loading D:\pkl\16-2timothy.pkl... loading D:\pkl\17-titus.pkl... loading D:\pkl\18-philemon.pkl... loading D:\pkl\19-hebrews.pkl... loading D:\pkl\20-james.pkl... loading D:\pkl\21-1peter.pkl... loading D:\pkl\22-2peter.pkl... loading D:\pkl\23-1john.pkl... loading D:\pkl\24-2john.pkl... loading D:\pkl\25-3john.pkl... loading D:\pkl\26-jude.pkl... loading D:\pkl\27-revelation.pkl...
API info: https://annotation.github.io/text-fabric/tf/convert/walker.html
Explanatory notes about the data interpretation logic are incorporated within the Python code of the director function.
TF = Fabric(locations=BaseDir, silent=False)
cv = CV(TF)
###############################################
# Common helper functions #
###############################################
#Function to prevent errors during conversion due to missing data
def sanitize(input):
if isinstance(input, float): return ''
if isinstance(input, type(None)): return ''
else: return (input)
# Function to expand the syntactic categories of words or wordgroup
# See also "MACULA Greek Treebank for the Nestle 1904 Greek New Testament.pdf"
# page 5&6 (section 2.4 Syntactic Categories at Clause Level)
def ExpandRole(input):
if input=="adv": return 'Adverbial'
if input=="io": return 'Indirect Object'
if input=="o": return 'Object'
if input=="o2": return 'Second Object'
if input=="s": return 'Subject'
if input=="p": return 'Predicate'
if input=="v": return 'Verbal'
if input=="vc": return 'Verbal Copula'
if input=='aux': return 'Auxiliar'
return ''
# Function to expantion of Part of Speech labels. See also the description in
# "MACULA Greek Treebank for the Nestle 1904 Greek New Testament.pdf" page 6&7
# (2.2. Syntactic Categories at Word Level: Part of Speech Labels)
def ExpandSP(input):
if input=='adj': return 'Adjective'
if input=='conj': return 'Conjunction'
if input=='det': return 'Determiner'
if input=='intj': return 'Interjection'
if input=='noun': return 'Noun'
if input=='num': return 'Numeral'
if input=='prep': return 'Preposition'
if input=='ptcl': return 'Particle'
if input=='pron': return 'Pronoun'
if input=='verb': return 'Verb'
return ''
# Small function to remove accents from Greek words
def removeAccents(text):
return ''.join(c for c in unicodedata.normalize('NFD', text) if unicodedata.category(c) != 'Mn')
###############################################
# The director routine #
###############################################
def director(cv):
###############################################
# Innitial setup of data etc. #
###############################################
NoneType = type(None) # needed as tool to validate certain data
IndexDict = {} # init an empty dictionary
WordGroupDict={} # init a dummy dictionary
PrevWordGroupSet = WordGroupSet = []
PrevWordGroupList = WordGroupList = []
RootWordGroup = 0
WordNumber=FoundWords=WordGroupTrack=0
# The following is required to recover succesfully from an abnormal condition
# in the LowFat tree data where a <wg> element is labeled as <error>
# this number is arbitrary but should be high enough not to clash with 'real' WG numbers
DummyWGN=200000
# Following variables are used for textual critical data
criticalMarkCharacters = "[]()—"
punctuationCharacters = ",.;·"
translationTableMarkers = str.maketrans("", "", criticalMarkCharacters)
translationTablePunctuations = str.maketrans("", "", punctuationCharacters)
punctuations=('.',',',';','·')
for bo,bookinfo in bo2book.items():
###############################################
# start of section executed for each book #
###############################################
# note: bookinfo is a list! Split the data
Book = bookinfo[0]
BookNumber = int(bookinfo[1])
BookShort = bookinfo[2]
BookLoc = os.path.join(PklDir, f'{bo}.pkl')
# load data for this book into a dataframe.
# make sure wordorder is correct
print(f'\tWe are loading {BookLoc}...')
pkl_file = open(BookLoc, 'rb')
df_unsorted = pickle.load(pkl_file)
pkl_file.close()
'''
Fill dictionary of column names for this book
sort to ensure proper wordorder
'''
ItemsInRow=1
for itemname in df_unsorted.columns.to_list():
IndexDict.update({'i_{}'.format(itemname): ItemsInRow})
# This is to identify the collumn containing the key to sort upon
if itemname=="{http://www.w3.org/XML/1998/namespace}id": SortKey=ItemsInRow-1
ItemsInRow+=1
df=df_unsorted.sort_values(by=df_unsorted.columns[SortKey])
del df_unsorted
# Set up nodes for new book
ThisBookPointer = cv.node('book')
cv.feature(ThisBookPointer, book=Book, booknumber=BookNumber, bookshort=BookShort)
ThisChapterPointer = cv.node('chapter')
cv.feature(ThisChapterPointer, chapter=1)
PreviousChapter=1
ThisVersePointer = cv.node('verse')
cv.feature(ThisVersePointer, verse=1)
PreviousVerse=1
ThisSentencePointer = cv.node('sentence')
cv.feature(ThisSentencePointer, sentence=1)
PreviousSentence=1
###############################################
# Iterate through words and construct objects #
###############################################
for row in df.itertuples():
WordNumber += 1
FoundWords +=1
# Detect and act upon changes in sentences, verse and chapter
# the order of terminating and creating the nodes is critical:
# close verse - close chapter - open chapter - open verse
NumberOfParents = sanitize(row[IndexDict.get("i_parents")])
ThisSentence=int(row[IndexDict.get("i_Parent{}SN".format(NumberOfParents-1))])
ThisVerse = sanitize(row[IndexDict.get("i_verse")])
ThisChapter = sanitize(row[IndexDict.get("i_chapter")])
if (ThisSentence!=PreviousSentence):
cv.terminate(ThisSentencePointer)
if (ThisVerse!=PreviousVerse):
cv.terminate(ThisVersePointer)
if (ThisChapter!=PreviousChapter):
cv.terminate(ThisChapterPointer)
PreviousChapter = ThisChapter
ThisChapterPointer = cv.node('chapter')
cv.feature(ThisChapterPointer, chapter=ThisChapter)
if (ThisVerse!=PreviousVerse):
PreviousVerse = ThisVerse
ThisVersePointer = cv.node('verse')
cv.feature(ThisVersePointer, verse=ThisVerse, chapter=ThisChapter)
if (ThisSentence!=PreviousSentence):
PreviousSentence=ThisSentence
ThisSentencePointer = cv.node('sentence')
cv.feature(ThisSentencePointer, verse=ThisVerse, chapter=ThisChapter)
###############################################
# analyze and process <WG> tags #
###############################################
PrevWordGroupList=WordGroupList
WordGroupList=[] # stores current active WordGroup numbers
for i in range(NumberOfParents-2,0,-1): # important: reversed itteration!
_WGN=row[IndexDict.get("i_Parent{}WGN".format(i))]
if isinstance(_WGN, type(None)):
# handling conditions where XML data has <error role="err_clause-complex-met-no-conditionsClCl2"> e.g. Acts 26:12
# to recover, we need to create a dummy WG with a sufficient high WGN so it can never match any real WGN.
WGN=DummyWGN
else:
WGN=int(_WGN)
if WGN!='':
WordGroupList.append(WGN)
WordGroupDict[(WGN,0)]=WGN
WGclass=sanitize(row[IndexDict.get("i_Parent{}Class".format(i))])
WGrule=sanitize(row[IndexDict.get("i_Parent{}Rule".format(i))])
WGtype=sanitize(row[IndexDict.get("i_Parent{}Type".format(i))])
if WGclass==WGrule==WGtype=='':
WGclass='to be skipped?'
if WGrule[-2:]=='CL' and WGclass=='':
WGclass='cl*' # to simulate the way Logos presents this condition
WordGroupDict[(WGN,6)]=WGclass
WordGroupDict[(WGN,1)]=WGrule
WordGroupDict[(WGN,8)]=WGtype
WordGroupDict[(WGN,3)]=sanitize(row[IndexDict.get("i_Parent{}Junction".format(i))])
WordGroupDict[(WGN,2)]=sanitize(row[IndexDict.get("i_Parent{}Cltype".format(i))])
WordGroupDict[(WGN,7)]=sanitize(row[IndexDict.get("i_Parent{}Role".format(i))])
WordGroupDict[(WGN,9)]=sanitize(row[IndexDict.get("i_Parent{}Appos".format(i))])
WordGroupDict[(WGN,10)]=NumberOfParents-1-i # = number of parent wordgroups
if not PrevWordGroupList==WordGroupList:
if RootWordGroup != WordGroupList[0]:
RootWordGroup = WordGroupList[0]
SuspendableWordGoupList = []
# we have a new sentence. rebuild suspendable wordgroup list
# some cleaning of data may be added here to save on memmory...
#for k in range(6): del WordGroupDict[item,k]
for item in reversed(PrevWordGroupList):
if (item not in WordGroupList):
# CLOSE/SUSPEND CASE
SuspendableWordGoupList.append(item)
cv.terminate(WordGroupDict[item,4])
for item in WordGroupList:
if (item not in PrevWordGroupList):
if (item in SuspendableWordGoupList):
# RESUME CASE
#print ('\n resume: '+str(item),end=' ')
cv.resume(WordGroupDict[(item,4)])
else:
# CREATE CASE
#print ('\n create: '+str(item),end=' ')
WordGroupDict[(item,4)]=cv.node('wg')
WordGroupDict[(item,5)]=WordGroupTrack
WordGroupTrack += 1
cv.feature(WordGroupDict[(item,4)], wgnum=WordGroupDict[(item,0)], junction=WordGroupDict[(item,3)],
clausetype=WordGroupDict[(item,2)], wgrule=WordGroupDict[(item,1)], wgclass=WordGroupDict[(item,6)],
wgrole=WordGroupDict[(item,7)],wgrolelong=ExpandRole(WordGroupDict[(item,7)]),
wgtype=WordGroupDict[(item,8)],appos=WordGroupDict[(item,8)],wglevel=WordGroupDict[(item,10)])
# These roles are performed either by a WG or just a single word.
Role=row[IndexDict.get("i_role")]
ValidRoles=["adv","io","o","o2","s","p","v","vc","aux"]
DistanceToRoleClause=0
if isinstance (Role,str) and Role in ValidRoles:
# Role is assign to this word (uniqely)
WordRole=Role
WordRoleLong=ExpandRole(WordRole)
else:
# Role details needs to be taken from some uptree wordgroup
WordRole=WordRoleLong=''
for item in range(1,NumberOfParents-1):
Role = sanitize(row[IndexDict.get("i_Parent{}Role".format(item))])
if isinstance (Role,str) and Role in ValidRoles:
WordRole=Role
WordRoleLong=ExpandRole(WordRole)
DistanceToRoleClause=item
break
# Find the number of the WG containing the clause definition
for item in range(1,NumberOfParents-1):
WGrule = sanitize(row[IndexDict.get("i_Parent{}Rule".format(item))])
if row[IndexDict.get("i_Parent{}Class".format(item))]=='cl' or WGrule[-2:]=='CL':
ContainedClause=sanitize(row[IndexDict.get("i_Parent{}WGN".format(item))])
break
###############################################
# analyze and process <W> tags #
###############################################
# Determine syntactic categories at word level.
PartOfSpeech=sanitize(row[IndexDict.get("i_class")])
PartOfSpeechFull=ExpandSP(PartOfSpeech)
# The folling part of code reproduces feature 'word' and 'after' that are
# currently containing incorrect data in a few specific cases.
# See https://github.com/tonyjurg/Nestle1904LFT/blob/main/resources/identifying_odd_afters.ipynb
# Get the word details and detect presence of punctuations
# it also creates the textual critical features
rawWord=sanitize(row[IndexDict.get("i_unicode")])
cleanWord= rawWord.translate(translationTableMarkers)
rawWithoutPunctuations=rawWord.translate(translationTablePunctuations)
markBefore=markAfter=PunctuationMarkOrder=''
if cleanWord[-1] in punctuations:
punctuation=cleanWord[-1]
after=punctuation+' '
word=cleanWord[:-1]
else:
after=' '
word=cleanWord
punctuation=''
if rawWithoutPunctuations!=word:
markAfter=markBefore=''
if rawWord.find(word)==0:
markAfter=rawWithoutPunctuations.replace(word,"")
if punctuation!='':
if rawWord.find(markAfter)-rawWord.find(punctuation)>0:
PunctuationMarkOrder="3" # punct. before mark
else:
PunctuationMarkOrder="2" # punct. after mark.
else:
PunctuationMarkOrder="1" #no punctuation, mark after word
else:
markBefore=rawWithoutPunctuations.replace(word,"")
PunctuationMarkOrder="0" #mark is before word
# Some attributes are not present inside some (small) books. The following is to prevent exceptions.
degree=''
if 'i_degree' in IndexDict: degree=sanitize(row[IndexDict.get("i_degree")])
subjref=''
if 'i_subjref' in IndexDict: subjref=sanitize(row[IndexDict.get("i_subjref")])
# Create the word slots
this_word = cv.slot()
cv.feature(this_word,
after= after,
unicode= rawWord,
word= word,
wordtranslit= unidecode(word),
wordunacc= removeAccents(word),
punctuation= punctuation,
markafter= markAfter,
markbefore= markBefore,
markorder= PunctuationMarkOrder,
monad= FoundWords,
orig_order= sanitize(row[IndexDict.get("i_word_order")]),
book= Book,
booknumber= BookNumber,
bookshort= BookShort,
chapter= ThisChapter,
ref= sanitize(row[IndexDict.get("i_ref")]),
sp= PartOfSpeech,
sp_full= PartOfSpeechFull,
verse= ThisVerse,
sentence= ThisSentence,
normalized= sanitize(row[IndexDict.get("i_normalized")]),
morph= sanitize(row[IndexDict.get("i_morph")]),
strongs= sanitize(row[IndexDict.get("i_strong")]),
lex_dom= sanitize(row[IndexDict.get("i_domain")]),
ln= sanitize(row[IndexDict.get("i_ln")]),
gloss= sanitize(row[IndexDict.get("i_gloss")]),
gn= sanitize(row[IndexDict.get("i_gender")]),
nu= sanitize(row[IndexDict.get("i_number")]),
case= sanitize(row[IndexDict.get("i_case")]),
lemma= sanitize(row[IndexDict.get("i_lemma")]),
person= sanitize(row[IndexDict.get("i_person")]),
mood= sanitize(row[IndexDict.get("i_mood")]),
tense= sanitize(row[IndexDict.get("i_tense")]),
number= sanitize(row[IndexDict.get("i_number")]),
voice= sanitize(row[IndexDict.get("i_voice")]),
degree= degree,
type= sanitize(row[IndexDict.get("i_type")]),
reference= sanitize(row[IndexDict.get("i_ref")]),
subj_ref= subjref,
nodeID= sanitize(row[4]), #this is a fixed position in dataframe
wordrole= WordRole,
wordrolelong= WordRoleLong,
wordlevel= NumberOfParents-1,
roleclausedistance = DistanceToRoleClause,
containedclause = ContainedClause
)
cv.terminate(this_word)
'''
wrap up the book. At the end of the book we need to close all nodes in proper order.
'''
# close all open WordGroup nodes
for item in WordGroupList:
#cv.feature(WordGroupDict[(item,4)], add some stats?)
cv.terminate(WordGroupDict[item,4])
cv.terminate(ThisSentencePointer)
cv.terminate(ThisVersePointer)
cv.terminate(ThisChapterPointer)
cv.terminate(ThisBookPointer)
# clear dataframe for this book, clear the index dictionary
del df
IndexDict.clear()
gc.collect()
###############################################
# end of section executed for each book #
###############################################
###############################################
# end of director function #
###############################################
###############################################
# Output definitions #
###############################################
slotType = 'word'
otext = { # dictionary of config data for sections and text formats
'fmt:text-orig-full': '{word}{after}',
'fmt:text-normalized': '{normalized}{after}',
'fmt:text-unaccented': '{wordunacc}{after}',
'fmt:text-transliterated':'{wordtranslit}{after}'
'fmt:text-critical': '{unicode} ',
'sectionTypes':'book,chapter,verse',
'sectionFeatures':'book,chapter,verse',
'structureFeatures': 'book,chapter,verse',
'structureTypes': 'book,chapter,verse',
}
# configure metadata
generic = { # dictionary of metadata meant for all features
'textFabriVersion': '{}'.format(VERSION), #imported from tf.parameter
'xmlSourceLocation': 'https://github.com/tonyjurg/Nestle1904LFT/tree/main/resources/xml/20230628',
'xmlSourceDate': 'June 28, 2023',
'author': 'Evangelists and apostles',
'availability': 'Creative Commons Attribution 4.0 International (CC BY 4.0)',
'converters': 'Tony Jurg',
'converterSource': 'https://github.com/tonyjurg/Nestle1904LFT/tree/main/resources/converter',
'converterVersion': '0.5',
'dataSource': 'MACULA Greek Linguistic Datasets, available at https://github.com/Clear-Bible/macula-greek/tree/main/Nestle1904/nodes',
'editors': 'Eberhart Nestle (1904)',
'sourceDescription': 'Greek New Testment (British Foreign Bible Society, 1904)',
'sourceFormat': 'XML (Low Fat tree XML data)',
'title': 'Greek New Testament (Nestle1904LFT)'
}
# set of integer valued feature names
intFeatures = {
'booknumber',
'chapter',
'verse',
'sentence',
'wgnum',
'orig_order',
'monad',
'wglevel'
}
# per feature dicts with metadata
featureMeta = {
'after': {'description': 'Characters (eg. punctuations) following the word'},
'book': {'description': 'Book name'},
'booknumber': {'description': 'NT book number (Matthew=1, Mark=2, ..., Revelation=27)'},
'bookshort': {'description': 'Book name (abbreviated)'},
'chapter': {'description': 'Chapter number inside book'},
'verse': {'description': 'Verse number inside chapter'},
'sentence': {'description': 'Sentence number (counted per chapter)'},
'type': {'description': 'Wordgroup type information (verb, verbless, elided, minor, etc.)'},
'wgrule': {'description': 'Wordgroup rule information'},
'orig_order': {'description': 'Word order (in source XML file)'},
'monad': {'description': 'Monad (word order in the corpus)'},
'word': {'description': 'Word as it appears in the text (excl. punctuations)'},
'wordtranslit':{'description': 'Transliteration of the text (in latin letters, excl. punctuations)'},
'wordunacc': {'description': 'Word without accents (excl. punctuations)'},
'unicode': {'description': 'Word as it arears in the text in Unicode (incl. punctuations)'},
'punctuation': {'description': 'Punctuation after word'},
'markafter': {'description': 'Text critical marker after word'},
'markbefore': {'description': 'Text critical marker before word'},
'markorder': {'description': 'Order of punctuation and text critical marker'},
'ref': {'description': 'ref ID'},
'sp': {'description': 'Part of Speech (abbreviated)'},
'sp_full': {'description': 'Part of Speech (long description)'},
'normalized': {'description': 'Surface word with accents normalized and trailing punctuations removed'},
'lemma': {'description': 'Lexeme (lemma)'},
'morph': {'description': 'Morphological tag (Sandborg-Petersen morphology)'},
# see also discussion on relation between lex_dom and ln
# @ https://github.com/Clear-Bible/macula-greek/issues/29
'lex_dom': {'description': 'Lexical domain according to Semantic Dictionary of Biblical Greek, SDBG (not present everywhere?)'},
'ln': {'description': 'Lauw-Nida lexical classification (not present everywhere?)'},
'strongs': {'description': 'Strongs number'},
'gloss': {'description': 'English gloss'},
'gn': {'description': 'Gramatical gender (Masculine, Feminine, Neuter)'},
'nu': {'description': 'Gramatical number (Singular, Plural)'},
'case': {'description': 'Gramatical case (Nominative, Genitive, Dative, Accusative, Vocative)'},
'person': {'description': 'Gramatical person of the verb (first, second, third)'},
'mood': {'description': 'Gramatical mood of the verb (passive, etc)'},
'tense': {'description': 'Gramatical tense of the verb (e.g. Present, Aorist)'},
'number': {'description': 'Gramatical number of the verb'},
'voice': {'description': 'Gramatical voice of the verb'},
'degree': {'description': 'Degree (e.g. Comparitative, Superlative)'},
'type': {'description': 'Gramatical type of noun or pronoun (e.g. Common, Personal)'},
'reference': {'description': 'Reference (to nodeID in XML source data, not yet post-processes)'},
'subj_ref': {'description': 'Subject reference (to nodeID in XML source data, not yet post-processes)'},
'nodeID': {'description': 'Node ID (as in the XML source data, not yet post-processes)'},
'junction': {'description': 'Junction data related to a wordgroup'},
'wgnum': {'description': 'Wordgroup number (counted per book)'},
'wgclass': {'description': 'Class of the wordgroup ()'},
'wgrole': {'description': 'Role of the wordgroup (abbreviated)'},
'wgrolelong': {'description': 'Role of the wordgroup (full)'},
'wordrole': {'description': 'Role of the word (abbreviated)'},
'wordrolelong':{'description': 'Role of the word (full)'},
'wgtype': {'description': 'Wordgroup type details'},
'clausetype': {'description': 'Clause type details'},
'appos': {'description': 'Apposition details'},
'wglevel': {'description': 'Number of parent wordgroups for a wordgroup'},
'wordlevel': {'description': 'Number of parent wordgroups for a word'},
'roleclausedistance': {'description': 'Distance to wordgroup defining the role of this word'},
'containedclause': {'description': 'Contained clause (WG number)'}
}
###############################################
# the main function #
###############################################
good = cv.walk(
director,
slotType,
otext=otext,
generic=generic,
intFeatures=intFeatures,
featureMeta=featureMeta,
warn=True,
force=False
)
if good:
print ("done")
This is Text-Fabric 11.4.10 56 features found and 0 ignored 0.00s Importing data from walking through the source ... | 0.00s Preparing metadata... | SECTION TYPES: book, chapter, verse | SECTION FEATURES: book, chapter, verse | STRUCTURE TYPES: book, chapter, verse | STRUCTURE FEATURES: book, chapter, verse | TEXT FEATURES: | | text-orig-full after, word | 0.00s OK | 0.00s Following director... We are loading D:\pkl\01-matthew.pkl... We are loading D:\pkl\02-mark.pkl... We are loading D:\pkl\03-luke.pkl... We are loading D:\pkl\04-john.pkl... We are loading D:\pkl\05-acts.pkl... We are loading D:\pkl\06-romans.pkl... We are loading D:\pkl\07-1corinthians.pkl... We are loading D:\pkl\08-2corinthians.pkl... We are loading D:\pkl\09-galatians.pkl... We are loading D:\pkl\10-ephesians.pkl... We are loading D:\pkl\11-philippians.pkl... We are loading D:\pkl\12-colossians.pkl... We are loading D:\pkl\13-1thessalonians.pkl... We are loading D:\pkl\14-2thessalonians.pkl... We are loading D:\pkl\15-1timothy.pkl... We are loading D:\pkl\16-2timothy.pkl... We are loading D:\pkl\17-titus.pkl... We are loading D:\pkl\18-philemon.pkl... We are loading D:\pkl\19-hebrews.pkl... We are loading D:\pkl\20-james.pkl... We are loading D:\pkl\21-1peter.pkl... We are loading D:\pkl\22-2peter.pkl... We are loading D:\pkl\23-1john.pkl... We are loading D:\pkl\24-2john.pkl... We are loading D:\pkl\25-3john.pkl... We are loading D:\pkl\26-jude.pkl... We are loading D:\pkl\27-revelation.pkl... | 52s "edge" actions: 0 | 52s "feature" actions: 267467 | 52s "node" actions: 129688 | 52s "resume" actions: 9627 | 52s "slot" actions: 137779 | 52s "terminate" actions: 277221 | 27 x "book" node | 260 x "chapter" node | 8011 x "sentence" node | 7943 x "verse" node | 113447 x "wg" node | 137779 x "word" node = slot type | 267467 nodes of all types | 52s OK | 0.00s checking for nodes and edges ... | 0.00s OK | 0.00s checking (section) features ...
| 0.24s ERROR feature metadata (2 x): | | node feature "wordtranslit" has no metadata | | node feature "wordunacc" has no metadata
# First, I have to laod different modules that I use for analyzing the data and for plotting:
import sys, os, collections
import pandas as pd
import numpy as np
import re
from tf.fabric import Fabric
from tf.app import use
The following cell loads the TextFabric files from github repository.
# Loading-the-New-Testament-Text-Fabric
N1904 = use ("tonyjurg/Nestle1904LFT", version="0.4", hoist=globals())
Locating corpus resources ...
The requested app is not available offline ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/app not found
findAppClass: invalid syntax (~/text-fabric-data/github/tonyjurg/Nestle1904LFT/app/app.py, line 5)
findAppClass: Api for "tonyjurg/Nestle1904LFT" not loaded The requested data is not available offline ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 not found
| 0.25s T otype from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 2.72s T oslots from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.56s T chapter from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.56s T after from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.69s T word from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.55s T verse from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.58s T book from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | | 0.06s C __levels__ from otype, oslots, otext | | 1.62s C __order__ from otype, oslots, __levels__ | | 0.07s C __rank__ from otype, __order__ | | 4.30s C __levUp__ from otype, oslots, __rank__ | | 2.21s C __levDown__ from otype, __levUp__, __rank__ | | 0.06s C __characters__ from otext | | 1.24s C __boundary__ from otype, oslots, __rank__ | | 0.05s C __sections__ from otype, oslots, otext, __levUp__, __levels__, book, chapter, verse | | 0.26s C __structure__ from otype, oslots, otext, __rank__, __levUp__, book, chapter, verse | 0.44s T appos from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.49s T booknumber from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.58s T bookshort from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.55s T case from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.40s T clausetype from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.62s T containedclause from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.49s T degree from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.66s T gloss from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.54s T gn from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.42s T junction from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.62s T lemma from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.59s T lex_dom from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.61s T ln from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.50s T monad from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.51s T mood from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.59s T morph from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.61s T nodeID from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.66s T normalized from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.56s T nu from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.55s T number from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.50s T orig_order from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.50s T person from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.76s T ref from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.58s T roleclausedistance from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.49s T rule from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.51s T sentence from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.60s T sp from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.58s T sp_full from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.61s T strongs from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.53s T subj_ref from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.53s T tense from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.53s T type from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.68s T unicode from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.51s T voice from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.47s T wgclass from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.41s T wglevel from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.43s T wgnum from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.43s T wgrole from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.44s T wgrolelong from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.41s T wgtype from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.58s T wordlevel from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.58s T wordrole from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4 | 0.60s T wordrolelong from ~/text-fabric-data/github/tonyjurg/Nestle1904LFT/tf/0.4
Name | # of nodes | # slots/node | % coverage |
---|---|---|---|
book | 27 | 5102.93 | 100 |
chapter | 260 | 529.92 | 100 |
verse | 7943 | 17.35 | 100 |
sentence | 8011 | 17.20 | 100 |
wg | 113447 | 7.58 | 624 |
word | 137779 | 1.00 | 100 |