Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PubNub Sentiment Analysis UI w/ Lexalytics Semantria API
<!doctype html>
<html>
<head>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.5.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://cdn.pubnub.com/sdk/pubnub-angular/pubnub-angular-4.0.2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
</head>
<body>
<div class="container" ng-app="PubNubAngularApp" ng-controller="MyTextCtrl">
<pre>
NOTE: make sure to update the PubNub keys below with your keys,
and ensure that the sentiment analysis BLOCK is configured properly!
</pre>
<h3>MyText Sentiment Analysis</h3>
<input ng-model="toSend" />
<input type="button" ng-click="publish()" value="Send!" />
<br /><br />
<ul>
<li ng-repeat="message in messages track by $index">
text: {{message.source_text}}
<br />
sentiment: <span ng-bind-html="to_trusted(emote(message.sentiment_polarity))"></span> (score: {{message.sentiment_score}})
</li>
</ul>
</div>
<script>
angular.module('PubNubAngularApp', ["pubnub.angular.service"])
.controller('MyTextCtrl', function($rootScope, $scope, $sce, Pubnub) {
$scope.messages = [];
$scope.pubChannel = 'lexalytics-channel';
$scope.subChannel = 'semoutput';
if (!$rootScope.initialized) {
Pubnub.init({
publishKey: 'YOUR_PUB_KEY',
subscribeKey: 'YOUR_SUB_KEY',
ssl:true
});
$rootScope.initialized = true;
}
var msgCallback = function(payload) {
$scope.$apply(function() {
$scope.messages = payload.concat($scope.messages);
});
};
$scope.publish = function() {
Pubnub.publish({
channel: $scope.pubChannel,
message: {docs:[{text:$scope.toSend,id:parseInt(Math.random(5000000) * 10000000).toString()}]}
});
$scope.toSend = "";
};
$scope.emote = function(type) {
if (type === 'positive') {
return '<i class="fa fa-smile-o" aria-hidden="true"></i>';
} else if (type === 'negative') {
return '<i class="fa fa-frown-o" aria-hidden="true"></i>';
} else {
return '<i class="fa fa-meh-o" aria-hidden="true"></i>';
}
};
$scope.to_trusted = function(html_code) {
return $sce.trustAsHtml(html_code);
};
Pubnub.subscribe({ channels: [$scope.subChannel] });
Pubnub.addListener({ message: msgCallback });
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment