class ConsoleHub(object): ''' text based dialog system ''' def __init__(self): # Dialogue Agent: #----------------------------------------- self.agent = DialogueAgent(hub_id='texthub') def run(self): ''' Runs one episode through Hub :returns: None ''' print ("#################") logger.warning("NOTE: texthub is not using any error simulation at present.") sys_act = self.agent.start_call(session_id='texthub_dialog') print ('Prompt > ' + sys_act.prompt) while not self.agent.ENDING_DIALOG: # USER ACT: #obs = input('User > ') obs = input('User > ') ''' # Confused user act. # lastHyps = self.errorSimulator.confuseAct(lastUserAct) # print 'lastHyps >', lastHyps # nullProb = 0.0 # for (act, prob) in lastHyps: # if act == 'null()': # nullProb += prob # print 'Semi >', act, '['+str(prob)+']' # if self.forceNullPositive and nullProb < 0.001: # lastHyps.append(('null()',0.001)) # print 'Semi > null() [0.001]' #-------------------------------- ''' domain = None if "domain(" in obs: match = re.search("(.*)(domain\()([^\)]+)(\))(.*)",obs) if match is not None: domain = match.group(3) obs = match.group(1) + match.group(5) # SYSTEM ACT: sys_act = self.agent.continue_call(asr_info = [(obs,1.0)], domainString = domain) print ('Prompt > ' + sys_act.prompt)
class DialogueGenerator(): #collector_agent_id = 'Restaurant bot' def __init__(self): self.agent = DialogueAgent(hub_id='texthub') def first_system_act(self): sys_act = self.agent.start_call(session_id='texthub_dialog') return sys_act.prompt def next_system_act(self, usr_utt): if not self.agent.ENDING_DIALOG: sys_act = self.agent.continue_call(asr_info=[(usr_utt, 1.0)], domainString=None) for item in sys_act.items: if item.slot == 'name' and item.op == '=': return sys_act.prompt, item.val return sys_act.prompt, "" return "", "" def end_call(self): self.agent.end_call()
class DialogueGenerator(): #collector_agent_id = 'Restaurant bot' def __init__(self): self.agent = DialogueAgent(hub_id='texthub') def first_system_act(self): sys_act = self.agent.start_call(session_id='texthub_dialog') return sys_act.prompt def next_system_act(self, usr_utt, turn_num=1): if not self.agent.ENDING_DIALOG: if turn_num == 0: sys_act = self.agent.continue_call(asr_info=[(usr_utt, 1.0)], domainString=None) else: sys_act = self.agent.continue_call( asr_info=[(usr_utt, 1.0)], domainString="CamRestaurants") return sys_act.prompt return "" def end_call(self): self.agent.end_call()
def __init__(self): # Dialogue Agent: #----------------------------------------- self.agent = DialogueAgent(hub_id='texthub')
class ConsoleHub(object): ''' text based dialog system ''' def __init__(self): # Dialogue Agent: #----------------------------------------- self.agent = DialogueAgent(hub_id='texthub') def run(self, domain): ''' Runs one episode through Hub :returns: None ''' # GENERATE A USER PREFERENCE: a * Length + (1-a) * Success preference = torch.randn(2) preference = (torch.abs(preference) / torch.norm(preference, p=1)).type(FloatTensor) logger.dial('User\'s preference: [{}, {}]'.format( preference[0], preference[1])) print 'User\'s preference: [Brevity: {}, Success: {}]'.format( preference[0], preference[1]) logger.warning( "NOTE: texthub is not using any error simulation at present.") sys_act = self.agent.start_call(session_id='texthub_dialog', preference=preference) print 'Prompt > ' + sys_act.prompt while not self.agent.ENDING_DIALOG: # USER ACT: obs = raw_input('User > ') ''' # Confused user act. # lastHyps = self.errorSimulator.confuseAct(lastUserAct) # print 'lastHyps >', lastHyps # nullProb = 0.0 # for (act, prob) in lastHyps: # if act == 'null()': # nullProb += prob # print 'Semi >', act, '['+str(prob)+']' # if self.forceNullPositive and nullProb < 0.001: # lastHyps.append(('null()',0.001)) # print 'Semi > null() [0.001]' #-------------------------------- ''' # domain = None # if "domain(" in obs: # match = re.search("(.*)(domain\()([^\)]+)(\))(.*)",obs) # if match is not None: # domain = match.group(3) # obs = match.group(1) + match.group(5) # SYSTEM ACT: sys_act = self.agent.continue_call(asr_info=[(obs, 1.0)], domainString=domain, preference=preference) print 'Prompt > ' + sys_act.prompt
def parley(self): # Each turn starts from the QA Collector agent del self.wrongturns[:] agent = DialogueAgent(hub_id='texthub') ad = {'episode_done': False} ad['id'] = self.__class__.collector_agent_id turn_num = 1 ad['text'] = "<b>" + self.task_generation() + "</b>" self.mturk_agent.observe(validate(ad)) sys_act = agent.start_call(session_id='texthub_dialog') ad['text'] = "<b>[Turn " + str(turn_num) + "] </b>" + sys_act.prompt self.mturk_agent.observe(validate(ad)) while not agent.ENDING_DIALOG: turn_num += 1 obs = self.mturk_agent.act()['text'] loop_turns = 0 while loop_turns < 3 and ( (obs in self.keywords) or (len(obs.split(" ")) == 1 and obs != "quit")): ad['text'] = "Please make a sentence as natural as possible and do not use only keywords. Please rephrase your answer." self.mturk_agent.observe(validate(ad)) obs = self.mturk_agent.act()['text'] loop_turns += 1 if loop_turns == 3: ad['text'] = "We are closing this HIT, since you were keeping texting in keywords and refused to rephrase your answer in a natural way. We are sorry that you can not be paid for this HIT." self.mturk_agent.observe(validate(ad)) self.shutdown() self.mturk_agent.reject_work( reason= "You were keeping texting in keywords and refused to rephrase your answer in a natural way." ) return domain = None sys_act = agent.continue_call(asr_info=[(obs, 1.0)], domainString=domain) ad['text'] = "<b>[Turn " + str( turn_num) + "] </b>" + sys_act.prompt if sys_act.prompt.find( "having trouble understanding what you want") > -1: self.wrongturns.append(str(turn_num)) self.mturk_agent.observe(validate(ad)) ad['text'] = "Please tell me if you could find a restaurant that meets your constraints using our system and get all information (phone number/address) you need. (yes/no)" self.mturk_agent.observe(validate(ad)) obs = self.mturk_agent.act()['text'] ad['text'] = "Please tell me the turns you believe system's actions are contextually wrong(e.g. misunderstanding, questions repeating, information missing...), using turn numbers connected by \',\' (e.g. 1,3,6). If you think all turns are contextually right, please write 0. Please note that you can't get your reward without giving this feedback." loop_turns = 0 while loop_turns < 3: loop_turns += 1 self.mturk_agent.observe(validate(ad)) obs = self.mturk_agent.act()['text'] if self.pattern.match(obs) == None: ad['text'] = "Sorry the format of your feedback was wrong. Please tell me the turns you believe system's actions are contextually wrong(e.g. misunderstanding, questions repeating, information missing...), using turn numbers connected by \',\' (e.g. 1,3,6). If you think all turns are contextually right, please write 0. Please include those turns in your feedback if the system asked you for some information that you already provided." continue olist = [item.strip() for item in obs.split(",")] not_inclued = [ item for item in self.wrongturns if item not in olist ] if len(not_inclued) > 0: ad['text'] = "We detected that contextually wrong turns [" + ", ".join( not_inclued ) + "] are not included in your feedback. Please resubmit your feedback." continue exceed_turns = [ item for item in olist if int(item) > turn_num or int(item) < 0 ] if len(exceed_turns) > 0: ad['text'] = "We detected that turns [" + ", ".join( exceed_turns ) + "] in your feedback are illegal values. Please resubmit your feedback." continue break if loop_turns == 3: ad['text'] = "We are closing this HIT, since the format of your feedbacks were wrong and you refused to rephrase your feedback in a right way. We are sorry that you can not be paid for this HIT." self.mturk_agent.observe(validate(ad)) self.shutdown() self.mturk_agent.reject_work( reason= "The format of your feedbacks were wrong and you refused to rephrase your feedback in a right way." ) return return False, ""