Continuing my series on how to leverage the various Machine Learning and AI services from Amazon, Azure and Google, previously we used Amazon’s Rekognition and Python to perform image recognition with KNIME.
Rather than having to custom code solutions, I can, in just a few minutes build a workflow or integrate into my existing workflows a comprehensive sentiment analysis service. More impressive, not a single line of code will be written.
Combining Twitter data and Azure Cognitive Services for Sentiment Analysis
We’ll be leveraging KNIME’s built in Twitter functionality, along with Microsoft Azure’s Cognitive Services.
Before we start, you’ll need;
- a Twitter Developer Account (Free)
- a Microsoft Azure Account (Free for up to 5,000 requests)
- KNIME Analytics Platform
Make sure you’ve written down your from your Twitter developer account the API key, API secret, Access token and Access token secret.
You’ll also need your Azure Cognitive Services subscription key that we’ll use with the Azure REST API.
Creating your Azure Cognitive Service
When you login to your Azure Portal. Navigate to Cognitive Services and we’ll create a new service for KNIME.
- Click add and search for the Text Analytics service.
- Click Create to provision your service giving it a name, location and resource group. You may need to create a new Resource group if this is your first Azure service.
- Once created, navigate to the Quick Start section under Resource Management where you can find your web API key and API endpoint. Save these as you’ll need them in your workflow.
The KNIME Workflow
You can download the KNIME workflow to follow along here;
The Workflow comprises of 3 parts;
- Pulling the data from Twitter and wrapping it into a JSON format compatible with the Cognitive Services API.
- Submitting that request via a POST Request.
- Taking the output and combining it with the Tweets metadata into a single table, colour coded and ranked by sentiment.
As an input, Azure is expecting the format;
"text": "I loved the meal"
"text": "I left hungry and unhappy"
"text": "The service was excellent"
KNIME includes excellent Twitter nodes (if you don’t have them, you can download them from KNIME extensions) this will allow you to quickly and easily connect to Twitter and download tweets. By using the Twitter Search node we can search for particular terms.
We can then restructure the response from Twitter into a form that Azure is expecting. The Constant Value Column and JSON Row Combiner nodes wrap the response with the documents elements and this is then submitted via the POST Request.
KNIME’s POST Request node makes it ridiculously easy to interact with REST services. You’ll need to grab the respective URL for your region, here in Australia the URL is;
We can leave the Authentication blank as we’ll be adding a couple of Request Headers.
We need to add for the Header Key;
and Header Value;
And another Header Key;
The Header Value for the Subscription Key will be the key provided as part of your Azure Cognitive Services you created.
If you’re using my KNIME Workflow as a guide, make sure to update the Twitter API Connector node with your Twitter API Key, API Secret, Access token and Access token secret.
You can modify this workflow to submit any text data you choose as long as there is an ID column and a text column.
Presenting the data
Once created you can use a Table View node to display the information in an interactive table, sorted by sentiment . This can be distributed to PR and Social Media teams for action, improving customer service.
To really supercharge your KNIME deployment and make this service truly accessible, you can use KNIME Server’s Web Portal to create an interactive online sentiment service for your social media team allowing them to refresh reports, submit their own twitter queries or provide alerting so your team can jump on issues.
As always, any questions, please feel free to message me on Twitter or LinkedIn.