Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate one plus two plus three and math fifteen over three #37

Open
cclauss opened this issue Oct 7, 2021 · 5 comments
Open

calculate one plus two plus three and math fifteen over three #37

cclauss opened this issue Oct 7, 2021 · 5 comments

Comments

@cclauss
Copy link
Contributor

cclauss commented Oct 7, 2021

This works on macOS.

elif query.split()[0] in ("calculate", "math"):
    print_and_speak(str(eval(query.replace("calculate", "").replace("math", ""))))
@Jothin-kumar
Copy link
Contributor

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"

@cclauss
Copy link
Contributor Author

cclauss commented Oct 21, 2021

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?

@Jothin-kumar
Copy link
Contributor

Jothin-kumar commented Oct 21, 2021

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!

@cclauss
Copy link
Contributor Author

cclauss commented Oct 21, 2021

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

@Jothin-kumar
Copy link
Contributor

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

I am on ubuntu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants