-
Notifications
You must be signed in to change notification settings - Fork 1
/
definitions-dump.py
57 lines (39 loc) · 1.38 KB
/
definitions-dump.py
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
import argparse
import json
import logging
import os
import sys
from umlsrat.api.metathesaurus import MetaThesaurus
from umlsrat.lookup import lookup_defs
from umlsrat.util.iterators import definitions_to_md
logger = logging.getLogger(os.path.basename(__file__))
def main():
parser = argparse.ArgumentParser()
parser = MetaThesaurus.add_args(parser)
parser = lookup_defs.add_args(parser)
parser.add_argument(
"--out-dir",
help="Write output to this directory.",
type=str,
default="definitions",
)
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
api = MetaThesaurus.from_namespace(args)
find_fn = lookup_defs.find_builder(api, args)
definitions = find_fn()
markdown = definitions_to_md(definitions)
logger.info("Definitions:\n\n" + markdown)
out_dir = args.out_dir
out_dir = os.path.join(out_dir, args.language)
if args.concept_id:
of_base = f"{args.source_vocab}-{args.concept_id}"
else:
of_base = f"{args.source_desc}"
os.makedirs(out_dir, exist_ok=True)
with open(os.path.join(out_dir, f"{of_base}.json"), "w", encoding="utf-8") as ofp:
json.dump(definitions, ofp, indent=2)
with open(os.path.join(out_dir, f"{of_base}.md"), "w", encoding="utf-8") as ofp:
print(markdown, file=ofp)
if __name__ == "__main__":
sys.exit(main())