def get_features(datasetFile:str): paragraphs = [] for para in datasetFile.split('\n'): if (len(para.strip()) > 0): paragraphs.append(para.strip()) # Processing Paragraphs drm = DRM(paragraphs, True, True) questions = ['experience','size'] answers = [] for i in TITLES: for j in TITLES[i]: userQuery = j pq = PQ(userQuery, True, False, True) response = drm.query(pq) if response != ' ': break else: continue break else: i = '' answers.append(i) for i in questions: userQuery = i pq = PQ(userQuery, True, False, True) # Get Response From Bot response = drm.query(pq) answers.append(response) return answers
def get_bot_response(): userText = request.args.get('msg') # print(userText) res = '' # try: datasetFile = open("./dataset/basic.txt", "r") except FileNotFoundError: # print("Bot> Oops! I am unable to locate \"" + datasetName + "\"") # add email return exit() paragraphs = [] for para in datasetFile.readlines(): if (len(para.strip()) > 0): paragraphs.append(para.strip()) drm = DRM(paragraphs, True, True) # greetPattern = re.compile("^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$", re.IGNORECASE) userQuery = userText if (not len(userQuery) > 0): # print(len(userQuery)) print("You need to ask something") # elif greetPattern.findall(userQuery): # response = "Hello!" else: pq = PQ(userQuery, True, False, True) response = drm.query(pq) res = ':' + response return res
def question(): userText = request.args.get('msg') # try: questionFile = open("./dataset/question.txt", "r") except FileNotFoundError: # print("Bot> Oops! I am unable to locate \"" + datasetName + "\"") # add email return function exit() paragraphs = [] for para in questionFile.readlines(): if (len(para.strip()) > 0): paragraphs.append(para.strip()) qrm = QRM(paragraphs, True, True) # greetPattern = re.compile("^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$", re.IGNORECASE) userQuery = userText pq = PQ(userQuery, True, False, True) response = qrm.query(pq) ques = response return ques
def main(datasetName, use_word_embeddings): print("Bot> Please wait, while I am loading my dependencies") # Loading Dataset try: datasetFile = open(datasetName,"r", encoding="utf-8") except FileNotFoundError: print("Bot> Oops! I am unable to locate \"" + datasetName + "\"") exit() # Retrieving paragraphs : Assumption is that each paragraph in dataset is # separated by new line character paragraphs = [] for para in datasetFile.readlines(): if(len(para.strip()) > 0): paragraphs.append(para.strip()) # Loading Model modelName = datasetName.replace("dataset", "models").replace(".txt", ".h5") model = Word2Vec.load(modelName) # Processing Paragraphs if use_word_embeddings: drm = DRMWM(paragraphs, model, True, True) else: drm = DRM(paragraphs, True, True) print("Bot> Hey! I am ready. Ask me factoid based questions only :P") print("Bot> You can say me Bye anytime you want") # Greet Pattern greetPattern = re.compile("^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$",re.IGNORECASE) isActive = True while isActive: userQuery = input("You> ") if(not len(userQuery)>0): print("Bot> You need to ask something") elif greetPattern.findall(userQuery): response = "Hello!" elif userQuery.strip().lower() == "bye": response = "Bye Bye!" isActive = False else: # Proocess Question if use_word_embeddings: pq = PQWM(userQuery, model, True,False,True) else: pq = PQ(userQuery,True,False,True) # Get Response From Bot response =drm.query(pq) print("Bot>",response)
paragraphs.append(para.strip()) # Processing Paragraphs drm = DRM(paragraphs, True, True) print("Bot> Hey! I am ready. Ask me factoid based questions only :P") print("Bot> You can say me Bye anytime you want") # Greet Pattern greetPattern = re.compile( "^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$", re.IGNORECASE) isActive = True while isActive: userQuery = input("You> ") if (not len(userQuery) > 0): print("Bot> You need to ask something") elif greetPattern.findall(userQuery): response = "Hello!" elif userQuery.strip().lower() == "bye": response = "Bye Bye!" isActive = False else: # Proocess Question pq = PQ(userQuery, True, False, True) # Get Response From Bot response = drm.query(pq) print("Bot>", response)
def main(): print("Bot> Please wait, Loading preferences from Home Base") #trainer() name = recognise() #datafile=input("Input Data File Name : ") try: datasetName = "dataset/" + name + ".txt" print(datasetName) except FileNotFoundError: print("Bot> Oops! I am unable to locate \"" + datasetName + "\"") print("Bot> Hey! I am ready. Ask me factoid based questions only :P") print("Bot> You can say me Bye anytime you want") greetPattern = re.compile( "^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$", re.IGNORECASE) isActive = True response = "Hello User" while isActive: name = recognise() if (name != '0'): datasetName = "dataset/" + name + ".txt" print(datasetName) try: datasetFile = open(datasetName, "r") except FileNotFoundError: print("Bot> Oops! I am unable to locate \"" + datasetName + "\"") datasetFile = open("dataset/1.txt", "r") #exit() paragraphs = [] try: for para in datasetFile.readlines(): if (len(para.strip()) > 0): paragraphs.append(para.strip()) drm = DRM(paragraphs, True, True) except: print("Bot> Oops! Error in reading dataset") continue userQuery = input("You> ") if (not len(userQuery) > 0): print("Bot> You need to ask something") elif greetPattern.findall(userQuery): response = "Hello!" elif userQuery.strip().lower() == "bye": response = "Bye Bye!" isActive = False elif userQuery.strip().lower() == "train faces": trainer() elif userQuery.strip().lower() == "new": detect() elif userQuery.strip().lower() == "train": trainer() elif userQuery.strip().lower() == "new user": detect() trainer() elif userQuery.strip().lower() == "detect faces": recognise1() elif userQuery.strip().lower() == "speech": try: userQuery = get_audio() except: print("Error Recognizing Audio") continue print("User Said : ", userQuery) if greetPattern.findall(userQuery): response = "Hello!" else: print(name, " said : ", userQuery) pq = PQ(userQuery, True, False, True) response = drm.query(pq) else: pq = PQ(userQuery, True, False, True) response = drm.query(pq) print("Bot>", response)
def reply(): greetPattern = re.compile( "^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$", re.IGNORECASE) receive = request.form['msg'] # receive = request.data userQuery = receive print(userQuery) # userQuery="Hi" print("sess: ", session) # userQuery = "Hi" response = {} response['text'] = [] # session['filecheck'] = False # drm = None if (not len(userQuery) > 0): print("Bot> You need to ask something") elif greetPattern.findall(userQuery): response['text'].append("Hello!") response['text'].append( "You can now send me your topic in the format Topic_Name.txt") # res = jsonify({'text': [response,response2]}) # print(res) # return res elif userQuery.strip().lower() == "bye": response['text'].append("Bye Bye!") if 'topic' in session: del session['topic'] elif userQuery.split('.')[-1:] == ['txt']: response['text'].append( "Thanks for giving the topic, let me load the required files") topic = str(userQuery) response['topic'] = topic # drm = retrievePara(path) session['topic'] = topic print("now: ", session) elif 'topic' in session: # Proocess Question topic = session['topic'] sc = SC() pq = PQ(userQuery, True, False, True) sq = pq.searchQuery sq = list( filter(lambda x: x, map(lambda x: re.sub(r'[^A-Za-z]', '', x), sq))) print("\nsq: ", sq) corrected = [] # self.stopWords = stopwords.words("english") for word in sq: # if word in stopwords.words("english"): # continue poss = sc.correction(word, topic) if word != poss: corrected.append(poss) else: corrected.append(word) print("corrected: ", corrected) if sq != corrected: corrected = ' '.join(corrected) print("\n\nDid you mean {}?".format(corrected)) response['text'].append("Did you mean: {}?".format(corrected)) else: # Get Response From Bot print("session topic: ", session['topic']) path = 'dataset/' + topic drm = retrievePara(path) if type(drm) == str: response['text'].append(drm) del session['topic'] else: response['text'].append(drm.query(pq)) else: response['text'].append( "Please provide a relevant topic before you start asking me a question." ) if 'topic' in session: print("topic is in session") return jsonify( response) # execute.decode_line(sess, model, enc_vocab, rev_dec_vocab
def response(): print("session at start: ", session) json_data = request.get_json() session['id'] = "".join(json_data["session"].split('/')[-1:]) id = session['id'] print("session id: ", session['id']) print("id in session: ", 'id' in session) intent = json_data["queryResult"]["intent"]["displayName"] response = {} response['fulfillmentText'] = [] response["outputContexts"] = [] id_context = { "name": "764fc47b-8f33-fae5-e75b-472d135ffbfc", "lifespanCount": 550, "parameters": { "id.original": "4", "id": "4" } } preID = "projects/chatbot-6fb36/agent/sessions/" postID = "/contexts/id" quickReply = { "platform": "ACTIONS_ON_GOOGLE", "quickReplies": { "title": "Choose one from the list of topics: ", "quickReplies": [ ] } } text = { "text": { "text": "" } } if intent == "send_id": session['id'] = "".join(json_data["session"].split('/')[-1:]) response['fulfillmentText'].append(session['id']) id_context["name"] = preID + session['id'] + postID id_context["parameters"]["id.original"] = session["id"] id_context["parameters"]["id"] = session["id"] response["outputContexts"].append(id_context) quickReply["quickReplies"]["title"] = "Click if you want to view all topics." quickReply["quickReplies"]["quickReplies"].append("show topics") response["fulfillmentMessages"] = [quickReply] return jsonify(response) # print("intent: ", intent) if intent == "end_session": store =[] for entry in session: store.append(str(entry)) for ent in store: del session[ent] print("empty session: ", session) response['fulfillmentText'].append("Thank you for talking to me, hope you found what you were looking for. Until next time!") return jsonify(response) elif intent == "Find ID": response['fulfillmentText']= "Your id is {}".format(id) text["text"]["text"] = ["Your id is {}".format(id)] response['fulfillmentMessages']=[text] id_context["name"] = preID + session['id'] + postID id_context["parameters"]["id.original"] = session["id"] id_context["parameters"]["id"] = session["id"] response["outputContexts"].append(id_context) return jsonify(response) elif intent == "get_topics": allFiles = getTopics(id) for top in allFiles: quickReply["quickReplies"]['quickReplies'].append("select {}".format(top)) params = json_data["queryResult"]["parameters"] if params["topictype"] == "": quickReply["quickReplies"]["title"] = "Please specify the type of topics you want to view. " topicTypes = ["Old", "Recent", "Completed", "All"] quickReply["quickReplies"]["quickReplies"] = topicTypes response["fulfillmentMessages"]= [quickReply] response['fulfillmentText'] = "What type of topics do you want to view? (Old, New, Completed, All)" # text["text"]["text"] = response["fulfillmentText"] # response["fulfillmentMessages"].append(text) elif params["id"] == "": quickReply["quickReplies"]["title"] = "ID is required before view respective topics. Click to send ID" quickReply["quickReplies"]["quickReplies"] = ["send ID"] response['fulfillmentMessages'] = [quickReply] response['fulfillmentText'].append("You have not entered your ID yet, please do so.") text["text"]["text"] = response["fulfillmentText"] response["fulfillmentMessages"].append(text) else: response["fulfillmentMessages"] = [quickReply] response['fulfillmentText'].append("Here are all your topics: pick one. \n{}".format(" ".join(allFiles))) text["text"]["text"] = response["fulfillmentText"] response["fulfillmentMessages"].append(text) # del response['fulfillmentText'] print("sess: ", session) return jsonify(response) elif intent == "storeID" or intent == "Retain Id": print("store id intent") contexts = json_data['queryResult']['outputContexts'] for entry in contexts: if 'id' in entry['parameters']: id = entry['parameters']['id'] session['id'] = id print("Just received ID: ", id) break response['fulfillmentText'] = "ID has been stored." return jsonify(response) print(request.get_json()['queryResult']['outputContexts']) # receive = request.form.get('msg') contexts = json_data['queryResult']['outputContexts'] for entry in contexts: print(entry) if 'topic_name' in entry['parameters']: topic = entry['parameters']['topic_name'] print("topic: ", topic) session['topic'] = topic # receive = request.data userQuery = json_data['queryResult']['queryText'] print(userQuery) # userQuery="Hi" print("sess: ", session) # userQuery = "Hi" # session['filecheck'] = False # drm = None if (userQuery is None): print("Bot> You need to ask something") response['fulfillmentText'].append("Just trying") return jsonify(response) elif 'topic' in session: # Proocess Question topic = session['topic'] print("session topic: ", topic) path = 'dataset/' + topic drm = retrievePara(path) if type(drm) == str: response['fulfillmentText'].append(drm) del session['topic'] else: # Input spellcheck code from TestCode here if topic specific spellcheck needs to be implemented # Get Response From Bot pq = PQ(userQuery, True, False, True) response['fulfillmentText'].append(drm.query(pq)) if 'spellcheck' in session: del session['spellcheck'] else: response['fulfillmentText'].append("Please provide a relevant topic before you start asking me a question.") if 'topic' in session: print("topic is in session") # response['fullfillmentText'] = response['fulfillmentText'] response['source'] = 'ques answer' return jsonify(response)
def main(self): if len(sys.argv) > 1: print("Bot> Parameters are not needed!") print("Bot> Please! Rerun me using following syntax") print("\t\t$ python3 main.py ") print("Bot> Thanks! Bye") exit() print( "Bot> Hey! My name is Naubot and I am ready to answer your queries." ) print( "Bot> I was designed to give you the best answers to your questions based on what is stored in my database. You can start asking your\ questions or just say Hi!") print("Bot> To quit, you can say Bye anytime you want.") # Greet Pattern greetPattern = re.compile( "^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$", re.IGNORECASE) print( "Enter 1 to Set question and answers & 2 to interact with the chatbot to retrieve answers & anything else to quit" ) user_input = int(input("You> ")) if user_input == 1: while True: print("Bot> Enter a question or 'quit' to exit") user_q = input("You> ") if (not len(user_q) > 0): print("Bot> Enter a valid question") else: if user_q == "quit": break else: print("Bot> Enter a response for this question") user_a = input("You> ") if (not len(user_a) > 0): print( "Bot> Enter a valid response for this question" ) else: p = PQ(question=user_q) a = ARM(question=user_q) q_type = p.determineAnswerType(question=user_q) a_type = p.determineQuestionType(question=user_q) tf_count = a.getTermFrequencyCount(question=user_q) Add_row(user_q, user_a, q_type, a_type, tf_count) elif user_input == 2: question = [] query = """ SELECT question FROM Chat """ r = self.conn.execute(query) for row in r: question.append(row[0]) arm = ARM(question=question) while True: userQuery = input("You> ") if (not len(userQuery) > 0): print("Bot> You need to ask something") elif greetPattern.findall(userQuery): response = "Hello!" elif userQuery.strip().lower() == "bye": response = "Bye Bye!" isActive = False else: # Process Question pq = PQ(userQuery, True, False, True) # Get Response From Bot response = arm.query(pq) print("Bot>", response) else: print("Quitting...")