作者|Dipesh Pal
编译|Flin
来源|analyticsvidhya
介绍
虚拟助手(也称为AI助手或数字助手)是一款应用程序,可以理解自然语言的语音命令并为用户完成任务。
我们应该都知道什么是虚拟助手。打开手机并说“ Ok Google”或“ Hey Siri”。Google助手,Siri,Alexa都是虚拟助手的示例。
演示和编码YouTube视频:
内容
-
我们要做什么
-
代码说明
-
完整的代码
-
GitHub储存库
-
你如何贡献
-
参考文献
1.我们要做什么
我们的虚拟助手将能够执行以下操作:
天气预报,启动游戏,启动Windows应用程序,打开网站,告诉你几乎你所要求的一切,告诉你日期和时间,问候,新闻等。
你可以与笔记本电脑的麦克风/控制台进行交互。助手生成的响应将显示在控制台上,或者通过扬声器直接说出来。
未来的可能:自拍,与人聊天更多,等等。
2. 代码说明
让我们一起来创建自己的虚拟助手。
- 所有代码都可以在我的GitHub上找到。
- 我的频道上还提供了演示YouTube视频和代码YouTube视频。
- 所需的链接和软件包如下所述。
- 如果你愿意分享,我将不胜感激。
2.1 所需的软件包和库
pip install JarvisAI
这是我创建的最新虚拟助手模块。它提供任何虚拟助手的基本功能。前提条件是Python版本 > 3.6。
用法和功能
安装库后,你可以导入模块
import JarvisAI
obj = JarvisAI.JarvisAssistant()
response = obj.mic_input()
print(response)
功能通过方法名称清除。例如,你可以检查代码。
- mic_input
- text2speech
- shutdown
- website_opener
- send_mail
- tell_me_date
- tell_me_time
- launch_any_app
- weather
- news
- tell_me
在这里阅读更多关于它的信息
你也可以在这里为这个存储库做贡献。
2.2 编码
导包
import JarvisAI
import re
import pprint
import random
根据文档创建 JarvisAI的对象
obj = JarvisAI.JarvisAssistant()
我们已经创建了这个“t2s(text)”函数。这会将任何文本转换为语音。我们将使用(调用)此函数的整个程序从文本产生语音。
def t2s(text):
obj.text2speech(text)
我们希望不断听取用户的输入,因此此“ mic_input() ”将尝试从计算机的麦克风中连续获取音频。它将处理音频并在“ res”变量中返回文本。我们可以使用此“ res”变量根据用户输入执行某些操作。
while True:
res = obj.mic_input()
天气预报:我们使用正则表达式来匹配用户输入中的查询。如果在用户输入“ res”中找到“天气”或“温度”,则我们要进行天气预报。无需从头开始编写东西,只需调用“ obj.weather(city = city)”即可。
你只需要从用户输入中获取城市并将其传递给天气功能即可。它会告诉你你所在城市的天气预报。
我们可以将此返回的“ weather_res”传递到“ t2s(weather_res)”,以从“ weather_res”字符串中产生语音。
while True:
res = obj.mic_input()
if re.search(&39;weather|temperature&39;, res):
city = res.split(&39; &39;)[-1]
weather_res = obj.weather(city=city)
print(weather_res)
t2s(weather_res)
新闻:与上述类似,匹配用户输入“ res”中的“新闻”一词。如果匹配,则调用“ obj.news”。
它将返回15条新闻作为字符串列表。因此,我们可以将新闻作为“ news_res [0]”来获取,并将其传递给“ t2s(news_res [0])”。
while True:
res = obj.mic_input()
if re.search(&39;news&39;, res):
news_res = obj.news()
pprint.pprint(news_res)
t2s(f&34;I have found {len(news_res)} news. You can read it. Let me tell you first 2 of them&34;)
t2s(news_res[0])
t2s(news_res[1])
讲述几乎所有内容:它将从维基百科中获取前500个字符,并将它们作为字符串返回。你可以使用&39;obj.tell_me(topic)&39;。
你需要将“主题”传递给“ tell_me(topic = topic)”。主题是你想知道的关键字。
while True:
res = obj.mic_input()
if re.search(&39;tell me about&39;, res):
topic = res.split(&39; &39;)[-1]
wiki_res = obj.tell_me(topic)
print(wiki_res)
t2s(wiki_res)
日期和时间:它将告诉你系统的当前日期和时间。
while True:
res = obj.mic_input()
if re.search(&39;date&39;, res):
date = obj.tell_me_date()
print(date)
print(t2s(date))
if re.search(&39;time&39;, res):
time = obj.tell_me_time()
print(time)
t2s(time)
打开任何网站:此&39;obj.website_opener(domain)&39;将为你打开任何网站。你只需要从用户输入中获取domain,然后传递给&39;obj.website_opener(domain)&39;。它将在你的默认浏览器中打开网站。
while True:
res = obj.mic_input()
if re.search(&39;open&39;, res):
domain = res.split(&39; &39;)[-1]
open_result = obj.website_opener(domain)
print(open_result)
启动任何应用程序,游戏等:
这有点棘手,在“ obj.launch_any_app(path_of_app = path)”中,你需要传递“ .exe”文件路径的函数。
因此,我们创建了“ dict_app”字典,其中以“应用名称”作为键,以“路径”作为值。我们可以使用此“ dict_app”进行查找。如果字典中存在用户输入的应用程序,那么我们将通过获取路径来打开它。
以下示例仅适用于Chrome和Epic Games。
while True:
res = obj.mic_input()
if re.search(&39;launch&39;, res):
dict_app = {
&39;chrome&39;: &39;C:\Program Files (x86)\Google\Chrome\Application\chrome.exe&39;,
&39;epic games&39;: &39;C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe&39;
}
app = res.split(&39; &39;, 1)[1]
path = dict_app.get(app)
if path is None:
t2s(&39;Application path not found&39;)
print(&39;Application path not found&39;)
else:
t2s(&39;Launching: &39; + app)
obj.launch_any_app(path_of_app=path)
问候和聊天,你现在可以像这样创建问候和聊天。
我正在 https://pypi.org/project/JarvisAI/ 上使用Tensorflow添加聊天功能。你可以为使其更好而做出贡献。
while True:
res = obj.mic_input()
if re.search(&39;hello&39;, res):
print(&39;Hi&39;)
t2s(&39;Hi&39;)
if re.search(&39;how are you&39;, res):
li = [&39;good&39;, &39;fine&39;, &39;great&39;]
response = random.choice(li)
print(f&34;I am {response}&34;)
t2s(f&34;I am {response}&34;)
if re.search(&39;your name|who are you&39;, res):
print(&34;My name is Jarvis, I am your personal assistant&34;)
t2s(&34;My name is Jarvis, I am your personal assistant&34;)
问“你能做什么?”:在这里,我们只是使用“ obj.t2s()”来发表讲话。如果你了解python,则可以轻松理解以下代码
while True:
res = obj.mic_input()
if re.search(&39;what can you do&39;, res):
li_commands = {
&34;open websites&34;: &34;Example: &39;open youtube.com&34;,
&34;time&34;: &34;Example: &39;what time it is?&39;&34;,
&34;date&34;: &34;Example: &39;what date it is?&39;&34;,
&34;launch applications&34;: &34;Example: &39;launch chrome&39;&34;,
&34;tell me&34;: &34;Example: &39;tell me about India&39;&34;,
&34;weather&34;: &34;Example: &39;what weather/temperature in Mumbai?&39;&34;,
&34;news&34;: &34;Example: &39;news for today&39; &34;,
}
ans = &34;&34;&34;I can do lots of things, for example you can ask me time, date, weather in your city,
I can open websites for you, launch application and more. See the list of commands-&34;&34;&34;
print(ans)
pprint.pprint(li_commands)
t2s(ans)
3.完整的代码
import JarvisAI
import re
import pprint
import random
obj = JarvisAI.JarvisAssistant()
def t2s(text):
obj.text2speech(text)
while True:
res = obj.mic_input()
if re.search(&39;weather|temperature&39;, res):
city = res.split(&39; &39;)[-1]
weather_res = obj.weather(city=city)
print(weather_res)
t2s(weather_res)
if re.search(&39;news&39;, res):
news_res = obj.news()
pprint.pprint(news_res)
t2s(f&34;I have found {len(news_res)} news. You can read it. Let me tell you first 2 of them&34;)
t2s(news_res[0])
t2s(news_res[1])
if re.search(&39;tell me about&39;, res):
topic = res.split(&39; &39;)[-1]
wiki_res = obj.tell_me(topic)
print(wiki_res)
t2s(wiki_res)
if re.search(&39;date&39;, res):
date = obj.tell_me_date()
print(date)
print(t2s(date))
if re.search(&39;time&39;, res):
time = obj.tell_me_time()
print(time)
t2s(time)
if re.search(&39;open&39;, res):
domain = res.split(&39; &39;)[-1]
open_result = obj.website_opener(domain)
print(open_result)
if re.search(&39;launch&39;, res):
dict_app = {
&39;chrome&39;: &39;C:\Program Files (x86)\Google\Chrome\Application\chrome.exe&39;,
&39;epic games&39;: &39;C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe&39;
}
app = res.split(&39; &39;, 1)[1]
path = dict_app.get(app)
if path is None:
t2s(&39;Application path not found&39;)
print(&39;Application path not found&39;)
else:
t2s(&39;Launching: &39; + app)
obj.launch_any_app(path_of_app=path)
if re.search(&39;hello&39;, res):
print(&39;Hi&39;)
t2s(&39;Hi&39;)
if re.search(&39;how are you&39;, res):
li = [&39;good&39;, &39;fine&39;, &39;great&39;]
response = random.choice(li)
print(f&34;I am {response}&34;)
t2s(f&34;I am {response}&34;)
if re.search(&39;your name|who are you&39;, res):
print(&34;My name is Jarvis, I am your personal assistant&34;)
t2s(&34;My name is Jarvis, I am your personal assistant&34;)
if re.search(&39;what can you do&39;, res):
li_commands = {
&34;open websites&34;: &34;Example: &39;open youtube.com&34;,
&34;time&34;: &34;Example: &39;what time it is?&39;&34;,
&34;date&34;: &34;Example: &39;what date it is?&39;&34;,
&34;launch applications&34;: &34;Example: &39;launch chrome&39;&34;,
&34;tell me&34;: &34;Example: &39;tell me about India&39;&34;,
&34;weather&34;: &34;Example: &39;what weather/temperature in Mumbai?&39;&34;,
&34;news&34;: &34;Example: &39;news for today&39; &34;,
}
ans = &34;&34;&34;I can do lots of things, for example you can ask me time, date, weather in your city,
I can open websites for you, launch application and more. See the list of commands-&34;&34;&34;
print(ans)
pprint.pprint(li_commands)
t2s(ans)
4. Github仓库
你可以随意使用我的代码。如果你喜欢我的作品,请为其点亮star;如果你喜欢,请在YouTube上订阅。
只需克隆存储库
pip install -r requirements.txt
它将自动安装所有内容。
5. 如何贡献
只需打开此GitHub存储库,阅读该书,你将了解你如何做出贡献。
你的贡献将反映在这个项目上。
6. 参考
GitHub存储库和代码
贡献的GitHub Pypi存储库
JarvisAI库
YouTube频道
演示和代码(YouTube)
欢迎关注磐创AI博客站:
http://panchuang.net/
sklearn机器学习中文官方文档:
http://sklearn123.com/
欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/