0% found this document useful (0 votes)
8 views3 pages

Vcaluc

Uploaded by

thulasiram369369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Vcaluc

Uploaded by

thulasiram369369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pyttsx3

import speech_recognition as sr
import math
from sympy import sympify, pi, sqrt
from word2number import w2n # To convert words to numbers

# Initialize the speech engine


engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)

def voice_calculator():
# Function for speaking text
def speak(audio):
engine.say(audio)
engine.runAndWait()

# Greet the user


speak("Hi, I'm Multi. Your scientific calculator")

# Listen for user's command


def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening for your command...")
speak("Listening for your command...")
try:
# Adjust for ambient noise and listen continuously without timing
out
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source) # Removed timeout for continuous
listening
return recognizer.recognize_google(audio)
except sr.UnknownValueError:
print("Sorry, I didn't understand. Please try again.")
return None
except sr.RequestError:
print("Sorry, there was a problem with the service.")
return None

# Function to convert words like 'five' to numbers


def word_to_number(command):
words = command.split()
for i, word in enumerate(words):
try:
# Try to convert the word to a number
words[i] = str(w2n.word_to_num(word))
except ValueError:
# If it's not a number word, skip it
continue
return ' '.join(words)

# Function to convert 'x' to '*' for multiplication


def convert_multiplication(command):
return command.replace('x', '*')

# Main function to handle the program flow


while True:
command = listen()

if command:
command = command.lower()

# Convert words to numbers like 'five' to '5'


command = word_to_number(command)

# Convert 'x' to '*' for multiplication


command = convert_multiplication(command)

print(f"Command after conversion: {command}")

# Check for specific commands


if command in ['see you later', 'quit', 'bye']:
speak("Goodbye!")
print("Goodbye!\n")
break # Exit the program immediately

# Handle mathematical operations directly here


elif "square root of" in command or "root of" in command:
# Extract the number and calculate square root
number = float(command.split()[-1]) # Get the number after 'square
root of'
result = sqrt(number) # Calculate square root using sympy's sqrt
speak(f"The square root is: {result}")
print(f"Result: The square root is: {result}\n")

elif "pi" in command:


# If 'pi' is mentioned, evaluate pi
result = pi # Use sympy's pi constant
speak(f"The value of pi is: {result}")
print(f"Result: The value of pi is: {result}\n")

elif "plus" in command or "minus" in command or "times" in command or


"divided by" in command:
# Split the command and perform the operation
elements = command.split()
try:
num1 = float(elements[0]) # First number
operator = elements[1] # The operator (plus, minus, etc.)
num2 = float(elements[2]) # Second number

# Perform the operation based on the operator


if operator == "plus":
result = num1 + num2
elif operator == "minus":
result = num1 - num2
elif operator == "times":
result = num1 * num2
elif operator == "divided":
result = num1 / num2

speak(f"The result is: {result}")


print(f"Result: {result}\n")
except Exception as e:
speak(f"Error in operation: {str(e)}")
print(f"Error in operation: {str(e)}")
else:
try:
# Use sympify to evaluate complex expressions
result = sympify(command) # Use sympify for more complex math
expressions
speak(f"The result is: {result}")
print(f"Result: {result}\n")
except Exception as e:
speak(f"Sorry, there was an error: {str(e)}")
print(f"Error: {str(e)}")

else:
speak("Please say your command again.")
print("Please say your command again.\n")

if __name__ == '__main__':
voice_calculator()

You might also like