Analyze tweets in under 30 minutes using Node-RED

By: Sean Franklin

Analyze tweets in under 30 minutes using Node-RED

I created a Node-RED app in Bluemix which searches for a specified keyword, username, or hashtag phrase in the public Twitter universe whenever a new tweet is posted. This application can be created in less then 30 minutes by anyone and requires no coding experience. When a tweet is posted that matches the provided search criteria, the tweet is passed through the sentiment analysis node which returns a sentiment value.

icon

If that sentiment value has a value less than -5 (which is a very negative tweet) then the app sends me an email with the word “tweets/” and the username who wrote the tweet in the subject header and the actual tweet in the body. This allows me to catch any very negative tweets about the search terms within a couple of seconds.

Below is an example email alert:

email alert

Quick introduction to creating Node-RED applications

A Node-RED app is created by dragging and dropping nodes—pre-written pieces of code that perform desired actions—on the left hand side of the Node-RED editor into the middle work space. The nodes are then connected by clicking the output point of one node and dragging it to the input point of another node (the little grey squares on either side of a node):

Node-RED applications

Lastly and most importantly, each node has to be given the proper parameters by double clicking on it and filling in the form that pops up:

pops up

Once those steps are completed, you click the Deploy button on the top right corner of the editor and your application will be running. Simply stated, Node-RED applications are just programmable nodes that you wire together.

My Node-RED tweet sentiment app

As shown earlier, my entire Node-RED application consists of just five nodes. The first node will search Twitter for the desired keywords entered into the form after double clicking on the node. You will need to authenticate to your Twitter username, which will allow the application to search all public tweets. To specify the search keywords fill in the “for” parameter with a word, username, or hashtag keyword that you want to retrieve all tweets containing that term (I used #testnegative as my keyword for testing purposes).

Next I added the sentiment analysis node and connected its input to the output of the Twitter node. This node will analyze all tweets that match the search term and assign them a sentiment value (this node does not need any parameters unless you want to change the name of the node itself). Average sentiment values will range from -5 to 5 with -5 being a very negative tweet, 0 being a neutral tweet and 5 being a very positive tweet. Anything outside of those ranges are extreme values and the tweets I want to capture. I then added a debug node called “debug sentiment value” to print out the sentiment value to the Node-RED debug log on the right side of the editor (this node is not required but helps to see sentiment values).

Below are some example sentiment values based on the given tweet:

  • Sentiment value of 4: #testnegative Jim’s fries is awesome!

  • Sentiment value of -3: #testnegative Jim’s fries is the worst restaurant ever!

  • Sentiment value of -4: #testnegative Jim’s fries is the worst restaurant ever! I could not pay with a debit card!

  • Sentiment value of -5: #testnegative Jim’s fries is the worst restaurant ever! I could not pay with a debit card! No one should ever go here!

  • Sentiment value of -8: #testnegative Jim’s fries is the worst restaurant ever! I could not pay with a debit card! No one should ever go here! Horrible service!

The sentiment value is then passed to a “switch” node which looks for tweets with a sentiment value that is less than or equal to -5. In the diagram above, noted that I renamed my switch node to “Analyze how negative”. If the sentiment value falls into this category, then an email is sent from my Gmail account to my work email:

work email

Conclusion

That is the entire application; as you can see, there is absolutely no coding necessary. The only requirements are to drag and drop nodes, fill them out, and connect them. This app idea can be transformed in many ways. The app can be used to find very positive tweets, the app can send specified tweets containing multiple keywords to a Slack chat, the Twilio node can be integrated to have tweets be sent to your cell phone as a text, and many more implementations to analyze any tweet by anyone within a couple of seconds of it being posted. To try out Node-RED on Bluemix, go to the Bluemix Node-RED Page after you have signed up for your free 30 day Bluemix trial.

Be the first to hear about news, product updates, and innovation from IBM Cloud