I assume that you know a little about the language python, and understand some of the basic syntax and functions used. Although I have tried to make this as simple and interesting as possible. Your feedback and contributions will be greatly appreciated. The program also has been adapted for the windows OS, to run this on Macintosh or Linux (Ubuntu) you may be required to make some changes to the application codes.
If you are new to python, you might want to ask what python is: Python is an interpreted, interactive, object-oriented programming language. Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. Python is still under development by a vast team of collaborators headed by GvR. It is available currently in two versions: Python 2.x & Python 3.x, and available for all operating systems on: http://www.python.org/ You might also ask the question of which version should I use:
- If you are working on a new, fully independent project, you can use 3.x
- If your work has dependencies it is safer to use 2.x… But for this project we will be making use of python 3.x since its relative new and all new users are likely to download this versions. Download and let’s proceed. FYI, I will be making use of the visual studio code text editor, python has it’s own text editor (IDLE) that comes with the python libraries and shell. You can access this searching with “IDLE” in windows.
What is a virtual assistant?
A virtual assistant also called a digital assistant, is an application program that can understand natural language (text or voice) and complete electronic tasks for the end user. Today’s virtual assistants are programmed with artificial intelligence, machine learning and voice recognition technology. As the end user interacts with his digital assistant, the AI programming uses sophisticated algorithms to learn from data input and become better at predicting the end user’s needs. Although our virtual assistant will only be performing some basic tasks like maths, finance, temperature and history we will be looking at adapting voice recognition technology to make it more robust and personal. Popular virtual assistants currently include Apple’s Siri, Google Now and Cortana, the digital assistant built into windows 8.1. Hope I have gotten you excited enough. Let’s start building!!!
The core of this application is the wolframalpha API (http://www.wolframalpha.com). Basically wolframalpha is a computational knowledge engine. It can get statistical data, places & geography, mathematics problem, money & finance and all sort of stuffs you can think of. To make use of this API, you’ve to sign up (free) to obtain your API key. To do do, proceed to http://products.wolframlpha.com/api/. After sign up, click on “Get an AppID”, and fill out the Application name, and Description and finally copy your AppID for use in the application. NB: Your AppID is confidential and you’ll do well to keep it so. To get the python wolframalpha API on your system OS, you can install using pip command. open your terminal, copy and paste the command below
pip install wolframalpha
N|B: The “pip” command is a special pipeline tool that comes with python 3.x, that enables us easily install any resources or modules to our system python libraries for local use. Now, we’re going to write a simple program to consume the wolframalpha API web services, to ensure it’s working properly.
import wolframalpha app_id = "YOUR-APP-ID-HERE" client = wolframalpha.Client(app_id) my_input = input("Question: ") res = client.query(my_input) answer = next(res.results).text print(answer)
The above code, shows the basic usage of the wolframalpha API in python 3.x. Save the code and run on your terminal. To run on terminal, navigate to the path you saved the program and run “Python ”. In my case, see below
When the program is run, you will see some results displayed on the terminal to give an idea of how the app is coming together.
Ok now we know what the wolframalpha API is and how to get data from it, we are going to see the Wikipedia API so we can get info from it.
Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia. It’s another core feature of this app. For more info visit https://pypi.python.org/pypi/wikipedia to try and get an understanding of what we are going to be doing with this API. To get the python wikipedia API on your system OS, you can install using pip command. open your terminal, copy and paste the command below
pip install wikipedia
As done initially, we’re going to write a simple program to consume the wikipedia API web services, to ensure it’s working properly.
import wikipedia while True: my_input = input("Question: ") print(wikipedia.summary(my_input))
The above code, shows the basic usage of the wikipedia API in python 3.x. Save the code and run on your terminal as before. The “While True” statement is added to enable the program to keep on asking a question to enable us get information more than once.
Let us look at more advanced features of the Wikipedia API that we can use to personalize our application. First off, we run the above program and ask a question, the results produced are very long and sometimes we may need to reduce it to two or three sentences. To do this see the code below.
import wikipedia while True: my_input = input("Question: ") print(wikipedia.summary(my_input, sentences=2))
Save and run again, and you’ll observe that the results are in two sentences only. Ok, now what if the user doesn’t speak English, we can easily modify an application to display information in another language using the “wikipedia.set_lang()” function. Usage: fr for French, es for Spanish, zh for Chinese, de for german. To do this see the code below for Spanish users;
import wikipedia while True: my_input = input("Question: ") wikipedia.set_lang("es") print(wikipedia.summary(my_input, sentences=2))
Save and run again.
Ok the next phase, assuming all the above is working properly, is to implement the wolframalpha and the Wikipedia API into one main application and put into a GUI (Graphical User Interface). We shall look at this in the next article here
Like this article? Follow @LK_wapiti on Twitter