0

我正在尝试使用此代码制作一个像星期五一样的虚拟助手

import os
from gtts import gTTS
import time
import playsound
import speech_recognition as sr

while True:
    def speak(text):
        tts = gTTS(text=text, lang="en")
        filename = "voice.mp3"
       tts.save(filename)
       playsound.playsound(filename)

    def get_audio():
        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            said = ""

            try:
                said = r.recognize_google(audio)
                print(said)
            except Exception as e:
                print("Exception: " + str(e))

        return said


    text = get_audio()

    
    if "who are you" in text:
        speak(" I am Monday the  virtual assistant")

我想知道如何将 wolfram alpha 放入其中,所以我会说搜索 ...,然后它会说出 wolfram alpha 的答案。

任何帮助都会很棒:)

4

1 回答 1

0

安装 wolframalpha 然后将以下内容添加到您的代码中:

import wolframalpha

if 'search for ' in text:
  text = text.replace("search for ", "")
  client = wolframalpha.Client(app_id)
  res = client.query(text)
  print(next(res.results).text)
  speak(next(res.results).text)

要使用 API,您必须转到主页,注册一个帐户,创建一个应用程序并获取一个应用程序 ID。为避免出现任何错误,请保持“说话”功能中的缩进一致。

于 2020-10-19T06:44:55.373 回答