def process(sentence): """ Process the sentence, and go to Google """ if is_math(sentence): return sentence.forward("math") sentence.replace_it() engines = { "google": "http://www.google.com/search?q={}", "bing": "http://www.bing.com/search?q={}", "yahoo": "http://search.yahoo.com/search?p={}", "duckduckgo": "https://duckduckgo.com/?q={}", "baidu": "http://www.baidu.com/s?wd={}", } engine = paul.get_search_engine().lower() keywords = sentence.keywords(ignore=[engine]) paul.log("KEYWORDS:", keywords) query = "+".join([word.replace(" ", "+") for word, _ in keywords if word not in VERBS + NOUNS]) url = engines[engine].format(query) paul.log("URL: " + url) paul.loading() paul.open_URL(url) return "Here, try this."
def scrape_first_paragraph(url): ''' Get the first paragraph of the specified (print-formatted) wikipedia article ''' gross_url = url.replace("/wiki/", "/w/index.php?title=")+"&printable=yes" paul.log("URL ATTEMPT: " + gross_url) d = paul.DOM.fromURL(gross_url) para = d["#mw-content-text"].get_immediate_child('p')[0].extract_raw_text() paul.set_it(url) para = stripIt(para) if para.endswith("may refer to:") or para.endswith("may refer to"): paul.open_URL(url) return "This is a diambiguation page. I'll bring it up now..." return para + "\n\n" + url
def findIt(what, sentence): ''' Get wikipedia's opening statement on the topic ''' url = ('http://en.wikipedia.org/wiki/{}'.format(re.sub(' ', '_', what))) try: result = scrape_first_paragraph(url) return result except urllib.error.HTTPError: if sentence.forward("discover"): return "I'll try something else, just a moment..." else: return "I found nothing!" except urllib.error.URLError: return "I couldn't complete the research for some reason. Are you connected to the internet?" except AttributeError: paul.set_it(url) paul.open_URL(url) return "I can't seem to read about this. Let me open it for you..."
def process(sentence): ''' Process the sentence ''' sentence.replace_it() keywords = sentence.keywords() paul.log("KEYWORDS: " + str(keywords)) ignores = ["i", "wikipedia"] filtered_keywords = [word for word in keywords if word[0] not in ignores] paul.loading() if filtered_keywords == []: return "I don't understand. Sorry!" if filtered_keywords[0][0] in ["joke", "jokes"]: if sentence.has_word("about"): pass else: return sentence.forward("personality") elif filtered_keywords[0][0].startswith("http"): paul.open_URL(filtered_keywords[0][0]) return findIt(filtered_keywords[0][0], sentence)
def process(sentence): ''' Process the sentence. ''' sentence.replace_it() eqns = make_equations(sentence) if len(eqns) == 0: return "I'm not sure what you want me try try and solve. Sorry!" subs = sentence.has_one_of(["sub", "substitute", "solve"]) rearrange = sentence.has_one_of(["rearrange", "solve"]) if subs: rearrange = False if (paul.get_it() and paul.Sentence(paul.get_it()).has_one_of("+/^*-=1234567890")): eqns.append(paul.get_it()) if len(eqns) < 2 else 0 paul.log(eqns) eqn_string = eqns[0] if subs: try: eqn_string = eqns[1] except IndexError: eqn_string = eqns[0] if rearrange: for e in eqns: if len(e) > 1: eqn_string = e break paul.log("SUBS:", subs, "REARRANGE:", rearrange) paul.log("EQUATION:", eqn_string) had_equals = True if not paul.has_word(eqn_string, "="): eqn_string = "y=" + eqn_string had_equals = False elif eqn_string.strip()[0] == "=": eqn_string = "y" + eqn_string paul.log("EQUATION:", eqn_string) # try: eqn = Equation(eqn_string) # except: # return "Something went horribly wrong when I tried to math. Oops." # if subs and len(eqns) > 1: eq2 = Equation(eqns[0]) if type(eq2.head.right.value) == float: eqn.substitute((eq2.head.left.value, eq2.head.right.value)) elif type(eqn.head.right.value) == float: eq2.substitute((eqn.head.left.value, eqn.head.right.value)) eqn, eq2 = eq2, eqn else: url = "http://www.wolframalpha.com/input/?i=" query = "sub {} into {}".format(eq2, eqn) query.replace("+", "%2D").replace("=", "%3D").replace("/", "%2F") paul.open_URL(url+query) return '''I was unsuccessful in finding the solution:\n{}\n{}'''.format(eq2, eqn) if rearrange: targ = "x" targets = sentence.keywords() for word, _ in targets: if len(word) == 1: targ = word try: eqn.rearrange_linear_for(targ) except RuntimeError: url = "http://www.wolframalpha.com/input/?i=" query = "rearrange {} for {}".format(eqn, targ) query.replace("+", "%2D").replace("=", "%3D").replace("/", "%2F") paul.open_URL(url+query) return "Oh dear, not a clue. Try this instead." result = str(eqn) if not had_equals: result = result[4:] paul.set_it(result) try: if result[0] == "(" and result[-1] == ")": result = result[1:-1] except IndexError: pass return result
def process(sentence): ''' Process the sentence ''' commands = { 'open': lambda location: get(location), 'get': lambda location: get(location), 'launch': lambda location: get(location), 'show': lambda location: reveal(location), 'find': lambda location: reveal(location), 'search': lambda location: reveal(location), 'reveal': lambda location: reveal(location), 'locate': lambda location: reveal(location), 'be': lambda location: reveal(location), # Catch in case, use safer reveal than get } types = { 'folder': "folder ", 'powerpoint': "presentation ", 'keynote': "presentation ", 'document': "word ", 'word': "word ", 'spreadsheet': "spreadsheet ", 'excel': "spreadsheet ", 'picture': "image ", 'image': "image ", 'movie': "movie ", 'film': "movie", 'video': "movie ", 'audio': "audio ", 'music': "music ", 'email': "email ", 'event': "event ", 'pdf': "pdf", 'preference': "preferences ", 'bookmark': "bookmark ", 'favourite': "bookmark ", 'font': "font ", 'widget': "widget ", "app": "application ", "application": "application ", "program": "application ", "executable": "application ", } apps = ["app", "application", "program", "executable"] replaced = sentence.replace_it() preps = sentence.get_part("PP", indexes=True) paul.log(preps) preps = paul.filter_out(preps, "for") paul.log(preps) try: object = sentence.get_part("NO")[0] except: object = None verb = sentence.get_part("VB")[0] if replaced: return commands[verb](replaced) ignore = list(types.keys()) + list(commands.keys()) keywords = sentence.keywords(ignore=["file"]) paul.log("KEYWORDS: " + str(keywords)) if keywords == []: return "I don't understand. Sorry." filters = generate_filters(keywords, preps) if object in types.keys(): filters.append("kind:{}".format(types[object])) if object in apps: where = "/Applications" params = keywords[0][0] + " kind:application" paul.log("FINDING APP") else: where = False params = " ".join(filters) paul.log("PARAMETERS: " + str(params)) search = filters[0] paul.log(search) if search.startswith("http") or search.startswith("www."): s = search paul.open_URL(s if s.startswith("http") else "http://"+s) return "Opening the website..." elif paul.has_word(keywords, "all"): return show_all(params) else: return commands[verb](find(params, where))