How to use the parlai.core.worlds.validate function in parlai

To help you get started, we’ve selected a few parlai examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ConvLab / ConvLab / convlab / human_eval / worlds.py View on Github external
n_word_match = 0
            for per in self.agentpersona_data:
                per_parse = per.split(' ')
                regular_words = ['', ' ', 'I', 'I\'m', 'My', 'i']
                for r_w in regular_words:
                    if r_w in per_parse:
                        per_parse.remove(r_w)
                per_subseq = [' '.join(per_parse[i:i + len(per_parse) -
                                                   tolerance]) for i in range(tolerance + 1)]
                for pp in per_subseq:
                    if pp in ['', ' ', '  ', '   ']:
                        per_subseq.remove(pp)
                n_word_match += sum([(paa in text) for paa in per_subseq])
            if n_word_match > 0:
                control_msg['text'] = COPIED_CHARACTER_MSG
                self.agentobserve(validate(control_msg))
                return True
            else:
                return False
github facebookresearch / ParlAI / parlai / mturk / tasks / personachat / personachat_rephrase / worlds.py View on Github external
text = act['text']
        if text not in ['', ' ', '  ', '   ']:
            n_word_match = 0
            per_parse = persona_data.split(' ')
            regular_words = ['', ' ', 'I', 'I\'m', 'My', 'i']
            for r_w in regular_words:
                if r_w in per_parse:
                    per_parse.remove(r_w)
            n_word_match += sum([word in text for word in per_parse])
            if n_word_match / (len(per_parse) + 1) > tolerance:
                control_msg['text'] = (
                    'We found that you <b><span style="color:red">'
                    'trivially copied character descriptions</span></b>. '
                    'Please rephrase your message again.'
                )
                ag.observe(validate(control_msg))
                return True
            else:
                return False
github facebookresearch / ParlAI / parlai / mturk / tasks / personachat / personachat_chat / worlds.py View on Github external
def check_timeout(self, act):
        if act['text'] == '[TIMEOUT]' and act['episode_done']:
            control_msg = {'episode_done': True}
            control_msg['id'] = 'SYSTEM'
            control_msg['text'] = self.get_instruction(
                agent_id=act['id'], tag='timeout'
            )
            for ag in self.agents:
                if ag.id != act['id']:
                    ag.observe(validate(control_msg))
            self.chat_done = True
            return True
        else:
            return False
github facebookresearch / ParlAI / parlai / mturk / tasks / image_chat / image_chat_collection / worlds.py View on Github external
acts = self.acts
                acts[idx] = agent.act(timeout=self.max_resp_time)
                agent_left = self.check_timeout(acts[idx])
                if agent_left:
                    break
                response = acts[idx]['text']
                offensive = self.offensive_lang_detector.contains_offensive_language(
                    response
                )
                if offensive:
                    # Tell Turker to not be offensive!
                    offensive_counter += 1
                    if offensive_counter == 3:
                        break
                    offensive_msg = {'id': 'SYSTEM', 'text': OFFENSIVE_MSG}
                    agent.observe(validate(offensive_msg))
                else:
                    break

            if self.chat_done:
                break
            ex_to_save = example.copy()
            key = 'second_response' if self.second_resp else 'first_response'
            ex_to_save[key] = response
            ex_to_save['{}_personality'.format(key)] = personality
            ex_to_save['contains_offensive_language'] = offensive
            self.data.append(ex_to_save)
            self.turn_idx += 1

        if self.turn_idx == self.num_images:
            control_msg['text'] = CHAT_ENDED_MSG.format(self.num_images)
            agent.observe(validate(control_msg))
github facebookresearch / ParlAI / parlai / mturk / tasks / personachat / personachat_chat / worlds.py View on Github external
per_parse.remove(r_w)
                per_subseq = [
                    ' '.join(per_parse[i : i + len(per_parse) - tolerance])
                    for i in range(tolerance + 1)
                ]
                for pp in per_subseq:
                    if pp in ['', ' ', '  ', '   ']:
                        per_subseq.remove(pp)
                n_word_match += sum([(paa in text) for paa in per_subseq])
            if n_word_match &gt; 0:
                control_msg['text'] = (
                    'We found that you <b><span style="color:red">trivially '
                    'copied character descriptions</span></b>. Please '
                    'rephrase your message again.'
                )
                ag.observe(validate(control_msg))
                return True
            else:
                return False
