Exemplo n.º 1
0
    def process(self, statement):
        """
        Takes a statement string.
        Returns the equation from the statement with the mathematical terms solved.
        """
        from mathparse import mathparse

        input_text = statement.text

        # Use the result cached by the process method if it exists
        if input_text in self.cache:
            cached_result = self.cache[input_text]
            self.cache = {}
            return cached_result

        # Getting the mathematical terms within the input statement
        expression = mathparse.extract_expression(input_text, language=self.language)

        response = Statement(text=expression)

        try:
            response.text += ' = ' + str(
                mathparse.parse(expression, language=self.language)
            )

            # The confidence is 1 if the expression could be evaluated
            response.confidence = 1
        except mathparse.PostfixTokenEvaluationException:
            response.confidence = 0

        return response
Exemplo n.º 2
0
    def process_input(self, statement):
        """
        Process input from the HipChat room.
        """
        new_message = False

        response_statement = self.chatbot.storage.get_latest_response(
            self.session_id
        )

        if response_statement:
            last_message_id = response_statement.extra_data.get(
                'hipchat_message_id', None
            )
            if last_message_id:
                self.recent_message_ids.add(last_message_id)

        while not new_message:
            data = self.get_most_recent_message(self.hipchat_room)

            if data and data['id'] not in self.recent_message_ids:
                self.recent_message_ids.add(data['id'])
                new_message = True
            else:
                pass
            sleep(3.5)

        text = data['message']

        statement = Statement(text)
        statement.add_extra_data('hipchat_message_id', data['id'])

        return statement
    def process_input(self, statement):
        input_type = self.detect_type(statement)

        # Return the statement object without modification
        if input_type == self.OBJECT:
            return statement

        # Convert the input string into a statement object
        if input_type == self.TEXT:
            return Statement(statement)

        # Convert input dictionary into a statement object
        if input_type == self.JSON:
            input_json = dict(statement)
            text = input_json['text']
            del input_json['text']

            return Statement(text, **input_json)
Exemplo n.º 4
0
    def process_input(self, statement):
        urls = self.get_stored_email_urls()
        url = list(urls)[0]

        response = self.get_message(url)
        message = response.json()

        text = message.get('stripped-text')

        return Statement(text)
Exemplo n.º 5
0
    def process_input(self, statement):
        new_message = False

        while not new_message:
            data = self.get_most_recent_message()
            if self.should_respond(data):
                self.mark_messages_as_read([data['id']])
                new_message = True
            sleep(self.sleep_time)

        text = self.remove_mentions(data['text'])
        statement = Statement(text)

        return statement
Exemplo n.º 6
0
    def __init__(self, **kwargs):
        super(LowConfidenceAdapter, self).__init__(**kwargs)

        self.confidence_threshold = kwargs.get('threshold', 0.65)

        default_responses = kwargs.get('default_response',
                                       "I'm sorry, I do not understand.")

        # Convert a single string into a list
        if isinstance(default_responses, str):
            default_responses = [default_responses]

        self.default_responses = [
            Statement(text=default) for default in default_responses
        ]
Exemplo n.º 7
0
    def process_input(self, statement):
        new_message = False
        data = None
        while not new_message:
            data = self.get_most_recent_message()
            if data and data['id']:
                new_message = True
            else:
                pass
            sleep(3.5)

        text = data['text']
        statement = Statement(text)
        self.logger.info('processing user statement {}'.format(statement))

        return statement
Exemplo n.º 8
0
 def process_input(self, *args, **kwargs):
     """
     Read the user's input from the terminal.
     """
     user_input = input_function()
     return Statement(user_input)