Harsha200105/DesktopAssistant

`calculate one plus two plus three` and `math fifteen over three`

Opened this issue · 5 comments

This works on macOS.

elif query.split()[0] in ("calculate", "math"):
    print_and_speak(str(eval(query.replace("calculate", "").replace("math", ""))))

This works!

elif query.split()[0] in ("calculate", "math"):
    print_and_speak(str(eval(query.replace("calculate", "").replace("math", ""))))

@cclauss, The code you posted would likely raise a SyntaxError since, speech_recognition would return "calculate one plus two" when the user says "calculate one plus two" and not return "calculate 1 + 2" I think this code would be better:

from word2number import w2n  # pip install word2number
# Some if elif statements here
elif query.split()[0] in ("calculate", "math"):
    query = query.replace("calculate", "").replace("math", "")  # Remove the words "calculate" and "math"
    query = query.replace("plus", "+").replace("minus", "-").replace("multiplication", "*").replace("divided by", "/").replace("by", "/")  # Replace sign words with their respective signs
    for word in query:
        try:
            word = w2n.word_to_num(word)
        except ValueError:  # If word is a sign like +, -, etc...
            pass
        query += f'{word} '
    query = query.strip()  # Remove unwanted whitespaces.
    print_and_speak(str(eval(query)))

Also, try to include statements like "divide two by four", "add one and two", "multiply ten and two", "subtract nine from thirty"

The code you posted would likely raise a SyntaxError since, speech_recognition would return "calculate one plus two"

Did you try it or did you guess?

The code you posted would likely raise a SyntaxError since, speech_recognition would return "calculate one plus two"

Did you try it or did you guess?

@cclauss I tried it!

What OS are you running on? The macOS engine returns `1 + 2 + 3ˋ.

What OS are you running on? The macOS engine returns `1 + 2 + 3ˋ.

I am on ubuntu