Skip to content

code #199

@adityassuryawanshi25-bot

Description

import speech_recognition as sr
import pyttsx3
import wikipedia

recognizer = sr.Recognizer()
recognizer.energy_threshold = 300
recognizer.dynamic_energy_threshold = False

engine = pyttsx3.init("sapi5")
engine.setProperty("rate", 190)

def speak(text):
engine.say(text)
engine.runAndWait()

print("Say 'laptop' to activate...")

while True:
try:
# Wake word
with sr.Microphone() as source:
audio = recognizer.listen(source, timeout=1, phrase_time_limit=1)

    word = recognizer.recognize_google(audio).lower()

    if word == "laptop":
        speak("ya")

        # Question
        with sr.Microphone() as source:
            print("Ask your question...")
            audio = recognizer.listen(source, timeout=3, phrase_time_limit=5)

        question = recognizer.recognize_google(audio)
        print("Question:", question)

        # Wikipedia answer
        try:
            answer = wikipedia.summary(question, sentences=2)
            print("Answer:", answer)
            speak(answer)

        except wikipedia.exceptions.DisambiguationError:
            speak("Your question is unclear. Please be more specific.")
        except wikipedia.exceptions.PageError:
            speak("I could not find an answer.")

except sr.UnknownValueError:
    pass
except sr.WaitTimeoutError:
    pass
except Exception as e:
    print("Error:", e)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions