python开发助理s
An AI personal assistant is a piece of software that understands verbal or written commands and completes task assigned by the client. It is an example of weak AI that is it can only execute and perform quest designed by the user.
AI个人助理是一种软件,可以理解口头或书面命令并完成客户端分配的任务。 这是弱AI的一个示例,它只能执行和执行用户设计的任务。
Want to build your own personal AI assistant like Apple Siri, Microsoft Cortana and Google assistant?
是否想建立自己的个人AI助手,例如Apple Siri,Microsoft Cortana和Google助手?
You can check out this blog to build one in a few simple steps!
您可以通过几个简单的步骤查看此博客以构建一个博客!
With the python programming language, a script most commonly used by the developers can be used to build your personal AI assistant to perform task designed by the users.
通过python编程语言,开发人员最常用的脚本可用于构建您的个人AI助手来执行用户设计的任务。
Now, let’s write a script for our personal voice assistant using python.
现在,让我们使用python为我们的个人语音助手编写脚本。
技能专长 (Skills:)
The implemented voice assistant can perform the following task it can open YouTube, Gmail, Google chrome and stack overflow. Predict current time, take a photo, search Wikipedia to abstract required data, predict weather in different cities, get top headline news from Times of India and can answer computational and geographical questions too.
已实现的语音助手可以执行以下任务,可以打开YouTube,Gmail,Google chrome和堆栈溢出。 预测当前时间,拍照,搜索Wikipedia以提取所需的数据,预测不同城市的天气,从《印度时报》获得头条新闻,还可以回答计算和地理问题。
The following queries of the voice assistant can be manipulated as per the users need.
语音助手的以下查询可以根据用户需要进行处理。
所需包装: (Packages required:)
To build a personal voice assistant it’s necessary to install the following packages in your system using the pip command.
要构建个人语音助手,必须使用pip命令在系统中安装以下软件包。
1) Speech recognition — Speech recognition is an important feature used in house automation and in artificial intelligence devices. The main function of this library is it tries to understand whatever the humans speak and converts the speech to text.
1) 语音识别 -语音识别是房屋自动化和人工智能设备中使用的重要功能。 该库的主要功能是尝试理解人类的讲话并将语音转换为文本。
2) pyttsx3 — pyttxs3 is a text to speech conversion library in python. This package supports text to speech engines on Mac os x, Windows and on Linux.
2) pyttsx3 — pyttxs3是python中的文本到语音转换库。 此软件包在Mac OS X,Windows和Linux上支持文本到语音引擎。
3) wikipedia — Wikipedia is a multilingual online encyclopedia used by many people from academic community ranging from freshmen to students to professors who wants to gain information over a particular topic. This package in python extracts data’s required from Wikipedia.
3) Wikipedia — Wikipedia是一种多语言的在线百科全书,许多学术界人士使用它,从新生,学生到想获取特定主题信息的教授。 python中的此软件包从Wikipedia中提取了所需的数据。
4) ecapture — This module is used to capture images from your camera
4) 捕获 —此模块用于捕获相机中的图像
5) datetime — This is an inbuilt module in python and it works on date and time
5) datetime —这是python中的内置模块,可按日期和时间运行
6) os — This module is a standard library in python and it provides the function to interact with operating system
6) os —该模块是python中的标准库,它提供与操作系统交互的功能
7) time — The time module helps us to display time
7) 时间 -时间模块可帮助我们显示时间
8) Web browser — This is an in-built package in python. It extracts data from the web
8) Web浏览器 -这是python中的内置软件包。 它从网上提取数据
9) Subprocess — This is a standard library use to process various system commands like to log off or to restart your PC.
9) 子进程 —这是一个标准库,用于处理各种系统命令,例如注销或重新启动PC。
10) Json- The json module is used for storing and exchanging data.
10) Json -json模块用于存储和交换数据。
11) request- The request module is used to send all types of HTTP request. Its accepts URL as parameters and gives access to the given URL’S.
11) request-请求模块用于发送所有类型的HTTP请求。 它接受URL作为参数,并允许访问给定的URL。
12) wolfram alpha — Wolfram Alpha is an API which can compute expert-level answers using Wolfram’s algorithms, knowledge base and AI technology. It is made possible by the Wolfram Language.
12) wolfram alpha — Wolfram Alpha是一种API,可以使用Wolfram的算法,知识库和AI技术来计算专家级答案。 Wolfram语言使之成为可能。
实现方式: (Implementation:)
Import the following libraries
导入以下库
设置语音引擎: (Setting up the speech engine:)
The pyttsx3 module is stored in a variable name engine.
pyttsx3模块存储在变量名引擎中。
Sapi5 is a Microsoft Text to speech engine used for voice recognition.
Sapi5是用于语音识别的Microsoft文本到语音引擎。
The voice Id can be set as either 0 or 1,
语音ID可以设置为0或1,
0 indicates Male voice
0表示男声
1 indicates Female voice
1表示女性声音
Now define a function speak which converts text to speech. The speak function takes the text as its argument,further initialize the engine.
现在定义一个功能speak ,它将文本转换为语音。 语音功能以文本为参数,进一步初始化引擎。
runAndWait: This function Blocks while processing all currently queued commands. It Invokes callbacks for engine notifications appropriately and returns back when all commands queued before this call are emptied from the queue.
runAndWait:此功能在处理所有当前排队的命令时阻塞。 它会适当地调用引擎通知的回调,并在从队列中清空此调用之前排队的所有命令时返回。
启动一个功能来迎接用户: (Initiate a function to greet the user:)
Define a function wishMe for the AI assistant to greet the user.
为AI助手定义一个功能wishMe来迎接用户。
The now().hour function abstract’s the hour from the current time.
now()。hour函数从当前时间开始抽象小时。
If the hour is greater than zero and less than 12, the voice assistant wishes you with the message “Good Morning”.
如果小时数大于零且小于12,则语音助手希望您收到消息“早安”。
If the hour is greater than 12 and less than 18, the voice assistant wishes you with the following message “Good Afternoon”.
如果小时数大于12且小于18,则语音助手希望您收到以下消息“下午好”。
Else it voices out the message “Good evening”
否则,它会发出“晚安”的信息
为您的AI助手设置命令功能: (Setting up the command function for your AI assistant :)
Define a function takecommand for the AI assistant to understand and to accept human language. The microphone captures the human speech and the recognizer recognizes the speech to give a response.
为AI助手定义一个功能接受命令 ,以理解和接受人类语言。 麦克风捕获人类语音,识别器识别语音以做出响应。
The exception handling is used to handle the exception during the run time error and,the recognize_google function uses google audio to recognize speech.
异常处理用于在运行时错误期间处理异常, identify_google函数使用google音频识别语音。
主要功能: (The Main function:)
The main function starts from here,the commands given by the humans is stored in the variable statement.
主要功能从这里开始,人类给出的命令存储在变量语句中 。
If the following trigger words are there in the statement given by the users it invokes the virtual assistant to speak the below following commands.
如果用户给出的语句中包含以下触发词,它将调用虚拟助手说出以下命令。
技能1-从Wikipedia获取数据: (Skill 1 -Fetching data from Wikipedia:)
The following commands helps to extract information from wikipedia. The wikipedia.summary() function takes two arguments, the statement given by the user and how many sentences from wikipedia is needed to be extracted is stored in a variable result.
以下命令有助于从Wikipedia中提取信息。 wikipedia.summary()函数采用两个参数,用户给出的语句以及需要从Wikipedia中提取多少个句子存储在变量结果中。
技能2-访问Web浏览器-Google chrome,G-Mail和YouTube: (Skill 2 -Accessing the Web Browsers — Google chrome , G-Mail and YouTube:)
The web browser extracts data from web. The open_new_tab function accepts URL as a parameter that needs to be accessed.
Web浏览器从Web提取数据。 open_new_tab函数接受URL作为需要访问的参数。
The Python time sleep function is used to add delay in the execution of a program. We can use this function to halt the execution of the program for given time in seconds.
Python时间睡眠功能用于增加程序执行的延迟。 我们可以使用此函数在给定的时间(以秒为单位)中暂停程序的执行。
技能3-预测时间: (Skill 3 -Predicting time:)
The current time is abstracted from datetime.now() function which displays the hour, minute and second and is stored in a variable name strTime.
当前时间是从datetime.now()函数中抽象出来的,该函数显示时,分和秒,并存储在变量名strTime中。
技能4-获取最新消息: (Skill 4 -To fetch latest news:)
If the user wants to know the latest news , The voice assistant is programmed to fetch top headline news from Time of India by using the web browser function.
如果用户想知道最新消息,则可以对语音助手进行编程,以使用网络浏览器功能从《印度时间》中获取头条新闻。
技能5-拍摄照片: (Skill 5 -Capturing photo:)
The ec.capture() function is used to capture images from your camera. It accepts 3 parameter.
ec.capture()函数用于从相机捕获图像。 它接受3个参数。
Camera index — The first connected webcam will be indicated as index 0 and the next webcam will be indicated as index 1
摄像机索引 —第一个连接的网络摄像机将显示为索引0,下一个网络摄像机将显示为索引1
Window name — It can be a variable or a string. If you don’t wish to see the window, type as False
窗口名称 -可以是变量或字符串。 如果您不希望看到该窗口,请输入False
Save name — A name can be given to the image and if you don’t want to save the image, type as false
保存名称 -可以为图像指定名称,如果您不想保存图像,请输入false
技能6-从网络搜索数据: (Skill 6-Searching data from web:)
From the web browser you can search required data by passing the user statement (command) to the open_new_tab() function.
在Web浏览器中,您可以通过将用户语句(命令)传递给open_new_tab()函数来搜索所需的数据。
User: Hey G-One, please search images of butterfly
用户 : 嗨,G-One,请搜索蝴蝶的图片
The Voice assistant opens the google window & fetches butterfly images from web.
语音助手会打开google窗口并从网络中获取蝴蝶图像。
技能7-设置AI助手回答地理和计算问题: (Skill 7- Setting your AI assistant to answer geographical and computational questions:)
Here we can use a third party API called Wolfram alpha API to answer computational and geographical questions.It is made possible by the Wolfram Language. The client is an instance (class) created for wolfram alpha. The res variable stores the response given by the wolfram alpha.
在这里,我们可以使用称为Wolfram alpha API的第三方API来回答计算和地理问题,这是由Wolfram语言实现的。 客户端是为Wolfram alpha创建的实例(类)。 res变量存储Wolfram alpha给出的响应。
To access the wolfram alpha API an unique App ID is required which can be generated by the following ways:
要访问wolfram alpha API,需要唯一的应用程序ID,可以通过以下方式生成:
- Login to the official page of wolfram alpha and create an account if you do not possess one. 登录到Wolfram alpha的官方页面,并创建一个帐户(如果您没有该帐户)。

