mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/ocr.git
				synced 2025-10-31 21:13:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			55 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/env python3.7
 | |
| # coding=utf-8
 | |
| 
 | |
| """"Merges hOCR files into a TEI file."""
 | |
| 
 | |
| from xml.sax.saxutils import escape
 | |
| from argparse import ArgumentParser
 | |
| import re
 | |
| import xml.etree.ElementTree as ET
 | |
| 
 | |
| parser = ArgumentParser(description='Merges hOCR files into a TEI file.')
 | |
| parser.add_argument('i', metavar='hOCR-sourcefile')
 | |
| parser.add_argument('o', metavar='TEI-destfile')
 | |
| args = parser.parse_args()
 | |
| 
 | |
| output_file = open(args.o, 'w')
 | |
| output_file.write(
 | |
|       '<?xml version="1.0" encoding="UTF-8"?>\n'
 | |
|       + '<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="dtabf">\n'
 | |
|       + '    <teiHeader>\n'
 | |
|       + '        <fileDesc>\n'
 | |
|       + '            <titleStmt/>\n'
 | |
|       + '            <publicationStmt/>\n'
 | |
|       + '            <sourceDesc/>\n'
 | |
|       + '        </fileDesc>\n'
 | |
|       + '        <encodingDesc/>\n'
 | |
|       + '        <profileDesc/>\n'
 | |
|       + '    </teiHeader>\n'
 | |
|       + '    <text>\n'
 | |
|       + '        <body>\n'
 | |
| )
 | |
| tree = ET.parse(args.i)
 | |
| for page in tree.findall('.//*[@class="ocr_page"]'):
 | |
|     page_properties = page.attrib.get('title')
 | |
|     facsimile = re.search(r'image \"(.*?)\"', page_properties).group(1)
 | |
|     page_number = re.search(r'ppageno (\d+)', page_properties).group(1)
 | |
|     output_file.write('            <pb facs="%s" n="%s"/>\n' % (facsimile, page_number))  # noqa
 | |
|     for para in page.findall('.//*[@class="ocr_par"]'):
 | |
|         output_file.write('            <p>\n')
 | |
|         for line in para.findall('.//*[@class="ocr_line"]'):
 | |
|             output_file.write('                <lb/>')
 | |
|             indent = ''
 | |
|             for word in line.findall('.//*[@class="ocrx_word"]'):
 | |
|                 if word.text is not None:
 | |
|                     output_file.write(indent + escape(word.text.strip()))
 | |
|                     indent = ' '
 | |
|             output_file.write('\n')
 | |
|         output_file.write('            </p>\n')
 | |
| output_file.write(
 | |
|       '        </body>\n'
 | |
|       + '    </text>\n'
 | |
|       + '</TEI>'
 | |
| )
 | |
| output_file.close()
 |