Esempio 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
Esempio n. 2
0
def rl_test(sent_1,sent_2,sent_3,turn_id,q_table,theme,TemplateLib,TopicLib,Template,init_id,joke_id,more_id):
    action_list = ['switch','end','more','joke','init']
    state = (sent_1,sent_2,sent_3,turn_id)
    q_list =[]
    q_utt =[]
    for action in action_list:
        theme_new, utt, init_id, joke_id,more_id, engagement_input = NLG.FillTemplate(theme,TemplateLib,TopicLib,Template[action],init_id,joke_id,more_id,0,'','' )
        sent_3 = sentiment_vader.get_sentiment(utt)
        next_state = (sent_1,sent_2,sent_3,turn_id)
        if next_state in q_table.keys():
            q_list.append(q_table[next_state,action])
        else:
            q_list.append(0)
        q_utt.append(utt)
        maxQ = max(q_list)
    count = q_list.index(maxQ)
    if count>1:
        best = [i for i in range(len(action_list)) if q_list[i]==maxQ]
        i = random.choice(best)
    else:
        i = q_list.index(maxQ)
    output = q_utt[i]
    action_selected = action_list[i]
    if action_selected =='init':
        init_id +=1
    elif action_selected == 'joke':
        joke_id +=1
    elif action_selected == 'more':
        more_id +=1
    return theme_new, action_selected,output, init_id,joke_id,more_id
Esempio n. 3
0
def rl_test(sent_1,sent_2,sent_3,turn_id,q_table,theme,TemplateLib,TopicLib,Template,init_id,joke_id,more_id):
    action_list = ['switch','end','more','joke','init']
    state = (sent_1,sent_2,sent_3,turn_id)
    q_list =[]
    q_utt =[]
    for action in action_list:
        theme_new, utt, init_id, joke_id,more_id, engagement_input = nlg.FillTemplate(theme,TemplateLib,TopicLib,Template[action],init_id,joke_id,more_id,0,'','' )
        sent_3 = sentiment_vader.get_sentiment(utt)
        next_state = (sent_1,sent_2,sent_3,turn_id)
        if next_state in q_table.keys():
            q_list.append(q_table[next_state,action])
        else:
            q_list.append(0)
        q_utt.append(utt)
        maxQ = max(q_list)
    count = q_list.index(maxQ)
    if count>1:
        best = [i for i in range(len(action_list)) if q_list[i]==maxQ]
        i = random.choice(best)
    else:
        i = q_list.index(maxQ)
    output = q_utt[i]
    action_selected = action_list[i]
    if action_selected =='init':
        init_id +=1
    elif action_selected == 'joke':
        joke_id +=1
    elif action_selected == 'more':
        more_id +=1
    return theme_new, action_selected,output, init_id,joke_id,more_id
Esempio n. 4
0
def rl_test(sent_1,sent_2,sent_3,turn_id, theme,init_id,init_joke,more_id):
    action_list = ['switch','end','more','joke','init']
    state = (sent_1,sent_2,sent_3,turn_id)
    for action in action_list:
                    theme, utt, init_id, joke_id,more_id, engagement_input = NLG.FillTemplate(theme,TemplateLib,TopicLib,Template[action],init_id,joke_id,more_id,0,'','' )
                    next_sent_3 = sentiment_vader.get_sentiment(utt)
                    next_state = (next_sent_1,next_sent_2,next_sent_3,turn_id+1)
                    q_list.append(q_table[next_state,action])
                    #q_sent.append(next_sent_3)
                    q_utt.append(utt)
                    maxQ = max(q_list)
                count = q_list.index(maxQ)
                if count>1:
                    best = [i for i in range(len(action_list)) if q_list[i]==maxQ]
                    #print best
                    i = random.choice(best)
                else:
                    i = q_list.index(maxQ)
                output = q_tt[i]
                action_selected = action_list[i]
Esempio n. 5
0
import sentiment_online
import sentiment_vader
import pickle

f = open('test_sentiment.txt', 'w')
all_conversation = pickle.load(open('all_conversation.pkl'))
id = 1
for utt in all_conversation:
    id = id + 1
    sent = sentiment_online.get_sentiment(utt)
    sent_vader = sentiment_vader.get_sentiment(utt)
    f.write('utterance:' + utt + '\n')
    f.write('online:' + sent + '\n')
    f.write('vader:' + sent_vader + '\n')
    f.write('\n')
    if id > 1000:
        break
