You might be familiar with Watson as the computer that defeated Ken Jennings at Jeopardy, but that was only the beginning. Today, the Watson ecosystem spans the gamut from cognitive, language, vision developer APIs, health specific offerings in the areas of oncology, clinical trial matching, imaging, as well as general purpose products for enabling discovery of new insights, as well as engaging customers. When it comes to augmented your mobile app however, the ones that are of the most interest are the Watson Developer Cloud APIs. These fall into several categories:
- Data insights
For iOS developers, IBM recently unveiled the Swift SDK for Watson to allow you to easily call these APIs from within Xcode. I decided to try it out to see how easy it is to use. First you need to have a Bluemix account which will host the Watson service instance. Then you have to decide which service you wish to use. I decided to write an app that would analyze your email / message before you sent it for underlying emotion and sentiment. For example, before sending out an email to your girlfriend the app will help you determine if it is too negative, too disparaging, or maybe even too upbeat! In order to do that I wanted to use the Emotion Analysis service from Alchemy Language. Add the service, then get your API key credentials, you will need it later to call the service.
The first thing I did was go to the Github page for the Watson Developer Cloud Swift SDK and follow the instructions to install it in my project. The SDK uses Carthage to install itself and all the necessary dependencies. I found out shortly after running carthage update however that AlamoFire was failing to build. After some trial and error I determined that this was because it needed the latest version of Xcode 7.3 and I was running 7.2.1. So I updated Xcode, crossing my fingers that it would not break any of my existing projects. Thankfully the update proceeded successfully and I was able to install the SDK. However, upon perusing the classes and methods I realized that the Emotion Analysis feature was not available through the SDK. I guess it is labeled a beta feature, but still I had assumed the SDK would match the list of services available publicly. I thought I would try out some of the other language services instead so I attempted to instantiate an AlchemyLanguage object as per the documentation, but the TokenAuthenticationStrategy object doesn’t seem to exist so I was unable to proceed for.
let token = TokenAuthenticationStrategy(token: <API_KEY>)
So, I decided to go back to the tried and tested approach of calling the HTTP REST API instead. The SDK had pulled in AlamoFire so all I had to do was call it with the appropriate endpoint for the Emotion Analysis service.
The iOS app itself is very simple. There is a ViewController to enter your message text.
When you click on the Analyze button it takes you via a navigation controller to second ViewController with the results of the analysis. This analysis is performed by calling the Emotion Analysis REST endpoint and passing in the text of the UITTextView. AlamoFire makes this easy with a GET call
var parameters = [ "apikey":"<your API key here>", "outputMode": "json" ] parameters["text"] = message Alamofire.request(.GET,"http://gateway-a.watsonplatform.net/calls/text/TextGetEmotion",parameters:parameters).responseJSON
SwiftyJSON makes it easy to parse through the resulting JSON payload using a subscript notation. The results for each of the different emotions are fed into an array which is loaded into ios-charts, a very powerful and flexible charts control available on github.
From the chart you can determine if your text is suitable for the audience or if there is too much sadness, anger, or disgust showing through. Click on the back button to go back and modify your text and re-run the analysis as necessary.
When you are satisfied with your message, simply tap on the share button to send it to mail, messages, Facebook, twitter, or any other application that is available through the share sheet.
A video showing the app in action is available here. Hopefully this gives you a good idea of the power of Watson APIs and how you can quickly harness it within your mobile app.