Python 关于制作聊天机器人的方法

Python 关于制作聊天机器人的方法,python,speech-recognition,text-to-speech,chatbot,Python,Speech Recognition,Text To Speech,Chatbot,我正在创建一个聊天机器人。聊天机器人包含一些功能,例如它会玩一些笑话,它会在互联网上搜索有关问题,如告诉我塔吉玛哈和圣雄甘地是什么时候出生的,因为我使用的是维基搜索API和网页抓取。我还添加了一个对话,如“你好吗?”,我是你的朋友还是不喜欢正常的交谈。但当我说我是你的朋友时,它会搜索维基百科,但我不想这样。那么,我该如何训练聊天机器人何时在维基百科上搜索,何时从数据库中回复。使用关键字,这就是我在我的个人助理中使用的。 我这样做的方式是,我创建了一个包含多个字符串的列表,如下所示: Hello_

我正在创建一个聊天机器人。聊天机器人包含一些功能,例如它会玩一些笑话,它会在互联网上搜索有关问题,如告诉我塔吉玛哈和圣雄甘地是什么时候出生的,因为我使用的是维基搜索API和网页抓取。我还添加了一个对话,如“你好吗?”,我是你的朋友还是不喜欢正常的交谈。但当我说我是你的朋友时,它会搜索维基百科,但我不想这样。那么,我该如何训练聊天机器人何时在维基百科上搜索,何时从数据库中回复。

使用关键字,这就是我在我的个人助理中使用的。 我这样做的方式是,我创建了一个包含多个字符串的列表,如下所示:

Hello_Keywords = ["Hello", "Cheers", "How are you"]
Jokes_Keywords = ["Tell me a joke", "Can you tell me a joke", "Make me laugh"
def Hello():
    print("Hello there")

def Jokes():
    print("Why did the chicken cross the road?")
    time.sleep(3) 
    print("To get to the other side!")

if User in Hello_KeyWords
   Hello()
elif User in Jokes_Keywords
   Jokes()
else:
    print("Excuse me what?")
然后,您要做的是将这些列表分配给函数。比如说:

Hello_Keywords = ["Hello", "Cheers", "How are you"]
Jokes_Keywords = ["Tell me a joke", "Can you tell me a joke", "Make me laugh"
def Hello():
    print("Hello there")

def Jokes():
    print("Why did the chicken cross the road?")
    time.sleep(3) 
    print("To get to the other side!")

if User in Hello_KeyWords
   Hello()
elif User in Jokes_Keywords
   Jokes()
else:
    print("Excuse me what?")
现在,为了回答您关于维基百科的具体问题,您可以做以下几点:

User = "What is Python"

if "what is" in User:
    User = User.split("is")
    User = User[1].strip()
    User = wikipedia.summary(User)
    print(User)
上面的代码将检查用户是否说了
“what is”
,然后将变量拆分成一个列表,
[“what”,“Python”]
,然后取第二部分(用户[1]),在维基百科上查找,然后打印出来。显然,您也可以使用其他关键字来实现这一点,例如
“who is”
“在维基百科中搜索”
,等等


这就是我所用的,我希望它有帮助

使用关键字,这就是我为我的私人助理使用的关键字。 我这样做的方式是,我创建了一个包含多个字符串的列表,如下所示:

Hello_Keywords = ["Hello", "Cheers", "How are you"]
Jokes_Keywords = ["Tell me a joke", "Can you tell me a joke", "Make me laugh"
def Hello():
    print("Hello there")

def Jokes():
    print("Why did the chicken cross the road?")
    time.sleep(3) 
    print("To get to the other side!")

if User in Hello_KeyWords
   Hello()
elif User in Jokes_Keywords
   Jokes()
else:
    print("Excuse me what?")
然后,您要做的是将这些列表分配给函数。比如说:

Hello_Keywords = ["Hello", "Cheers", "How are you"]
Jokes_Keywords = ["Tell me a joke", "Can you tell me a joke", "Make me laugh"
def Hello():
    print("Hello there")

def Jokes():
    print("Why did the chicken cross the road?")
    time.sleep(3) 
    print("To get to the other side!")

if User in Hello_KeyWords
   Hello()
elif User in Jokes_Keywords
   Jokes()
else:
    print("Excuse me what?")
现在,为了回答您关于维基百科的具体问题,您可以做以下几点:

User = "What is Python"

if "what is" in User:
    User = User.split("is")
    User = User[1].strip()
    User = wikipedia.summary(User)
    print(User)
上面的代码将检查用户是否说了
“what is”
,然后将变量拆分成一个列表,
[“what”,“Python”]
,然后取第二部分(用户[1]),在维基百科上查找,然后打印出来。显然,您也可以使用其他关键字来实现这一点,例如
“who is”
“在维基百科中搜索”
,等等

这就是我所用的,我希望它有帮助