| import xml.etree.ElementTree as ET |
| import re |
| import os |
| from os import listdir |
| from os.path import isfile, join |
|
|
|
|
| INPUT_CHARSET="utf-8" |
|
|
| |
|
|
|
|
| def saveFile(OutputFolder,OutputHeader,name,content): |
| outputFooter='</div>\n</body>\n</text>\n</TEI>' |
| foutput=open(OutputFolder+"/"+name+".xml","w",encoding=INPUT_CHARSET) |
| foutput.write(OutputHeader.replace("\\n","\r")) |
| foutput.write(content.replace("\\n","\r")[2:-1]) |
| foutput.write(outputFooter) |
| foutput.close() |
|
|
| def saveFileWITHOUTHEADER(OutputFolder,name,content): |
| foutput=open(OutputFolder+"/"+name+".xml","w",encoding=INPUT_CHARSET) |
| |
| foutput.write(content.replace("\\n","\r")[2:-1]) |
| |
| foutput.close() |
|
|
| def listallFilesinFolder(mypath,extension): |
| extension="."+extension.lower() |
| onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) and f.lower().endswith(extension) ] |
| return onlyfiles |
|
|
| def ParseFile(OutputFolder,path): |
| ET.register_namespace('', "http://www.tei-c.org/ns/1.0") |
| tree = ET.parse(path) |
| root = tree.getroot() |
| header=root[0] |
| OutputHeader='<TEI xmlns="http://www.tei-c.org/ns/1.0">\n' |
| OutputHeader+=str(ET.tostring(header))[2:-1] |
| OutputHeader+='<text>\n<body>\n<div type="edition">\n' |
| outputFooter='</div>\n</body>\n</text>\n</TEI>' |
| index=0 |
| unknown=0 |
| mms=0 |
| addenda=0 |
| toc=0 |
| fragmenta=0 |
| dedication=0 |
| |
| for child in root[1][0][0]: |
| print(child.attrib) |
| if str(child.tag)=="{http://www.tei-c.org/ns/1.0}div" and 'subtype' in child.attrib: |
| att=child.attrib; |
| if att['subtype']=="preface": |
| saveFile(OutputFolder,OutputHeader,"Introduction",str(ET.tostring(child))) |
| elif att['subtype']=="work": |
| title="" |
| if 'n' in child.attrib: |
| title=att['n'] |
| else: |
| unknown+=1 |
| title="UNKNOWN"+str(unknown) |
| saveFile(OutputFolder,OutputHeader,title,str(ET.tostring(child))) |
| elif att['subtype']=="index": |
| index+=1 |
| saveFile(OutputFolder,OutputHeader,"INDEX"+str(index),str(ET.tostring(child))) |
| elif att['subtype']=="mss": |
| mms+=1 |
| saveFileWITHOUTHEADER(OutputFolder,"MMS"+str(mms),str(ET.tostring(child))) |
| elif att['subtype']=="addenda": |
| addenda+=1 |
| saveFile(OutputFolder,OutputHeader,"ADDENDA"+str(addenda),str(ET.tostring(child))) |
| elif att['subtype']=="toc": |
| toc+=1 |
| saveFile(OutputFolder,OutputHeader,"TOC"+str(toc),str(ET.tostring(child))) |
| elif att['subtype']=="fragmenta": |
| fragmenta+=1 |
| saveFile(OutputFolder,OutputHeader,"FRAGMENTA"+str(fragmenta),str(ET.tostring(child))) |
| elif att['subtype']=="dedication": |
| dedication+=1 |
| saveFileWITHOUTHEADER(OutputFolder,"Dedication"+str(dedication),str(ET.tostring(child))) |
| |
|
|
| |
|
|
| |
|
|
| |
|
|
| folderPath="/u/stoyanova/Desktop/csel-dev" |
|
|
| |
| files=listallFilesinFolder(folderPath,"xml") |
| for f in files: |
| print(f) |
| OutputFolder=folderPath+"/"+f+"_Output" |
| filepath=folderPath+"/"+f |
| if not os.path.exists(OutputFolder): |
| os.makedirs(OutputFolder) |
| ParseFile(OutputFolder,filepath) |
|
|
|
|
|
|