-
Notifications
You must be signed in to change notification settings - Fork 0
/
metamap_fetch.py
42 lines (32 loc) · 1.14 KB
/
metamap_fetch.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
import subprocess
import tempfile
def metamap_fetch(sentences, mm_location):
if sentences is not None:
in_file = tempfile.NamedTemporaryFile(mode="wb", delete=False)
for sentence in sentences:
in_file.write(b'%r\n' % sentence)
else:
print("No input defined.")
in_file.flush()
out_file = tempfile.NamedTemporaryFile(mode="r", delete=False)
command = [mm_location]
command.append("-f") #number the mappings
command.append("-s") #short semantic types
command.append("--negex") #negex
command.append("--silent") #hide header information
command.append("-G") #show sources
command.append("-c") #show candidates
'''
add other options here in the format as below
command.append("")
'''
command.append(in_file.name)
command.append(out_file.name)
run_metamap = subprocess.Popen(command, stdout=subprocess.PIPE)
while run_metamap.poll() is None:
stdout = str(run_metamap.stdout.readline())
if 'ERROR' in stdout:
run_metamap.terminate()
error = stdout.rstrip()
output = str(out_file.read())
return output