def process(self,
                statement,
                additional_response_selection_parameters=None):
        """
        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.ISO_639.upper())

        response = Statement(text=expression)

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

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

        return response
    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
示例#3
0
    def solve(self, statement):

        de_word = ['mathparse']
        statement = get_stem(statement, de_word)

        statement = statement.replace("to the power", "^")
        statement = statement.replace("into", "*")
        statement = statement.replace("multiplied by", "*")
        statement = statement.replace("divided by", "/")
        statement = statement.replace("by", "/")

        expression = mathparse.extract_expression(statement, language='ENG')
        ans = mathparse.parse(expression)

        gui.display(f"{expression} = {ans}")
        talk(f"{expression} = {ans}")
示例#4
0
    def test_extract_expression_simple_additon_words(self):
        result = mathparse.extract_expression('What is three plus three?',
                                              language='ENG')

        self.assertEqual(result, 'three plus three')
示例#5
0
    def test_extract_expression_simple_additon(self):
        result = mathparse.extract_expression('What is 3 + 3?', language='ENG')

        self.assertEqual(result, '3 + 3')
示例#6
0
    def test_ignore_punctuation(self):
        result = mathparse.extract_expression('3?', language='ENG')

        self.assertEqual(result, '3')
示例#7
0
    def test_extract_expression(self):
        result = mathparse.extract_expression('3 + 3', language='ENG')

        self.assertEqual(result, '3 + 3')
示例#8
0
    def test_empty_string(self):
        result = mathparse.extract_expression('', language='ENG')

        self.assertEqual(result, '')