Ejemplo n.º 1
0
def SelectState_rel_only(str_rule, relavance, user_input, pre_history, TreeState,dictionary_value,oov_mode,name_entity_mode,short_answer_mode,policy_mode, q_table, theme, TemplateLib,TopicLib,Template,init_id,joke_id,more_id):
    branch_idx = TreeState.keys()[0]
    branch = TreeState[branch_idx]['node']
    if user_input in pre_history:
        return {'name':'not_repeat'},'You already said that!',theme, init_id, joke_id, more_id

    if relavance >= branch['threshold_relavance']:
        return TreeState[branch_idx][True][0],None,theme, init_id, joke_id, more_id
 # only use the continue, don't expand

    else:
        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'
                        output_oov = name_entity.name_entity_generation(name_entity_list, name_entity_disp)
                        #if output_oov != previous_history[user_id][-1]:
                        return {'name':'name_entity'},output_oov,theme, init_id, joke_id, more_id

        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?'
                            return {'name': 'short_answer'},output,theme, init_id, joke_id, more_id

        if oov_mode is 1:
	    chosen, dictionary_value,output_oov = oov.oov_out(user_input,dictionary_value)
            if chosen is 1:
			    print 'oov is triggerd'
			    output = output_oov
                            return {'name': 'oov'},output_oov,theme,init_id, joke_id, more_id


        if policy_mode ==0 or  pre_history==None:
		    return random.choice(TreeState[branch_idx][False][0:-1]),None, theme, init_id, joke_id, more_id
# don't choose the last leave, go back
        curr_1 = sentiment_vader.get_sentiment(user_input)
        curr_2 = sentiment_vader.get_sentiment(pre_history[-1])
        curr_3 = sentiment_vader.get_sentiment(pre_history[-2])

        if policy_mode ==1 and pre_history is not None:
            strategy = str_rule[(curr_1,curr_2,curr_3)]
            return {'name':strategy},None,theme,init_id,joke_id, more_id
        if policy_mode == 'rl':
            turn_id = len(pre_history)/2
            if turn_id >11:
                turn_id ==11
            theme_new, action, output, init_id, joke_id, more_id = rl_test.rl_test(curr_1,curr_2,curr_3,turn_id,q_table, theme,TemplateLib,TopicLib,Template, init_id,joke_id,more_id)
            return {'name':action}, output,theme_new, init_id, joke_id, more_id
    raise Exception
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,more_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,more_id, engagement_input = NLG.FillTemplate(theme[user_id], TemplateLib, TopicLib, Template[state['name']],topic_id, init_id,joke_id,more_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, dictionary_value,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]
        if user_input in previous_history[user_id][:-2]:
            output = "You already said that!"
        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 #,dictionary_value
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
Ejemplo n.º 4
0
def SelectState_rel_only(str_rule, relavance, user_input, pre_history,
                         TreeState, dictionary_value, oov_mode,
                         name_entity_mode, short_answer_mode, policy_mode,
                         q_table, theme, TemplateLib, TopicLib, Template,
                         init_id, joke_id, more_id):
    branch_idx = TreeState.keys()[0]
    branch = TreeState[branch_idx]['node']
    if user_input in pre_history:
        return {
            'name': 'not_repeat'
        }, 'You already said that!', theme, init_id, joke_id, more_id

    if relavance >= branch['threshold_relavance']:
        return TreeState[branch_idx][True][
            0], None, theme, init_id, joke_id, more_id
# only use the continue, don't expand

    else:
        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'
                    output_oov = name_entity.name_entity_generation(
                        name_entity_list, name_entity_disp)
                    #if output_oov != previous_history[user_id][-1]:
                    return {
                        'name': 'name_entity'
                    }, output_oov, theme, init_id, joke_id, more_id

        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?'
                        return {
                            'name': 'short_answer'
                        }, output, theme, init_id, joke_id, more_id

        if oov_mode is 1:
            chosen, dictionary_value, output_oov = oov.oov_out(
                user_input, dictionary_value)
            if chosen is 1:
                print 'oov is triggerd'
                output = output_oov
                return {
                    'name': 'oov'
                }, output_oov, theme, init_id, joke_id, more_id

        if policy_mode == 0 or pre_history == None:
            return random.choice(
                TreeState[branch_idx][False]
                [0:-1]), None, theme, init_id, joke_id, more_id


# don't choose the last leave, go back
        curr_1 = sentiment_vader.get_sentiment(user_input)
        curr_2 = sentiment_vader.get_sentiment(pre_history[-1])
        curr_3 = sentiment_vader.get_sentiment(pre_history[-2])

        if policy_mode == 1 and pre_history is not None:
            strategy = str_rule[(curr_1, curr_2, curr_3)]
            return {'name': strategy}, None, theme, init_id, joke_id, more_id
        if policy_mode == 'rl':
            turn_id = len(pre_history) / 2
            if turn_id > 11:
                turn_id == 11
            theme_new, action, output, init_id, joke_id, more_id = rl_test.rl_test(
                curr_1, curr_2, curr_3, turn_id, q_table, theme, TemplateLib,
                TopicLib, Template, init_id, joke_id, more_id)
            return {
                'name': action
            }, output, theme_new, init_id, joke_id, more_id
    raise Exception