xmlgat_to_EVT_parser.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # %%
  2. # Imports
  3. import xml.etree.ElementTree as ET
  4. import re
  5. import json
  6. # %%
  7. # This is to handle the xmnls attribute in the TEI element in the templates
  8. uri1 = "{http://www.tei-c.org/ns/1.0}"
  9. namespaces = {
  10. '': "http://www.tei-c.org/ns/1.0",
  11. }
  12. for prefix, uri in namespaces.items():
  13. ET.register_namespace(prefix, uri)
  14. # Reference directories
  15. basedir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/'
  16. baseindir = basedir + 'OVI/datiniXML/xmlgat/'
  17. baseoutdir = basedir + 'OVI/datiniXML/xmlevt/'
  18. # /Users/federicaspinelli/TEAMOVI/Parser/DATA/OVI/datiniXML
  19. # %%
  20. # Import lems list + xml info file
  21. # get lem list as a json object
  22. lemfile = basedir + 'OVI/datiniXML/power_lemmarioB.json'
  23. lems = json.load(open(lemfile, 'r'))
  24. # Get BiblioDatini.xml, extract a list of the <Biblio> nodes with ElementTree
  25. infofile = basedir + 'OVI/datiniXML/BiblioDatini.xml'
  26. infotree = ET.parse(infofile)
  27. inforoot = infotree.getroot()
  28. infoBiblioNodeList = list(inforoot.iter('Biblio'))
  29. # %%
  30. # FUNCTIONS TO PROCESS THE XMLGAT FILEs
  31. # Get a lem index
  32. def lemIndex(lem):
  33. for item in lems:
  34. if lem.attrib['n'] in item['coordinate']:
  35. return item['id']
  36. raise ValueError("code " + lem.attrib['n'] + " not found")
  37. # Get the ElementTree node ('Element' class object) associated to an OVI sigla in the BiblioDatini.xml file
  38. def getBiblioNodeBySigla(sigla):
  39. for node in infoBiblioNodeList:
  40. for child in node:
  41. if child.tag=='sigla' and child.text==sigla:
  42. return node
  43. # Get the ElemenTree object of the whole xmlgat file corresponding to a given OVI sigla
  44. def getLetterRootFromFile(filecode, inputdirectory=baseindir):
  45. fileName = inputdirectory + 'xmlgat.' + filecode + '.xml'
  46. try:
  47. letterRoot = ET.parse(fileName).getroot()
  48. except ET.ParseError:
  49. with open(fileName, encoding="ISO-8859-1") as fp:
  50. xml_string = fp.read()
  51. xml_string = xml_string.replace('&Csic&c', "<hi rend='italic'>sic</hi>")
  52. # return xml_string
  53. letterRoot = ET.fromstring(xml_string)
  54. return letterRoot
  55. ##################################
  56. # ELABORATING LEMS IN XMLGAT FILES
  57. ##################################
  58. # PREMISE:
  59. # in the xmlgat files, the <lem> tag doesn't surround lems:
  60. #
  61. # 1. Single-word lems are in the 'tail' of the corr. lem tags, as in:
  62. # <lem>A_LEM
  63. # with no closing </lem>
  64. #
  65. # 2. Multiple-word lems are inside <w> tags immediately following the <lem>, as in:
  66. # <lem><w>A MULTIWORD LEM</w>
  67. # The body of the text is inside a single <div>
  68. # This functions puts all lems inside a <lem> tag to make xmlgat files more standard xml-compliant and easier to process, also dropping the <w> tag.
  69. # Basically:
  70. #
  71. # <lem>A_LEM --> <lem>A_LEM</lem>
  72. # <lem><w>A MULTIWORD LEM</w> --> <lem>A MULTIWORD LEM</lem>
  73. def surroundLems(letterRoot):
  74. textRoot = list(letterRoot.iter('div'))[0]
  75. texttags = [node for node in textRoot if node.tag == 'lem' or node.tag == 'w']
  76. doit = False
  77. for node in texttags:
  78. if doit and node.tag=='w':
  79. node.tag = 'NEWlem'
  80. node.attrib = prev_node.attrib
  81. prev_node.tag = 'OLDlem'
  82. if node.tag == 'lem' and node.tail != None:
  83. thelem = re.findall(r'\w+', node.tail)[0] # First word
  84. node.text = thelem
  85. node.tail = node.tail.replace(thelem, '')
  86. doit = False
  87. else:
  88. doit = True
  89. prev_node = node
  90. for node in textRoot.findall('OLDlem'):
  91. textRoot.remove(node)
  92. for node in textRoot.findall('NEWlem'):
  93. node.tag = 'lem'
  94. return textRoot
  95. # This function tries to match a lem inside <lem> node (ElementTree Element object 'node'), by its attribute 'n',
  96. # the a lem in the lem list, the json object 'lems'
  97. def getLemByCode(lem):
  98. for item in lems:
  99. if lem.attrib['n'] in item['coordinate']:
  100. return item
  101. raise ValueError("code " + lem.attrib['n'] + " not found")
  102. # Dictionary assigning to each OVI lem type a tag useful for the final TEI output
  103. lemTypeDict = {'s.m.': "sostantivo maschile", 's.f.': 'sostantivo femminile', 'antr.': 'antroponimo', 'agg.': 'aggettivo', 'n.g.': 'nome di luogo', 'v.': 'verbo'}
  104. # This function processes each lem attributes and adds more tags around the lem useful for the final TEI output
  105. def redefineLems(textRoot, fileCode):
  106. for node in textRoot.iter('lem'):
  107. node.attrib['n'] = fileCode + '_' + node.attrib['n']
  108. thisLem = getLemByCode(node)
  109. lemRef = '#' + str(thisLem['id'])
  110. node.attrib.pop('n')
  111. lemType = thisLem['lemma']['categoria']
  112. lemStandard = thisLem['lemma']['forma_standard']
  113. lemNote = thisLem['lemma']['note']
  114. node.attrib['type'] = lemType
  115. if (lemStandard != ''):
  116. node.attrib['sameAs'] = lemStandard
  117. sub = ET.SubElement(node, 'rdg')
  118. sub.text = lemStandard
  119. sub.attrib['type'] = 'forma standard'
  120. if lemType=='antr.':
  121. node.tag = 'persName'
  122. node.attrib['ref'] = lemRef
  123. #node.attrib['type'] = lemTypeDict[lemType]
  124. elif lemType=='n.g.':
  125. node.tag = 'placeName'
  126. node.attrib['ref'] = lemRef
  127. #node.attrib['type'] = lemTypeDict[lemType]
  128. else:
  129. node.tag = 'lem'
  130. node.attrib['ref'] = lemRef
  131. if (lemNote != ''):
  132. sub = ET.SubElement(node, 'note')
  133. sub.text = lemNote
  134. #sub.text = node.text
  135. node.text = node.text
  136. #node.tag = 'note'
  137. #for node in textRoot.iter('lem'):
  138. # node.tag = 'lem'
  139. # node.attrib['ref'] = lemRef
  140. # node.attrib['type'] = lemTypeDict[lemType]
  141. def replacepbcode(textRoot, fileCode):
  142. for ii, node in enumerate(textRoot.iter('pb')):
  143. node.attrib['n'] = fileCode + ' ' + str(ii + 1)
  144. node.attrib['xml:id'] = fileCode + '_' + str(ii + 1)
  145. def surroundPages(textRoot):
  146. # Create a new, 'clean', root
  147. newRoot = ET.fromstring("<div/>")
  148. # Add a <p/> to the new root for each page in the old one
  149. for node in textRoot.iter('pb'):
  150. ET.SubElement(newRoot, 'p')
  151. # Fill the pages in the new root
  152. page = None
  153. elementInPage = None
  154. for child in textRoot:
  155. if child.tag=='pb' and page is None:
  156. page = 0
  157. elementInPage = 0
  158. elif child.tag=='pb':
  159. page = page+1
  160. elementInPage = 0
  161. if page is not None and elementInPage is not None and child.tag!='milestone':
  162. newRoot[page].append(child)
  163. newRoot[page][elementInPage].tail = child.tail
  164. elementInPage = elementInPage+1
  165. return newRoot
  166. # Get the letter template as a string
  167. def getTemplateString():
  168. preLetterTemplateTree = ET.parse('/Users/federicaspinelli/TEAMOVI/Parser/OVI/EVT/pre_letter_template.xml')
  169. where = list( list( preLetterTemplateTree.getroot().iter(uri1+'body') )[0].iter(uri1+'div') )[0]
  170. #
  171. ET.SubElement(where, 'letterBody')
  172. #
  173. letterString = ET.tostring(preLetterTemplateTree.getroot(), encoding='unicode', method='xml')
  174. return letterString
  175. # Get a file by OVI sigla 'filecode' as an ElementTree object, process its lem, transform it to string and format it
  176. def newProcessFile(filecode, inputdirectory=baseindir):
  177. tree1 = getLetterRootFromFile(filecode, inputdirectory)
  178. #ET.dump(tree1)
  179. #
  180. textRoot1 = surroundLems(tree1)
  181. #
  182. redefineLems(textRoot1, filecode)
  183. #
  184. replacepbcode(textRoot1, filecode)
  185. #
  186. textRoot2 = surroundPages(textRoot1)
  187. #
  188. indent1 = " "
  189. textString1 = ET.tostring(textRoot2, encoding='unicode', method='xml')
  190. textString2 = textString1.replace("<lb />\n", "<lb />\n"+indent1)
  191. return textString2
  192. # %%
  193. letterTemplateString = getTemplateString()
  194. # %%
  195. # Example
  196. filecodeExample = '99b'
  197. with open('/Users/federicaspinelli/TEAMOVI/evt-angular-ovi/src/assets/data/lettere/test.xml', 'w') as f1:
  198. newString = letterTemplateString.replace('<letterBody />', newProcessFile(filecodeExample))
  199. f1.write(newString)
  200. # %%
  201. # %%