class WitSentBot: def __init__(self): access_token = 'some_token' self._client = Wit(access_token) def do_analysis(self, text): # pass self._client.message(text)
class WitService(ResponseComposer): @staticmethod def compose_response(message_from_wit, msg_id): end_value = 0 if 'entities' in message_from_wit.keys(): if 'sentiment' in message_from_wit['entities'].keys(): for sugestion in message_from_wit['entities']['sentiment']: if all(k in sugestion.keys() for k in ('confidence', 'value')): if sugestion['value'].upper() == "NEGATIVE": end_value = end_value - sugestion['confidence'] if sugestion['value'].upper() == "POSITIVE": end_value = end_value - sugestion['confidence'] if 'exaggeration' in message_from_wit['entities'].keys(): for sugestion in message_from_wit['entities']['exaggeration']: if all(k in sugestion.keys() for k in ('confidence', 'value')): end_value = end_value - 2 * sugestion['confidence'] return end_value def __init__(self, access_token, location_response_logic_list): super().__init__(location_response_logic_list) self.__client = Wit(access_token) def write_to(self, message): msg_text = message['message'] msg_text_length = len(msg_text) print(' ## MESSAGE: {}'.format(msg_text)) if msg_text_length in range(0, 250): wit_response = self.__client.message(msg_text) Logger.json_print(wit_response) return WitService.compose_response(wit_response, message['id']) return None def talk_to(self, message): msg_text = message['message'] msg_text_length = len(msg_text) print(' ## MESSAGE: {}'.format(msg_text)) if msg_text_length in range(0, 250): wit_response = self.__client.message(msg_text) Logger.json_print(wit_response) if 'entities' in wit_response.keys(): return self.__create_response_to_msg(wit_response['entities']) return None def __create_response_to_msg(self, wit_response): entities = [] # TODO: each step here inside isteed of returning response value will calculate tensor. # This tensor will give response type and entities that will be used for answare if Validator.check_if_any_keys_in(wit_response, 'exaggeration'): entities.append( self.get_with_highest_confidance(wit_response['exaggeration'])) if Validator.check_if_any_keys_in(wit_response, 'sentiment'): entities.append( self.get_with_highest_confidance(wit_response['sentiment'])) return entities
def index(): client = Wit(BASE_TOKEN) #Passes Auth key to the Wit API data = request.get_json( ) #fetches the data sent as a POST request from the front end into the variable query = data.get( "query" ) #singles out the user input that needs to be processed by Wit API resp = client.message( query ) #sends the user input extracted(into the query variable) and stores the response in 'resp' variable entities = resp.get( "entities" ) #The response from the Wit API contains the entities which it has detected from the user input location = entities.get("location")[0].get( "value" ) #This extracts the City (location) which has been detected by the Wit API and stores it in location for furthur use z = Zomato(ZOMATO_TOKEN) #Zomato Authentication by passing token zomato_locations = z.location.get_locations( location ) #sends the city name extracted previously to the locations end point to receive 2 parameters furthur required to get recommendations for the particular location entity_type = zomato_locations.raw_data.get("location_suggestions")[0].get( "entity_type") city_id = zomato_locations.raw_data.get("location_suggestions")[0].get( "city_id") zomato_suggestions = z.location.get_location_details( city_id, entity_type ) #returns the zomato's suggestions of resturants for the particular location return jsonify( zomato_suggestions.raw_data ) #converts the response into JSON to be sent to the front end for rendering
class ClientAction(): def __init__(self, TokenFile, Ip, Port): with open(TokenFile, 'r') as File: self._Token = File.readline() self._ClientWit = Wit(self._Token) self._ClientMqtt = mqtt.Client() self._Recognizer = sr.Recognizer() self._Speech = sr.Microphone() self._Ip = Ip self._Port = Port def AudioMessage(self): with self._Speech as source: self._Recognizer.adjust_for_ambient_noise(source) audio = self._Recognizer.listen(source) msg = self._Recognizer.recognize_wit(audio, key=self._Token) return self.TextMessage(msg) def TextMessage(self, msg): # send the message to the wit.ai server return self._ClientWit.message(msg) def SendMessage(self, msg): # send the message to BOBB3E self._ClientMqtt.connect(self._Ip, self._Port, 1) self._ClientMqtt.publish("Action", json.dumps(msg)) self._ClientMqtt.disconnect()
class WitCommand(Command): def __init__(self, token, intent, *args, **kwargs): super().__init__(*args, **kwargs) self.wit = Wit(token) self.intent = intent def __run(self): self.process_wit_response() if self.is_valid(): return super().run(**self.params) return False def run(self, text=None): if text: self.text = text error = '' success = False if self.is_valid(): logger.info("Executando o comando...") success = self.__run() error = self.error return success, error def is_valid(self): return self.text and self.intent.is_valid(self.text) def process_wit_response(self): self.params.clear() logger.info(f'Enviando para wit.ai: {self.text}') response = self.wit.message(self.text) entities = response['entities'] for entitie, data in entities.items(): self.params[entitie] = data[0]['value'] logger.info(f'Parâmetros wit.ai: {self.params}')
def get_list_keyword(message_text): print('get_list_keyword') # gửi lên wit tìm nhận dạng client = Wit(ServerAccessToken) data = client.message(message_text) print(data) # xử lý json wit list_keywords = [] # data { '_text': '@cmm', # 'entities' : {'__debug__': [{'confidence': 1, 'value': '@cmm', 'type': 'value'}]}, # 'msg_id': '1VNu6WveQrN1pCBae'} # 'entities': {'__debug__': [{'confidence': 1, 'value': '@cmm', 'type': 'value'}]} for keys in tuple(data.get('entities').keys()): # print(keys) # trượt qua keys các giá trị entities : for x in data.get('entities').get(keys): confi = x.get('confidence') # {'confidence': 1, 'value':'menu'} if confi >= 0.6: value = x.get('value') # 'menu' # print(value) if value == '@cmm': global command_state command_state = 1 print('set_command_state_on') send_message(PAGE_ID, MASTER_FBID, 'Command mode ON') return "@KtrLoi" elif {"entity": keys, 'value': value} not in list_keywords: # tạo dict {"entity":"Noun","value":"menu"} dict_entity = {"entity": keys, "value": value} # nối vào list các dict entities list_keywords.append(dict_entity) print(list_keywords) return list_keywords
def get_sentiment(text): client = Wit("B6WBHQST6FHDCLWXLJP3SQD2YNIRPAIL") resp = client.message(text) try: return resp['entities']['sentiment'][0]['value'] except: return None
def query(): access_token = "PJUSBTGJ6VXSGG3HERAZEBT3NKKV7JSH" query = request.form['search'] client = Wit(access_token) res = client.message(query) if (('song' in res['entities']) and ('code' in res['entities']) and ('singer' in res['entities'])): result = {"error": 1, "message": "I don't know"} elif (('song' in res['entities']) and ('code' in res['entities'])): result = searchTuneCode(res["entities"]["song"][0]["value"].lower()) elif (('song' in res['entities']) and ('singer' in res['entities'])): result = searchTuneArtist(res["entities"]["song"][0]["value"].lower()) else: result = {"error": 1, "message": "I don't know"} # if('singer' in res['entities']): # return "mean" # else: # return "ot mean" # i=0 # for a in res["entities"]: # i+=1 # if(i<2): # result = {"error": 1,"message":"I don't know"} # else: # result = searchTuneCode(res["entities"]["song"][0]["value"].lower()) return jsonify(result)
def get(self, request, *args, **kwargs): data = request.query_params.get("Name", "") print(data) access_token = '*************************' client = Wit(access_token) qwe = client.message(data) print(qwe) json_string1 = json.dumps(qwe) field_x = json_string1.split('entities')[1] print("skjbwejbc ibeb erh !@#$%^&*%^ #$%^&") print(field_x) list_all = [] list_all.append({"attribute": "DATE", "value": "empty string"}) print(list_all) new_data = { "entries": [{ "template_type": "set_attr", "attributes": list_all }] } return JsonResponse(new_data, status=201)
def getResult(args): client = Wit("J55QIEQPKJ4PBJOVFDXYKVZNW3PWONEI") x = args resp = client.message(x) #print(resp) value = "" if resp["entities"] == {}: return "Wrong text" else: entities = resp["entities"] intents = entities["intent"] intent = intents[0] #print(intent); value = intent["value"] #print(value); if value == "greeting": return "Hello Sir !! May I help you?" if value == "agree": return "Thanks , What Can I do for you? " if value == "helps": return "Yes , I can search anything for you. I can open your any apps I can call to any number from your contats any other things similar to that." if value == "exit": return "Closing" #sys.exit(0); if value == "discard": return "Okay ! Thanks I'll be working on myself" if value == "ok": return "Yepp !!" if value == "call": return "Ok Calling " + x[5:]
def readSheet(self): import gspread from oauth2client.service_account import ServiceAccountCredentials import pandas as pd import requests WIT_ACCESS_TOKEN = <your token here> from wit import Wit cliente = Wit(access_token=WIT_ACCESS_TOKEN) # use creds to create a client to interact with the Google Drive API scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] creds = ServiceAccountCredentials.from_json_keyfile_name('cliente_secret.json', scope) client = gspread.authorize(creds) # Find a workbook by name and open the first sheet # Make sure you use the right name here. sheet = client.open("SP1encuesta").sheet1 # Extract and print all of the values opinion = sheet.col_values(24) course = sheet.col_values(2) data = pd.DataFrame([]) for i in range(1,len(opinion)): message = opinion[i] resp = cliente.message(message) if resp['traits'] != {}: data = data.append(pd.DataFrame({'Curso': course[i],'Valor': str(resp['traits']['sentiment'][0]['confidence']),'Clase':str(resp['traits']['sentiment'][0]['value'])}, index=[0]), ignore_index=True) #ver el codigo de abajo sirve para ajustar el training de NLP de Wit.ai #else: #print (str(resp)) return data
def main(): status_ui = aiy.voicehat.get_status_ui() status_ui.status('starting') assistant = aiy.assistant.grpc.get_assistant() led = aiy.voicehat.get_led() button = aiy.voicehat.get_button() access_token = 'XVV53YJBHIOA3ELM2ZPBPFX5LI7PJQFY' client = Wit(access_token) print(".....check client........OK") with aiy.audio.get_recorder(): while True: led.set_state(aiy.voicehat.LED.BLINK) LED_RED.set_state(aiy.voicehat.LED.BLINK) #print(led) #print(LED_RED) status_ui.status('ready') print('Press the button and text input') button.wait_for_press() status_ui.status('listening') print('Listening...') text = input() if text: print("recognize text is :" + text) resp = client.message(text) print(resp)
class WitInterface(object): def __init__(self, access_token): self.client = Wit(access_token = access_token) def get_response(self, text): res = self.client.message(text) confidence_threshold = {'intent': 0.6, 'movie':0.7, 'genre':0.7} print(res) intent, entities = '', [] try: for key, val in res['entities'].items(): if val[0]['confidence'] <= confidence_threshold[key]: continue if key=='intent': intent = val[0]['value'] else: for entity in val: entities.append({'type': key, 'value': entity['value']}) except KeyError as e: print("Key not found in Wit response: ", e) return (intent, entities)
def geo_parse(transcribed_text, parser): if parser == 'google': query = transcribed_text + ", " + city search_payload = {"key": key, "query": query} search_req = requests.get(search_url, params=search_payload) search_json = search_req.json() if bool(search_json["results"]): # return lat, long adress, and raw JSON return str(search_json["results"][0]["geometry"]["location"]["lat"]), str( search_json["results"][0]["geometry"]["location"]["lng"]), \ search_json["results"][0]["formatted_address"], search_json else: return None, None, None, search_json elif parser == 'wit': client = Wit(wit_ai_token) resp = client.message(transcribed_text) if bool(resp["entities"]): query = resp["entities"]["location"][0]["value"] + ", " + city search_payload = {"key": key, "query": query} search_req = requests.get(search_url, params=search_payload) search_json = search_req.json() if bool(search_json["results"]): # return lat, long adress, and raw JSON return str(search_json["results"][0]["geometry"]["location"]["lat"]), str( search_json["results"][0]["geometry"]["location"]["lng"]), \ search_json["results"][0]["formatted_address"], search_json else: return None, None, None, search_json else: return None, None, None, resp
def main(): status_ui = aiy.voicehat.get_status_ui() status_ui.status('starting') assistant = aiy.assistant.grpc.get_assistant() button = aiy.voicehat.get_button() logger = log_manager.MyLogger(level=logging.INFO, get="MAIN") logger.add_file_stream_handler('logging.log') with aiy.audio.get_recorder(): play_sound.playAudioText('power on') while True: access_token = 'access_token' client = Wit(access_token) play_sound.playAudioText('press button and speak') button.wait_for_press() text, audio = assistant.recognize() if text: try: with Timeout(3): resp = client.message(text) logger.info(resp) json_manager.saveJson(resp) json_manager.decodeJson() logger.logger.info('%s ', text) except Timeout.Timeout: logger.logger.info('timeout') except: logger.logger.info('error')
def clarify_image(url): item_list = { 'batteries': 0, 'electronics': 0, 'clothes': 0, 'plastics': 0, 'glass': 0, 'makeup': 0, 'aluminum cans': 0, 'paper': 0, 'hazardous waste': 0 } # Pywit Initialization client = Wit(WIT_API_TOKEN) # ClarifaiApp Initialization app = ClarifaiApp(api_key=CLARIFAI_API_TOKEN) model = app.public_models.general_model # Parse response response = model.predict_by_url(url=url) concepts = response['outputs'][0]['data']['concepts'] # print(str(concepts)) for concept in concepts: resp = client.message(concept['name']) # print(concept['name'], concept['value']) # We only consider the value of clarification over 0.9 if concept['value'] > 0.9 and len(resp['entities']) != 0: # print(str(resp)) for item in item_list: # We only consider the value of confidence over 0.75 if resp['entities']['item'][0]['value'] == item and \ resp['entities']['item'][0]['confidence'] > 0.75: item_list[item] += 1 # Return the highest frequency category result = "" max_count = 0 max_value = 0 for item in item_list.keys(): if item_list[item] > max_count: result = item max_value = concept['value'] # When the max_count is equal and the 'value' is bigger, we replace the result elif item_list[item] == max_count: for concept in concepts: if concept['name'] == item and concept['value'] > max_value: result = item max_value = concept['value'] # Return type is string. If no matches, return is "" if result == "": return final_response_handler(listOfAnswers=[ "We're having trouble identifying what you are trying to recycle." ]) return final_response_handler( listOfAnswers=['\n'.join(categories_dict[result])])
def input_func(): input_text = request.args.get('input') print(input_text) input_text = unquote(input_text) print(input_text) #input_text = request.form['input'] if input_text.strip() != "": # The client access token of the wit.ai app access_token = "EJGNYVPJ7SXYETU2FAHNRCYAQJGR7YO7" # Creating a client instance and passing the input client = Wit(access_token) resp = client.message(input_text) confidence = 0 value = "None" data = {} for i in resp["entities"]: temp = resp["entities"][i][0]["confidence"] if temp > confidence: confidence = temp entity = i value = resp["entities"][i][0]["value"] # The entity identified from the given input if confidence > 0: data["entity"] = entity data["value"] = value return jsonify(data)
class VoiceAssistant: """ Voice Assistant object. Responds to text inputs and checks datetime for calendar appointments. """ def __init__(self, wit_access_token, actions, emo_train_filename=None): self.client = Wit(access_token=wit_access_token) self.actions = actions self.emotion_list = [] # Respond to text input def respond(self, input_text, context=None, voice_output=False): resp = self.client.message(input_text) entities = resp['entities'] intents = entities['intent'] for intent in intents: text_resp = self.actions[intent['value']](entities) return text_resp def calendar(self, datetime): pass def emotion_update(self): pass
def chat(bot, update): user_id = update.message.from_user.id answer = update.message.text if answer == 'О марафонах': text = "Марафоны - это круто!" bot.sendMessage(user_id, text=text, reply_markup=main_kbd) elif answer == 'Категории': text = "Пока категории вопросов не созданы. Вы можете ввести вопрос самостоятельно" bot.sendMessage(user_id, text=text, reply_markup=main_kbd) elif answer == 'Моя программа': text = "Подождите, какая еще программа? Вы же даже не знаете, что такое марафон. Сначала узнайте, а потом уже спрашивайте про программу!" bot.sendMessage(user_id, text=text, reply_markup=main_kbd) elif answer == 'INFO': text = "Появление информации ожидается в скором времени" bot.sendMessage(user_id, text=text, reply_markup=main_kbd) else: actions = dict() client = Wit(access_token=wit_token, actions=actions) client_answer = client.message(answer) try: if client_answer['entities']['intent'][0]['confidence'] < 0.6: text = "К сожалению, ответ на этот вопрос мне не известен. Попробуйте другой вопрос." bot.sendMessage(user_id, text=text, reply_markup=main_kbd) else: codec = client_answer['entities']['intent'][0]['value'] text = dictionary[codec] bot.sendMessage(user_id, text=text, reply_markup=main_kbd) except KeyError: text = "К сожалению, ответ на этот вопрос мне не известен. Попробуйте другой вопрос." bot.sendMessage(user_id, text=text, reply_markup=main_kbd)
class WitAnalysis: def __init__(self): self.client = Wit(WIT_ACCESS_TOKEN) def extract_sentiments(self, title): has_emoji = bool(emoji.get_emoji_regexp().search(title)) if has_emoji: return None resp = self.client.message(title) return resp def extract_intents(self, sentiment): if not sentiment: return None if len(sentiment['intents']) == 0: return '' return sentiment['intents'][0]['name'] def extract_intents_confidence(self, sentiment): if not sentiment: return None if len(sentiment['intents']) == 0: return '' return sentiment['intents'][0]['confidence'] def extract_traits(self, sentiment): if not sentiment: return None if len(sentiment['traits']) == 0: return '' return sentiment['traits']['wit$sentiment'][0]['value'] def extract_traits_confidence(self, sentiment): if not sentiment: return None if len(sentiment['traits']) == 0: return '' return sentiment['traits']['wit$sentiment'][0]['confidence'] def get_sentiments(self, master, top_tickers): tickers_sentiments = [] for ticker in top_tickers: sentiment_df = master.loc[master['extracted'] == {ticker}] sentiment_df['sentiments'] = sentiment_df['title'].apply(self.extract_sentiments) sentiment_df['intents'] = sentiment_df['sentiments'].apply(self.extract_intents) sentiment_df['intents_confidence'] = sentiment_df['sentiments'].apply(self.extract_intents_confidence) sentiment_df['traits'] = sentiment_df['sentiments'].apply(self.extract_traits) sentiment_df['traits_confidence'] = sentiment_df['sentiments'].apply(self.extract_traits_confidence) tickers_sentiments.append((sentiment_df, ticker)) for sentiment, ticker in tickers_sentiments: data_directory = Path('./data/sentiments') data_directory.mkdir(parents=True, exist_ok=True) output_path = data_directory / f'{dt.date.today()}_{ticker}_sentiment_df.csv' sentiment.to_csv(output_path, index=False) return tickers_sentiments
class WitAIService: def __init__(self): self.access_token = config['wit_ai']['Server_Access_Token'] self.client = Wit(self.access_token) def retrive_message_entity(self, message): res_dict = self.client.message(message) return res_dict['entities']
def WitTest(): token = '7F63TXDBHOOOTQZ52MM5PALSEWPMXA6F' client = Wit(token) client.logger.setLevel(logging.DEBUG) print("Hello1") resp = client.message('I need more info about flex') print("Hello2") return ('Yay, got Wit.ai response: ' + str(resp))
class WitAi(object): __ENTITIES_TAG = 'entities' __DEFAULT = None def __init__(self, access_token, actions): self.__wit_client = Wit(access_token, actions) def analyze(self, msg): return self.__wit_client.message(msg) def get_entities(self, msg): return self.__wit_client.message(msg).get(self.__ENTITIES_TAG, self.__DEFAULT) def talk(self, session_id, msg, context): return self.__wit_client.run_actions(session_id, msg, context)
class BotWit(): client = None def __init__(self, access_token): self.client = Wit(access_token) def get_wit_response(self, message): if self.client is None: return resp = self.client.message(message) print(str(resp)) return resp def get_intent(self, message): if self.client is None: return print("Tweet: " + message) response = self.client.message(message) entities = response['entities'] lost_intent = self.first_entity_value(entities, 'lost_intent') search_type = self.first_entity_value(entities, 'search_type') lost_adj = self.first_entity_value(entities, 'lost_adj') bot_name = self.first_entity_value(entities, 'bot_name') print(search_type, " ", lost_intent, " ", lost_adj) if bot_name: return True if search_type: if lost_intent or lost_adj: return True return False def first_entity_value(self, entities, entity): if entity not in entities: return None else: val = entities[entity][0]['value'] if val: return val return None
def get_intent(bot, update): client = Wit(wit_access_token) response = client.message(update.message.text) replies = parse_response(response) for reply in replies: bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) sleep(0.7) update.message.reply_text(reply)
def predict(): def backend(intent): if intent == 'app_name': return "The name of our application is Appendly" elif intent == 'app_func': return "Our application build upon the DrChrono API and provides a lot of helpful features ranging from interactive dashboards,notification management to drug and patient management." elif intent == 'app_nav': return "You can find a handy navigation bar to the left and search bar at the top right." elif intent == 'chart_location': return "You can view all your charts and reports in the dashboard." elif intent == 'patients_list': return "You can navigate to the Patient's List Page through the navigation bar which is to the left of the webpage." elif intent == 'edit_profile': return "You can edit your profile details by either clicking on the User Profile button in the sidebar to the left or by clicking on your account avatar on the top right." elif intent == 'edit_website': return "By clicking on the settings button hovering on the right, you can change the color of the sidebar." elif intent == 'notifications': return "Click on Notifications button in the sidebar. In the page that opens, you can simultaneously view all your notifications and can also remove them." elif intent == 'send_notifications': return "To send a notification, firstly select the Send Notifications option in the sidebar. Here, select a drug present in the list and fill the details in the Patients list form. After this, click on the Fire Notification button to send your notification to the concerned patient." elif intent == 'dashboard_features': return "In the dashboard, you can view your monthly reports, useful metrics like drug sales and marketing report. You can also plot and visualize your custom data. Moreover, in this page, you can manage your tasks and also view your Patients list." elif intent == 'add_patient': return "Click on the Patients List button in the sidebar. Here, click on the Add Patient button, fill the patient details and finally,click the Add to patients list button. You can now view your new patient in the list as well." elif intent == 'patient_use': return "The Patients List page is really helpful for doctors as it allows them to quickly filter the patients with the help of powerful search features based on say,their name or ongoing medication and allows them to prioritize their patients according to their needs." elif intent == 'drug_use': return "In the Drugs List Page you can filter the existing drugs based on various parameters and also add new drugs to the list. To add a new drug, click on the Add drug button,fill the drug details and press Add to drugs list. The existing list will be updated." elif intent == 'add_drug': return "To add a new drug, click on the Add drug button in the Drugs List page,fill the drug details and press Add to drugs list. The existing list will be updated." elif intent == 'custom_data': return "The charts/reports in the dashboard can be replaced to visualize your custom data as well." elif intent == 'todo_list': return "The todo/Today list feature on the Dashboard page lets you schedule your appointments and ensure that you do not miss them. You can filter and edit them according to your convenience." elif intent == 'analysis': return "The Covid Prediction Feature lets you get an idea of the estimated csaes of the same over the next few days." elif intent == 'datasource': return "The Machine Learning model trained to predict the no. of cases was trained on accurate data obtained from Kaggle." file = request.files['input'].read() prompt = str(file) access_token = "62UGLHWGE5ZJPTETZKHVUFMM5GXCLQLL" client=Wit(access_token) response = client.message(prompt) try: intent = response['intents'][0]['name'] if intent is None: answer = 'Try framing the question in a different way...' else: output = backend(intent) answer = output except: answer = "Try a different question" #output = {'result':message} return {'result':answer}
def witprocess(input): client = Wit(access_token='WW2J4SJKFGPZ5ASMTUMLDW3IT5STJWON') resp = client.message(input) dic = resp['entities'] modulename = list(dic.keys()) if len(modulename) == 0: module = 'general' else: module = modulename return module
def main(): message_input = input() if message_input.lower() not in ['goodbye', 'bye', 'bye bye']: client = Wit(access_token=WIT_TOKEN) response = client.message(message_input) intent = get_first_intent(response) print(select_answer(intent)) else: print(select_answer('bye')) exit()
class WitApi: def retrieve_token(): return os.environ["WIT_TOKEN"] def __init__(self): self.token = WitApi.retrieve_token() self.wit_client = Wit(self.token) def send_text(self, text): return self.wit_client.message(text)
def wit_response(messaging_text): access_token = 'F6XCF3CXEVRQXDIHU7IKN5HJRD5YHHCG' client = Wit(access_token=access_token) resp = client.message(messaging_text) entity = resp['intents'][0]['name'] return entity # print(bool(resp['traits']['wit$thanks']))
class WitInterpreter(RegexInterpreter): """ DialogflowInterpreter is an Interpreter for use in rasa_core that performs Natural Language processing using dialogflow API v2 """ def __init__( self, access_token, timezone="Africa/Nairobi", language_code="en", raise_on_error=False, flip_text_and_intent=False, ): self.language_code = language_code self.raise_on_error = raise_on_error self.flip_text_and_intent = flip_text_and_intent self.timezone = timezone self.session_client = Wit(access_token=access_token) def parse(self, text): """ parse takes a string and responds with the NLU results sent by Wit """ context = {} if self.timezone: context['timezone'] = self.timezone try: response = self.session_client.message(text, context=context) print('raw response', response, type(response)) except Exception as e: if self.raise_on_error: raise UpstreamError(str(e)) else: logger.error("Failed to parse text '{}' using Wit " "Error: {}".format(text, e)) return build_response(text) entities = response['entities'] try: intent_raw = entities.pop('intent')[0] intent = { "name": intent_raw['value'], "confidence": intent_raw['confidence'], } except KeyError: intent = None return build_response( response['_text'], intent=intent, entities=entities, )
def do(text): witClient = Wit(access_token='Z2M5NG4DUAOD3IH24BNQSXGM4LGIK4PU') wolframClient = wolframalpha.Client('5G696A-TT6AEK7L74') response = witClient.message(text) intent = response['entities']['intent'][0]['value'] if intent == 'weather': loc = Nominatim() loc = loc.geocode(response['entities']['location'][0]['value']) forecast = forecastio.load_forecast('17e86a360729736b727899a8135e33ad',loc.latitude, loc.longitude) return forecast.hourly().summary elif intent == 'greeting': return random.choice(greetings) elif intent == 'wikipedia': return wikipedia.summary(response['entities']['contact'][0]['value'], sentences=1) elif intent == 'curse': return 'F**k you too!' else: return 'I did not understand what you said.'
class WitRos(object): def __init__(self, api_key): self.wit = Wit(api_key) self.pub = rospy.Publisher('stt', Outcome, queue_size=1) def start(self): rospy.Service('wit/interpret', Interpret, self.interpret) # rospy.Service('wit/listen_interpret', ListenAndInterpret, self.listen_and_interpret) def parse_response(self, response, klass): rospy.logdebug("Data: '{0}'".format(json.dumps(response, indent=4, separators=(',', ': ')))) ros_entities = [] if "WARNING" in response: rospy.logwarn("Response contains a warning: {warn}".format(warn=response["WARNING"])) outcome = None entities = [] if "entities" in response: entities = response["entities"] elif "outcomes" in response: outcome = response["outcomes"][0] entities = outcome["entities"] for entity_name, entity_properties in entities.iteritems(): entity_properties = entity_properties[0] rospy.logdebug("Entity '{name}' has properties{prop}".format(name=entity_name, prop=entity_properties)) entity = Entity(name=str(entity_name)) if 'type' in entity_properties: entity.type = str(entity_properties["type"]) if 'value' in entity_properties: entity.value = str(entity_properties["value"]) if 'unit' in entity_properties: entity.unit = str(entity_properties["unit"]) if 'suggested' in entity_properties: entity.suggested = str(entity_properties["suggested"]) if 'confidence' in entity_properties: entity.confidence = float(entity_properties["confidence"]) rospy.logdebug("Adding {ent}".format(ent=entity)) ros_entities += [entity] outcome = Outcome(entities = ros_entities, intent = str(outcome["intent"]) if outcome else None, text = str(response["_text"])) response = klass( msg_body = str(response), msg_id = str(response["msg_id"]), outcome = outcome) self.pub.publish(outcome) return response def interpret(self, rosrequest): sentence = rosrequest.sentence rospy.logdebug("Interpreting '{0}'".format(sentence)) wit_response = self.wit.message(sentence) rospy.logdebug("WitResponse: {0}".format(wit_response)) #response = json.loads(wit_response) #rospy.logdebug("Response: {0}".format(response)) return self.parse_response(wit_response, InterpretResponse)
class ExternalApiParser(object): """ This calls external APIs for responses. api.ai for conversational queries wit.ai for entities and intents - each entity gets a confidence value from 0 to 1. "1 is extremely confident. Lower than 0.5 is considered very low confidence." - can add intents using api: https://wit.ai/docs/http/20160330#get-intent-via-text-link - self.actions : this is for merge and context functionality """ def __init__(self, wit_key, rive, bot, nyt_api, mongo): self.BOT = bot self.NYT_API = nyt_api self.wit_actions = {} self.wit_client = Wit(access_token=wit_key, actions=self.wit_actions) self.wit_empty_response = {'entities': []} self.WIT_SEARCH_QUERY_CONFIDENCE_THRESH = 0.5 self.RIVE = rive self.MONGO = mongo self.emojis = pickle.load(open("message_processor/unicode_emoji.pickle", "rb")) def wit_api_call(self, text): try: wit_response = self.wit_client.message(text) except WitError as we: print(we) # the Wit API call failed due to a WitError, so let's make the response empty wit_response = self.wit_empty_response wit_parsed_message = self.parse_wit_response(wit_response) return wit_parsed_message def parse_wit_response(self, wit_return_dict): """ Takes a Wit response dict and converts into a WitParsedMessage object """ wit_entities = wit_return_dict['entities'] wit_parsed_message = WitParsedMessage() intent = None if 'intent' in wit_entities: intent = (wit_entities['intent'][0]['value'], wit_entities['intent'][0]['confidence']) wit_parsed_message.intent = intent search_queries = [] if 'search_query' in wit_entities: for search_query in wit_entities['search_query']: search_queries.append((search_query['value'], search_query['confidence'])) wit_parsed_message.search_queries = search_queries locations = [] if 'location' in wit_entities: for loc in wit_entities['location']: locations.append((loc['value'], loc['confidence'])) wit_parsed_message.locations = locations return wit_parsed_message def take_external_action(self, message_text, recipient_id, num_articles=3, wit_parsed_message=None, rive_parsed_message=None): """ Sends messages to the user. if Wit Parser finds intent, return on behalf of wit.ai else returns on behalf of api.ai else returns helper callback params: message_text: str or unicode recipient_id: str num_articles: int wit_parsed_message: WitParsedMessage api_ai_parsed_message: ApiAIParsedMessage - DEPRECATED rive_parsed_message: str returns: http response """ # Search RIVE for a valid response if rive_parsed_message is None: # get user info from the db user = User() user_dict = self.MONGO.db.users.find_one({'recipient_id': recipient_id}) if user_dict is None: # get user information from Facebook fb_user_profile_info = self.BOT.get_user_profile_info(recipient_id) user.set_user_dict(recipient_id, fb_user_profile_info, timestamp=int(time.time())) # write the user information to our database self.MONGO.db.users.insert_one(user.user_dict) user_dict = user.user_dict else: user.user_dict = user_dict # give rivescript our user_vars if user_dict.get('user_vars'): for key, value in user_dict['user_vars'].items(): self.RIVE.set_uservar(recipient_id, key, value) # get the rivescript response rive_parsed_message = self.RIVE.reply(recipient_id, message_text) # get all the user vars back out of the RIVE to insert into DB new_user_vars = self.RIVE.get_uservars(recipient_id) # print(new_user_vars) user.update_user_vars(new_user_vars) self.MONGO.db.users.update({'recipient_id': recipient_id}, user.user_dict) # print(rive_parsed_message) if rive_parsed_message != "UNDEFINED_RESPONSE": # print("HIT RIVE") response = self.BOT.send_text_message(recipient_id, rive_parsed_message) return response # Rive had UNDEFINED_RESPONSE, so let's search WIT.AI for a response if wit_parsed_message is None: wit_parsed_message = self.wit_api_call(message_text) # Parse the WIT.AI response if wit_parsed_message.intent and wit_parsed_message.intent[0] == 'search_article' \ and wit_parsed_message.intent[1] > self.WIT_SEARCH_QUERY_CONFIDENCE_THRESH \ and wit_parsed_message.has_at_least_one_entity(): # print("HIT WIT.AI") nyt_query_string = "" # take search query entitiy with highest confidence if len(wit_parsed_message.search_queries) > 0: query = sorted(wit_parsed_message.search_queries, key=lambda x: x[1], reverse=True)[0] nyt_query_string += query[0] # get location entities if len(wit_parsed_message.locations) > 0: location = sorted(wit_parsed_message.locations, key=lambda x: x[1], reverse=True)[0] nyt_query_string += " in " + location[0] # query for NYT articles to send to user nyt_response = self.NYT_API.return_article_list(nyt_query_string, num=num_articles) template_elements = self.make_nyt_response_templates(nyt_response) # if NYT api returns valid articles, then send them to the user if len(template_elements) > 0: response = self.BOT.send_text_message(recipient_id, "Here are some articles for you:") response = self.BOT.send_generic_payload_message(recipient_id, elements=template_elements) return response else: response = self.BOT.send_text_message(recipient_id, "Sorry I couldn't find articles with those search terms. Try something more general.") return response elif wit_parsed_message.intent and wit_parsed_message.intent[0] == 'latest' \ and wit_parsed_message.intent[1] > self.WIT_SEARCH_QUERY_CONFIDENCE_THRESH: response = self.send_trending_articles(recipient_id) return response # Wit.ai and Rive couldn't compute a valid response # If the user searches keywords, not complex sentences, return NYT articles based on keywords # Else, return a helper callback template nyt_query_string = self.extract_keywords(message_text) if nyt_query_string is not None: # print("HIT ENTITY EXTRACTOR") nyt_response = self.NYT_API.return_article_list(nyt_query_string, num=num_articles) template_elements = self.make_nyt_response_templates(nyt_response) if len(template_elements) > 0: response = self.BOT.send_text_message(recipient_id, "Here are some articles for you:") response = self.BOT.send_generic_payload_message(recipient_id, elements=template_elements) return response else: response = self.BOT.send_text_message(recipient_id, "Sorry I couldn't find articles with those search terms. Try something more general.") return response response = self.send_cannot_compute_helper_callback(recipient_id) return response def extract_keywords(self, message_text): """ params: message_text: str or unicode returns: returns: str or None, if no keywords extracted we return None 1. Strip conjunctions 2. Only accept queries with length <= 5 excluding CC 3. Only accept POS tags in POS_to_accepts To do: - test with emojis, REMOVE emojis - add emoji dictionary """ keywords = [] POS_to_accept = ['VBG', 'NN', 'NNP', 'NNS', 'JJ', 'CC', 'DT'] num_words_thresh = 5 # Remove emojis) message_text = [word for word in message_text.split() if word not in self.emojis.keys()] message_text = ' '.join(message_text) parsed = TextBlob(message_text).tags for p in parsed: if p[1] in POS_to_accept: if p[1] != 'CC': # if any POS tags are in POS_to_accept and not a CC, then append it to the keywords keywords.append(p[0]) else: # if any POS tags are not in POS_to_accept, then don't return any keywords return None if len(keywords) >= num_words_thresh: return None if len(keywords) > 0: return ' '.join(keywords) return None def make_nyt_response_templates(self, nyt_response): template_elements = [] for nyt in nyt_response: if nyt.get("image_url"): nyt_image_url = nyt["image_url"] else: nyt_image_url = None if nyt["abstract"] is None: # Facebook requires that the subtitle is not None nyt["abstract"] = nyt["title"] share_button = self.BOT.create_button(button_type=ButtonType.SHARE.value) template_elements.append( self.BOT.create_generic_template_element( element_title=nyt["title"], element_item_url=nyt["web_url"], element_image_url=nyt_image_url, element_subtitle=nyt["abstract"], element_buttons=[share_button] ) ) return template_elements def send_trending_articles(self, recipient_id): #call return_trending_list function for intent = latest nyt_response = self.NYT_API.return_trending_list() template_elements = self.make_nyt_response_templates(nyt_response) if len(template_elements) > 0: response = self.BOT.send_text_message(recipient_id, "Here's the latest on climate change:") response = self.BOT.send_generic_payload_message(recipient_id, elements=template_elements) return response # no trending articles were returned! response = self.BOT.send_text_message(recipient_id, u"Sorry, couldn't find anything trending in climate change today. Please check back tomorrow! \U0001f30e") return response def send_cannot_compute_helper_callback(self, recipient_id): # help_button = self.BOT.create_button( # button_type=ButtonType.POSTBACK.value, title="Help", # payload="HELP_POSTBACK" # ) # response = self.BOT.send_button_payload_message( # recipient_id, # button_title=bot_response_text.help_button_title, # buttons=[help_button] # ) max_int = len(bot_response_text.help_button_title) - 1 idx = random.randint(0, max_int) response = self.BOT.send_text_message(recipient_id, bot_response_text.help_button_title[idx]) return response def send_welcome_message(self, recipient_id): """ Sends the user different welcome messages depending on whether they exist in our database or not """ user_dict = self.MONGO.db.users.find_one({'recipient_id': recipient_id}) if user_dict is None: # The first welcome_message is a template welcome_message = self.BOT.create_generic_payload_message( recipient_id, attachment=bot_response_text.welcome_messages_new_user[0]) response = self.BOT._send(welcome_message) # The rest of the welcome_messages are plain text for j in xrange(1, len(bot_response_text.welcome_messages_new_user)): response = self.BOT.send_text_message( recipient_id, bot_response_text.welcome_messages_new_user[j] ) else: response = self.BOT.send_text_message( recipient_id, bot_response_text.welcome_back % user_dict["user_vars"]["first_name"] ) return response
from wit import Wit import json try: with open("config.json") as f : access_token = json.load(f)['witai'] except: print("open config.json error") def send(request, response): print(response['text']) actions = { 'send': send, } client = Wit(access_token=access_token, actions=actions) resp = client.message('Turn on the light in my bedroom') print('Yay, got Wit.ai response: ' + str(resp))