def setNeedyData(self): #set the images and plain texts into arrays for pod in self.WolframAlphaQueryResult.Pods(): podNew = wap.Pod(pod) for subpod in podNew.Subpods(): subpodNew = wap.Subpod(subpod) self.plainTexts.append(subpodNew.Plaintext()[0]) self.images.append(dict(subpodNew.Img()[0])['src'])
def get_results(equation): queryStr = waeo.CreateQuery(equation) wap.WolframAlphaQuery(queryStr, WA_APP_ID) result = waeo.PerformQuery(queryStr) result = wap.WolframAlphaQueryResult(result) data = [] answer = [] for pod in result.Pods(): waPod = wap.Pod(pod) title = "Pod.title: " + waPod.Title()[0] print title for subpod in waPod.Subpods(): waSubpod = wap.Subpod(subpod) plaintext = waSubpod.Plaintext()[0] img = waSubpod.Img() src = wap.scanbranches(img[0], 'src')[0] alt = wap.scanbranches(img[0], 'alt')[0] print "-------------" print "img.src: " + src image = "img.src: " + src data.append(src) answer.append(plaintext) print "img.alt: " + alt print "\n" return data, answer
def _result_callback(self, msg, result, fail_silent): """ Deferred callback to handle result data. Called when page has sucessfully been downloaded. Negotiate (necessarily) nasty query result data and reply with nice string. """ try: if result[:7] == u"<error>": self.handle_error(msg, "Error in PerformQuery()") return waeqr = wap.WolframAlphaQueryResult(result) pods = [wap.Pod(p) for p in waeqr.Pods()] if len(pods) < 2: if not fail_silent: msg.reply(MESSAGES['no-result-error']) return pod = pods[1] # Usually the second pod has useful stuff in it. title = pod.Title()[0] results = [] # Probably only one, but may be more for subpod in [wap.Subpod(s) for s in pod.Subpods()]: results.append(subpod.Plaintext()[0]) # Unicode resultstring = u'\n'.join(results) # Pretty print, omitting "Result" if possible if title == u'Result': msg.reply(u"{0}".format(resultstring)) else: msg.reply(u"{0}: {1}".format(title, resultstring)) except Exception as e: self.handle_error(msg, "Exception occurred: " + str(e))
def call_api(self, val): '''call API from Wolfram Alpha and return output''' server = 'http://api.wolframalpha.com/v2/query.jsp' appid = '6LA36U-7V45PGUA6E' input = val waeo = wap.WolframAlphaEngine(appid, server) queryStr = waeo.CreateQuery(val) wap.WolframAlphaQuery(queryStr, appid) result = waeo.PerformQuery(queryStr) result = wap.WolframAlphaQueryResult(result) for pod in result.Pods(): waPod = wap.Pod(pod) title = "Pod.title: " + waPod.Title()[0] self.ls_pod.append(waPod.Title()[0]) for subpod in waPod.Subpods(): waSubpod = wap.Subpod(subpod) plaintext = waSubpod.Plaintext()[0] img = waSubpod.Img() src = wap.scanbranches(img[0], 'src')[0] alt = wap.scanbranches(img[0], 'alt')[0] self.ls_src.append(src) self.ls_alt.append(alt) self.ls_src = map(str, self.ls_src) break return self.ls_pod
def textDetailsAboutFormula( self, query): #get the formual text details from file fileName = fh.FileHandler().createFilePathForTheQuery(query) try: f = open(fileName, 'r') resArray = f.readlines() res = ''.join(resArray) f.close() textList = [] waeqr = wap.WolframAlphaQueryResult(res) #get from titles for pod in waeqr.Pods(): podObject = wap.Pod(pod) for subPod in podObject.Subpods(): subPodObject = wap.Subpod(subPod) if (subPodObject.Plaintext() != [[]]): title = subPodObject.Title() if (title[0] != ''): textList.append(title[0]) else: textList.append(podObject.Title()[0]) return textList except Exception: textList = ["No Text Found"] print("exeption thrown") return textList
def calcResult(self): query = self.waeo.CreateQuery(self.inpt) raw_result = self.waeo.PerformQuery(query) content = wap.WolframAlphaQueryResult(raw_result) result = None resultImg = None if content.IsSuccess(): dataType = content.DataTypes()[0] if dataType == 'Math': for pod in content.Pods(): waPod = wap.Pod(pod) print waPod.Title()[0] pTitle = str(waPod.Title()[0]) if pTitle == 'Result' or 'result' in pTitle: waSubpod = wap.Subpod(waPod.Subpods()[0]) result = waSubpod.Plaintext()[0] elif 'MathematicalFunctionIdentity' in dataType: for pod in content.Pods(): waPod = wap.Pod(pod) if str(waPod.Title()[0]) == 'Decimal approximation': waSubpod = wap.Subpod(waPod.Subpods()[0]) result = waSubpod.Plaintext()[0] else: for pod in content.Pods(): waPod = wap.Pod(pod) solvedResults = ['Differential equation solution', 'Limit'] waPodTitle = str(waPod.Title()[0]) if 'integral' in waPodTitle or waPodTitle in solvedResults: waSubpod = wap.Subpod(waPod.Subpods()[0]) result = waSubpod.Plaintext()[0] rightIndex = result.rindex('=') + 2 result = result[rightIndex:] img = waSubpod.Img() resultImg = wap.scanbranches(img[0], 'src')[0] break elif 'Result' in waPodTitle: waSubpod = wap.Subpod(waPod.Subpods()[0]) result = waSubpod.Plaintext()[0] img = waSubpod.Img() resultImg = wap.scanbranches(img[0], 'src')[0] else: continue return result, resultImg
def running(): """ Method for actually running the display """ global img, queries for e in pygame.event.get(): if e.type is QUIT or (e.type is KEYDOWN and e.key == K_ESCAPE): return False elif e.type is KEYDOWN and e.key == K_SPACE: server = 'http://api.wolframalpha.com/v2/query.jsp' appid = 'RV3P54-H477JEA2XE' pos_queries = [ 'When is the next full moon?', 'What time is it in India?', 'How long is the flight from New York to Atlanta?', 'How far is DC to Anchorage?', 'Where is Emory University?', 'What is the current local time in Portland, Oregon?', ] rand_query = random.choice(pos_queries) queries.add(text='Query: ' + rand_query) waeo = wap.WolframAlphaEngine(appid, server) queryStr = waeo.CreateQuery(rand_query) result = waeo.PerformQuery(queryStr) result = wap.WolframAlphaQueryResult(result) p = result.Pods() try: waPod = wap.Pod(p[1]) waSubpod = wap.Subpod(waPod.Subpods()[0]) plaintext = waSubpod.Plaintext()[0] imgg = waSubpod.Img() alt = wap.scanbranches(imgg[0], 'alt')[0] queries.add(text=str(alt)) except IndexError: queries.add(text='No results found') rand_block(rr(10, 100)) screen = pygame.display.get_surface() screen.fill((0, 0, 0)) for screen_object in SCREEN_OBJECTS: screen_object.update() # draw the image screen.blit(img, (WIDTH - BUFFER - 200, HEIGHT - BUFFER - 200)) pygame.display.flip() return True
def WolframPy(): #from urllib import request #url = "http://api.wolframalpha.com/v2/query?input=pi&appid=4WKYHL-AWUQL4GWA3" #urllib.getproxies() #fp = request.urlopen(url) #fp = urllib.urlopen(url) #data = fp.read() #f = open('workfile', 'w') #f.write(data) #n = int(data) #giveTheInt = [n] #print(giveTheInt) #print(data) result = getFromWolfram() #result = readFile() print(result) waeqr = wap.WolframAlphaQueryResult(result) #desktop = XSCRIPTCONTEXT.getDesktop() #model = desktop.getCurrentComponent() #if not hasattr(model, "Text"): # model = desktop.loadComponentFromURL("private:factory/swriter","_blank", 0, () ) #text = model.Text #cursor = text.createTextCursor() for pod in waeqr.Pods(): waep = wap.Pod(pod) xtit = waep.Title() for u in xtit: #text.insertString( cursor, u, 0 ) print(u) if (u == 'Plots of sample individual solutions'): plotSub = waep.Subpods() for pltpod in plotSub: spod = wap.Subpod(pltpod) imgData = spod.Img() a = dict(imgData.pop()) print(a['src']) imgurl = a['src'] f = open('00000001.gif', 'wb') f.write(urllib.urlopen(imgurl).read()) f.close() print("done") #text.insertString( cursor, "Hello World", 0 ) print(waep.Title())
def imageDetailsAboutFormula(self,query): fileName = os.sys.path[0]+"/equationFiles/"+query.lower()+".txt" try: f = open(fileName, 'r') resArray =f.readlines() res = ''.join(resArray) f.close() imageList=[] waeqr = wap.WolframAlphaQueryResult(res) count = 0 for pod in waeqr.Pods(): podObject = wap.Pod(pod) for subPod in podObject.Subpods(): subPodObject = wap.Subpod(subPod) if(subPodObject.Img()!=[[]]): image = subPodObject.Img() imageList.append("image"+`count`) count +=1 return imageList except Exception: imageList=["No Text Found"] print("exeption thrown") return imageList
def imageDetailsAboutFormula( self, query): #get the formula image details from file fileName = fh.FileHandler().createFilePathForTheQuery(query) try: f = open(fileName, 'r') resArray = f.readlines() res = ''.join(resArray) f.close() imageList = [] waeqr = wap.WolframAlphaQueryResult(res) #get from images count = 0 for pod in waeqr.Pods(): podObject = wap.Pod(pod) for subPod in podObject.Subpods(): subPodObject = wap.Subpod(subPod) if (subPodObject.Img() != [[]]): imageList.append("image" + ` count `) count += 1 return imageList except Exception: imageList = ["No Text Found"] print("exeption thrown") return imageList
def getResult(self, query): #method to get result if (self.isConfigured()): try: waeq = wap.WolframAlphaQuery(query, self.appid) waeq.Async = False waeq.ToURL() result = self.waeo.PerformQuery(waeq.Query) waeqr = wap.WolframAlphaQueryResult(result) for pod in waeqr.Pods(): waep = wap.Pod(pod) if (waep.PodStates()[0][1][2][1] == 'Step-by-step solution' ): self.stepinput = waep.PodStates()[0][1][1][1] break waeq = wap.WolframAlphaQuery(query, self.appid) waeq.AddPodState(self.stepinput) waeq.Async = False waeq.ToURL() result = self.waeo.PerformQuery(waeq.Query) f = open(query + '.txt', 'w') f.write(result) f.close() except Exception: print("connection problem")
def textDetailsAboutFormula(self,query): fileName = os.sys.path[0]+"/equationFiles/"+query.lower()+".txt" try: f = open(fileName, 'r') resArray =f.readlines() res = ''.join(resArray) f.close() textList=[] waeqr = wap.WolframAlphaQueryResult(res) for pod in waeqr.Pods(): podObject = wap.Pod(pod) for subPod in podObject.Subpods(): subPodObject = wap.Subpod(subPod) if(subPodObject.Plaintext()!=[[]]): title = subPodObject.Title() if(title[0]!=''): textList.append(title[0]) else: textList.append(podObject.Title()[0]) return textList except Exception: textList=["No Text Found"] print("exeption thrown") return textList
def getScanner(self): #get the scanner of the first pod firstPod = wap.Pod(self.WolframAlphaQueryResult.Pods()[0]) return firstPod.Scanner()[0]
#!/usr/bin/env python # -*- coding: utf-8 -*- import wap server = 'http://api.wolframalpha.com/v2/query.jsp' appid = 'XXXX' input = 'pi' waeo = wap.WolframAlphaEngine(appid, server) queryStr = waeo.CreateQuery(input) wap.WolframAlphaQuery(queryStr, appid) result = waeo.PerformQuery(queryStr) result = wap.WolframAlphaQueryResult(result) for pod in result.Pods(): waPod = wap.Pod(pod) title = "Pod.title: " + waPod.Title()[0] print title for subpod in waPod.Subpods(): waSubpod = wap.Subpod(subpod) plaintext = waSubpod.Plaintext()[0] img = waSubpod.Img() src = wap.scanbranches(img[0], 'src')[0] alt = wap.scanbranches(img[0], 'alt')[0] print "-------------" print "img.src: " + src print "img.alt: " + alt print "\n"
errormessage = waeqr.ErrorMessage() print '\n', type(errormessage), 'errormessage=', errormessage pods = waeqr.Pods() print '\n', type(pods), 'pods=', pods xmlpods = waeqr.XMLPods() print '\n', type(xmlpods), 'xmlpods=', xmlpods assumptions = waeqr.Assumptions() print '\n', type(assumptions), 'assumptions=', assumptions warnings = waeqr.Warnings() print '\n', type(warnings), 'warnings=', warnings sources = waeqr.Sources() print '\n', type(sources), 'sources=', sources for pod in pods: waep = wap.Pod(pod) iserror = waep.IsError() print '\n', type(iserror), 'pod iserror=', iserror numsubpods = waep.NumSubpods() print '\n', type(numsubpods), 'numsubpods=', numsubpods title = waep.Title() print '\n', type(title), 'title=', title scanner = waep.Scanner() print '\n', type(scanner), 'scanner=', scanner position = waep.Position() print '\n', type(position), 'position=', position asynchurl = waep.AsynchURL() print '\n', type(asynchurl), 'asynchurl=', asynchurl subpods = waep.Subpods() print '\n', type(subpods), 'subpods=', subpods
def __init__(self, pod): p = wap.Pod(pod) self.__title = p.Title()[0] self.__subpods = list(map(SubPod, p.Subpods())) self.__states = list(map(PodState, p.PodStates()))