首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python语音助手,对触发短语起作用

Python语音助手,对触发短语起作用
EN

Code Review用户
提问于 2022-07-21 14:26:45
回答 1查看 63关注 0票数 1

我做了个Python语音助理。它接受用户的语音输入,并且有多个if - the语句指定一个条件,如果它满足该条件,则执行特定的函数。如何以更有效的方式实现if- way语句?

代码语言:javascript
复制
async def main():
    
    def there_exists(terms):
        for term in terms:
            if term in response or SequenceMatcher(None, response, term).ratio() > 0.85:
                return True

    while True:
        print("Listening..")
        response = takeCommand()

        if there_exists(["close current tab", 'close tab']):
            keyboard.press_and_release('ctrl+w') 

        elif there_exists(['goodbye', 'bye', 'see you later', 'ok bye']):
            speak("Nice talking with you!")
            sys.exit(0)
        
        elif there_exists(['open google', 'open new tab in google', 'new tab in google']):
            webbrowser.open_new_tab("https://www.google.com")
            speak("Google chrome is open now")
            time.sleep(5)

        elif there_exists(['open gmail', 'gmail']):
            webbrowser.open_new_tab("https://mail.google.com/mail/u/0/#inbox")
            speak("Gmail is open now")
            time.sleep(5)

        elif 'open youtube' in response:
            openYoutube()

        elif there_exists(['whats the day today', 'what day is it today', 'day']):
            speak(f'Today is {getDay()}')

        elif there_exists(['battery percentage', 'what is the battery like right now', 'tell me the battery percentage']):
            speak("Current battery percentage is at" + str(percent) + "percent")

        elif there_exists(['what is the current brightness', 'current brightness', 'what is the brightness like right now']):
            speak(str(sbc.get_brightness()) + "percent")

        elif there_exists(["current location", "location", "where am i", "where am I right now"]):
            location()

        elif there_exists(['increase volume', 'volume up']):
            for i in range(3):
                pyautogui.press('volumeup')
            speak("Increase volume by 10 percent")

        elif there_exists(['decrease volume', 'volume down']):
            for i in range(3):
                pyautogui.press('volumedown')
            speak("Decreased volume by 10 percent")

        elif there_exists(["play"]):
            search_term = response.replace("play", '')
            kit.playonyt(search_term)
            speak(f"Playing {search_term}")

        elif there_exists(["on youtube"]):
            search_term = response.replace("on youtube", '')
            url = f"https://www.youtube.com/results?search_query={search_term}"
            webbrowser.get().open(url)
            speak(f'Here is what I found for {search_term} on youtube')

        elif there_exists(["price of", "what is the price of", "tell me the price of"]):
            engine.setProperty("rate", 150)
            search_term = response.lower().split(" of ")[-1].strip()
            stock = getStock(search_term)
            speak(stock)
            print(stock)
            engine.setProperty("rate", 175)

        elif there_exists(['take a note', 'note', 'note this down', 'remember this', 'take this down']):
            speak("What do you want me to note down?")
            response = takeCommand()
            note(response)
            speak("I have made a note of that")

        elif there_exists(['tell me a joke', 'not funny', 'make me laugh', 'joke', 'tell me another joke']):
            joke = (pyjokes.get_joke())
            speak(joke)
            print(joke)

        elif there_exists(["search for"]) and 'youtube' not in response:
            search_term = response.split("for")[-1]
            url = f"https://google.com/search?q={search_term}"
            webbrowser.get().open(url)
            speak(f'Here is what I found for {search_term} on google')

        elif there_exists(['what is the time now', 'what time is it', 'time']):
            strTime = datetime.datetime.now().strftime("%H:%M")
            speak(f"the time is {strTime}")

        elif 'search' in response:
            response = response.replace("search", "")
            webbrowser.open_new_tab(response)
            time.sleep(5)

        elif there_exists(['sign out', 'log off']):
            speak(
                "Your pc will log off in 10 sec make sure you exit from all applications")
            subprocess.call(["shutdown", "/l"])

        elif there_exists(['shutdown the pc', 'shutdown', 'shutdown the laptop']):
            speak("Shutting down your pc, make sure you exit from all applications")
            subprocess.call(["shutdown", "/s"])

        elif there_exists(['restart', 'restart the pc', 'restart the laptop']):
            speak("Restarting your pc, make sure you exit from all applications")
            subprocess.call(["shutdown", "/r"])

        elif there_exists(['what is the weather like right now', 'current temperature', 'climate']):
            weather = getWeather()
            speak(weather)
            print(weather)

        elif there_exists(['increase brightness', 'the brightness is low']):
            if sbc.get_brightness() == 100:
                speak("Brightness is already at max")
            else:
                brightness = sbc.set_brightness(current_brightness + 10)
                speak(f"Increased brightness by 10 percent")

        elif there_exists(
                ['decrease brightness', 'dim', 'dim the laptop', 'dim the screen', 'the screen is too bright']):
            sbc.set_brightness(current_brightness - 10)
            speak(f"Decreased brightness by 10 percent")

        elif there_exists(['take a screenshot', 'screenshot', 'capture the screen', 'take a photo of this']):
            image = pyscreenshot.grab()
            speak("Should I open the image?")
            response = takeCommand()
            if there_exists(['yes', 'show', 'show the screenshot']):
                image.show()
            else:
                speak("Ok")

        elif there_exists(['what is the weather in']):
            search_term = response.replace("what is the weather in", '')
            weather = getWeatherLocation(search_term)
            speak(weather)
            print(weather)

        elif there_exists(['what', 'who', 'why', 'where', 'when', 'which']):
            ans = getQuickAnswers(response)
            speak(ans)
            print(ans)

        elif there_exists(['send a message to']):
            search_term = response.replace('send a message to', '').replace(' ', '')
            await Methods().sendUserMessage(search_term)

asyncio.run(main())
time.sleep(3)
EN

回答 1

Code Review用户

回答已采纳

发布于 2022-07-21 14:53:03

您可以使用预期的输入作为键,操作作为值来创建一个dict。

代码语言:javascript
复制
@dataclass
class ActionInput(Hashable):
    def __hash__(self) -> int:
        return str(self.value).__hash__()

    value: list[str]


actions = {
    ActionInput(["close current tab", 'close tab']): (lambda: print('ctrl+w')),
    # ...
}

然后绕过去:

代码语言:javascript
复制
for key in actions:
    if there_exists(key.value):
        actions[key]()
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/278241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档