-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
339 lines (298 loc) · 7.81 KB
/
Copy pathparser.py
File metadata and controls
339 lines (298 loc) · 7.81 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#==============
#Imports
#=============
from xml.dom import minidom
import pyttsx
import os
import subprocess
#==============
#End of Imports
#==============
#==============
# Global Vars
#==============
arr = []
userChoices = []
dialog = []
filled= []
finalans = {}
scripte = []
fileName = "audio.xml"
#===================
# End of Global Vars
#===================
#===============================================
#Giving to the computer a voice to speak with :D
#================================
def onStart(name):
print 'Im speaking please wait'
def onWord(name, location, length):
print ''
def onEnd(name, completed):
print 'Im done please interact with me'
def say(sentence):
engine = pyttsx.init()
engine.connect('started-utterance', onStart)
#engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say(sentence)
engine.runAndWait()
#=========================
#End of computer Speaking
#========================
#======================
#Exploring Children
#=====================
def parseItem(nodes):
retVal = "";
for node in nodes:
retVal += node.firstChild.data.replace(" ","")+ "/"
return retVal
def parseItemArray(nodes):
retVal = []
x = nodes.firstChild
print x.tagName
retVal.append(x)
# while x.nextSibling:
# retVal.append(x.nextSibling)
return retVal
#=========================
#End of Exploring Children
#========================
#==================================
#Printing list of available choices
#==================================
def printChoices(input):
choices = "Available Choices : "
fixedInput = input.split("/")
for i in fixedInput:
choices = choices + " " + i
return choices
#=======================================
#End Printing list of available choices
#=======================================
def parseFields():
xmldoc = minidom.parse(fileName)
itemlist = xmldoc.getElementsByTagName('field')
for s in itemlist :
fieldItem = []
isQuestion = ""
fieldName = s.attributes['name'].value
try:
fieldType = s.attributes['type'].value
except:
fieldType = ""
prompt = s.getElementsByTagName('prompt')
promptMessage = prompt
item = s.getElementsByTagName('item')
itemVals = parseItem(item)
noinput= s.getElementsByTagName('noinput')
noinputVals = parseItem(noinput)
nomatch = s.getElementsByTagName('nomatch')
nomatchVals = parseItem(nomatch)
fieldItem.append(fieldName)
fieldItem.append(promptMessage)
fieldItem.append(itemVals)
fieldItem.append(noinputVals)
fieldItem.append(nomatchVals)
fieldItem.append(fieldType)
#fieldItem.append(filled)
#print fieldItem
arr.append(fieldItem)
def getWelcomeMessage():
xmldoc = minidom.parse(fileName)
itemlist = xmldoc.getElementsByTagName('block')
for s in itemlist:
x = s.getElementsByTagName("audio")
if x[0]:
audiofile = x[0].attributes['src'].value
return_code = subprocess.call(["afplay", audiofile])
else:
dialog.append(s.firstChild.data)
def getfilled():
xmldoc = minidom.parse(fileName)
filledlist = xmldoc.getElementsByTagName('filled')
for itemlist in filledlist:
conditiondict = {}
filledName = itemlist.attributes['namelist'].value
conditiondict['filledName'] = filledName
ifcond = itemlist.getElementsByTagName('if')
prompt = itemlist.getElementsByTagName('prompt')
if ifcond:
conditiondict['ifcond'] = True
m = ifcond[0].childNodes
for x in range(1,len(m)):
#print "hiiii"
#print x
if m[x].nodeType == 3:
x = x + 1
continue
if (m[x].tagName == 'if' or m[x].tagName == 'elseif' and m[x].nodeType == 1):
#print m[x].nodeType
condcheck = m[x].attributes['cond'].value.replace(" ","").replace("'","").split("==")[1]
#print condcheck
x = x+1
elif (m[x].tagName == 'else'):
condcheck = 'else'
x = x+1
if m[x].nodeType == 3:
x = x + 1
continue
if (m[x].tagName == 'prompt' and m[x].nodeType == 1):
prompt = m[x].firstChild.data
if x == 1 :
condcheck = ifcond[0].attributes['cond'].value.replace(" ","").replace("'","").split("==")[1]
conditiondict[condcheck] = prompt
x = x+1
else:
conditiondict['ifcond'] = False
conditiondict['prompt'] = prompt
#print conditiondict
filled.append(conditiondict)
print filled
def getScript():
xmldoc = minidom.parse(fileName)
filledlist = xmldoc.getElementsByTagName('script')
x = ""
for f in filledlist:
for s in f.childNodes:
x = x + s.data
scripte.append(x)
print scripte
def evaluateScript(callingScript):
#calculatePrice(city,travellers)
#print scripte[0] + "helllooooll"
boundVariables = ""
variables = callingScript.split("(")[1].split(")")[0].split(",")
for v in variables:
#print v, ", ", finalans[v]
temp = ""
try:
int(finalans[v])
temp = finalans[v]
except:
temp = "'%s'" % finalans[v]
boundVariables = boundVariables + temp + ","
boundVariables += "3"
#print boundVariables
finalcall = callingScript.split("(")[0] + "(" + boundVariables + ")"
f = open("javapython.txt", "w")
f.write(scripte[0])
f.write("\n")
f.write(finalcall)
f.flush()
f.close()
os.system("javac JavaScriptExecutor.java")
value = os.system('java JavaScriptExecutor "javapython.txt" "javaoutput.txt"')
f = open("javaoutput.txt", "r")
value = f.read()
f.close()
print value , "heloo"
return str(value)
def retFilled(i):
for item in filled:
if arr[i][0] == item['filledName']:
if item['ifcond']:
print item[finalans[arr[i][0]]]
return item[finalans[arr[i][0]]]
else:
retText = ""
for x in item['prompt']:
for y in x.childNodes:
if y.nodeType == 3:
retText = retText + y.data
elif (y.nodeType == 1 and y.tagName == 'value'):
expr = y.attributes['expr'].value
if "(" in expr:
retText = retText + evaluateScript(expr)
else:
retText = retText + finalans[expr]
print retText
return retText
def generateQuestion(i):
fieldName = arr[i][0].split("_")[0]
fieldPrompt = arr[i][1]
print fieldPrompt
retText = ""
if (fieldName == "confirm"):
retText = fieldPrompt[0].firstChild.data + " " + userChoices[-1]
else:
for x in fieldPrompt[0].childNodes:
print x
if x.nodeType == 3:
retText = retText + x.data
elif (x.nodeType == 1 and x.tagName == 'value'):
expr = x.attributes['expr'].value
retText = retText + finalans[expr]
print retText +"retText"
return retText
def validateAnswer(userInput , i):
ret = []
retText = ""
noinput = arr[i][3]
nomatch = arr[i][4]
if (userInput == ""):
if noinput:
retText = noinput
else:
retText = "no input"
ret.append(retText)
ret.append(i)
return ret
if(arr[i][5]):
if(arr[i][5] == 'number'):
if int(userInput):
userChoices.append(userInput)
finalans[arr[i][0]] = userInput
i = i+1
ret.append(retText)
ret.append(i)
return ret
else:
if nomatch:
retText = nomatch
else:
retText = "please enter a valid number"
availableChoices = printChoices(arr[i][2]).split(" ")
if availableChoices:
if (userInput in availableChoices):
if (arr[i][0].split("_")[0] == "confirm" and userInput == "no"):
i = i-1
del userChoices[-1]
else:
userChoices.append(userInput)
finalans[arr[i][0]] = userInput
i = i+1
else:
if nomatch:
retText = nomatch
else:
retText = "please enter a valid choice"
else:
retText = "next promt"
ret.append(retText)
ret.append(i)
return ret
def main():
getScript()
getWelcomeMessage()
if dialog:
print dialog[0]
say(dialog[0])
parseFields()
getfilled()
i = 0
while (i < len(arr)):
say(generateQuestion(i))
choices = printChoices(arr[i][2])
print choices
userInput = raw_input().replace(" ","")
validated = validateAnswer(userInput , i)
if validated[0]:
say(validated[0])
say(retFilled(i))
i = validated[1]
#print userChoices
print finalans
if __name__ =='__main__':
main()