Esempio n. 1
0
    def run(self):
        """
        Run the test.
        """
        self.connection = connect_to_tester()

        # Start new game
        while True:
            self._start_new_game()
            self._display_help()
            # Start new round
            while True:
                self._start_new_round()
                print(get_chat_line_separator())
                while self.n_questions_left > 0:
                    # Read message
                    message = input(chat_tag(DISPLAY_NAME_YOU))

                    # Check if the message is a command
                    if message[:2] == "--":
                        command_was_guess = self._execute_command(message)
                        if command_was_guess:
                            break
                        print(get_chat_line_separator())
                    # Else, send the message to the tester
                    else:
                        self._send_chat_message(message)
                        print(
                            chat_tag(DISPLAY_NAME_OTHER) +
                            self._receive_message())
                        print(get_chat_line_separator())
                        self.n_questions_left -= 1
                if self.n_questions_left == 0:
                    print(
                        info_message(
                            "No questions left. Yoy now need to make a guess.")
                    )
                self._make_guess()
                print(get_chat_line_separator())
                if 'y' not in input("Start new round (y/N): "):
                    break

            print(
                info_message(
                    "Game ended. Your final score is: {:.2f} out of 100.0".
                    format(self._compute_score())))
            self.connection.request('POST', ROUTE_ENDED_GAME, "")
            self._receive_message()
            if 'y' not in input("Start new game (y/N): "):
                break
Esempio n. 2
0
    def start_new_round(self):
        """
        Start a new round. For the round to be started, you need to select whether you our the bot is answering.

        Returns:
            str: Confirmation message.
        """

        # Get the tester type
        print(get_chat_line_separator())
        print(info_message("New round started by the user."))
        tester_type = input("Will the bot or you be answering the questions ({}/{}): ".format(TESTER_BOT, TESTER_HUMAN))
        while tester_type not in [TESTER_HUMAN, TESTER_BOT]:
            tester_type = input("'{}' is not a valid tester type, select either {} or {}: ".format(tester_type, TESTER_BOT, TESTER_HUMAN))

        if tester_type == TESTER_BOT:
            print(info_message("The bot will be answering the questions. Sit back and relax!"))
        if tester_type == TESTER_HUMAN:
            print(info_message("You will be answering the questions. Good luck."))
        print(info_message("Waiting for first message..."))

        self.tester_type = tester_type

        # Reset the bot
        self.bot.reset()

        return "New round ready"
Esempio n. 3
0
    def __init__(self):
        """
        Used to host a Turing Test.
        """

        super(Tester, self).__init__()

        # Connect to the bot
        self.bot = connect_to_cleverbot()
        print(info_message("Successfully connected to cleverbot"))

        # Set up the routes.
        self.route(ROUTE_INBOX, method='POST', callback=self.receive_and_send_message)
        self.route(ROUTE_CONNECT_INFO, method='POST', callback=self.has_connected)
        self.route(ROUTE_NEW_ROUND, method='POST', callback=self.start_new_round)
        self.route(ROUTE_CHECK_GUESS, method='POST', callback=self.check_guess)
        self.route(ROUTE_ENDED_GAME, method='POST', callback=self.game_ended)

        # Initalized later
        self.tester_type = None

        # Estimate writing speed
        # If false, a random writing speed will be chosen
        # The writing speed is in chars/sec
        print(get_chat_line_separator())
        if 'y' in input("Estimate writing speed (y/N): "):
            self.writing_speed = min(estimate_writing_speed(), 0.35)
        else:
            self.writing_speed = 0.15 + random() * 0.2
        print(info_message("Writing speed estimated to {:.3f} characters/sec".format(self.writing_speed)))
Esempio n. 4
0
    def game_ended(self):
        """
        Inform the tester that the previous game was ended.
        """

        print(get_chat_line_separator())
        print(info_message("Current game ended. Waiting for new game to start..."))
        return "Ok"
Esempio n. 5
0
    def game_ended(self):
        """
        Inform the tester that the previous game was ended.
        """

        print(get_chat_line_separator())
        print(
            info_message(
                "Current game ended. Waiting for new game to start..."))
        return "Ok"
