[6c353a]: / medacy / tools / get_metamap.py

Download this file

53 lines (37 with data), 1.7 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
import json
import os
_this_dir = os.path.dirname(__file__)
_config_path = os.path.join(_this_dir, "../../config.json")
def _validate_path(path):
"""Raises an error if the provided path is not valid"""
if not os.path.isfile(path):
raise FileNotFoundError("The MetaMap path provided is not a file")
if not path.endswith("metamap"):
raise ValueError("The name of this file is not 'metamap', therefore it cannot be the MetaMap binary")
def get_metamap():
"""
Gets the path to MetaMap from the config json and returns it, or prompts
the user to specify the MetaMap path if it has not been set for this installation
"""
with open(_config_path, 'rb') as f:
config_data = json.load(f)
mm_path = config_data['metamap_path']
# 0 is the default value, indicating that the path has never been specified
if mm_path == 0:
print("The path to MetaMap has not been specified for this installation")
new_path = input("Please specify path to the MetaMap binary: ")
_validate_path(new_path)
config_data['metamap_path'] = new_path
with open(_config_path, 'w') as f:
json.dump(config_data, f)
mm_path = new_path
elif not os.path.isfile(mm_path):
new_path = input("MetaMap was not found at the given location; please specify a new path: ")
_validate_path(new_path)
mm_path = new_path
return mm_path
def get_metamap_path():
"""Returns the path to the MetaMap binary, or 0 if it has not been set."""
with open(_config_path, 'rb') as f:
config_data = json.load(f)
return config_data['metamap_path']