IBM Bluemix http://ibm.biz/bluemixph A platform where developers can act like kids in a sandbox except this box is enterprise-grade. This laboratory exercise demonstrates how to perform sentiment analysis using Bluemix. Prerequisite Item Getting Twitter Messages using Bluemix Laboratory Exercise Comment the Getting Twitter Messages and Saving to MongoDB using Bluemix laboratory exercise should be completed already so that the needed twitter node is already existing. Procedure 1. Open a web browser. Go to http://twitterapp-<your name>.mybluemix.net/red to open the Node- RED flow editor of your twitterapp-<your name> application. 1
2. In the Node palette, drag the following nodes (use the screenshot below as a guide for the position of the nodes): sentiment input/output node function input/output node debug output node 3. Click (not double-click) the sentiment input/output node. Read the information about the sentiment input/output node from the info tab in the right pane. 2
4. Double-click the function input/output node in the work area. 5. In the Edit function node window, type (or copy and paste) the following code in the function text area: var d = new Date(); var utc = d.gettime() - (d.gettimezoneoffset() * 60000); var nd = new Date(utc + (3600000*+8)); var year = nd.getfullyear(); var month = nd.getmonth()+1; // 1-Jan, 2-Feb, etc. var day = nd.getdate(); var hour = nd.gethours(); var minute = nd.getminutes(); var second = nd.getseconds(); var newmsg = { payload: { tweet: msg.payload, score: msg.sentiment.score, year: year, month: month, day: day, hour: hour, minute: minute, second: second } }; return newmsg; 3
Click Ok. 6. Connect the following: twitter output node to sentiment input/output node sentiment input/output node to function input/output node function input/output node to debug node 4
7. Click the switch of the debug output node that is directly connected to the twitter output node. This will disable this debug output node. Since there are two debug output nodes, disabling one of them makes it easier to understand the output displayed in the debug tab when viewed later. 8. Click the Deploy button. 9. Make sure that you see a Successfully deployed message. 5
10. In the right pane, click the debug tab. 11. Click the clear log button to clear the contents of the debug tab. 6
12. Wait for Twitter messages to appear. Take note that aside from the actual tweet, the score (i.e, sentiment score), and time stamp (in year, month, day, hour, minute, and second) are also displayed. Once you are done with this exercise, you might want to change the key word configured in the twitter node (e.g., instead of using the keyword bluemix change it to another word you want to track). 13. Drag and drop a mongodb output node to the work area at the right of the function node and at the top of the debug node. 7
14. Double-click the mongodb output node (the one that is at the right of the function node). 15. In the Edit mongodb out node window, use the following values: Service: MongoLab-twitter Collection: Sentiment Operation: save Only store msg.payload object: yes Click Ok. 16. Connect the function input/output node to the mongodb output node. 8
17. Click the Deploy button. 18. All the sentiment scores coming from the function input/output node are now being saved to your MongoLab-twitter database through the mongodb output node. Take note that in the Getting Twitter Messages using Bluemix exercise, the same MongoLab-twitter database is used to save the tweets. In order for the results of the Getting Twitter Messages using Bluemix exercise not to mix with the current exercise, a different collection is used. In the Getting Twitter Messages using Bluemix exercise, the collection used is Tweet. On the other hand, the current exercise uses a collection called Sentiment. You can create another Bluemix application to access the contents of the MongoLab-twitter database. You may use the Uploading and Running a PHP Web Application (with MongoDB) in Bluemix) exercise as a guide to accomplish this. However, the sample PHP application (mongodbphp.zip) that is used in the Uploading and Running a PHP Web Application (with MongoDB) in Bluemix) exercise displays the Tweet collection and not the Sentiment collection. You need to do some modifications to the sample PHP application to display the contents of the Sentiment collection. Discussed below is an alternative way to view the contents of the Sentiment collection in the MongoLab-twitter database. 9
19. In the Node palette, drag the following nodes (use the screenshot below as a guide for the position of the nodes): http input node monogodb input/output node http response output node 20. Double-click the http input node in the work area. 21. In the Edit http in node window, use the following values: Method: GET url: /view_sentiments Click Ok. 22. Double-click the mongodb input/output node in the work area. 10
23. In the Edit mongodb in node window, use the following values: Service: MongoLab-twitter Collection: Sentiment Click Ok. 24. Connect the http input node to the mongodb input/output node. Connect the mongodb input/output node to the http response output node. 25. Click the Deploy button. 11
26. Open another web browser window (or tab). Go to the following URL: http://twitterapp-<your name>.mybluemix.net/view_sentiments 27. You should see the contents of the Sentiment collection of the MongoLab-twitter database. 12