import sys
def most_common(lst):
        return max(set(lst), key=lst.count)

conn = sqlite3.connect('rs_ratings.db')
c = conn.cursor()
c.execute('''SELECT * FROM responses''')
table_num = {}
table_sum = {}
table_entry = {}
tmplist =[]
for item in c.fetchall():
    print item
    #print item[0]
    #break
    turn_1_user_sent = sentiment_vader.get_sentiment(item[0])
    turn_1_tt_sent = sentiment_vader.get_sentiment(item[1])
    turn_0_user_sent = sentiment_vader.get_sentiment(item[7])
    turn_0_tt_sent = sentiment_vader.get_sentiment(item[8])
    turn_str = item[10]
    if item[11] == None or item[12] == None or item[13] ==None:
        break
    score_list = [ item[11], item[12], item[13] ]
    score = most_common(score_list)
    tmplist.append([turn_0_user_sent,turn_0_tt_sent,turn_1_user_sent,turn_str,score])
    key = (turn_0_user_sent, turn_0_tt_sent,turn_1_user_sent,turn_str)
    if table_sum.has_key(key):
        table_sum[key]= table_sum[key]+score
        table_num[key] = table_num[key]+1
        table_entry[key].append(score)
    else:
import sentiment_online
import sentiment_vader
import pickle
f = open('test_sentiment.txt','w')
all_conversation = pickle.load(open('all_conversation.pkl'))
id =1
for utt in all_conversation:
    id = id+1
    sent = sentiment_online.get_sentiment(utt)
    sent_vader = sentiment_vader.get_sentiment(utt)
    f.write('utterance:' + utt +'\n')
    f.write('online:' + sent +'\n' )
    f.write('vader:' + sent_vader+'\n')
    f.write('\n')
    if id >1000:
        break
Esempio n. 8
0
def most_common(lst):
    return max(set(lst), key=lst.count)


conn = sqlite3.connect('rs_ratings.db')
c = conn.cursor()
c.execute('''SELECT * FROM responses''')
table_num = {}
table_sum = {}
table_entry = {}
tmplist = []
for item in c.fetchall():
    print item
    #print item[0]
    #break
    turn_1_user_sent = sentiment_vader.get_sentiment(item[0])
    turn_1_tt_sent = sentiment_vader.get_sentiment(item[1])
    turn_0_user_sent = sentiment_vader.get_sentiment(item[7])
    turn_0_tt_sent = sentiment_vader.get_sentiment(item[8])
    turn_str = item[10]
    if item[11] == None or item[12] == None or item[13] == None:
        break
    score_list = [item[11], item[12], item[13]]
    score = most_common(score_list)
    tmplist.append(
        [turn_0_user_sent, turn_0_tt_sent, turn_1_user_sent, turn_str, score])
    key = (turn_0_user_sent, turn_0_tt_sent, turn_1_user_sent, turn_str)
    if table_sum.has_key(key):
        table_sum[key] = table_sum[key] + score
        table_num[key] = table_num[key] + 1
        table_entry[key].append(score)
Esempio n. 9
0
     # here we do testing.
         epsilon = 0
         f = open('simulate_conv/'+str(conv_index)+'_test.txt','w')
     else:
         f = open('simulate_conv/'+str(conv_index)+'.txt','w')
 else:
     epsilon = 0.8
     f = open('simulate_conv/'+str(conv_index)+'.txt','w')
 f.write('Turn: 0'+'\n')
 f.write('You: Hello'+'\n' )
 f.write('TickTock: ' + tt_utt +'\n')
 f.write('Appropriateness: ' + '\n')
 f.write('Strategy: new' + '\n')
 f.write('')
 f.write('\n')
 sent_3 = sentiment_vader.get_sentiment(tt_utt)
 sent_2 = 'null'
 sent_1 = 'null'
 theme[str(conv_index)] = random.choice(TopicLib)
 old_theme = theme[str(conv_index)]
 previous_history[str(conv_index)] = ['Hello',tt_utt]
 reward_list = []
 for turn_id in range(1,11):
     print 'turn_id' +str(turn_id)+'\n'
     al_utt = alice.alice(tt_utt)
     conv["Turns"][turn_id] ={}
     conv["Turns"][turn_id]["You"] = al_utt
     f.write('Turn: ' + str(turn_id) +'\n')
     f.write('You: ' + al_utt+'\n')
     next_sent_1 = sent_3
     next_sent_2 = sentiment_vader.get_sentiment(al_utt)
Esempio n. 10
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