2. Sign in using your wolfram ID
2.使用您的Wolfram ID登录

3. Now you will view the homepage of the website. Head to the account section in the top right corner where you see your email. In the drop down menu, select the My Apps (API) option.
3.现在,您将查看网站的主页。 转到右上角的帐户部分,您会在其中看到您的电子邮件。 在下拉菜单中,选择我的应用程序(API)选项。

4. You will see this following window, now click Get APP_ID button
4.您将看到以下窗口,现在单击“获取APP_ID”按钮

5. Now you will get the following dialog box, give a suitable name and description and click the App ID button, an App ID will be generated and this is an unique ID. Using the App Id use can access the Wolfram alpha API.
5.现在,您将获得以下对话框,提供适当的名称和描述,然后单击“ App ID”按钮,将生成一个App ID,这是唯一的ID。 使用App ID可以访问Wolfram alpha API。

Human: Hey G-One ,what is the capital of California?
人 :嘿,G-One,加州的首都是什么?
G-One Voice assistant: Sacramento, United States of America
G-One语音助手: 美国萨克拉曼多
技能8-额外功能: (Skill 8- Extra features:)
It would be interesting to program your AI assistant to answer the following questions like what it can and who created it,isn't it?
对您的AI助手进行编程以回答以下问题,例如它可以做什么以及由谁创建,是不是很有趣?
技能9-预测天气: (Skill 9- To forecast weather:)
Now to program your AI assistant to detect weather we need to generate an API key from Open Weather map.
现在要对您的AI助手进行编程以检测天气,我们需要从“开放天气”地图中生成API密钥。
Open weather map is an online service which provides weather data. By generating an API ID in the official website you can use the APP_ID to make your voice assistant detect weather of all places whenever required. The necessary modules needed to be imported for this weather detection is json and request module.
开放式气象图是提供天气数据的在线服务。 通过在官方网站上生成API ID,您可以在需要时使用APP_ID使语音助手检测所有地方的天气。 为此天气检测需要导入的必要模块是json和request模块。
The city_name variable takes the command given by the human using the takeCommand() function.
city_name变量采用人类使用takeCommand()函数给出的命令。
The get method of request module returns a response object. And the json methods of response object converts json format data into python format.
请求模块的get方法返回一个响应对象。 响应对象的json方法将json格式的数据转换为python格式。
The variable X contains list of nested dictionaries which checks whether the value of ‘COD’ is 404 or not that is if the city is found or not.
变量X包含嵌套字典列表,该列表检查“ COD”的值是否为404,即是否找到城市。
The values such as temperature and humidity is stored in the main key of variable Y.
温度和湿度等值存储在变量Y的主键中。
Human: Hey G-One ,I want to get the weather data
人: 嘿,G-One,我想获取天气数据
G-One: What is the city name?
G-One: 城市名称是什么?
Human: Himachal Pradesh
人类: 喜马al尔邦
G-One: Temperature in kelvin unit is 301.09 , Humidity in percentage is 52 and Description is light rain.
G-One: 以开尔文为单位的温度为301.09,相对湿度为52,描述为小雨。
技能10-要注销PC: (Skill 10- To log off your PC:)
The subprocess.call() function here is used to process the system function to log off or to turn off your PC. This invokes your AI assistant to automatically turn off your PC.
这里的subprocess.call()函数用于处理系统功能以注销或关闭PC。 这将调用您的AI助手来自动关闭PC。
Hurray , We have finally built our own AI voice assistant . Further you can still add more functionalities to your AI voice assistant to perform more task.
华友世纪,我们终于建立了自己的AI语音助手。 此外,您仍然可以向AI语音助手添加更多功能以执行更多任务。

Check out my GitHub profile for code:
查看我的GitHub个人资料以获取代码:
https://github.com/mmirthula02/AI-Personal-Voice-assistant-using-Python
https://github.com/mmirthula02/AI-Personal-Voice-assistant-using-Python
Happy Coding !!
快乐编码!
翻译自: https://towardsdatascience.com/how-to-build-your-own-ai-personal-assistant-using-python-f57247b4494b
python开发助理s