Esempio n. 6
0
    def run(self):
        """
        Run the test.
        """
        self.connection = connect_to_tester()

        # Start new game
        while True:
            self._start_new_game()
            self._display_help()
            # Start new round
            while True:
                self._start_new_round()
                print(get_chat_line_separator())
                while self.n_questions_left > 0:
                    # Read message
                    message = input(chat_tag(DISPLAY_NAME_YOU))

                    # Check if the message is a command
                    if message[:2] == "--":
                        command_was_guess = self._execute_command(message)
                        if command_was_guess:
                            break
                        print(get_chat_line_separator())
                    # Else, send the message to the tester
                    else:
                        self._send_chat_message(message)
                        print(chat_tag(DISPLAY_NAME_OTHER) + self._receive_message())
                        print(get_chat_line_separator())
                        self.n_questions_left -= 1
                if self.n_questions_left == 0:
                    print(info_message("No questions left. Yoy now need to make a guess."))
                self._make_guess()
                print(get_chat_line_separator())
                if 'y' not in input("Start new round (y/N): "):
                    break

            print(info_message("Game ended. Your final score is: {:.2f} out of 100.0".format(self._compute_score())))
            self.connection.request('POST', ROUTE_ENDED_GAME, "")
            self._receive_message()
            if 'y' not in input("Start new game (y/N): "):
                break
Esempio n. 7
0
    def has_connected(self):
        """
        Get notified that the user has connected.

        Returns:
            str: Confirmation.
        """

        host = request.body.read().decode()
        print(get_chat_line_separator())
        print(info_message("Host {} just connected".format(host)))
        return "Successfully connected"
Esempio n. 8
0
    def has_connected(self):
        """
        Get notified that the user has connected.

        Returns:
            str: Confirmation.
        """

        host = request.body.read().decode()
        print(get_chat_line_separator())
        print(info_message("Host {} just connected".format(host)))
        return "Successfully connected"
Esempio n. 9
0
    def check_guess(self):
        """
        Check the guess made by the subject.

        Returns:
            str: Whether or not the result was correct.
        """

        guess_tester_type =  request.body.read().decode()
        print(get_chat_line_separator())
        print(info_message("The human guess that you're a {}".format(guess_tester_type)))
        print(info_message("Waiting for a new round to start..."))
        if guess_tester_type == self.tester_type:
            return GUESS_CORRECT
        return GUESS_WRONG
Esempio n. 10
0
    def check_guess(self):
        """
        Check the guess made by the subject.

        Returns:
            str: Whether or not the result was correct.
        """

        guess_tester_type = request.body.read().decode()
        print(get_chat_line_separator())
        print(
            info_message(
                "The human guess that you're a {}".format(guess_tester_type)))
        print(info_message("Waiting for a new round to start..."))
        if guess_tester_type == self.tester_type:
            return GUESS_CORRECT
        return GUESS_WRONG
Esempio n. 11
0
def start_server():
    """
    Start the Turing Test Server.
    """

    tester = Tester()
    host = None
    while True:
        try:
            print(get_chat_line_separator())
            host = input("IP to host on (nothing for localhost): ").strip()
            if not host:
                host = "localhost"
            print(info_message("Starting Turning Test Server on {}".format(host)))
            print(info_message("Waiting for connection from subject..."))
            tester.run(host=host, port=PORT, quiet=True)
        except socket.gaierror:
            print(info_message("Invalid host '{}'".format(host)))
Esempio n. 12
0
    def __init__(self):
        """
        Used to host a Turing Test.
        """

        super(Tester, self).__init__()

        # Connect to the bot
        self.bot = connect_to_cleverbot()
        print(info_message("Successfully connected to cleverbot"))
        self.conv = self.bot.new_conversation()

        # Set up the routes.
        self.route(ROUTE_INBOX,
                   method='POST',
                   callback=self.receive_and_send_message)
        self.route(ROUTE_CONNECT_INFO,
                   method='POST',
                   callback=self.has_connected)
        self.route(ROUTE_NEW_ROUND,
                   method='POST',
                   callback=self.start_new_round)
        self.route(ROUTE_CHECK_GUESS, method='POST', callback=self.check_guess)
        self.route(ROUTE_ENDED_GAME, method='POST', callback=self.game_ended)

        # Initalized later
        self.tester_type = None

        # Estimate writing speed
        # If false, a random writing speed will be chosen
        # The writing speed is in chars/sec
        print(get_chat_line_separator())
        if 'y' in input("Estimate writing speed (y/N): "):
            self.writing_speed = min(estimate_writing_speed(), 0.35)
        else:
            self.writing_speed = 0.15 + random() * 0.2
        print(
            info_message(
                "Writing speed estimated to {:.3f} characters/sec".format(
                    self.writing_speed)))
