def load(self, text): '''Load data into spacy nlp''' log.info("Loading text {0} into spacy.".format(str(text))) text = unicode(text, "utf-8") parsed_data = parser(text) #Return the parsed data return parsed_data
def event(events, **kwargs): log.info("In event, events are {0}".format(str(events))) log.info("In event, kwargs are {0}".format(str(kwargs))) def subscribe(evt, func): dispatcher.connect(func, signal=evt) if event not in _event_handlers: _event_handlers[evt] = [] _event_handlers[evt].append(func) def decorator(func): log.info(func) # Append the plugin data to the nlp parsing que log.info("Appending {0} to nlp.current_plugins".format(str(events))) nlp.current_plugins.append(events) if not isinstance(events, str) and isinstance(events, Iterable) and not isinstance(events, dict): for evt in events: log.info("subscribing evt {0}".format(evt)) subscribe(evt, func) elif isinstance(events, dict): log.info(events) evt_keywords = events["key_words"] if evt_keywords: for key_word in evt_keywords: log.info("Subscribing {0} to {1}".format(key_word, str(func))) subscribe(key_word, func) else: log.info("Error: event {0} has no attribute name".format(str(events))) else: subscribe(events, func) return func return decorator
def get_ents(parsed_data): '''Get the entity data''' log.info("Getting entity data") ents = self.entity_recognition(parsed_data) current_data['ents'] = ents done['ents'] = True log.info("Finished getting entity data")
def get_shows(ents): works_of_art = [] for e_name, e_type in ents: if e_type == "WORK_OF_ART": log.info("Appending {0} to works of art".format(e_name)) works_of_art.append(e_name) return works_of_art
def get_qcheck(sentence): '''Get the question data''' log.info("Checking if the command is a question") q_check = self.question_check(sentence) log.info("Question check returned {0}".format(str(q_check))) current_data["questions"] = q_check done["questions"] = True
def cast(chromecast): #cast = pychromecast.get_chromecast(friendly_name=chromecast) #cast.wait() if leader == "netflix" or sentence.split(" ")[1] == "netflix" or "netflix" in args["ents"].keys().lower(): log.info("Sentence is {0}".format(sentence)) full_sentence = (leader, sentence) full_text = ' '.join(full_sentence) netflix(full_text, args)
def search_main(*args, **kwargs): word = args[0] query = args[1] if word not in ("google", "search"): query = ' '.join((word, query)) log.info("In main, query is:" + str(query)) log.info("Going into wolfram search") answer = wlfram_search(query, app_id()) return answer
def skwiki(titlequery): log.info("in skwiki") log.info(titlequery) try: return wikipedia.summary(titlequery, sentences=2). \ encode("ascii", "ignore") except wikipedia.exceptions.DisambiguationError as e: return wikipedia.summary(e.options[0], sentences=2). \ encode("ascii", "ignore")
def get_structure(parsed_data): '''Get the POS/Structure data''' #This will, at the moment, just append the tags to strucutre without any position data. #TODO: improve this log.info("Getting POS tags") #Get POS tags pos_tags = self.POS(parsed_data) current_data['structure']['tags'].update(pos_tags) done["structure"] = True log.info("Finished getting POS tags")
def question_check(self, sentence): '''Check if the sentence is a question''' q_words = [ "is", "are", "am", "was", "were", "will", "do", "does", "did", "have", "had", "has", "can", "could", "should", "shall", "may", "might", "would", "how", "who", "what", "where", "when", "wouldn't", "shouldn't", "couldn't", "hadn't", "didn't", "weren't", "don't", "doesn't", "haven't", "can't", "wasn't" ] log.info('Checking if the sentence is a question') first_word = sentence.split(" ")[0].lower() return first_word in q_words
def elimination(self): '''Thread to eliminate plugins that are incompatible''' #Check if the keywords match before anything else starts global plugins_left data_keys = current_data["key_words"] log.info("current_plugins is {0}".format(current_plugins)) log.info("current_data is {0}".format(current_data)) if data_keys: for plugin in current_plugins: found = False plug_keys = plugin["key_words"] if plug_keys: for keyword in data_keys: if keyword in plug_keys: found = True break if found: plugins_left.append(plugin) else: plugins_left = current_plugins #While the parsing part of the program isn't finished while not done["parsing"]: #Iterate over the plugins that haven't been eliminated for plugin in plugins_left: #Check if required entities have been obtained if done["ents"]: log.info("Ents is done, checking") ents_category = plugin['ents_needed'] if ents_category: for ent in ents_category: if ent not in current_data["ents"].values(): log.info("Removing plugin {0} because of ent {1}".format(str(plugin['name']),str(ent))) plugins_left.remove(plugins_left[plugin]) break #Check if required tags are had and in the correct order if done["structure"]: structure_category = plugin["structure"] if structure_category: struct_needed = structure_category['needed'] for tag in struct_needed.keys(): if tag not in current_data["structure"]["tags"].values(): plugins_left.remove(plugins_left[plugin]) break #If it needs a question check if there is one if done["questions"]: q_category = plugin['questions_needed'] if q_category: if not current_data['questions']: plugins_left.remove(plugins_left[plugin]) break left_len = len(plugins_left) if left_len == 1: done['elimination'] = True return plugins_left done['elimination'] = True return plugins_left
def parse_thread(self, sentence): '''Thread in which parsing functions are called''' log.info("In parsing thread") #Get spacey loaded data parsed_data = self.load(sentence) #Threaded functions for each part of the nlp def get_ents(parsed_data): '''Get the entity data''' log.info("Getting entity data") ents = self.entity_recognition(parsed_data) current_data['ents'] = ents done['ents'] = True log.info("Finished getting entity data") def get_structure(parsed_data): '''Get the POS/Structure data''' #This will, at the moment, just append the tags to strucutre without any position data. #TODO: improve this log.info("Getting POS tags") #Get POS tags pos_tags = self.POS(parsed_data) current_data['structure']['tags'].update(pos_tags) done["structure"] = True log.info("Finished getting POS tags") def get_qcheck(sentence): '''Get the question data''' log.info("Checking if the command is a question") q_check = self.question_check(sentence) log.info("Question check returned {0}".format(str(q_check))) current_data["questions"] = q_check done["questions"] = True #Start all the threads ent_thread = threading.Thread(target=get_ents(parsed_data)) struct_thread = threading.Thread(target=get_structure(parsed_data)) question_thread = threading.Thread(target=get_qcheck(sentence)) ent_thread.start() struct_thread.start() question_thread.start() log.info("Started the parsing threads") #Wait for everything to be done while not done["structure"] and not done["questions"] and not done[ "ents"]: time.sleep(0.001) done["parsing"] = True
def cast_main(leader, sentence, *args, **kwargs): log.info("In cast") def cast(chromecast): #cast = pychromecast.get_chromecast(friendly_name=chromecast) #cast.wait() if leader == "netflix" or sentence.split(" ")[1] == "netflix" or "netflix" in args["ents"].keys().lower(): log.info("Sentence is {0}".format(sentence)) full_sentence = (leader, sentence) full_text = ' '.join(full_sentence) netflix(full_text, args) known_chromecasts = config.load_config("chromecasts") log.info("Known chromecasts are {0}".format(str(known_chromecasts))) chromecasts_available = pychromecast.get_chromecasts_as_dict().keys() chromecast_name = None for chromecast in chromecasts_available: if isinstance(known_chromecasts, list): if chromecast in known_chromecasts: chromecast_name = chromecast elif isinstance(known_chromecasts, str): if chromecast == known_chromecasts: chromecast_name = chromecast else: return "Error: unrecognized chromecast conifg {0}".format(str(known_chromecasts)) if chromecast_name: cast(chromecast_name) else: chromecast_choice = easygui.buttonbox(title="Chromecasts Found", msg="Please choose a chromecast", choices=chromecasts_available) if chromecast_choice: if easygui.ynbox("Would you like to save this chromecast in your W.I.L.L config?"): config.add_config({"known_chromecasts":[chromecast_choice]}) cast(chromecast_choice) else: return "Error: No chromecast chosen" # cast.wait() # print(cast.device) # # print(cast.status) # # mc = cast.media_controller # mc.play_media('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', 'video/mp4') # print(mc.status) # # mc.pause() # time.sleep(5) # mc.play()
def run(): '''Open logs, check log settings, and start the flask server and slack thread''' log.info(''' \ / | | | \ / | | | \ / | | | \ /\ / | | | \ / \ / | | | \/ \/ | ------------ ------------ ''') if log.getEffectiveLevel() == logging.DEBUG: debugval = True else: debugval = False #Load the plugins plugins.load("plugins/") log.info("Debug value is {0}".format(debugval))
def parse_thread(self, sentence): '''Thread in which parsing functions are called''' log.info("In parsing thread") #Get spacey loaded data parsed_data = self.load(sentence) #Threaded functions for each part of the nlp def get_ents(parsed_data): '''Get the entity data''' log.info("Getting entity data") ents = self.entity_recognition(parsed_data) current_data['ents'] = ents done['ents'] = True log.info("Finished getting entity data") def get_structure(parsed_data): '''Get the POS/Structure data''' #This will, at the moment, just append the tags to strucutre without any position data. #TODO: improve this log.info("Getting POS tags") #Get POS tags pos_tags = self.POS(parsed_data) current_data['structure']['tags'].update(pos_tags) done["structure"] = True log.info("Finished getting POS tags") def get_qcheck(sentence): '''Get the question data''' log.info("Checking if the command is a question") q_check = self.question_check(sentence) log.info("Question check returned {0}".format(str(q_check))) current_data["questions"] = q_check done["questions"] = True #Start all the threads ent_thread = threading.Thread(target=get_ents(parsed_data)) struct_thread = threading.Thread(target=get_structure(parsed_data)) question_thread = threading.Thread(target=get_qcheck(sentence)) ent_thread.start() struct_thread.start() question_thread.start() log.info("Started the parsing threads") #Wait for everything to be done while not done["structure"] and not done["questions"] and not done["ents"]: time.sleep(0.001) done["parsing"] = True
def google_search(user_query): log.info("In google search with query: " + str(user_query)) query = user_query query = urllib.urlencode({'q': query}) response = urllib2.urlopen( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query).read() json = m_json.loads(response) results = json['responseData']['results'] wiki_bool = False for result in results: title = result['title'] url = result['url'] pattern = re.compile('<.*?>') title = re.sub(pattern, '', title) if "Wikipedia, the free encyclopedia" in title: log.info("wiki_bool is true") titlelst = title.split('-') titlequery = titlelst[0].strip() return skwiki(titlequery) wiki_bool = True break if wiki_bool == False: log.info("wiki_bool is false") return print_gsearch(results)
def event(events, **kwargs): log.info("In event, events are {0}".format(str(events))) log.info("In event, kwargs are {0}".format(str(kwargs))) def subscribe(evt, func): dispatcher.connect(func, signal=evt) if event not in _event_handlers: _event_handlers[evt] = [] _event_handlers[evt].append(func) def decorator(func): log.info(func) # Append the plugin data to the nlp parsing que log.info("Appending {0} to nlp.current_plugins".format(str(events))) nlp.current_plugins.append(events) if not isinstance(events, str) and isinstance( events, Iterable) and not isinstance(events, dict): for evt in events: log.info("subscribing evt {0}".format(evt)) subscribe(evt, func) elif isinstance(events, dict): log.info(events) evt_keywords = events["key_words"] if evt_keywords: for key_word in evt_keywords: log.info("Subscribing {0} to {1}".format( key_word, str(func))) subscribe(key_word, func) else: log.info("Error: event {0} has no attribute name".format( str(events))) else: subscribe(events, func) return func return decorator
def action_return(message, answer_json): response = {} answer_action = answer_json["return_action"] answer_type = answer_json["return_type"] response.update({"response_args": message.teext, "response_type": answer_type, "return_action": answer_action}) for key in answer_json.keys(): if not response[key]: response.update({key: answer_json[key]}) returned = will.main(response) log.info("Returned is {0}".format(str(returned))) return_json = json.loads(returned) return_type = return_json["return_type"] log.info("Return type is {0}".format(return_type)) return_text = answer_json["text"] log.info("Return text is {0}".format(return_text)) if return_type == "text": log.info("Return type is text, returning {0}".format(str(return_text))) bot.reply_to(message, return_text) else: return_message = bot.reply_to(message, return_text) bot.register_next_step_handler(return_message, return_json)
def dispatch_event(self): return_values = [] log.info("In dispatch_event") while not nlp.plugins_final: time.sleep(0.001) plugins_final = nlp.plugins_final log.info("plugins_final is {0}".format(str(plugins_final))) handlers = pyplugins._event_handlers log.info("Event handlers are {0}".format(str(handlers))) return_values.extend(dispatcher.send(pyplugins.EVT_ANY_INPUT, dispatcher.Any, self.expression)) return_values.extend(dispatcher.send(self.event, dispatcher.Any, self.word, self.expression, plugins_final)) if len(return_values) > 0: return map(lambda x: "" if x is None else x, map(lambda x: x[1], return_values)) return tuple()
def wlfram_search(user_query, appid): log.info("in wolfram search") log.info(user_query) try: client = wolframalpha.Client(appid) res = client.query(user_query) if next(res.results).text != 'None': # for i in res.pods: # print "\n" # print i.text # phrase = str(next(res.results).text) # TTS_Talk.tts_talk(phrase) print next(res.results).text assert isinstance(next(res.results).text, object) return next(res.results).text else: google_search(user_query) except StopIteration: log.info("Hit stop iteration, going into google search") return google_search(user_query)
def dispatch_event(self): return_values = [] log.info("In dispatch_event") while not nlp.plugins_final: time.sleep(0.001) plugins_final = nlp.plugins_final log.info("plugins_final is {0}".format(str(plugins_final))) handlers = pyplugins._event_handlers log.info("Event handlers are {0}".format(str(handlers))) return_values.extend( dispatcher.send(pyplugins.EVT_ANY_INPUT, dispatcher.Any, self.expression)) return_values.extend( dispatcher.send(self.event, dispatcher.Any, self.word, self.expression, plugins_final)) if len(return_values) > 0: return map(lambda x: '' if x is None else x, map(lambda x: x[1], return_values)) return tuple()
def decorator(func): log.info(func) # Append the plugin data to the nlp parsing que log.info("Appending {0} to nlp.current_plugins".format(str(events))) nlp.current_plugins.append(events) if not isinstance(events, str) and isinstance(events, Iterable) and not isinstance(events, dict): for evt in events: log.info("subscribing evt {0}".format(evt)) subscribe(evt, func) elif isinstance(events, dict): log.info(events) evt_keywords = events["key_words"] if evt_keywords: for key_word in evt_keywords: log.info("Subscribing {0} to {1}".format(key_word, str(func))) subscribe(key_word, func) else: log.info("Error: event {0} has no attribute name".format(str(events))) else: subscribe(events, func) return func
#Internal libs import will import will.config as config from will.logger import log #Builtin libs import json import sys #External libs import telebot log.info("Running will") will.run() log.info("Loading telegram config") telegram_config = config.load_config("telegram") if telegram_config: log.info("Telegram configuration was found. Configuration data is {0}".format(str(telegram_config))) bot_key = telegram_config["key"] allowed_users = telegram_config["allowed_users"] log.info("Starting telegram bot with key {0}".format(bot_key)) bot = telebot.TeleBot(bot_key) else: log.info("Error: telgram config not found, shutting down") sys.exit() def user_check(message): sender = message.from_user user = sender.username log.info("Message sender is {0}".format(sender)) return user in allowed_users
def main(command): log.info("In main function, command is {0}".format(command)) if isinstance(command, dict): return_type = command["return_type"] return_action = command["return_action"] return_args = command["return_args"] if return_type.lower() == "python": try: return_module = command["return_module"] module_import = __import__("{0}.{1}".format( return_module, return_action)) if return_args: dispatcher.send(module_import, dispatcher.Any, return_args) else: dispatcher.send(module_import, dispatcher.Any) except Exception as python_exception: error_string = "Error {0},{1} occurred while trying to run {2} with args {3}".format( python_exception.message, python_exception.args, return_action, str(return_args)) log.info(error_string) return error_string elif return_type.lower() == "url": payload = {} if return_args: if isinstance(return_args, dict): payload.update(return_args) else: log.info( "Error, return_type was url but return_args wasn't a dict and wasn't none. It was {0}" .format(str(type(return_args)))) return "Error, return_args needs to be a dict of url parameters or none if return_type is url" try: url_request = requests.post(url=return_action, data=payload) return url_request.text except Exception as url_exception: error_string = "Error {0},{1} occurred while trying to fetch url {2} with data {3}".format( url_exception.message, url_exception.args, return_action, str(payload)) log.info(error_string) return error_string log.info("Starting plugin parsing") plugin_command = plugins.Command(command) answer = plugin_command.dispatch_event() log.info("Answer is {0}".format(str(answer))) answer = answer[0] response = { "return_type": None, "return_action": None, "text": None, } if not isinstance(answer, str): log.info("Answer is not a string") log.info("Answer is {0} and answer type is {1}".format( str(answer), str(type(answer)))) if isinstance(answer, dict): return_type = answer["return_type"] return_action = answer["return_action"] query = answer["query"] response.update({ "return_type": return_type, "text": query, "return_action": return_action }) else: log.info("Answer is a string") response.update({"return_type": "answer", "text": answer}) log.info("response is {0}".format(response)) log.info("Response data is {0}".format(str(response))) response_json = json.dumps(response) return response_json
def user_check(message): sender = message.from_user user = sender.username log.info("Message sender is {0}".format(sender)) return user in allowed_users
def load(self): if self.is_plugin(): log.info("Loading plugin: {0}".format(self.file_path)) self.update_path() importlib.import_module(self.import_name())
def print_gsearch(results): import requests log.info("In print_gsearch") for result in results: title = result['title'] log.info(title) url = result['url'] log.info(url) r = requests.get(url).text log.info("Got html from {0}".format(url)) textresult = None for line in r.split('\n'): line = line.encode('ascii', 'ignore') line = str(line.decode('utf8')) log.info("Analyzing line {0}".format(line)) if "h1" not in line: if "<p>" in line and "</p>" in line: textresult = line.split("<p>")[1].split("</p>")[0] pattern = re.compile('<.*?>') textresult = re.sub(pattern, '', textresult) log.info("Text result is {0}".format(textresult)) break if textresult != None: phrase = "According to {0}, {1}".format(url, textresult) return (phrase).decode('utf8') else: return ("Could not successfully extract information from {0}". format(url))
def question_check(self, sentence): '''Check if the sentence is a question''' q_words = ["is","are","am","was","were","will","do","does","did","have","had","has","can","could","should","shall","may","might","would","how","who","what","where","when","wouldn't","shouldn't","couldn't","hadn't","didn't","weren't", "don't","doesn't","haven't","can't","wasn't"] log.info('Checking if the sentence is a question') first_word = sentence.split(" ")[0].lower() return first_word in q_words
def main(command): log.info("In main function, command is {0}".format(command)) if isinstance(command,dict): return_type = command["return_type"] return_action = command["return_action"] return_args = command["return_args"] if return_type.lower() == "python": try: return_module = command["return_module"] module_import = __import__("{0}.{1}".format(return_module,return_action)) if return_args: dispatcher.send(module_import, dispatcher.Any, return_args) else: dispatcher.send(module_import,dispatcher.Any) except Exception as python_exception: error_string = "Error {0},{1} occurred while trying to run {2} with args {3}".format(python_exception.message, python_exception.args, return_action, str(return_args)) log.info(error_string) return error_string elif return_type.lower() == "url": payload = {} if return_args: if isinstance(return_args, dict): payload.update(return_args) else: log.info("Error, return_type was url but return_args wasn't a dict and wasn't none. It was {0}".format(str(type(return_args)))) return "Error, return_args needs to be a dict of url parameters or none if return_type is url" try: url_request = requests.post(url=return_action,data=payload) return url_request.text except Exception as url_exception: error_string = "Error {0},{1} occurred while trying to fetch url {2} with data {3}".format(url_exception.message,url_exception.args,return_action,str(payload)) log.info(error_string) return error_string log.info("Starting plugin parsing") plugin_command = plugins.Command(command) answer = plugin_command.dispatch_event() log.info("Answer is {0}".format(str(answer))) answer = answer[0] response = { "return_type" : None, "return_action" : None, "text" : None, } if not isinstance(answer, str): log.info("Answer is not a string") log.info("Answer is {0} and answer type is {1}".format(str(answer),str(type(answer)))) if isinstance(answer, dict): return_type = answer["return_type"] return_action = answer["return_action"] query = answer["query"] response.update({"return_type":return_type,"text":query,"return_action":return_action}) else: log.info("Answer is a string") response.update({"return_type":"answer","text":answer}) log.info("response is {0}".format(response)) log.info("Response data is {0}".format(str(response))) response_json = json.dumps(response) return response_json
def exit_func(): #End the session logging.info("Ending the session") #webapi.session().end(session_data) log.info("Shutting down.") plugins.unload_all()
def parse(self, sentence): '''Main parsing function''' global plugins_final global current_data current_data = { "ents": {}, "structure": { "tags": {} }, "questions": False, "key_words": [] } plugins_final = None log.info("In parsing function.") log.info("Sentence is {0}".format(str(sentence))) #TODO: add a function or thread that detects key words from synonyms or similar words current_data["key_words"].append(sentence.split(' ')[0]) # Start the elimination thread log.info("Starting elimination thread") e_thread = threading.Thread(target=self.elimination) e_thread.start() log.info("Starting parsing thread") p_thread = threading.Thread(target=self.parse_thread(sentence)) p_thread.start() while not done['elimination']: time.sleep(0.001) log.info("plugins_left is {0}. Returning.".format(plugins_left)) log.info("Finalizing nlp, current_data is {0}".format( str(current_data))) plugins_final = [] for plugin in plugins_left: final_plugin = { "name": plugin["name"], "ents": current_data["ents"], "struct": current_data["structure"]["tags"] } plugins_final.append(final_plugin)
def elimination(self): '''Thread to eliminate plugins that are incompatible''' #Check if the keywords match before anything else starts global plugins_left data_keys = current_data["key_words"] log.info("current_plugins is {0}".format(current_plugins)) log.info("current_data is {0}".format(current_data)) if data_keys: for plugin in current_plugins: found = False plug_keys = plugin["key_words"] if plug_keys: for keyword in data_keys: if keyword in plug_keys: found = True break if found: plugins_left.append(plugin) else: plugins_left = current_plugins #While the parsing part of the program isn't finished while not done["parsing"]: #Iterate over the plugins that haven't been eliminated for plugin in plugins_left: #Check if required entities have been obtained if done["ents"]: log.info("Ents is done, checking") ents_category = plugin['ents_needed'] if ents_category: for ent in ents_category: if ent not in current_data["ents"].values(): log.info( "Removing plugin {0} because of ent {1}". format(str(plugin['name']), str(ent))) plugins_left.remove(plugins_left[plugin]) break #Check if required tags are had and in the correct order if done["structure"]: structure_category = plugin["structure"] if structure_category: struct_needed = structure_category['needed'] for tag in struct_needed.keys(): if tag not in current_data["structure"][ "tags"].values(): plugins_left.remove(plugins_left[plugin]) break #If it needs a question check if there is one if done["questions"]: q_category = plugin['questions_needed'] if q_category: if not current_data['questions']: plugins_left.remove(plugins_left[plugin]) break left_len = len(plugins_left) if left_len == 1: done['elimination'] = True return plugins_left done['elimination'] = True return plugins_left
def command(message): '''Setup basic information and get started''' log.info("In setup with message {0}".format(str(message))) if user_check(message): command = message.text log.info("Command is {0}".format(str(command))) answer = will.main(str(command)) log.info("Answer is {0}".format(str(answer))) answer_json = json.loads(answer) answer_text = answer_json["text"] log.info("Answer text is {0}".format(answer_text)) answer_type = answer_json["return_type"] log.info("Answer type is {0}".format(answer_type)) if answer_type == "text": log.info("Answer type is text, responding with {1}".format(str(answer_text))) bot.reply_to(message, answer_text) else: return_message = bot.reply_to(message, answer_text) bot.register_next_step_handler(return_message, answer_json)
def print_gsearch(results): import requests log.info("In print_gsearch") for result in results: title = result['title'] log.info(title) url = result['url'] log.info(url) r = requests.get(url).text log.info("Got html from {0}".format(url)) textresult = None for line in r.split('\n'): line = line.encode('ascii', 'ignore') line = str(line.decode('utf8')) log.info("Analyzing line {0}".format(line)) if "h1" not in line: if "<p>" in line and "</p>" in line: textresult = line.split("<p>")[1].split("</p>")[0] pattern = re.compile('<.*?>') textresult = re.sub(pattern, '', textresult) log.info("Text result is {0}".format( textresult)) break if textresult != None: phrase = "According to {0}, {1}".format(url, textresult) return (phrase).decode('utf8') else: return ("Could not successfully extract information from {0}".format(url))
def parse(self, sentence): '''Main parsing function''' global plugins_final global current_data current_data = { "ents": {}, "structure": {"tags": {}}, "questions": False, "key_words": [] } plugins_final = None log.info("In parsing function.") log.info("Sentence is {0}".format(str(sentence))) #TODO: add a function or thread that detects key words from synonyms or similar words current_data["key_words"].append(sentence.split(' ')[0]) # Start the elimination thread log.info("Starting elimination thread") e_thread = threading.Thread(target=self.elimination) e_thread.start() log.info("Starting parsing thread") p_thread = threading.Thread(target=self.parse_thread(sentence)) p_thread.start() while not done['elimination']: time.sleep(0.001) log.info("plugins_left is {0}. Returning.".format(plugins_left)) log.info("Finalizing nlp, current_data is {0}".format(str(current_data))) plugins_final = [] for plugin in plugins_left: final_plugin = { "name" : plugin["name"], "ents" : current_data["ents"], "struct" : current_data["structure"]["tags"] } plugins_final.append(final_plugin)
def load(self): if self.is_json_file(): log.info("Loading plugin: {0}".format(self.file_path)) with open(os.path.abspath(self.file_path), 'r') as file_stream: return JsonData(json.load(file_stream)) raise IOError
def decorator(func): log.info(func) # Append the plugin data to the nlp parsing que log.info("Appending {0} to nlp.current_plugins".format(str(events))) nlp.current_plugins.append(events) if not isinstance(events, str) and isinstance( events, Iterable) and not isinstance(events, dict): for evt in events: log.info("subscribing evt {0}".format(evt)) subscribe(evt, func) elif isinstance(events, dict): log.info(events) evt_keywords = events["key_words"] if evt_keywords: for key_word in evt_keywords: log.info("Subscribing {0} to {1}".format( key_word, str(func))) subscribe(key_word, func) else: log.info("Error: event {0} has no attribute name".format( str(events))) else: subscribe(events, func) return func