-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpennpipeline.py
executable file
·214 lines (177 loc) · 8.15 KB
/
pennpipeline.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
"""Functions for accessing the SUBTLE MURI NLP Pipeline.
Depends on the SUBTLE Pipeline and Java."""
import sys
import os
import re
from subprocess import Popen, PIPE, check_call
import shlex
## Global constants for configuration
# Current version, which must match the downloaded pipeline
CURRENT_VERSION = "1.1.2"
# Paths
PIPELINE_NAME = "SUBTLEPipeline-master"
CLASSPATH_SEP = ";" if sys.platform == "win32" else ":"
JAVA = "java"
TEST_COMMAND = "{} -version".format(JAVA)
ROOT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), PIPELINE_NAME)
# Check for up-to-date pipeline
if not os.path.exists(ROOT_DIR):
raise ImportError("The folder for the SUBTLE Pipeline %s does not exist. "
"Run download.py in the root of the SLURP repository to automatically "
"download and decompress the current version." % ROOT_DIR)
PIPELINE_VERSION = "unknown"
try:
PIPELINE_VERSION = open(os.path.join(ROOT_DIR, "VERSION")).read().strip()
if PIPELINE_VERSION != CURRENT_VERSION:
raise IOError
except IOError:
raise ImportError("The copy of the SUBTLE Pipeline in %s (version %s) is not the same version "
"as pennpipeline.py (version %s). You should:\n"
"1. Make sure you have the latest updates from the PennNLP/SLURP GitHub "
"repository.\n"
"2.Run download.py in the root of the SLURP repository "
"to update the SUBTLE Pipeline." % (ROOT_DIR, PIPELINE_VERSION, CURRENT_VERSION))
# Tagger
TAGGER_NAME = "stanford-postagger-streaminterface"
TAGGER_JAR = os.path.join(ROOT_DIR, TAGGER_NAME, TAGGER_NAME + ".jar")
TAGGER_MODEL = os.path.join(ROOT_DIR, TAGGER_NAME, "models", "english-caseless-left3words-distsim.tagger")
# Parser
PARSE_PROPS = os.path.join(ROOT_DIR, "models", "eatb3.properties")
PARSE_MODEL = os.path.join(ROOT_DIR, "models", "wsjall.obj")
PARSER_DIR = os.path.join(ROOT_DIR, "dbparser-0.9.9c-modified")
PARSER_CLASS = "danbikel.parser.Parser"
PARSER_SETTINGS = '-Dparser.settingsFile="%s"' % PARSE_PROPS
PARSER_CLASSPATH = (os.path.join(PARSER_DIR, "dbparser.jar") + CLASSPATH_SEP +
os.path.join(PARSER_DIR, "dbparser-ext.jar"))
# EC Restorer
ECRESTORER_DIR = os.path.join(ROOT_DIR, "restore-ecs")
ECRESTORER_JAR = os.path.join(ECRESTORER_DIR, "restore-ecs.jar")
# Pipeline commands
TAGGER = '%s -Xmx256m -jar "%s" "%s"' % (JAVA, TAGGER_JAR, TAGGER_MODEL)
PARSER = '%s -Xmx1024m -cp "%s" %s %s -is "%s" -sa - -out -' % \
(JAVA, PARSER_CLASSPATH, PARSER_SETTINGS, PARSER_CLASS, PARSE_MODEL)
ECRESTORER = ('%s -Xmx256m -jar "%s" '
"run - --perceptron --ante_perceptron") % (JAVA, ECRESTORER_JAR)
# Pipeline constants
OUTER_PARENS_RE = re.compile(r"\(\s*(.+)\s*\)")
class PennPipeline(object):
"""Provide access to a pipeline of NLP tools."""
def __init__(self):
# Set up the pipes
self.tag_proc, self.parse_proc, self.ecrestore_proc = (None, None, None)
self.tag_proc = _setup_pipe(TAGGER)
self.parse_proc = _setup_pipe(PARSER)
self.ecrestore_proc = _setup_pipe(ECRESTORER, ECRESTORER_DIR)
def __del__(self):
"""Terminate all pipelines."""
# This is possibly unnecessary, but doesn't do any harm.
for proc in (self.tag_proc, self.parse_proc, self.ecrestore_proc):
_terminate_with_extreme_prejudice(proc)
def parse_text(self, text, force_nouns=None, force_verbs=None, correct_punc=True,
verbose=False):
"""Run the text through the pipelines."""
# Check for empty text
if not text:
return ""
# Replace any newlines or carriage returns with space
text = text.replace('\n', ' ')
text = text.replace('\r', ' ')
# Create default set arguments
if not force_nouns:
force_nouns = set()
if not force_verbs:
force_verbs = set()
# Add in final punctuation if it's missing
if correct_punc and text[-1] not in ('.', '?', '!'):
text += '.'
# TODO: Deal with multiple sentences
try:
tagged_sent = _process_pipe_filter(text, self.tag_proc, read_until_empty=True)
except IOError:
raise IOError("Tagger process has died. Make sure that the command\n{}\ncan "
"be run successfully.".format(TAGGER))
if verbose:
# Show the tags without any forced tags
display_tagged_sent = _tag_convert(tagged_sent, set(), set())
print "Original tags:"
print display_tagged_sent
clean_tagged_sent = _tag_convert(tagged_sent, force_nouns, force_verbs)
if verbose and (force_nouns or force_verbs) and (clean_tagged_sent != display_tagged_sent):
print "Modified tags:"
print clean_tagged_sent
try:
parsed_sent = _process_pipe_filter(clean_tagged_sent, self.parse_proc, "(")
except IOError:
raise IOError("Parser process has died. Make sure that the command\n{}\ncan "
"be run successfully.".format(PARSER))
# Wrap input to the null restorer as ( x) exactly as wrap-stream.pl used to do
try:
restored_sent = _process_pipe_filter("( " + parsed_sent + ")", self.ecrestore_proc, "(")
except IOError:
raise IOError("ECRestorer process has died. Make sure that the command\n{}\ncan "
"be run successfully in the {} directory.".format(ECRESTORER, ECRESTORER_DIR))
# Remove extra parens from the parse with elements restored
final_parse = OUTER_PARENS_RE.match(restored_sent).group(1)
return final_parse
def _terminate_with_extreme_prejudice(proc):
"""Terminate a process without any regard for exceptions."""
try:
proc.terminate()
# pylint: disable=W0702
except:
pass
def _setup_pipe(command, cwd=None):
"""Set up a pipeline using the given command."""
# Windows expects a string for the command, so only lex other systems
if sys.platform != "win32":
command = shlex.split(command)
try:
return Popen(command, stdin=PIPE, stdout=PIPE, stderr=open(os.devnull, 'w'), cwd=cwd)
except OSError:
print >> sys.stderr, "Subprocess failed to run command: %s" % command
raise
def _process_pipe_filter(text, process, line_filter="", read_until_empty=False):
"""Run text through the pipe, returning the first output line starting with the filter."""
print >> process.stdin, text
process.stdin.flush()
if filter:
text = process.stdout.readline().strip()
while not text.startswith(line_filter):
text = process.stdout.readline().strip()
else:
text = process.stdout.readline().strip()
# Read additional lines until we get a blank one
if read_until_empty:
while True:
if not process.stdout.readline().strip():
break
return text
def _tag_convert(sent, force_nouns, force_verbs):
"""Convert from MXPOST to Bikel style tags, coercing tags."""
# MXPOST: word_tag
# We use rsplit with one to make sure underscores in the token aren't harmed.
token_tags = [token.rsplit('_', 1) for token in sent.rstrip().split()]
# Coerce the tags
token_tags = [(word, _coerce_tag(word, tag, force_nouns, force_verbs))
for word, tag in token_tags]
# Bikel: (word (tag)), with () around the line
return "(" + " ".join("(%s (%s))" % (word, tag) for word, tag in token_tags) + ")"
def _coerce_tag(word, tag, force_nouns, force_verbs):
"""Coerce a word's tag if it's in the sets force_nouns or force_verbs."""
tag = "NN" if word.lower() in force_nouns else tag
tag = "VB" if word.lower() in force_verbs else tag
return tag
if __name__ == "__main__":
print "Pipeline paths:"
print "Tagger:"
print TAGGER
print "Parser:"
print PARSER
print "EC Restorer:"
print ECRESTORER
print
# Try to run dependencies
print "Trying to run dependencies..."
print "Testing {!r}...".format(JAVA)
check_call(shlex.split(TEST_COMMAND) if sys.platform != "win32" else TEST_COMMAND)