blob: c1425673aa4c45d22be8df755feca6ffd675cd1d [file] [log] [blame]
import sys
import xml.etree.ElementTree as ET
def main():
corpusRoot = ET.Element("teiCorpus")
for j in range(1, len(sys.argv)):
try:
doc_data = extract_data(sys.argv[j])
doc_tree = create_tree(doc_data, j-1)
currentRoot = doc_tree.getroot()
corpusRoot.append(currentRoot)
except:
print("Warning: could not parse file: " + sys.argv[j], file=sys.stderr)
continue
corpusTree = ET.ElementTree(corpusRoot)
ET.indent(corpusTree, " ")
corpusTree.write(sys.stdout, encoding='unicode')
def extract_data(file):
''' Parses an xml file and saves the metadata and texts into a dictionary that is returned.
The dictionary is of the following form:
data = {filenumber: {title}, {url}, {author}, {date}, {time}, {text}}
'''
tree = ET.parse(file)
root = tree.getroot()
ET.register_namespace("", "http://www.tei-c.org/ns/1.0")
data = {}
for i, text in enumerate(root.iter("{http://www.tei-c.org/ns/1.0}text")):
data[i] = {}
data[i]['title'] = text.get('title')
data[i]['url'] = text.get('url')
data[i]['author'] = text.get('author')
data[i]['date'] = text.get('date').split(' ')[0]
data[i]['time'] = text.get('date').split(' ')[1]
textelem = text.find(".{http://www.tei-c.org/ns/1.0}body/{http://www.tei-c.org/ns/1.0}div/"\
"{http://www.tei-c.org/ns/1.0}div")
data[i]['text'] = []
for p in textelem.findall(".{http://www.tei-c.org/ns/1.0}p"):
data[i]['text'].append(p)
return data
def create_tree(data, filenumber):
''' Receives a dictionary containing the data and returns an xml tree in the
desired format. Generates text sigles of the following format: BNC/filenumber.textnumber,
e.g. BNC/000.00000
'''
docRoot = ET.Element("teiDoc")
for i in range(len(data)):
tei = ET.SubElement(docRoot, "TEI")
teiHeader = ET.SubElement(tei, "teiHeader")
fileDesc = ET.SubElement(teiHeader, "fileDesc")
titleStmt = ET.SubElement(fileDesc, "titleStmt")
textSigle = ET.SubElement(titleStmt, "textSigle")
textSigle.text = "BNC/" + f"{filenumber:03}" + "." + f"{i:05}"
sourceDesc = ET.SubElement(fileDesc, "sourceDesc")
analytic = ET.SubElement(sourceDesc, "analytic")
htitle = ET.SubElement(analytic, "h.title")
htitle.text = data[i]['title']
hauthor = ET.SubElement(analytic, "h.author")
hauthor.text = data[i]['author']
imprint = ET.SubElement(sourceDesc, "imprint")
pubDateYear = ET.SubElement(imprint, "pubDate")
pubDateYear.set("type", "year")
pubDateYear.text = data[i]['date'][0:4]
pubDateMonth = ET.SubElement(imprint, "pubDate")
pubDateMonth.set("type", "month")
pubDateMonth.text = data[i]['date'][5:7]
pubDateDay = ET.SubElement(imprint, "pubDate")
pubDateDay.set("type", "day")
pubDateDay.text = data[i]['date'][8:10]
pubDateTime = ET.SubElement(imprint, "pubDate")
pubDateTime.set("type", "time")
pubDateTime.text = data[i]['time']
pubPlace = ET.SubElement(imprint, "pubPlace")
ref = ET.SubElement(pubPlace, "ref")
ref.set("type", "page_url")
ref.set("target", data[i]['url'])
text = ET.SubElement(tei, "text")
body = ET.SubElement(text, "body")
for p in data[i]['text']:
body.append(p)
docTree = ET.ElementTree(docRoot)
ET.indent(docTree, " ")
return docTree
main()