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 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 __init__(self, subpod): sp = wap.Subpod(subpod) self.__title = sp.Title()[0] self.__plaintext = sp.Plaintext()[0] if sp.Plaintext( ) and not isinstance(sp.Plaintext()[0], list) else '' self.__img = Image(dict(sp.Img()[0])) self.__mathml = sp.MathML()[0] if sp.MathML() else ''
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 findAnswer(waPod): for subpod in waPod.Subpods(): waSubpod = wap.Subpod(subpod) plaintext = waSubpod.Plaintext()[0] if isSingleResult(plaintext) == 1: print plaintext + "\n" else: print "\n I am not sure. I can think of following things \n. " print plaintext + "\n"
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 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
#!/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"
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 for subpod in subpods: waesp = wap.Subpod(subpod) title = waesp.Title() print '\n', type(title), 'subpod title=', title plaintext = waesp.Plaintext() print '\n', type(plaintext), 'plaintext=', plaintext img = waesp.Img() print '\n', type(img), 'img=', img podstates = waep.PodStates() print '\n', type(podstates), 'podstates=', podstates infos = waep.Infos() print '\n', type(infos), 'infos=', infos