def process_query(session, toklist, result): """ Check whether the parse tree is describes a query, and if so, execute the query, store the query answer in the result dictionary and return True """ q = Query(session) if not q.parse(toklist, result): if Settings.DEBUG: print("Unable to parse query, error {0}".format(q.error())) result["error"] = q.error() return False if not q.execute(): # This is a query, but its execution failed for some reason: return the error if Settings.DEBUG: print("Unable to execute query, error {0}".format(q.error())) result["error"] = q.error() return True # Successful query: return the answer in response result["response"] = q.answer() # ...and the query type, as a string ('Person', 'Entity', 'Title' etc.) result["qtype"] = qt = q.qtype() result["key"] = q.key() if qt == "Person": # For a person query, add an image (if available) img = get_image_url(q.key(), enclosing_session=session) if img is not None: result["image"] = dict( src=img.src, width=img.width, height=img.height, link=img.link, origin=img.origin, name=img.name, ) return True
def process_query(session, toklist, result): """ Check whether the parse tree is describes a query, and if so, execute the query, store the query answer in the result dictionary and return True """ q = Query(session) if not q.parse(toklist, result): # if Settings.DEBUG: # print("Unable to parse query, error {0}".format(q.error())) result["error"] = q.error() return False if not q.execute(): # This is a query, but its execution failed for some reason: return the error # if Settings.DEBUG: # print("Unable to execute query, error {0}".format(q.error())) result["error"] = q.error() return True # Successful query: return the answer in response result["response"] = q.answer() # ...and the query type, as a string ('Person', 'Entity', 'Title' etc.) result["qtype"] = qt = q.qtype() result["key"] = q.key() if qt == "Person": # For a person query, add an image (if available) img = get_image_url(q.key(), enclosing_session=session) if img is not None: result["image"] = dict( src=img.src, width=img.width, height=img.height, link=img.link, origin=img.origin, name=img.name, ) return True
def image(): """ Get image for (person) name """ resp = dict(found=False) name = request.args.get("name", "") try: thumb = int(request.args.get("thumb", 0)) except: thumb = 0 if name: img = get_image_url(name, thumb=thumb, cache_only=True) if img: resp["found"] = True resp["image"] = img return better_jsonify(**resp)
def execute(self): """ Check whether the parse tree is describes a query, and if so, execute the query, store the query answer in the result dictionary and return True """ if Query._parser is None: Query.init_class() # By default, the result object contains the 'raw' query # string (the one returned from the speech-to-text processor) # as well as the beautified version of that string - which # usually starts with an uppercase letter and has a trailing # question mark (or other ending punctuation). result = dict(q_raw=self.query, q=self.beautified_query) # First, try to handle this from plain text, without parsing: # shortcut to a successful, plain response if not self.execute_from_plain_text(): if not self.parse(result): # Unable to parse the query if Settings.DEBUG: print("Unable to parse query, error {0}".format( self.error())) result["error"] = self.error() result["valid"] = False return result if not self.execute_from_tree(): # This is a query, but its execution failed for some reason: # return the error # if Settings.DEBUG: # print("Unable to execute query, error {0}".format(q.error())) result["error"] = self.error() or "E_UNABLE_TO_EXECUTE_QUERY" result["valid"] = True return result # Successful query: return the answer in response if self._answer: result["answer"] = self._answer if self._voice and self._voice_answer: # This is a voice query and we have a voice answer to it result["voice"] = self._voice_answer if self._voice: # Optimize the response to voice queries: # we don't need detailed information about alternative # answers or their sources result["response"] = dict(answer=self._answer or "") else: # Return a detailed response if not a voice query result["response"] = self._response # Re-assign the beautified query string, in case the query processor modified it result["q"] = self.beautified_query # ...and the query type, as a string ('Person', 'Entity', 'Title' etc.) result["qtype"] = qt = self.qtype() # ...and the key used to retrieve the answer, if any result["key"] = self.key() # ...and a URL, if any has been set by the query processor if self.url: result["open_url"] = self.url # .. and the source, if set by query processor if self.source: result["source"] = self.source if not self._voice and qt == "Person": # For a person query, add an image (if available) img = get_image_url(self.key(), enclosing_session=self._session) if img is not None: result["image"] = dict( src=img.src, width=img.width, height=img.height, link=img.link, origin=img.origin, name=img.name, ) result["valid"] = True if Settings.DEBUG: # Dump query results to the console def converter(o): """ Ensure that datetime is output in ISO format to JSON """ if isinstance(o, datetime): return o.isoformat()[0:16] return None print("{0}".format( json.dumps(result, indent=3, ensure_ascii=False, default=converter))) return result