Esempio n. 13
0
    def receive_and_send_message(self):
        """
        Receive a message from the subject. If tester_type=bot, the bot will answer the question. If tester_type=human,
        you will be answering the questions.

        Returns:
            str: The reply.
        """

        message_received = request.body.read().decode()
        print(get_chat_line_separator())
        print(chat_tag(DISPLAY_NAME_OTHER) + message_received)

        # Get reply for the message
        # Bot answers the message
        if self.tester_type == TESTER_BOT:
            # Get the reply from the bot
            t_start = time()
            bot_reply = self.bot.say(message_received)
            t = time() - t_start

            # Normalize and humanize the text
            bot_reply = humanize_text(normalize_text(bot_reply))

            # Add a thinking break
            sleep(0.5)

            # Add a delay to the message sending in case the bot responded
            # too fast
            estimated_writing_time = len(bot_reply) * self.writing_speed
            if t < estimated_writing_time:
                sleep(estimated_writing_time-t)

            print(chat_tag(DISPLAY_NAME_YOU) + bot_reply)
            return bot_reply

        # You answer the message
        return normalize_text(input(chat_tag(DISPLAY_NAME_YOU)))
Esempio n. 14
0
    def receive_and_send_message(self):
        """
        Receive a message from the subject. If tester_type=bot, the bot will answer the question. If tester_type=human,
        you will be answering the questions.

        Returns:
            str: The reply.
        """

        message_received = request.body.read().decode()
        print(get_chat_line_separator())
        print(chat_tag(DISPLAY_NAME_OTHER) + message_received)

        # Get reply for the message
        # Bot answers the message
        if self.tester_type == TESTER_BOT:
            # Get the reply from the bot
            t_start = time()
            bot_reply = self.conv.say(message_received)
            t = time() - t_start

            # Normalize and humanize the text
            bot_reply = humanize_text(normalize_text(bot_reply))

            # Add a thinking break
            sleep(0.5)

            # Add a delay to the message sending in case the bot responded
            # too fast
            estimated_writing_time = len(bot_reply) * self.writing_speed
            if t < estimated_writing_time:
                sleep(estimated_writing_time - t)

            print(chat_tag(DISPLAY_NAME_YOU) + bot_reply)
            return bot_reply

        # You answer the message
        return normalize_text(input(chat_tag(DISPLAY_NAME_YOU)))
Esempio n. 15
0
    def start_new_round(self):
        """
        Start a new round. For the round to be started, you need to select whether you our the bot is answering.

        Returns:
            str: Confirmation message.
        """

        # Get the tester type
        print(get_chat_line_separator())
        print(info_message("New round started by the user."))
        tester_type = input(
            "Will the bot or you be answering the questions ({}/{}): ".format(
                TESTER_BOT, TESTER_HUMAN))
        while tester_type not in [TESTER_HUMAN, TESTER_BOT]:
            tester_type = input(
                "'{}' is not a valid tester type, select either {} or {}: ".
                format(tester_type, TESTER_BOT, TESTER_HUMAN))

        if tester_type == TESTER_BOT:
            print(
                info_message(
                    "The bot will be answering the questions. Sit back and relax!"
                ))
        if tester_type == TESTER_HUMAN:
            print(
                info_message(
                    "You will be answering the questions. Good luck."))
        print(info_message("Waiting for first message..."))

        self.tester_type = tester_type

        # Reset the bot
        self.conv.reset()

        return "New round ready"
Esempio n. 16
0
def start_server():
    """
    Start the Turing Test Server.
    """

    tester = Tester()
    host = None
    while True:
        try:
            print(get_chat_line_separator())
            host = input("IP to host on (nothing for localhost): ").strip()
            if not host:
                host = "localhost"
            port = input("Port to host on (nothing for {}): ".format(PORT))
            if not port:
                port = PORT

            print(
                info_message("Starting Turing Test Server on {}:{}".format(
                    host, port)))
            print(info_message("Waiting for connection from subject..."))
            tester.run(host=host, port=port, quiet=True)
        except socket.gaierror:
            print(info_message("Invalid host '{}'".format(host)))