做者|Dipesh Pal
编译|Flin
来源|analyticsvidhyapython
虚拟助手(也称为AI助手或数字助手)是一款应用程序,能够理解天然语言的语音命令并为用户完成任务。git
咱们应该都知道什么是虚拟助手。打开手机并说“ Ok Google”或“ Hey Siri”。Google助手,Siri,Alexa都是虚拟助手的示例。github
演示和编码YouTube视频:web
咱们要作什么正则表达式
代码说明chrome
完整的代码浏览器
GitHub储存库app
你如何贡献dom
参考文献机器学习
咱们的虚拟助手将可以执行如下操做:
天气预报,启动游戏,启动Windows应用程序,打开网站,告诉你几乎你所要求的一切,告诉你日期和时间,问候,新闻等。
你能够与笔记本电脑的麦克风/控制台进行交互。助手生成的响应将显示在控制台上,或者经过扬声器直接说出来。
将来的可能:自拍,与人聊天更多,等等。
让咱们一块儿来建立本身的虚拟助手。
pip install JarvisAI
这是我建立的最新虚拟助手模块。它提供任何虚拟助手的基本功能。前提条件是Python版本 > 3.6。
安装库后,你能够导入模块
import JarvisAI obj = JarvisAI.JarvisAssistant() response = obj.mic_input() print(response)
功能经过方法名称清除。例如,你能够检查代码。
在这里阅读更多关于它的信息
你也能够在这里为这个存储库作贡献。
导包
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('weather|temperature', res): city = res.split(' ')[-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('news', res): news_res = obj.news() pprint.pprint(news_res) t2s(f"I have found {len(news_res)} news. You can read it. Let me tell you first 2 of them") t2s(news_res[0]) t2s(news_res[1])
讲述几乎全部内容:它将从维基百科中获取前500个字符,并将它们做为字符串返回。你能够使用'obj.tell_me(topic)'。
你须要将“主题”传递给“ tell_me(topic = topic)”。主题是你想知道的关键字。
while True: res = obj.mic_input() if re.search('tell me about', res): topic = res.split(' ')[-1] wiki_res = obj.tell_me(topic) print(wiki_res) t2s(wiki_res)
日期和时间:它将告诉你系统的当前日期和时间。
while True: res = obj.mic_input() if re.search('date', res): date = obj.tell_me_date() print(date) print(t2s(date)) if re.search('time', res): time = obj.tell_me_time() print(time) t2s(time)
打开任何网站:此'obj.website_opener(domain)'将为你打开任何网站。你只须要从用户输入中获取domain,而后传递给'obj.website_opener(domain)'。它将在你的默认浏览器中打开网站。
while True: res = obj.mic_input() if re.search('open', res): domain = res.split(' ')[-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('launch', res): dict_app = { 'chrome': 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', 'epic games': 'C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe' } app = res.split(' ', 1)[1] path = dict_app.get(app) if path is None: t2s('Application path not found') print('Application path not found') else: t2s('Launching: ' + app) obj.launch_any_app(path_of_app=path)
问候和聊天,你如今能够像这样建立问候和聊天。
我正在 https://pypi.org/project/JarvisAI/ 上使用Tensorflow添加聊天功能。你能够为使其更好而作出贡献。
while True: res = obj.mic_input() if re.search('hello', res): print('Hi') t2s('Hi') if re.search('how are you', res): li = ['good', 'fine', 'great'] response = random.choice(li) print(f"I am {response}") t2s(f"I am {response}") if re.search('your name|who are you', res): print("My name is Jarvis, I am your personal assistant") t2s("My name is Jarvis, I am your personal assistant")
问“你能作什么?”:在这里,咱们只是使用“ obj.t2s()”来发表讲话。若是你了解python,则能够轻松理解如下代码
while True: res = obj.mic_input() if re.search('what can you do', res): li_commands = { "open websites": "Example: 'open youtube.com", "time": "Example: 'what time it is?'", "date": "Example: 'what date it is?'", "launch applications": "Example: 'launch chrome'", "tell me": "Example: 'tell me about India'", "weather": "Example: 'what weather/temperature in Mumbai?'", "news": "Example: 'news for today' ", } ans = """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-""" print(ans) pprint.pprint(li_commands) t2s(ans)
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('weather|temperature', res): city = res.split(' ')[-1] weather_res = obj.weather(city=city) print(weather_res) t2s(weather_res) if re.search('news', res): news_res = obj.news() pprint.pprint(news_res) t2s(f"I have found {len(news_res)} news. You can read it. Let me tell you first 2 of them") t2s(news_res[0]) t2s(news_res[1]) if re.search('tell me about', res): topic = res.split(' ')[-1] wiki_res = obj.tell_me(topic) print(wiki_res) t2s(wiki_res) if re.search('date', res): date = obj.tell_me_date() print(date) print(t2s(date)) if re.search('time', res): time = obj.tell_me_time() print(time) t2s(time) if re.search('open', res): domain = res.split(' ')[-1] open_result = obj.website_opener(domain) print(open_result) if re.search('launch', res): dict_app = { 'chrome': 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', 'epic games': 'C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe' } app = res.split(' ', 1)[1] path = dict_app.get(app) if path is None: t2s('Application path not found') print('Application path not found') else: t2s('Launching: ' + app) obj.launch_any_app(path_of_app=path) if re.search('hello', res): print('Hi') t2s('Hi') if re.search('how are you', res): li = ['good', 'fine', 'great'] response = random.choice(li) print(f"I am {response}") t2s(f"I am {response}") if re.search('your name|who are you', res): print("My name is Jarvis, I am your personal assistant") t2s("My name is Jarvis, I am your personal assistant") if re.search('what can you do', res): li_commands = { "open websites": "Example: 'open youtube.com", "time": "Example: 'what time it is?'", "date": "Example: 'what date it is?'", "launch applications": "Example: 'launch chrome'", "tell me": "Example: 'tell me about India'", "weather": "Example: 'what weather/temperature in Mumbai?'", "news": "Example: 'news for today' ", } ans = """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-""" print(ans) pprint.pprint(li_commands) t2s(ans)
你能够随意使用个人代码。若是你喜欢个人做品,请为其点亮star;若是你喜欢,请在YouTube上订阅。
只需克隆存储库
而后运行pip install -r requirements.txt
它将自动安装全部内容。
只需打开此GitHub存储库,阅读该书,你将了解你如何作出贡献。
你的贡献将反映在这个项目上。
GitHub存储库和代码
贡献的GitHub Pypi存储库
JarvisAI库
YouTube频道
演示和代码(YouTube)
原文连接:https://www.analyticsvidhya.com/blog/2020/09/ai-virtual-assistant-using-python/
欢迎关注磐创AI博客站:
http://panchuang.net/
sklearn机器学习中文官方文档:
http://sklearn123.com/
欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/