关于人工智能:如何使用Python创建AI虚拟助手

0次阅读

共计 6874 个字符,预计需要花费 18 分钟才能阅读完成。

作者 |Dipesh Pal
编译 |Flin
起源 |analyticsvidhya

介绍

虚构助手(也称为 AI 助手或数字助手)是一款应用程序,能够了解自然语言的语音命令并为用户实现工作。

咱们应该都晓得什么是虚构助手。关上手机并说“Ok Google”或“Hey Siri”。Google 助手,Siri,Alexa 都是虚构助手的示例。

演示和编码 YouTube 视频:

  • https://youtu.be/LliTjuxDw_o

内容

  1. 咱们要做什么
  2. 代码阐明
  3. 残缺的代码
  4. GitHub 储存库
  5. 你如何奉献
  6. 参考文献

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)

性能通过办法名称革除。例如,你能够查看代码。

  1. mic_input
  2. text2speech
  3. shutdown
  4. website_opener
  5. send_mail
  6. tell_me_date
  7. tell_me_time
  8. launch_any_app
  9. weather
  10. news
  11. tell_me

在这里浏览更多对于它的信息

  • https://pypi.org/project/Jarv…

你也能够在这里为这个存储库做奉献。

  • https://github.com/Dipeshpal/…

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('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/Jarv… 上应用 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)

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('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)

4. Github 仓库

你能够随便应用我的代码。如果你喜爱我的作品,请为其点亮 star;如果你喜爱,请在 YouTube 上订阅。

只需克隆存储库

  • https://github.com/Dipeshpal/…

而后运行pip install -r requirements.txt

它将主动装置所有内容。

5. 如何奉献

只需关上此 GitHub 存储库,浏览该书,你将理解你如何做出奉献。

  • https://github.com/Dipeshpal/…

你的奉献将反映在这个我的项目上。

  • https://pypi.org/project/Jarv…

6. 参考

GitHub 存储库和代码

  • https://github.com/Dipeshpal/…

奉献的 GitHub Pypi 存储库

  • https://github.com/Dipeshpal/…

JarvisAI 库

  • https://pypi.org/project/Jarv…

YouTube 频道

  • https://www.youtube.com/DIPES…

演示和代码(YouTube)

  • https://youtu.be/LliTjuxDw_o

原文链接:https://www.analyticsvidhya.c…

欢送关注磐创 AI 博客站:
http://panchuang.net/

sklearn 机器学习中文官网文档:
http://sklearn123.com/

欢送关注磐创博客资源汇总站:
http://docs.panchuang.net/

正文完
 0