import os import json import string import numpy as np from datetime import date, datetime from sklearn.metrics.pairwise import cosine_similarity from src.processors.nlp.natural_language_understanding import get_intent_bert_avg from src.processors.nlp.natural_language_processing import preprocess_bn from src.processors.nlp.natural_language_generation import respond_greet_bn from definitions import ROOT_DIR history=[] intent=-1 turn=-1 eoc='' #end of conversations similarity_threshold=0.75 intent_threshold=0.80 def reset_chatbot(): """ This function is used for reseting the chatbot to the initial mode. """ global turn, history turn = -1 history.clear() def process_input_bn(user_input, model): """ This function is used for processing user input in Bangla. First the databases are loaded. The core structure is based on turn counts. A turn consists of one question from the user and one response from the chatbot. For the first turn, intent is identified. After intent identification, the response of the first turn of corresponding intent in picked from the database. For rest of the turns, chatbot searchs if the user query is valid or not. For valid query, it picks response of corresponding turn and intent. For invalid query, it asks for clearification. If predefined conversation length exceeds, the chatbot is reset. If it requires any verification and information retrieval task, it call anoter function for verification and information retrieval. Pseudocode ----------------------------------------------- Preprocess user input. Load databases. If first turn: Identify intent using another function. If intent is invalid: Show intent suggestions. Else: Return response of corresponding turn and intent Else: If conversation length exceeds: Show intent suggestions. Else if verification or retrieval is required: Retrieve information using another functionsss. Else: If user query is valid: Return response of corresponding turn and intent Else: Ask for clarification. """ global turn, history, intent, similarity_threshold, intent_threshold turn+=1 processed_input=preprocess_bn(user_input) user_kb_path=os.path.join(ROOT_DIR,'static','knowledge_base','user_queries_v1_bn.json') with open(user_kb_path, 'r') as u_f: user_kb_bn=json.load(u_f) chatbot_kb_path=os.path.join(ROOT_DIR,'static','knowledge_base','chatbot_queries_v1_bn.json') with open(chatbot_kb_path, 'r') as c_f: chatbot_kb_bn=json.load(c_f) if turn==0: # for the first turn intent, scores=get_intent_bert_avg(user_input, user_kb_bn, model) intent_score=scores[intent] print('Intent: ', intent) # print('Similarity score: ', intent_score) print('Similarity Scores: ', scores) if intent==-1 or intent_score' in response: response=respond_greet_bn(response) history.append('chatbot_'+response) else: history.append('user_'+user_input) if turn==len(user_kb_bn[str(intent)]): # if conversation length exceeds response='সরি, তোমার কথা আমি বুঝতে পারছিনা। আরেকবার বলো।' history.append('chatbot_'+response) turn=-1 history.clear() else: idx=-1 mx_score=0 user_input_emb=model.encode(processed_input) user_input_emb=np.reshape(user_input_emb, (1,user_input_emb.shape[0])) for candidate in user_kb_bn[str(intent)][str(turn)]: can_emb=model.encode(candidate) can_emb=np.reshape(can_emb, (1,can_emb.shape[0])) score=cosine_similarity(user_input_emb, can_emb) if score>mx_score: mx_score=score idx=user_kb_bn[str(intent)][str(turn)].index(candidate) if mx_score>=similarity_threshold: response=chatbot_kb_bn[str(intent)][str(turn)][idx] else: response='সরি, তোমার কথা আমি বুঝতে পারছিনা। আরেকবার বলো।' turn-=1 history.append('chatbot_'+response) if 'backToTurn' in response: _,turn=response.split('_') turn=int(turn[:-1]) response=chatbot_kb_bn[str(intent)][str(turn)][0] history.append('chatbot_'+response) if response.endswith(eoc): if intent==14: submit_complain(history) today=date.today() now=datetime.now() time=now.strftime("%H:%M:%S") history_merged='\n'.join(history) history_merged2=time+'\n'+history_merged+'\n\n' # print(history_merged2) sub_folder=str(today) history_path=os.path.join(ROOT_DIR,'static','history',sub_folder+'.txt') f_bn=open(history_path, 'a+') f_bn.write(history_merged2) f_bn.close() print('Chat history has been saved.') reset_chatbot() return response