在Python中创建语音助手并存储用户的输入

发布于 2025-01-31 01:32:02 字数 3827 浏览 3 评论 0原文

我创建了一个非常简单的语音助手,该助手听取了用户的输入(更具体地说,是用户的故事)。现在,我想创建一个存储用户输入的函数 - 特此我有多个问题:

  1. 我需要字典还是数据框架?
  2. 如何编写存储用户输入的函数(将用户的输入拆分为a = timestamp和b =用户输入列)?
  3. 每当有新用户输入时,如何编写一个创建新行的函数?

到目前为止,这是我的代码:

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

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>= 0 and hour<12:
        speak("Good Morning!")
  
    elif hour>= 12 and hour<18:
        speak("Good Afternoon!")  
  
    else:
        speak("Good Evening!") 
  
    assistantname =("Anna")
    speak("I am your personal storykeeper " + assistantname)
    
#why doesn't this come right after "I am your personal storykeeper??"
def username():
    speak("I would love to know more about you, so let's start off right away. What's your name?")
    username = takeCommand()
    speak("Nice to meet you " + username + ".")
    print("Nice to meet you " + username + ".")
    #columns = shutil.get_terminal_size().columns
     
    #print("Welcome", username.center(columns))
    
def storykeepergoal():
    speak("Would you like to know more about my goal to conserve your family's stories?")
    query = takeCommand().lower() #All the commands said by user will be stored here in 'query' and will be converted to lower case for easily recognition of command
    if "yes" in query:
        speak("Happy that you are interested to learn more about me. You've probably experienced the moment at a family's birthday party when it's all about telling stories from when you were little, or a funny story about a vacation. As life goes on, those stories might be forgotten or cannot be told anymore. This is where I come in - by telling me stories about your favourite moments in life, I can store these forever. Your family can do the same thing. And if you would like to hear a story that your family members told me, you can tune in.")
        print("Happy that you are interested to learn more about me. You've probably experienced the moment at a family's birthday party when it's all about telling stories from when you were little, or a funny story about a vacation. As life goes on, those stories might be forgotten or cannot be told anymore. This is where I come in - by telling me stories about your favourite moments in life, I can store these forever. Your family can do the same thing. And if you would like to hear a story that your family members told me, you can tune in.")

    else:
        speak("Alright, no problem.")
        print("Alright, no problem.")

def getstory():
    speak("I am curious to hear what memories and stories you have about your family. Please tell me your favourite memory with, or about your family.")

    query = takeCommand().lower() #All the commands said by user will be stored here in 'query' and will be converted to lower case for easily recognition of command


def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)
  
    try:
        print("Recognizing...")   
        query = r.recognize_google(audio, language ='en-GB')
        print(f"User said: {query}\n")

    except Exception as e:
        print(e)   
        speak("My apologies, I did not understand that. Could you please say that again?")
        print("My apologies, I did not understand that. Could you please say that again?") 
        return "None"
     
    return query 

wishMe()
username()
storykeepergoal()
getstory()
 
def append():
    for query in story:
        #df_story = pd.DataFrame(story)
        new_row = {"timestamp": (now), "query": takeCommand()}

        #df_story = df_story.append(new_row, ignore_index=True)
        #return(df_story)

#print(df_story)
print(story)

append()

I have created a very simple voice assistant that listens to the user's input (more specifically, the user's story). Now, I want to create a function that stores the user's input - hereby, I have multiple questions:

  1. Do I need a dictionary or a dataframe?
  2. How do I write a function that stores the user's input (splitted into column A = timestamp and column B = user's input)?
  3. How do I write a function that creates a new row whenever there's a new user input?

This is my code so far:

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

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>= 0 and hour<12:
        speak("Good Morning!")
  
    elif hour>= 12 and hour<18:
        speak("Good Afternoon!")  
  
    else:
        speak("Good Evening!") 
  
    assistantname =("Anna")
    speak("I am your personal storykeeper " + assistantname)
    
#why doesn't this come right after "I am your personal storykeeper??"
def username():
    speak("I would love to know more about you, so let's start off right away. What's your name?")
    username = takeCommand()
    speak("Nice to meet you " + username + ".")
    print("Nice to meet you " + username + ".")
    #columns = shutil.get_terminal_size().columns
     
    #print("Welcome", username.center(columns))
    
def storykeepergoal():
    speak("Would you like to know more about my goal to conserve your family's stories?")
    query = takeCommand().lower() #All the commands said by user will be stored here in 'query' and will be converted to lower case for easily recognition of command
    if "yes" in query:
        speak("Happy that you are interested to learn more about me. You've probably experienced the moment at a family's birthday party when it's all about telling stories from when you were little, or a funny story about a vacation. As life goes on, those stories might be forgotten or cannot be told anymore. This is where I come in - by telling me stories about your favourite moments in life, I can store these forever. Your family can do the same thing. And if you would like to hear a story that your family members told me, you can tune in.")
        print("Happy that you are interested to learn more about me. You've probably experienced the moment at a family's birthday party when it's all about telling stories from when you were little, or a funny story about a vacation. As life goes on, those stories might be forgotten or cannot be told anymore. This is where I come in - by telling me stories about your favourite moments in life, I can store these forever. Your family can do the same thing. And if you would like to hear a story that your family members told me, you can tune in.")

    else:
        speak("Alright, no problem.")
        print("Alright, no problem.")

def getstory():
    speak("I am curious to hear what memories and stories you have about your family. Please tell me your favourite memory with, or about your family.")

    query = takeCommand().lower() #All the commands said by user will be stored here in 'query' and will be converted to lower case for easily recognition of command


def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)
  
    try:
        print("Recognizing...")   
        query = r.recognize_google(audio, language ='en-GB')
        print(f"User said: {query}\n")

    except Exception as e:
        print(e)   
        speak("My apologies, I did not understand that. Could you please say that again?")
        print("My apologies, I did not understand that. Could you please say that again?") 
        return "None"
     
    return query 

wishMe()
username()
storykeepergoal()
getstory()
 
def append():
    for query in story:
        #df_story = pd.DataFrame(story)
        new_row = {"timestamp": (now), "query": takeCommand()}

        #df_story = df_story.append(new_row, ignore_index=True)
        #return(df_story)

#print(df_story)
print(story)

append()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文