def get_response(user_input): global database, resource, turn_id, time, wizard, socket global TemplateLib, TopicLib, TreeState, Template, connection, filepointer,engaged_input, topic_id filepointer.write('turn_id: ' + str(turn_id) + '\n') turn_id = turn_id+1 filepointer.write('time:' + str(datetime.datetime.now())+ '\n') filepointer.write('user_input:' + user_input + '\n') relavance, answer = Control.FindCandidate(database, resource, user_input) filepointer.write('relevance: ' + str(relavance)+ '\n') filepointer.write('RetrievedAnswer: ' + str(answer) + '\n') #connection.send('input engagement') #global connection, address #Log('before get response') if wizard is 1: if connection is None: #Log('I asm here') serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Log('serversocket') serversocket.bind(('localhost', 13011)) serversocket.listen(5) connection, address = serversocket.accept() print 'connection established' print connection connection.send('ready') engagement = connection.recv(64) #engagement = raw_input('What is the engagement state of the user of this turn?, score 1-5. ') #engagement = 2 elif wizard is 2: # this is taking the automatic engagement computed as the engagement input print 'Sending request' socket.send("ready\0") engagement = socket.recv() #print engagement #print("Received reply [ %s ]" % message) else:# this is random generating engagement for testing. engagement = random.choice('12345') print engagement filepointer.write('engagement: ' + engagement + '\n') if int(engagement)>3: engaged_input.append(user_input) state = Control.SelectState(relavance, int(engagement), TreeState,engaged_input) filepointer.write('State:' + str(state['name']) + '\n') Log('DM STATE is [ %s ]' %(state)) print 'state:', state['name'] print "candidate answer ", answer #relavance, unicodedata.normalize('NFKD',answer).encode('ascii','ignore')#answer #make an exception to back_state. output,topic_id, engagement_input = NLG.FillTemplate(TemplateLib, TopicLib, Template[state['name']],topic_id, engaged_input, answer) if isinstance(output, unicode): output = unicodedata.normalize('NFKD',output).encode('ascii','ignore') Log('OUTPUT is [ %s ]' %(output)) filepointer.write('TickTockResponse:' + output + '\n') print "end response generation ==================" filepointer.flush() #fileout = open('input_response_history.txt', 'a') #fileout.write(str(user_input) + '\n') #fileout.write(str(output) + '\n') #fileout.close() return output
def get_response(user_input): global database, resource global TemplateLib, TopicLib, TreeState, Template relavance, answer = Control.FindCandidate(database, resource, user_input) state = Control.SelectState(relavance, TreeState) Log('DM STATE is [ %s ]' %(state)) print 'state:', state['name'] print "candidate answer ", relavance, answer output = NLG.FillTemplate(TemplateLib, TopicLib, Template[state['name']], answer) Log('OUTPUT is [ %s ]' %(output)) fileout = open('input_response_history.txt', 'a') fileout.write(str(user_input) + '\n') fileout.write(str(output) + '\n') fileout.close() return output
def get_response(user_input): global database, resource global TemplateLib, TopicLib, TreeState, Template engagement = sys.stdin relavance, answer = Control.FindCandidate(database, resource, user_input) state = Control.SelectState(relavance, engagement, TreeState) Log('DM STATE is [ %s ]' % (state)) print 'state:', state['name'] print "candidate answer ", relavance, answer output = NLG.FillTemplate(TemplateLib, TopicLib, Template[state['name']], answer) #output2 = unicodedata.normalize('NFKD',output).encode('ascii','ignore') Log('OUTPUT is [ %s ]' % (output)) #fileout = open('input_response_history.txt', 'a') #fileout.write(str(user_input) + '\n') #fileout.write(str(output) + '\n') #fileout.close() return output
def get_response(policy_mode, user_input, user_id, previous_history, theme, oov_mode, name_entity_mode, short_answer_mode, anaphora_mode, word2vec_ranking_mode, tfidf_mode=0, force_strategy=None): #oov_mode is used to switch on and off if we ask the unkonwn words #name_entity_mode is used to switch on and off if we will detect the name_entity and use the wiki api to get some knowledge expansion. global database, resource, turn_id, time, wizard, socket, isAlltag, tfidfmodel, tfidfdict global TemplateLib, TopicLib, TreeState, Template, connection, filepointer, engaged_input, topic_id, init_id, joke_id, dictionary_value, model, table_state_strategy print 'user_input: ' + user_input print 'user_id:' + user_id strategy = [] filepointer.write('turn_id: ' + str(turn_id) + '\n') filepointer.write('user_id: ' + user_id + '\n') turn_id = turn_id + 1 filepointer.write('time:' + str(datetime.datetime.now()) + '\n') filepointer.write('user_input:' + user_input + '\n') #print 'tfidfmodel: ' #print tfidfmodel #print 'tfidfdict: ' #print tfidfdict if user_id in previous_history.keys(): history = previous_history[user_id] print 'we are here in history' print history else: previous_history = {} theme[user_id] = random.choice(TopicLib) output = 'Hello, I really like ' + theme[ user_id] + '. How about we talk about ' + theme[user_id] previous_history[user_id] = [user_input, output] print 'previous history' print previous_history return theme, 'new', output, previous_history, 0 if tfidf_mode is 1: #print '====history before response=====' #print history relavance, answer, anaphora_trigger, word2vec = Control.FindCandidate( model, database, resource, user_input, isAlltag, history, anaphora_mode, word2vec_ranking_mode, tfidfmodel=tfidfmodel, tfidfdict=tfidfdict) #print '====history after response=====' #print history else: relavance, answer, anaphora_trigger, word2vec = Control.FindCandidate( model, database, resource, user_input, isAlltag, history, anaphora_mode, word2vec_ranking_mode) filepointer.write('relevance: ' + str(relavance) + '\n') filepointer.write('RetrievedAnswer: ' + str(answer) + '\n') #print 'anaphora trigger' #print anaphora_trigger if anaphora_trigger is 1: strategy.append('anaphora') if engagement_mode is 1: if wizard is 1: if connection is None: #Log('I asm here') serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Log('serversocket') serversocket.bind(('localhost', 13011)) serversocket.listen(5) connection, address = serversocket.accept() print 'connection established' print connection connection.send('ready') engagement = connection.recv(64) elif wizard is 2: # this is taking the automatic engagement computed as the engagement input print 'Sending request' socket.send("ready\0") engagement = socket.recv() #print engagement #print("Received reply [ %s ]" % message) else: # this is random generating engagement for testing. engagement = random.choice('12345') print engagement filepointer.write('engagement: ' + engagement + '\n') if int(engagement) > 3: engaged_input.append(user_input) state = Control.SelectState_rel(relavance, int(engagement), TreeState, engaged_input) else: state = Control.SelectState_rel_only(policy_mode, table_state_strategy, relavance, user_input, history, TreeState, force_strategy=force_strategy) strategy.append(state['name']) output, topic_id, init_id, joke_id, engagement_input = NLG.FillTemplate( theme[user_id], TemplateLib, TopicLib, Template[state['name']], topic_id, init_id, joke_id, engaged_input, answer) if isinstance(output, unicode): output = unicodedata.normalize('NFKD', output).encode('ascii', 'ignore') if state['name'] is not 'continue' and force_strategy == None: if oov_mode is 1: is_chosen, output_oov = oov.oov_out(user_input, dictionary_value) if is_chosen is 1: print 'oov is triggerd' strategy.append('oov') output = output_oov if name_entity_mode is 1: name_entity_list = name_entity.name_entity_detection(user_input) if name_entity_list: name_entity_disp = name_entity.NE_kb(name_entity_list) if name_entity_disp: print 'name entity is triggerd' strategy.append('name_entity') output = name_entity.name_entity_generation( name_entity_list, name_entity_disp) if short_answer_mode is 1: if (user_input.find(' ') == -1): print 'it is a single word' word_list = nltk.word_tokenize(user_input) for word in word_list: if word not in dictionary_value: print user_input not in dictionary_value print 'short answer is triggered' strategy.append('short_answer') output = 'Will you be serious, and say something in a complete sentence?' if output.find("Piers") is not -1: output = output.replace("Piers", "dear") filepointer.write('TickTockResponse:' + output + '\n') if user_id in previous_history.keys(): previous_history[user_id].append(user_input) previous_history[user_id].append(output) else: print "we are in the else user_id" previous_history[user_id] = [user_input, output] if output[-2:-1] == ' ': output = output[0:-2] + output[-1] if strategy[-1] == 'switch': if output.split()[-1] == 'games': theme[user_id] = 'board games' else: theme[user_id] = output.split()[-1] print 'strategy' + str(strategy) print 'response: ' + output print "end response generation ==================" print "===========================================" filepointer.flush() #print "this is previous history" #print previous_history return theme, strategy, output, previous_history, word2vec
def get_response(fix_strategy, policy_mode, user_input, user_id, previous_history, theme, oov_mode=1, name_entity_mode=1, short_answer_mode=1, anaphora_mode=1, word2vec_ranking_mode=1, tfidf_mode=1): global database, resource, turn_id, time, wizard, socket, isAlltag, tfidfmodel, tfidfdict, turn_id, engagement_mode, engaged_input, isAlltag, wizard global TemplateLib, TopicLib, TreeState, Template, connection, filepointer, engaged_input, init_id, joke_id, more_id, dictionary_value, model, table_state_strategy, q_table #print 'user_input: ' + user_input #print 'user_id:' + user_id strategy = [] filepointer.write('turn_id: ' + str(turn_id) + '\n') filepointer.write('user_id: ' + user_id + '\n') turn_id = turn_id + 1 filepointer.write('time:' + str(datetime.datetime.now()) + '\n') filepointer.write('user_input:' + user_input + '\n') if fix_strategy is None: if user_id in previous_history.keys(): history = previous_history[user_id] else: if user_id not in theme.keys(): theme[user_id] = random.choice(TopicLib) output = 'Hello, I really like ' + theme[ user_id] + '. How about we talk about ' + theme[user_id] previous_history[user_id] = [user_input, output] return 'new', output, 0 if tfidf_mode is 1: relavance, answer, anaphora_trigger, word2vec = Control.FindCandidate( model, database, resource, user_input, isAlltag, history, anaphora_mode, word2vec_ranking_mode, tfidfmodel=tfidfmodel, tfidfdict=tfidfdict) else: relavance, answer, anaphora_trigger, word2vec = Control.FindCandidate( model, database, resource, user_input, isAlltag, history, anaphora_mode, word2vec_ranking_mode) filepointer.write('relevance: ' + str(relavance) + '\n') filepointer.write('RetrievedAnswer: ' + str(answer) + '\n') if anaphora_trigger is 1: strategy.append('anaphora') if engagement_mode is 1: if wizard is 1: if connection is None: serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(('localhost', 13011)) serversocket.listen(5) connection, address = serversocket.accept() print 'connection established' print connection connection.send('ready') engagement = connection.recv(64) elif wizard is 2: # this is taking the automatic engagement computed as the engagement input print 'Sending request' socket.send("ready\0") engagement = socket.recv() #print engagement` #print("Received reply [ %s ]" % message) else: # this is random generating engagement for testing. engagement = random.choice('12345') print engagement filepointer.write('engagement: ' + engagement + '\n') if int(engagement) > 3: engaged_input.append(user_input) state = Control.SelectState_rel(relavance, int(engagement), TreeState, engaged_input) else: state, output, theme_new, init_id, joke_id, more_id = Control.SelectState_rel_only( table_state_strategy, relavance, user_input, history, TreeState, dictionary_value, oov_mode, name_entity_mode, short_answer_mode, policy_mode, q_table, theme[user_id], TemplateLib, TopicLib, Template, init_id, joke_id, more_id) # print 'old_theme' + theme[user_id] theme[user_id] = theme_new # print 'new_theme' + theme_new else: state = {'name': fix_strategy} output = None answer = '' word2vec = 0 #print strategy #print state['name'] strategy.append(state['name']) if output == None: theme[ user_id], output, init_id, joke_id, more_id, engagement_input = NLG.FillTemplate( theme[user_id], TemplateLib, TopicLib, Template[state['name']], init_id, joke_id, more_id, engaged_input, answer, output) #print theme if isinstance(output, unicode): output = unicodedata.normalize('NFKD', output).encode('ascii', 'ignore') if output.find("Piers") is not -1: output = output.replace("Piers", "dear") filepointer.write('TickTockResponse:' + output + '\n') if user_id in previous_history.keys(): previous_history[user_id].append(user_input) previous_history[user_id].append(output) else: #if fix_strategy is None: print "we are in the else user_id" previous_history[user_id] = [user_input, output] #if output[-2:-1]==' ': # output = output[0:-2] +output[-1] #print 'strategy' + str(strategy) #print 'response: ' + output #print "end response generation ==================" #print "===========================================" filepointer.flush() #print "this is previous history" #print previous_history print 'init_id' + str(init_id) return strategy, output, word2vec #,dictionary_value