-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.py
More file actions
90 lines (73 loc) · 3.1 KB
/
hook.py
File metadata and controls
90 lines (73 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import subprocess
import logging
import time
import mkdocs.plugins
log = logging.getLogger('mkdocs')
def on_startup(command, dirty):
start = time.monotonic()
log.info('Converting ontologies and TEI resources...')
# Configuration
tbox_names = ['arg', 'persp', 'contro', 'data']
abox_names = ['example', 'data']
langs = ['en', 'it']
text_lang = 'it'
ann_lang = 'en'
# Paths
## Ontology
docs_dir = os.path.join(os.getcwd(), 'docs')
elements_dir = os.path.join(docs_dir, 'elements')
def get_ont_path(name):
return os.path.join(docs_dir, 'ont', f'{name}.ttl')
ontexport_jar = os.path.join('tools', 'ontexport', 'app', 'build', 'libs', 'ontexport.jar')
doc_template = os.path.join('templates', 'doc-template.md')
## TEI text
tei_path = os.path.join('tei', 'Apologia-Ragione-TEI.xml')
tei_to_md_xslt = os.path.join('tei', 'stylesheets', 'tei-to-markdown', 'tei-to-markdown-custom.xsl')
## TEI schema
odd_path = os.path.join('tei', 'tei_arg.odd')
rnc_path = os.path.join('tei', 'tei_arg.rnc')
tei_stylesheets_dir = os.path.join('tei', 'stylesheets', 'official')
trang_jar = os.path.join('tools', 'trang', 'trang.jar')
jing_jar = os.path.join('tools', 'jing', 'jing.jar')
## Scripts
ont_doc_script = os.path.join('scripts', 'run-doc-template.py')
tei_to_md_script = os.path.join('scripts', 'tei-to-markdown.py')
tei_to_ttl_script = os.path.join('scripts', 'tei-to-turtle.py')
odd_to_html_script = os.path.join('scripts', 'odd-to-html.py')
odd_to_rnc_script = os.path.join('scripts', 'odd-to-rnc.py')
# Step 1: Preprocess TEI schema
subprocess.run(['python', odd_to_html_script, odd_path, tei_stylesheets_dir], check=True)
subprocess.run(['python', odd_to_rnc_script, odd_path, tei_stylesheets_dir, trang_jar], check=True)
# Step 2: Validate TEI against schema
validation = subprocess.run(
['java', '-jar', 'tools/jing/jing.jar', '-c', 'tei/tei_arg.rnc', 'tei/Apologia-Ragione-TEI.xml'],
stdout=subprocess.PIPE,
text=True
)
if validation.returncode:
log.error(f'TEI validation failed:{validation.stdout.split(':', 4)[4]}')
else:
log.info('TEI validation successful')
# Step 3: Convert TEI to Markdown
subprocess.run(['python', tei_to_md_script, tei_path, tei_to_md_xslt, '--lang', *langs], check=True)
# Step 4: Extract argument instances to TTL
subprocess.run(['python', tei_to_ttl_script, tei_path, get_ont_path('data'), '--text', text_lang, '--ann', ann_lang], check=True)
# Step 5: Serialize RDF and infer ABoxes
subprocess.run(
['java', '-jar', ontexport_jar, os.path.join(docs_dir, 'ont'), '--abox'] +
[f'{abox}.ttl' for abox in abox_names],
check=True
)
# Step 6: Generate documentation for each TBox
for tbox in tbox_names:
subprocess.run(
['python', ont_doc_script, get_ont_path(tbox), elements_dir, doc_template],
check=True
)
# Summary
duration = time.monotonic() - start
if abox_names:
log.info(f'Converted {len(tbox_names)} ontologies and reasoned over {len(abox_names)} in {duration:.2f} seconds')
else:
log.info(f'Converted {len(tbox_names)} ontologies in {duration:.2f} seconds')