\n",
"\n",
"Be advised that this Text-Fabric version is a test version (proof of concept) and requires further finetuning, especialy with regards of nomenclature and presentation of (sub)phrases and clauses."
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## 2. Read LowFat XML data and store in pickle \n",
"##### [Back to TOC](#TOC)\n",
"\n",
"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). \n",
"\n",
"In the context of this script, 'Leaf' refers to those node containing the Greek word as data, which happen to be the nodes without any child (hence the analogy with the leaves on the tree). These 'leafs' can also be refered to as 'terminal nodes'. Futher, Parent1 is the leaf's parent, Parent2 is Parent1's parent, etc.\n",
"\n",
"For a full description of the source data see document [MACULA Greek Treebank for the Nestle 1904 Greek New Testament.pdf](https://github.com/Clear-Bible/macula-greek/blob/main/doc/MACULA%20Greek%20Treebank%20for%20the%20Nestle%201904%20Greek%20New%20Testament.pdf)"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Step 1: import various libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-28T02:58:14.739227Z",
"start_time": "2022-10-28T02:57:38.766097Z"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import sys\n",
"import os\n",
"import time\n",
"import pickle\n",
"\n",
"import re #regular expressions\n",
"from os import listdir\n",
"from os.path import isfile, join\n",
"import xml.etree.ElementTree as ET\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: initialize global data\n",
"\n",
"Change BaseDir, XmlDir and PklDir to match location of the datalocation and the OS used."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"BaseDir = 'C:\\\\Users\\\\tonyj\\\\my_new_Jupyter_folder\\\\Read_from_lowfat\\\\data\\\\'\n",
"XmlDir = BaseDir+'xml\\\\'\n",
"PklDir = BaseDir+'pkl\\\\'\n",
"XlsxDir = BaseDir+'xlsx\\\\'\n",
"# note: create output directory prior running this part\n",
"\n",
"# key: filename, [0]=book_long, [1]=book_num, [3]=book_short\n",
"bo2book = {'01-matthew': ['Matthew', '1', 'Matt'],\n",
" '02-mark': ['Mark', '2', 'Mark'],\n",
" '03-luke': ['Luke', '3', 'Luke'],\n",
" '04-john': ['John', '4', 'John'],\n",
" '05-acts': ['Acts', '5', 'Acts'],\n",
" '06-romans': ['Romans', '6', 'Rom'],\n",
" '07-1corinthians': ['I_Corinthians', '7', '1Cor'],\n",
" '08-2corinthians': ['II_Corinthians', '8', '2Cor'],\n",
" '09-galatians': ['Galatians', '9', 'Gal'],\n",
" '10-ephesians': ['Ephesians', '10', 'Eph'],\n",
" '11-philippians': ['Philippians', '11', 'Phil'],\n",
" '12-colossians': ['Colossians', '12', 'Col'],\n",
" '13-1thessalonians':['I_Thessalonians', '13', '1Thess'],\n",
" '14-2thessalonians':['II_Thessalonians','14', '2Thess'],\n",
" '15-1timothy': ['I_Timothy', '15', '1Tim'],\n",
" '16-2timothy': ['II_Timothy', '16', '2Tim'],\n",
" '17-titus': ['Titus', '17', 'Titus'],\n",
" '18-philemon': ['Philemon', '18', 'Phlm'],\n",
" '19-hebrews': ['Hebrews', '19', 'Heb'],\n",
" '20-james': ['James', '20', 'Jas'],\n",
" '21-1peter': ['I_Peter', '21', '1Pet'],\n",
" '22-2peter': ['II_Peter', '22', '2Pet'],\n",
" '23-1john': ['I_John', '23', '1John'],\n",
" '24-2john': ['II_John', '24', '2John'],\n",
" '25-3john': ['III_John', '25', '3John'], \n",
" '26-jude': ['Jude', '26', 'Jude'],\n",
" '27-revelation': ['Revelation', '27', 'Rev']}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### step 3: define Function to add parent info to each node of the XML tree\n",
"\n",
"In order to traverse from the 'leafs' (terminating nodes) upto the root of the tree, it is required to add information to each node pointing to the parent of each node.\n",
"\n",
"(concept taken from https://stackoverflow.com/questions/2170610/access-elementtree-node-parent-node)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"def addParentInfo(et):\n",
" for child in et:\n",
" child.attrib['parent'] = et\n",
" addParentInfo(child)\n",
"\n",
"def getParent(et):\n",
" if 'parent' in et.attrib:\n",
" return et.attrib['parent']\n",
" else:\n",
" return None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4: read and process the XML data and store panda dataframe in pickle"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Processing Matthew at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\01-matthew.xml\n",
"......................................................................................................................................................................................\n",
"Found 18299 items in 75.79531121253967 seconds\n",
"\n",
"Processing Mark at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\02-mark.xml\n",
"................................................................................................................\n",
"Found 11277 items in 45.66975522041321 seconds\n",
"\n",
"Processing Luke at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\03-luke.xml\n",
"..................................................................................................................................................................................................\n",
"Found 19456 items in 251.0713756084442 seconds\n",
"\n",
"Processing John at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\04-john.xml\n",
"............................................................................................................................................................\n",
"Found 15643 items in 62.22022581100464 seconds\n",
"\n",
"Processing Acts at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\05-acts.xml\n",
".......................................................................................................................................................................................\n",
"Found 18393 items in 80.11186122894287 seconds\n",
"\n",
"Processing Romans at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\06-romans.xml\n",
".......................................................................\n",
"Found 7100 items in 33.103615522384644 seconds\n",
"\n",
"Processing I_Corinthians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\07-1corinthians.xml\n",
"....................................................................\n",
"Found 6820 items in 28.27225947380066 seconds\n",
"\n",
"Processing II_Corinthians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\08-2corinthians.xml\n",
"............................................\n",
"Found 4469 items in 21.04596710205078 seconds\n",
"\n",
"Processing Galatians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\09-galatians.xml\n",
"......................\n",
"Found 2228 items in 9.386286497116089 seconds\n",
"\n",
"Processing Ephesians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\10-ephesians.xml\n",
"........................\n",
"Found 2419 items in 12.377203464508057 seconds\n",
"\n",
"Processing Philippians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\11-philippians.xml\n",
"................\n",
"Found 1630 items in 6.929890871047974 seconds\n",
"\n",
"Processing Colossians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\12-colossians.xml\n",
"...............\n",
"Found 1575 items in 7.252684116363525 seconds\n",
"\n",
"Processing I_Thessalonians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\13-1thessalonians.xml\n",
"..............\n",
"Found 1473 items in 6.500782251358032 seconds\n",
"\n",
"Processing II_Thessalonians at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\14-2thessalonians.xml\n",
"........\n",
"Found 822 items in 3.1195180416107178 seconds\n",
"\n",
"Processing I_Timothy at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\15-1timothy.xml\n",
"...............\n",
"Found 1588 items in 8.0901620388031 seconds\n",
"\n",
"Processing II_Timothy at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\16-2timothy.xml\n",
"............\n",
"Found 1237 items in 6.2979772090911865 seconds\n",
"\n",
"Processing Titus at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\17-titus.xml\n",
"......\n",
"Found 658 items in 2.693586826324463 seconds\n",
"\n",
"Processing Philemon at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\18-philemon.xml\n",
"...\n",
"Found 335 items in 1.189115047454834 seconds\n",
"\n",
"Processing Hebrews at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\19-hebrews.xml\n",
".................................................\n",
"Found 4955 items in 20.371201038360596 seconds\n",
"\n",
"Processing James at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\20-james.xml\n",
".................\n",
"Found 1739 items in 5.6465394496917725 seconds\n",
"\n",
"Processing I_Peter at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\21-1peter.xml\n",
"................\n",
"Found 1676 items in 8.954313278198242 seconds\n",
"\n",
"Processing II_Peter at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\22-2peter.xml\n",
"..........\n",
"Found 1098 items in 4.5939764976501465 seconds\n",
"\n",
"Processing I_John at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\23-1john.xml\n",
".....................\n",
"Found 2136 items in 8.40240740776062 seconds\n",
"\n",
"Processing II_John at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\24-2john.xml\n",
"..\n",
"Found 245 items in 0.5407323837280273 seconds\n",
"\n",
"Processing III_John at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\25-3john.xml\n",
"..\n",
"Found 219 items in 0.882843017578125 seconds\n",
"\n",
"Processing Jude at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\26-jude.xml\n",
"....\n",
"Found 457 items in 1.5982708930969238 seconds\n",
"\n",
"Processing Revelation at C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\xml\\27-revelation.xml\n",
"..................................................................................................\n",
"Found 9832 items in 46.32647252082825 seconds\n",
"\n"
]
}
],
"source": [
"# set some globals\n",
"monad=1\n",
"CollectedItems= 0\n",
"\n",
"# process books in order\n",
"for bo, bookinfo in bo2book.items():\n",
" CollectedItems=0\n",
" SentenceNumber=0\n",
" WordGroupNumber=0\n",
" full_df=pd.DataFrame({})\n",
" book_long=bookinfo[0]\n",
" booknum=bookinfo[1]\n",
" book_short=bookinfo[2]\n",
" InputFile = os.path.join(XmlDir, f'{bo}.xml')\n",
" OutputFile = os.path.join(PklDir, f'{bo}.pkl')\n",
" print(f'Processing {book_long} at {InputFile}')\n",
" DataFrameList = []\n",
"\n",
" # Send XML document to parsing process\n",
" tree = ET.parse(InputFile)\n",
" # Now add all the parent info to the nodes in the xtree [important!]\n",
" addParentInfo(tree.getroot())\n",
" start_time = time.time()\n",
" \n",
" # walk over all the XML data\n",
" for elem in tree.iter():\n",
" if elem.tag == 'sentence':\n",
" # add running number to 'sentence' tags\n",
" SentenceNumber+=1\n",
" elem.set('SN', SentenceNumber)\n",
" if elem.tag == 'wg':\n",
" # add running number to 'wg' tags\n",
" WordGroupNumber+=1\n",
" elem.set('WGN', WordGroupNumber)\n",
" if elem.tag == 'w':\n",
" # all nodes containing words are tagged with 'w'\n",
" \n",
" # show progress on screen\n",
" CollectedItems+=1\n",
" if (CollectedItems%100==0): print (\".\",end='')\n",
" \n",
" #Leafref will contain list with book, chapter verse and wordnumber\n",
" Leafref = re.sub(r'[!: ]',\" \", elem.attrib.get('ref')).split()\n",
" \n",
" #push value for monad to element tree \n",
" elem.set('monad', monad)\n",
" monad+=1\n",
" \n",
" # add some important computed data to the leaf\n",
" elem.set('LeafName', elem.tag)\n",
" elem.set('word', elem.text)\n",
" elem.set('book_long', book_long)\n",
" elem.set('booknum', int(booknum))\n",
" elem.set('book_short', book_short)\n",
" elem.set('chapter', int(Leafref[1]))\n",
" elem.set('verse', int(Leafref[2]))\n",
" \n",
" # folling code will trace down parents upto the tree and store found attributes\n",
" parentnode=getParent(elem)\n",
" index=0\n",
" while (parentnode):\n",
" index+=1\n",
" elem.set('Parent{}Name'.format(index), parentnode.tag)\n",
" elem.set('Parent{}Type'.format(index), parentnode.attrib.get('type'))\n",
" elem.set('Parent{}Appos'.format(index), parentnode.attrib.get('appositioncontainer'))\n",
" elem.set('Parent{}Class'.format(index), parentnode.attrib.get('class'))\n",
" elem.set('Parent{}Rule'.format(index), parentnode.attrib.get('rule'))\n",
" elem.set('Parent{}Role'.format(index), parentnode.attrib.get('role'))\n",
" elem.set('Parent{}Cltype'.format(index), parentnode.attrib.get('cltype'))\n",
" elem.set('Parent{}Unit'.format(index), parentnode.attrib.get('unit'))\n",
" elem.set('Parent{}Junction'.format(index), parentnode.attrib.get('junction'))\n",
" elem.set('Parent{}SN'.format(index), parentnode.attrib.get('SN'))\n",
" elem.set('Parent{}WGN'.format(index), parentnode.attrib.get('WGN'))\n",
" currentnode=parentnode\n",
" parentnode=getParent(currentnode) \n",
" elem.set('parents', int(index))\n",
" \n",
" #this will add all elements found in the tree to a list of dataframes\n",
" DataFrameChunk=pd.DataFrame(elem.attrib, index={monad})\n",
" DataFrameList.append(DataFrameChunk)\n",
" \n",
" #store the resulting DataFrame per book into a pickle file for further processing\n",
" full_df = pd.concat([df for df in DataFrameList])\n",
"\n",
" output = open(r\"{}\".format(OutputFile), 'wb')\n",
" pickle.dump(full_df, output)\n",
" output.close()\n",
" print(\"\\nFound \",CollectedItems, \" items in %s seconds\\n\" % (time.time() - start_time)) \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"## 3. Nestle1904 Text-Fabric production from pickle input \n",
"##### [Back to TOC](#TOC)\n",
"\n",
"This script creates the Text-Fabric files by recursive calling the TF walker function.\n",
"API info: https://annotation.github.io/text-fabric/tf/convert/walker.html\n",
"\n",
"The pickle files created by step 1 are stored on Github location [resources/pickle](pickle/README.md)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 1: Load libraries and initialize some data\n",
"\n",
"IMPORTANT: To ensure 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. In this Jupyter Notebook, Windows is the operating system employed.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-28T03:01:34.810259Z",
"start_time": "2022-10-28T03:01:25.745112Z"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import os\n",
"import re\n",
"import gc\n",
"from tf.fabric import Fabric\n",
"from tf.convert.walker import CV\n",
"from tf.parameters import VERSION\n",
"from datetime import date\n",
"import pickle\n",
"\n",
"BaseDir = 'C:\\\\Users\\\\tonyj\\\\my_new_Jupyter_folder\\\\Read_from_lowfat\\\\data\\\\'\n",
"XmlDir = BaseDir+'xml\\\\'\n",
"PklDir = BaseDir+'pkl\\\\'\n",
"XlsxDir = BaseDir+'xlsx\\\\'\n",
"\n",
"# key: filename, [0]=book_long, [1]=book_num, [3]=book_short\n",
"bo2book = {'01-matthew': ['Matthew', '1', 'Matt'],\n",
" '02-mark': ['Mark', '2', 'Mark'],\n",
" '03-luke': ['Luke', '3', 'Luke'],\n",
" '04-john': ['John', '4', 'John'],\n",
" '05-acts': ['Acts', '5', 'Acts'],\n",
" '06-romans': ['Romans', '6', 'Rom'],\n",
" '07-1corinthians': ['I_Corinthians', '7', '1Cor'],\n",
" '08-2corinthians': ['II_Corinthians', '8', '2Cor'],\n",
" '09-galatians': ['Galatians', '9', 'Gal'],\n",
" '10-ephesians': ['Ephesians', '10', 'Eph'],\n",
" '11-philippians': ['Philippians', '11', 'Phil'],\n",
" '12-colossians': ['Colossians', '12', 'Col'],\n",
" '13-1thessalonians':['I_Thessalonians', '13', '1Thess'],\n",
" '14-2thessalonians':['II_Thessalonians','14', '2Thess'],\n",
" '15-1timothy': ['I_Timothy', '15', '1Tim'],\n",
" '16-2timothy': ['II_Timothy', '16', '2Tim'],\n",
" '17-titus': ['Titus', '17', 'Titus'],\n",
" '18-philemon': ['Philemon', '18', 'Phlm'],\n",
" '19-hebrews': ['Hebrews', '19', 'Heb'],\n",
" '20-james': ['James', '20', 'Jas'],\n",
" '21-1peter': ['I_Peter', '21', '1Pet'],\n",
" '22-2peter': ['II_Peter', '22', '2Pet'],\n",
" '23-1john': ['I_John', '23', '1John'],\n",
" '24-2john': ['II_John', '24', '2John'],\n",
" '25-3john': ['III_John', '25', '3John'], \n",
" '26-jude': ['Jude', '26', 'Jude'],\n",
" '27-revelation': ['Revelation', '27', 'Rev']}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optional: export to Excel for investigation"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\01-matthew.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\02-mark.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\03-luke.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\04-john.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\05-acts.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\06-romans.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\07-1corinthians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\08-2corinthians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\09-galatians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\10-ephesians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\11-philippians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\12-colossians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\13-1thessalonians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\14-2thessalonians.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\15-1timothy.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\16-2timothy.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\17-titus.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\18-philemon.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\19-hebrews.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\20-james.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\21-1peter.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\22-2peter.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\23-1john.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\24-2john.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\25-3john.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\26-jude.pkl...\n",
"\tloading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\27-revelation.pkl...\n"
]
}
],
"source": [
"# test: sorting the data\n",
"import openpyxl\n",
"import pickle\n",
"\n",
"#if True:\n",
"for bo in bo2book:\n",
" '''\n",
" load all data into a dataframe\n",
" process books in order (bookinfo is a list!)\n",
" ''' \n",
" InputFile = os.path.join(PklDir, f'{bo}.pkl')\n",
" \n",
" print(f'\\tloading {InputFile}...')\n",
" pkl_file = open(InputFile, 'rb')\n",
" df = pickle.load(pkl_file)\n",
" pkl_file.close()\n",
" df.to_excel(os.path.join(XlsxDir, f'{bo}.xlsx'), index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2 Running the TF walker function\n",
"\n",
"API info: https://annotation.github.io/text-fabric/tf/convert/walker.html\n",
"\n",
"The logic of interpreting the data is included in the director function."
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is Text-Fabric 11.4.10\n",
"53 features found and 0 ignored\n",
" 0.00s Importing data from walking through the source ...\n",
" | 0.00s Preparing metadata... \n",
" | SECTION TYPES: book, chapter, verse\n",
" | SECTION FEATURES: book, chapter, verse\n",
" | STRUCTURE TYPES: book, chapter, verse\n",
" | STRUCTURE FEATURES: book, chapter, verse\n",
" | TEXT FEATURES:\n",
" | | text-orig-full after, word\n",
" | 0.00s OK\n",
" | 0.00s Following director... \n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\01-matthew.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\02-mark.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\03-luke.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\04-john.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\05-acts.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\06-romans.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\07-1corinthians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\08-2corinthians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\09-galatians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\10-ephesians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\11-philippians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\12-colossians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\13-1thessalonians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\14-2thessalonians.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\15-1timothy.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\16-2timothy.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\17-titus.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\18-philemon.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\19-hebrews.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\20-james.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\21-1peter.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\22-2peter.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\23-1john.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\24-2john.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\25-3john.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\26-jude.pkl...\n",
"\tWe are loading C:\\Users\\tonyj\\my_new_Jupyter_folder\\Read_from_lowfat\\data\\pkl\\27-revelation.pkl...\n",
" | 50s \"edge\" actions: 0\n",
" | 50s \"feature\" actions: 268899\n",
" | 50s \"node\" actions: 131120\n",
" | 50s \"resume\" actions: 9858\n",
" | 50s \"slot\" actions: 137779\n",
" | 50s \"terminate\" actions: 278886\n",
" | 27 x \"book\" node \n",
" | 260 x \"chapter\" node \n",
" | 8011 x \"sentence\" node \n",
" | 7943 x \"verse\" node \n",
" | 114879 x \"wg\" node \n",
" | 137779 x \"word\" node = slot type\n",
" | 268899 nodes of all types\n",
" | 50s OK\n",
" | 0.00s checking for nodes and edges ... \n",
" | 0.00s OK\n",
" | 0.00s checking (section) features ... \n",
" | 0.23s OK\n",
" | 0.00s reordering nodes ...\n",
" | 0.04s Sorting 27 nodes of type \"book\"\n",
" | 0.05s Sorting 260 nodes of type \"chapter\"\n",
" | 0.06s Sorting 8011 nodes of type \"sentence\"\n",
" | 0.09s Sorting 7943 nodes of type \"verse\"\n",
" | 0.11s Sorting 114879 nodes of type \"wg\"\n",
" | 0.25s Max node = 268899\n",
" | 0.25s OK\n",
" | 0.00s reassigning feature values ...\n",
" | | 0.00s node feature \"after\" with 137779 nodes\n",
" | | 0.05s node feature \"appos\" with 114879 nodes\n",
" | | 0.10s node feature \"book\" with 27 nodes\n",
" | | 0.10s node feature \"book_long\" with 137779 nodes\n",
" | | 0.15s node feature \"booknumber\" with 137806 nodes\n",
" | | 0.20s node feature \"bookshort\" with 137806 nodes\n",
" | | 0.25s node feature \"case\" with 137779 nodes\n",
" | | 0.31s node feature \"chapter\" with 153939 nodes\n",
" | | 0.36s node feature \"clausetype\" with 114879 nodes\n",
" | | 0.43s node feature \"containedclause\" with 137779 nodes\n",
" | | 0.48s node feature \"degree\" with 137779 nodes\n",
" | | 0.53s node feature \"gloss\" with 137779 nodes\n",
" | | 0.58s node feature \"gn\" with 137779 nodes\n",
" | | 0.63s node feature \"junction\" with 114879 nodes\n",
" | | 0.68s node feature \"lemma\" with 137779 nodes\n",
" | | 0.73s node feature \"lex_dom\" with 137779 nodes\n",
" | | 0.78s node feature \"ln\" with 137779 nodes\n",
" | | 0.83s node feature \"monad\" with 137779 nodes\n",
" | | 0.88s node feature \"mood\" with 137779 nodes\n",
" | | 0.93s node feature \"morph\" with 137779 nodes\n",
" | | 0.98s node feature \"nodeID\" with 137779 nodes\n",
" | | 1.03s node feature \"normalized\" with 137779 nodes\n",
" | | 1.08s node feature \"nu\" with 137779 nodes\n",
" | | 1.14s node feature \"number\" with 137779 nodes\n",
" | | 1.20s node feature \"orig_order\" with 137779 nodes\n",
" | | 1.25s node feature \"person\" with 137779 nodes\n",
" | | 1.30s node feature \"ref\" with 137779 nodes\n",
" | | 1.36s node feature \"reference\" with 137779 nodes\n",
" | | 1.42s node feature \"roleclausedistance\" with 137779 nodes\n",
" | | 1.47s node feature \"rule\" with 114879 nodes\n",
" | | 1.53s node feature \"sentence\" with 137806 nodes\n",
" | | 1.59s node feature \"sp\" with 137779 nodes\n",
" | | 1.65s node feature \"sp_full\" with 137779 nodes\n",
" | | 1.70s node feature \"strongs\" with 137779 nodes\n",
" | | 1.76s node feature \"subj_ref\" with 137779 nodes\n",
" | | 1.82s node feature \"tense\" with 137779 nodes\n",
" | | 1.87s node feature \"type\" with 137779 nodes\n",
" | | 1.96s node feature \"unicode\" with 137779 nodes\n",
" | | 2.02s node feature \"verse\" with 153706 nodes\n",
" | | 2.07s node feature \"voice\" with 137779 nodes\n",
" | | 2.12s node feature \"wgclass\" with 114879 nodes\n",
" | | 2.18s node feature \"wglevel\" with 114879 nodes\n",
" | | 2.24s node feature \"wgnum\" with 114879 nodes\n",
" | | 2.29s node feature \"wgrole\" with 114879 nodes\n",
" | | 2.35s node feature \"wgrolelong\" with 114879 nodes\n",
" | | 2.40s node feature \"wgtype\" with 114879 nodes\n",
" | | 2.46s node feature \"word\" with 137779 nodes\n",
" | | 2.52s node feature \"wordlevel\" with 137779 nodes\n",
" | | 2.57s node feature \"wordrole\" with 137779 nodes\n",
" | | 2.63s node feature \"wordrolelong\" with 137779 nodes\n",
" | 2.78s OK\n",
" 0.00s Exporting 51 node and 1 edge and 1 config features to ~/my_new_Jupyter_folder/Read_from_lowfat/data:\n",
" 0.00s VALIDATING oslots feature\n",
" 0.03s VALIDATING oslots feature\n",
" 0.03s maxSlot= 137779\n",
" 0.03s maxNode= 268899\n",
" 0.04s OK: oslots is valid\n",
" | 0.15s T after to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T appos to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.00s T book to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T book_long to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T booknumber to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T bookshort to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.16s T case to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.16s T chapter to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.12s T clausetype to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T containedclause to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T degree to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T gloss to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T gn to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.11s T junction to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.16s T lemma to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T lex_dom to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T ln to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T monad to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T mood to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T morph to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T nodeID to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.17s T normalized to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T nu to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T number to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T orig_order to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.06s T otype to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T person to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T ref to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T reference to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T roleclausedistance to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T rule to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T sentence to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T sp to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T sp_full to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T strongs to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T subj_ref to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T tense to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T type to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.17s T unicode to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.15s T verse to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T voice to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T wgclass to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.11s T wglevel to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.11s T wgnum to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T wgrole to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.11s T wgrolelong to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T wgtype to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.17s T word to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.13s T wordlevel to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T wordrole to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.14s T wordrolelong to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.40s T oslots to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" | 0.00s M otext to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
" 7.46s Exported 51 node features and 1 edge features and 1 config features to ~/my_new_Jupyter_folder/Read_from_lowfat/data\n",
"done\n"
]
}
],
"source": [
"TF = Fabric(locations=BaseDir, silent=False)\n",
"cv = CV(TF)\n",
"version = \"0.3\"\n",
"\n",
"###############################################\n",
"# Common helper functions #\n",
"###############################################\n",
"\n",
"#Function to prevent errors during conversion due to missing data\n",
"def sanitize(input):\n",
" if isinstance(input, float): return ''\n",
" if isinstance(input, type(None)): return ''\n",
" else: return (input)\n",
"\n",
"\n",
"# Function to expand the syntactic categories of words or wordgroup\n",
"# See also \"MACULA Greek Treebank for the Nestle 1904 Greek New Testament.pdf\" \n",
"# page 5&6 (section 2.4 Syntactic Categories at Clause Level)\n",
"def ExpandRole(input):\n",
" if input==\"adv\": return 'Adverbial'\n",
" if input==\"io\": return 'Indirect Object'\n",
" if input==\"o\": return 'Object'\n",
" if input==\"o2\": return 'Second Object'\n",
" if input==\"s\": return 'Subject'\n",
" if input==\"p\": return 'Predicate'\n",
" if input==\"v\": return 'Verbal'\n",
" if input==\"vc\": return 'Verbal Copula'\n",
" if input=='aux': return 'Auxiliar'\n",
" return ''\n",
"\n",
"# Function to expantion of Part of Speech labels. See also the description in \n",
"# \"MACULA Greek Treebank for the Nestle 1904 Greek New Testament.pdf\" page 6&7\n",
"# (2.2. Syntactic Categories at Word Level: Part of Speech Labels)\n",
"def ExpandSP(input):\n",
" if input=='adj': return 'Adjective'\n",
" if input=='conj': return 'Conjunction'\n",
" if input=='det': return 'Determiner' \n",
" if input=='intj': return 'Interjection' \n",
" if input=='noun': return 'Noun' \n",
" if input=='num': return 'Numeral' \n",
" if input=='prep': return 'Preposition' \n",
" if input=='ptcl': return 'Particle' \n",
" if input=='pron': return 'Pronoun' \n",
" if input=='verb': return 'Verb' \n",
" return ''\n",
"\n",
"###############################################\n",
"# The director routine #\n",
"###############################################\n",
"\n",
"def director(cv):\n",
" \n",
" ###############################################\n",
" # Innitial setup of data etc. #\n",
" ###############################################\n",
" NoneType = type(None) # needed as tool to validate certain data\n",
" IndexDict = {} # init an empty dictionary\n",
" WordGroupDict={} # init a dummy dictionary\n",
" PrevWordGroupSet = WordGroupSet = []\n",
" PrevWordGroupList = WordGroupList = []\n",
" RootWordGroup = 0\n",
" WordNumber=FoundWords=WordGroupTrack=0\n",
" # The following is required to recover succesfully from an abnormal condition\n",
" # in the LowFat tree data where a | Name | \n", "# of nodes | \n", "# slots/node | \n", "% coverage | \n", "
|---|---|---|---|
| book | \n", "27 | \n", "5102.93 | \n", "100 | \n", "
| chapter | \n", "260 | \n", "529.92 | \n", "100 | \n", "
| verse | \n", "7943 | \n", "17.35 | \n", "100 | \n", "
| sentence | \n", "8011 | \n", "17.20 | \n", "100 | \n", "
| wg | \n", "114879 | \n", "7.60 | \n", "633 | \n", "
| word | \n", "137779 | \n", "1.00 | \n", "100 | \n", "
verse 1"
],
"text/plain": [
"