metamaptaggerservice.py
4.49 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
'''
An example of a tagging service using metamap.
'''
from argparse import ArgumentParser
from os.path import join as path_join
from os.path import dirname
try:
from json import dumps
except ImportError:
# likely old Python; try to fall back on ujson in brat distrib
from sys import path as sys_path
sys_path.append(path_join(dirname(__file__), '../server/lib/ujson'))
from ujson import dumps
from subprocess import PIPE, Popen
from random import choice, randint
from sys import stderr
from urlparse import urlparse
try:
from urlparse import parse_qs
except ImportError:
# old Python again?
from cgi import parse_qs
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import re
# use the brat sentence splitter
from sentencesplit import sentencebreaks_to_newlines
# use this MetaMap output converter
from MetaMaptoStandoff import MetaMap_lines_to_standoff
### Constants
METAMAP_SCRIPT = path_join(dirname(__file__), './metamap_tag.sh')
METAMAP_COMMAND = [METAMAP_SCRIPT]
ARGPARSER = ArgumentParser(description='An example HTTP tagging service using MetaMap')
ARGPARSER.add_argument('-p', '--port', type=int, default=47111,
help='port to run the HTTP service on (default: 47111)')
###
def run_tagger(cmd):
# runs the tagger identified by the given command.
try:
tagger_process = Popen(cmd, stdin=PIPE, stdout=PIPE, bufsize=1)
return tagger_process
except Exception, e:
print >> stderr, "Error running '%s':" % cmd, e
raise
def _apply_tagger_to_sentence(text):
# can afford to restart this on each invocation
tagger_process = run_tagger(METAMAP_COMMAND)
print >> tagger_process.stdin, text
tagger_process.stdin.close()
tagger_process.wait()
response_lines = []
for l in tagger_process.stdout:
l = l.rstrip('\n')
response_lines.append(l)
try:
tagged_entities = MetaMap_lines_to_standoff(response_lines, text)
except:
# if anything goes wrong, bail out
print >> stderr, "Warning: MetaMap-to-standoff conversion failed for output:\n'%s'" % '\n'.join(response_lines)
raise
#return {}
# MetaMap won't echo matched text, so get this separately
for t in tagged_entities:
t.eText = text[t.startOff:t.endOff]
return tagged_entities
def _apply_tagger(text):
# MetaMap isn't too happy with large outputs, so process a
# sentence per invocation
try:
splittext = sentencebreaks_to_newlines(text)
except:
# if anything goes wrong, just go with the
# original text instead
print >> stderr, "Warning: sentence splitting failed for input:\n'%s'" % text
splittext = text
sentences = splittext.split('\n')
all_tagged = []
baseoffset = 0
for s in sentences:
tagged = _apply_tagger_to_sentence(s)
# adjust offsets
for t in tagged:
t.startOff += baseoffset
t.endOff += baseoffset
all_tagged.extend(tagged)
baseoffset += len(s)+1
anns = {}
idseq = 1
for t in all_tagged:
anns["T%d" % idseq] = {
'type': t.eType,
'offsets': ((t.startOff, t.endOff), ),
'texts': (t.eText, ),
}
idseq += 1
return anns
class MetaMapTaggerHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Get our query
query = parse_qs(urlparse(self.path).query)
try:
json_dic = _apply_tagger(query['text'][0])
except KeyError:
# We weren't given any text to tag, such is life, return nothing
json_dic = {}
# Write the response
self.send_response(200)
self.send_header('Content-type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(dumps(json_dic))
print >> stderr, ('Generated %d annotations' % len(json_dic))
def log_message(self, format, *args):
return # Too much noise from the default implementation
def main(args):
argp = ARGPARSER.parse_args(args[1:])
print >> stderr, 'Starting MetaMap ...'
server_class = HTTPServer
httpd = server_class(('localhost', argp.port), MetaMapTaggerHandler)
print >> stderr, 'MetaMap tagger service started'
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print >> stderr, 'MetaMap tagger service stopped'
if __name__ == '__main__':
from sys import argv
exit(main(argv))