-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalyzer.py
executable file
·294 lines (231 loc) · 9.49 KB
/
analyzer.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
.. A wrapper for the Analyzer. It runs it as an XML-RPC server to isolate from
lengthy grammar-building times.
.. moduleauthor:: Luca Gilardi <lucag@icsi.berkeley.edu>
------
See LICENSE.txt for licensing information.
------
"""
import sys
from utils import Struct, update, display # @UnusedImport
from xmlrpclib import ServerProxy # @UnresolvedImport
from SimpleXMLRPCServer import SimpleXMLRPCServer # @UnresolvedImport
from utils import interpreter
from pprint import pprint
from xmlrpclib import Fault
import time
from threading import Thread
# Possibly change this for your system
dll = {'linux': '/jre/lib/amd64/server/libjvm.so',
'darwin': '/jre/lib/server/libjvm.dylib',
'win32': '/jre/bin/server/jvm.dll'}
try:
import jpype, os # @UnresolvedImport
jpype.startJVM(os.environ['JAVA_HOME'] + dll[sys.platform],
'-ea', '-Xmx5g', '-Djava.class.path=lib/compling.core.jar')
compling = jpype.JPackage('compling')
SlotChain = getattr(compling.grammar.unificationgrammar, 'UnificationGrammar$SlotChain')
getParses = compling.gui.util.Utils.getParses
ParserException = jpype.JException(compling.parser.ParserException) # @UnusedVariable
ECGAnalyzer = compling.parser.ecgparser.ECGAnalyzer
getDfs = compling.grammar.unificationgrammar.FeatureStructureUtilities.getDfs # @UnusedVariable
except ImportError:
from compling.grammar.unificationgrammar.UnificationGrammar import SlotChain
from compling.gui.util.Utils import getParses
from compling.parser import ParserException # @UnusedImport
from compling.parser.ecgparser import ECGAnalyzer
from compling.grammar.unificationgrammar.FeatureStructureUtilities import getDfs # @UnusedImport
from compling.gui import AnalyzerPrefs
from compling.grammar.ecg.Prefs import Property
from compling.gui.AnalyzerPrefs import AP
class Analyzer(object):
def __init__(self, prefs):
self.analyzer = ECGAnalyzer(prefs)
self.grammar = self.analyzer.grammar
self.server = None
def get_mappings(self):
mappings = self.analyzer.getMappings()
m = dict()
for entry in mappings.entrySet():
m[entry.key] = entry.value
return m
def get_lexicon(self):
lexes = self.analyzer.getLexicon()
return list(lexes)
def get_utterances(self):
utterances = self.analyzer.getUtterances()
return list(utterances)
def get_parses(self, sentence):
try:
return getParses(sentence, self.analyzer)
except ParserException, p:
print(p.message)
raise Fault(-1, p.message)
def test_analysis(self, analysis):
""" For testing how to output spans, etc. In development.
Mostly just a way to store info about methods. """
featureStruct = analysis.featureStructure
slots = featureStruct.slots.toArray() # Puts slots into array
first = slots[0] # Get first slot, e.g. ROOT
entries = first.features.entrySet().toArray() # Puts features into entry set
value = entries[0].value # Gets actual value, e.g. EventDescriptor[2]
def get_mapping(self):
v = AP.valueOf("MAPPING_PATH")
return self.analyzer.getPrefs().getSetting(v)
def parse(self, sentence):
def root(parse):
return parse.analyses[0].featureStructure.mainRoot
def as_sequence(parse):
def desc(slot):
return (slot_type(slot), slot_index(slot), slot_typesystem(slot), slot_value(slot))
slots = dict()
root_ = root(parse)
seq = [(parent, role) + desc(slots[s_id]) for parent, role, s_id in dfs('<ROOT>', root_, None, slots) if parent != -1]
return (-1, '<ROOT>') + desc(root_), seq
def convert_span(span, fs):
""" Return span, like (0, 4), name of cxn, and slot ID for cxn. """
name = span.getType().getName() if span.getType() else "None"
identity = fs.getSlot(span.slotID).slotIndex if fs.getSlot(span.slotID) else "None"
return {'span': (span.left, span.right), 'type': name, 'id': identity}
def get_spans(parses):
all_spans = []
for parse in parses:
parse_spans = []
analysis = parse.getAnalyses()[0]
spans = list(analysis.getSpans())
for span in spans:
parse_spans.append(convert_span(span, analysis.featureStructure))
all_spans.append(parse_spans)
return all_spans
parses = self.get_parses(sentence)
return {'parse': [as_sequence(p) for p in parses], 'costs': [p.cost for p in parses], 'spans': get_spans(parses)}
def getConstructionSize(self):
return len(self.analyzer.getGrammar().getAllConstructions())
def getSchemaSize(self):
return len(self.analyzer.getGrammar().getAllSchemas())
def reload(self, prefs):
""" Reloads grammar according to prefs file. """
self.analyzer = ECGAnalyzer(prefs)
self.grammar = self.analyzer.grammar
def issubtype(self, typesystem, child, parent):
"""Is <child> a child of <parent>?
"""
_ts = dict(CONSTRUCTION=self.grammar.cxnTypeSystem,
SCHEMA=self.grammar.schemaTypeSystem,
ONTOLOGY=self.grammar.ontologyTypeSystem)
ts = _ts[typesystem]
return ts.subtype(ts.getInternedString(child), ts.getInternedString(parent))
def close(self):
self.server.shutdown()
def __json__(self):
return self.__dict__
def slot_index(slot):
return slot.slotIndex
def slot_type(slot):
# if not slot.typeConstraint: print '##', slot
return slot.typeConstraint.type if slot and slot.typeConstraint else None
def slot_typesystem(slot):
return slot.typeConstraint.typeSystem.name if slot and slot.typeConstraint else None
def slot_value(slot):
return slot.atom[1:-1] if slot.atom else None
def slot(semspec, path, relative=None):
"""Returns the slot at the end of <path>, a slot
chain (a dot-separated list of role names)."""
if relative:
return semspec.getSlot(relative, SlotChain(path))
else:
return semspec.getSlot(SlotChain(path))
def test(args):
"""Just test the analyzer.
"""
prefs, sent = args
display('Creating analyzer with grammar %s ... ', prefs, term=' ')
analyzer = Analyzer(prefs)
display('done.')
for p in analyzer.parse(sent):
pprint(p)
def atom(slot):
"Does slot contain an atomic type?"
return slot.atom[1:-1] if slot.atom else ''
def dfs(name, slot, parent, seen):
slotIndex = slot.slotIndex
seen[slotIndex] = slot
if slot.features:
for e in slot.features.entrySet():
# <name, slot> pairs
n, s = unicode(e.key).replace('-', '_'), e.value
if s.slotIndex not in seen:
for x in dfs(n, s, slot, seen):
yield x
else:
yield slotIndex, n, s.slotIndex
yield parent.slotIndex if parent else -1, name, slot.slotIndex
def server(obj, host='localhost', port=8090):
server = SimpleXMLRPCServer((host, port), allow_none=True, logRequests=False, encoding='utf-8')
server.register_instance(obj)
#display('server ready (listening to http://%s:%d/).', host, port)
server.serve_forever()
return server # Added
def usage_time(start, end, analyzer):
print("Inversion time:")
print(end - start)
print("Num constructions: ")
print(analyzer.getConstructionSize())
print("Num schemas: ")
print(analyzer.getSchemaSize())
print("Total: ")
print(analyzer.getConstructionSize() + analyzer.getSchemaSize())
def main(args):
display(interpreter())
#display('Starting up Analyzer ... ', term='')
start = time.time()
analyzer = Analyzer(args[1])
end = time.time()
print("Analyzer ready...")
#usage_time(start, end, analyzer)
try:
#server_thread = Thread(target=server, kwargs={'obj': analyzer, 'host': host, 'port': port})
#serve = server_thread.start()
serve = server(analyzer)
analyzer.server = serve
except Exception, e:
print(e)
#print "Address " + host + ":" + str(port) + " is already in use. Using Analyzer on existing server. Kill that process to restart with a new Analyzer."
def main2(args):
display(interpreter())
#display('Starting up Analyzer ... ', term='')
start = time.time()
analyzer = Analyzer(args[1])
end = time.time()
print("Analyzer ready...")
return analyzer
def test_remote(sentence ='Robot1, move to location 1 2!'):
from feature import as_featurestruct
a = ServerProxy('http://localhost:8090')
d = a.parse(sentence)
s = as_featurestruct(d[0])
return s
# TODO: update this
def test_local(sentence='Robot1, move to location 1 2!'):
from feature import as_featurestruct
display('Starting up Analyzer ... ', term='')
a = Analyzer('grammar/robots.prefs')
display('done.\n', 'analyzing', sentence)
d = a.parse(sentence)
pprint(d)
# s = as_featurestruct(d[0])
# return s
return d
def usage():
display('Usage: analyzer.py <preference file>')
sys.exit(-1)
if __name__ == '__main__':
if '-t' in sys.argv:
test(sys.argv[2:])
elif '-l' in sys.argv:
test_local(*sys.argv[2:3])
else:
if len(sys.argv) != 2:
usage()
main(sys.argv)
#analyzer = main2(sys.argv)