github facebookresearch / ParlAI / projects / controllable_dialogue / mturk / worlds.py View on Github external
def _evaluate_characteristic(self, question, choices, addto):
        control_msg = self.get_control_msg()
        control_msg['text'] = question
        control_msg['button_choices'] = ''.join(choices)
        self.eval_agent.observe(validate(control_msg))
        act = self.eval_agent.act(timeout=self.max_resp_time)
        timeout = self.check_timeout(act)
        if timeout:
            return False
        act_choice = choices.index(act.get('text'))
        addto.append(act_choice)
        return True
github facebookresearch / ParlAI / parlai / mturk / tasks / qualification_flow_example / worlds.py View on Github external
def parley(self):
        if self.curr_question == len(self.questions):
            ad = {
                'episode_done': True,
                'id': self.__class__.collector_agent_id,
                'text': 'Thank you for your answers!',
            }
            self.mturk_agent.observe(validate(ad))
            self.episodeDone = True
        else:
            ad = {
                'episode_done': True,
                'id': self.__class__.collector_agent_id,
                'text': self.questions[self.curr_question][0],
            }
            self.mturk_agent.observe(validate(ad))
            answer = self.mturk_agent.act()
            if answer == self.questions[self.curr_question][1]:
                self.correct += 1
            self.curr_question += 1
github facebookresearch / ParlAI / parlai / mturk / tasks / personachat / personachat_rephrase / worlds.py View on Github external
per_parse.remove(r_w)
                per_subseq = [
                    ' '.join(per_parse[i : i + len(per_parse) - tolerance])
                    for i in range(tolerance + 1)
                ]
                for pp in per_subseq:
                    if pp in ['', ' ', '  ', '   ']:
                        per_subseq.remove(pp)
                n_word_match += sum([(paa in text) for paa in per_subseq])
            if n_word_match &gt; 0:
                control_msg['text'] = (
                    'We found that you <b><span style="color:red">trivially '
                    'copied character descriptions</span></b>. Please '
                    'rephrase your message again.'
                )
                ag.observe(validate(control_msg))
                return True
            else:
                return False
github facebookresearch / ParlAI / parlai / mturk / tasks / personachat / worlds.py View on Github external
ag.observe(validate(control_msg))
                        return
                # agent ends chat after exceeding minimum number of turns
                if self.turn_idx > self.n_turn:
                    for ag in self.agents:
                        ag.observe(validate(acts[idx]))
                        control_msg['text'] = 'One of you ended the chat. Thanks for your time! Please click "Done with this HIT" button below to finish this HIT.'
                        control_msg['episode_done'] = True
                        ag.observe(validate(control_msg))
                return

            else:
                self.dialog.append((idx, acts[idx]['text']))
                for other_agent in self.agents:
                    if other_agent != agent:
                        other_agent.observe(validate(acts[idx]))
github facebookresearch / ParlAI / parlai / tasks / light_dialog / worlds.py View on Github external
Agent 0 goes first.

        Alternate between the two agents.
        """
        if self.cnt == 0:
            self.p1, self.p2 = self.get_new_personas()

        acts = self.acts
        agents = self.agents
        if self.cnt == 0:
            # add the persona on to the first message to agent 0
            act = {}
            act['text'] = self.p1
            act['episode_done'] = False
            act['id'] = 'persona'
            agents[0].observe(validate(act))
        act = deepcopy(agents[0].act())
        if self.cnt == 0:
            # add the persona on to the first message to agent 1
            act.force_set('text', self.p2 + act.get('text', 'hi'))
            agents[1].observe(validate(act))
        else:
            agents[1].observe(validate(act))
        acts[1] = agents[1].act()
        agents[0].observe(validate(acts[1]))
        self.update_counters()
        self.cnt += 1

        if act['episode_done']:
            print("CHAT DONE ")
            print("\n... preparing new chat... \n")
            self.cnt = 0