stream.on('data', function(tweet) { var result = sentiment(tweet.text); console.log('[' + result.score + ']', tweet.text); });
s_afinn = user.tweets.reduce(function (acc, tweet) { tweet.sentiment_afinn = sentiment(tweet.text).score; return (acc + tweet.sentiment_afinn); }, 0) / user.tweets.length;
router.get('/analyze', function (req, res, next) { var tweet = decodeURIComponent(req.query.tweet); var analyzed = sentiment(tweet); res.status(200).send(analyzed); });
/** * Renders display-only data for a match **/ function renderMatch(m) { //do render-only processing (not needed for aggregation, only for match display) m.players.forEach(function(p, i) { //converts hashes to arrays and sorts them var targets = ["ability_uses", "item_uses", "damage_inflictor"]; targets.forEach(function(target) { if (p[target]) { var t = []; for (var key in p[target]) { var a = constants.abilities[key]; var i = constants.items[key]; var def = { img: "/public/images/default_attack.png" }; def = a || i || def; var result = { img: def.img, name: key === "undefined" ? "Auto Attack/Other" : key, val: p[target][key], className: a ? "ability" : i ? "item" : "img-sm" }; if (p.hero_hits) { result.hero_hits = p.hero_hits[key]; } t.push(result); } t.sort(function(a, b) { return b.val - a.val; }); p[target + "_arr"] = t; } }); //filter interval data to only be >= 0 if (p.times) { var intervals = ["lh_t", "gold_t", "xp_t", "times"]; intervals.forEach(function(key) { p[key] = p[key].filter(function(el, i) { return p.times[i] >= 0; }); }); } }); if (m.chat) { //make a list of messages and join them all together for sentiment analysis var chat_words = m.chat.map(function(message) { return message.key; }).join(' '); m.sentiment = sentiment(chat_words, { "report": -2, "commend": 2, "noob": -2, "ff": -1, "bg": -1, "feed": -1, "ty": 1, "thanks": 1, "wp": 1, "end": -1, "garbage": -1, "trash": -1, "throw": -1, "salt": -1, "ez": -1, "mad": -1 }); } //create gold breakdown data if (m.players[0].gold_reasons) { m.incomeData = generateIncomeData(m); m.treeMapData = generateTreemapData(m); } //create graph data if (m.players[0].gold_t) { m.graphData = generateGraphData(m); } //create heatmap data m.posData = m.players.map(function(p) { return p.posData; }); //process objectives if (m.objectives) { m.objectives.forEach(function(entry) { entry.objective = constants.objectives[entry.subtype] || entry.subtype; var p = m.players[entry.slot]; if (p) { entry.team = entry.team === 2 || entry.key < 64 || p.isRadiant ? 0 : 1; entry.hero_img = constants.heroes[p.hero_id] ? constants.heroes[p.hero_id].img : ""; } }); } //process teamfight data if (m.teamfights) { m.teamfights.forEach(function(tf) { tf.posData = []; tf.radiant_gold_delta = 0; tf.radiant_xp_delta = 0; tf.radiant_participation = 0; tf.radiant_deaths = 0; tf.dire_participation = 0; tf.dire_deaths = 0; tf.players.forEach(function(p) { //lookup starting, ending level p.level_start = getLevelFromXp(p.xp_start); p.level_end = getLevelFromXp(p.xp_end); function getLevelFromXp(xp) { for (var i = 0; i < constants.xp_level.length; i++) { if (constants.xp_level[i] > xp) { return i; } } return constants.xp_level.length; } }); //add player's hero_id to each teamfight participant m.players.forEach(function(p, i) { var tfplayer = tf.players[p.player_slot % (128 - 5)]; tfplayer.hero_id = p.hero_id; tfplayer.player_slot = p.player_slot; tfplayer.isRadiant = isRadiant(p); tfplayer.personaname = p.personaname; tfplayer.account_id = p.account_id; tfplayer.participate = tfplayer.deaths > 0 || tfplayer.damage > 0; if (!p.teamfights_participated) { p.teamfights_participated = 0; } p.teamfights_participated += tfplayer.participate ? 1 : 0; //compute team gold/xp deltas if (isRadiant(p)) { tf.radiant_gold_delta += tfplayer.gold_delta; tf.radiant_xp_delta += tfplayer.xp_delta; tf.radiant_participation += tfplayer.participate ? 1 : 0; tf.radiant_deaths += tfplayer.deaths ? 1 : 0; } else { tf.radiant_gold_delta -= tfplayer.gold_delta; tf.radiant_xp_delta -= tfplayer.xp_delta; tf.dire_participation += tfplayer.participate ? 1 : 0; tf.dire_deaths += tfplayer.deaths ? 1 : 0; } //convert 2d hash to array tfplayer.posData = generatePositionData({ deaths_pos: 1 }, tfplayer); //console.log(player); //add player hero id to each death, push into teamfight death position array tfplayer.posData.deaths_pos.forEach(function(pt) { pt.hero_id = tfplayer.hero_id; tf.posData.push(pt); }); }); }); } }
stream.on('data', function(data) { console.log(data); var text = data.text; var sentimentResult = sentiment(text); console.log(sentimentResult); });
async.each(workers, function(item, callback) { // Check if the worker has timed out (Every worker has a 48hour lifetime) if (item.validateTimeout() === true) { // Emit the new status (Worker timed out) io.sockets. in (item.id).emit('statusChanged', storage.getWorkerById(item.id).getStatus()); // Delete the worker storage.deleteWorker(item.id); // Delete the job Job .findOne({ _id: item.id }) .exec(function(err, job) { if (err) console.log(err); if (!job) console.log(new Error('Failed to find Job ' + item.id)); // Remove the job job.remove(function(err) { if (err) console.log(err); console.log(new Date() + " Job " + item.id + " was delete because of timeout!"); }); }); return false; } // Check if the worker is active if (item.active === true) { // Check if the worker timer has not expired if (item.validateTimer()) { // Check for the right location if(item.checkLocation(tweet) === false) { return false; } // Check if the tweet contains any keyword if(item.checkKeywords(tweet.text) === false) { return false; } // Analyse the tweet senti(tweet.text, function(err, result) { // Let the worker calculate the results item.analyse(result, function(data) { // Emit the statistics to the user io.sockets. in (item.id).emit('statistics', data); }); // Add the sentiment result to the tweet object tweet.sentiment = result; // Store the new tweet in the active worker item.pushData(tweet); // Emit the new tweet to the user io.sockets. in (item.id).emit('tweet', tweet); }); // Worker has finished } else { // Emit the new status (Worker finished) io.sockets. in (item.workerId).emit('statusChanged', item.getStatus()); // Delete the worker storage.deleteWorker(item.id); // Find and save the job Job .findOne({ _id: item.id }) .exec(function(err, job) { if (err) console.log(err); if (!job) console.log(new Error('Failed to find Job ' + item.id)); // Update the job job.status = item.getStatus(); job.filterData = item.getFilterData(); job.streamData = item.getStreamData(); job.sentimentData = item.getSentimentData(); job.timer = item.getTimer(); job.save(function(err) { if (err) console.log(err); console.log(new Date() + " Job " + item.id + " was saved"); }); }); } } callback(); }, function(err) {
stream.on(channel, function(tweet) { cache[tag].total++; cache[tag].sentiment += sentiment(tweet.text).score; console.log(tweet.text); });
let sentimentnorm = _.mean(_.map(group, post => { let sent = Sentiment(post.raw); let num = sent.positive + sent.negative; if (num == 0) return 0; return sent.score / (num); }));
const analyze = (textObject, queryString) => { //console.log("MEE",textObject) queryString = Array.isArray(queryString)?queryString[0]:queryString; topics={}; let rankingHolder = []; let negativeSentiments = 0; let positiveSentiments = 0; let maxSentimentImpact = {actualTweet:'', sentimentStrength:0, votes:0, posNeg:""}; for (var postKey in textObject) { let curr = textObject[postKey]; let content = curr.content; for (var contentKey in content) { //console.log(content[contentKey].text) nlpAnalysis(content[contentKey].text); let currentSentiment = sentiment(content[contentKey].text); if (currentSentiment.positive.length != 0 || currentSentiment.negative.length != 0) { content[contentKey].sentiment = currentSentiment; if(currentSentiment.score<0) negativeSentiments++ if(currentSentiment.score>0) positiveSentiments++ content[contentKey].sentiment.w_rank = ranking(content[contentKey], textObject[postKey].author); let rankProp = content[contentKey].sentiment.w_rank[1]; if(maxSentimentImpact.sentimentStrength <rankProp){ maxSentimentImpact.sentimentStrength = content[contentKey].sentiment.w_rank[0]; maxSentimentImpact.actualTweet = content[contentKey].text; maxSentimentImpact.votes = content[contentKey].vote_count; maxSentimentImpact.posNeg = content[contentKey].sentiment<0?"Negative":"Positive" } rankingHolder.push(content[contentKey].sentiment.w_rank); }else{ delete textObject[postKey].content; } } } //console.log(JSON.stringify(textObject)) let normalData = normal_dist_data_filter(rankingHolder); negativeSentiments = negativeSentiments/rankingHolder.length positiveSentiments = positiveSentiments/rankingHolder.length normalData.percentPositive = positiveSentiments; normalData.percentNegative = negativeSentiments; let greater = normalData.mean + (2*normalData.standardDeviation); //console.log(normalData) let less = normalData.mean - (2*normalData.standardDeviation); normalData.set = normalData.set.filter(function(curr){ return curr[0]<greater && curr[0]>less; }); let data_W_analysis = { data: textObject } //extendOn(data_W_analysis, [normalData,maxSentimentImpact]); var sortable = []; for (var key in topics){ //TODO FIX FILTERING, IM JUST RUSHING if( key.trim().indexOf(" ")==-1&&key.trim().indexOf("'")==-1&&key.trim().indexOf(".")==-1&&key.trim().indexOf("@")==-1) sortable.push([key, topics[key]]) } sortable = sortable.sort( function(a, b) { return b[1] - a[1]; } ).slice(1,7); //console.log("HERE",sortable.slice(0,20)); for (let dataKey in normalData) { data_W_analysis[dataKey] = normalData[dataKey] } for (let dataKey in maxSentimentImpact) { data_W_analysis[dataKey] = maxSentimentImpact[dataKey] } var trendingFiltered = []; for(var key in sortable){ if(sortable[key][0].trim().toLowerCase().indexOf(queryString.trim().toLowerCase())==-1) trendingFiltered.push(sortable[key]); } data_W_analysis.trendingTopics = trendingFiltered; data_W_analysis.query = queryString; //console.log("DONE",data_W_analysis) //console.log("FINAL RETURN", trendingFiltered) return data_W_analysis; };
/** * Tally up the votes on a PR, and monitor which users are stargazers. */ function tallyVotes(pr) { var tally = pr.comments.reduce(function (result, comment) { var user = comment.user.login, body = comment.body; // Don't check comments from the config user. if (user === config.user) return result; // People that don't star the repo cannot vote. if (!cachedStarGazers[user]) { result.nonStarGazers.push(user); return result; } // Skip people who vote both ways. var voteCast = calculateUserVote(body); if (voteCast === null) { return result; } result.votes[user] = voteCast; return result; }, { votes: {}, nonStarGazers: [], }); // Add the PR author as a positive vote. tally.votes[pr.user.login] = true; // determine whether I like this PR or not (or don't care) var score = sentiment(pr.title + ' ' + pr.body).score; if (score > 1) { tally.votes[config.user] = true; } else if (score < -1) { tally.votes[config.user] = false; } var tallySpread = Object.keys(tally.votes).reduce(function (result, user) { // Increment the positive/negative counters. if (tally.votes[user]) { result.positive++; } else { result.negative++; } result.total++; return result; }, { positive: 0, negative: 0, total: 0 }); tallySpread.percentPositive = percent(tallySpread.positive / tallySpread.total); tallySpread.percentNegative = percent(tallySpread.negative / tallySpread.total); _.merge(tally, tallySpread); // Store this so that we can eventually make a votes webserver endpoint. votesPerPR[pr.number] = tally; return tally; }
var saveTweet = function(tag, tweet){ var sentimentData = sentiment(tweet.text); if(sentimentData.score !== 0 ) { twitterModels.Tweet.create({ tweet: tweet.text, sentiment: sentimentData.score, tag: tag}); } };
response.on('end', function() { var allArticles = ''; var responseFull = JSON.parse(body); var articles = responseFull.response.results.length; for (var i = articles - 1; i >= 0; i--) { var sentence = responseFull.response.results[i].fields.body; // get text //console.log(sentence); var stripped = sentence.replace(/(<([^>]+)>)/g,""); // strip html allArticles += stripped; }; var analysed = sentiment(allArticles); Mood.findOne(4).done(function(err, item) { // Error handling if (err) { return console.log(err); // The item was found successfully! } else { item.value = parseInt(item.value) + 1; // add sentiment to news total item.save(function (err) { if (err) return res.send(err,500); // Report back with the new state of the item Mood.publishUpdate( 4, { value: parseInt(item.value) }); }); } }); fs.writeFile(today, JSON.stringify({ numberwang: analysed.score, comparative: analysed.comparative, 'analysed articles': articles, date: date, 'positive words total': analysed.positive.length, 'negative words total': analysed.negative.length // , // positive: analysed.positive, // negative: analysed.negative, // body: responseFull }), function(err) { if(err) { console.log(err); } else { console.log(today +" was saved."); } }); return res.send({ numberwang: analysed.score, comparative: analysed.comparative, 'analysed articles': articles, 'positive words total': analysed.positive.length, 'negative words total': analysed.negative.length, // positive: analysed.positive, // negative: analysed.negative }) });
export default (state) => { const sentence = sentiment(state.sentence.toLowerCase()); state.sentiment = sentence.score; return(state); };
let sentiment = _.map(group, post => { let sent = Sentiment(post.raw); return sent.score; });
strings.forEach((string) => { sentences.push({ string: string, sentiment: sentiment(string) }); });
let sentimentcomp = _.map(group, post => { let sent = Sentiment(post.raw); return sent.comparative; });
stream.on('tweet', function(tweet) { let individualSent = sentiment(tweet.text) if (individualSent.score > 0) { pos++ } else if (individualSent.score < 0) { neg++ } else if (individualSent.score === 0) { neu++ } if (individualSent.score > 0) { scoreArray.push(individualSent.score) } else if (individualSent.score < 0) { scoreArray.push(individualSent.score * 1.35) } // push tweets to array streamedTweets.push(tweet.text) //Tweet location // if tweet has cooridnates if(tweet.coordinates) { //and if coordiantes don't = null if(tweet.coordinates != null) { //get location sentiment and lng / lat let outputPoint = {"lat": tweet.coordinates.coordinates[0],"lng": tweet.coordinates.coordinates[1]}; let locSentiment = sentiment(tweet.text) // push to the location array tweetsWithLoc.push({"tweet": tweet.text, "location": outputPoint, "sentiment": locSentiment}); } } // When there are 40 tweets push to client if(tweetsWithLoc.length === 80) { try { wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { client.send(JSON.stringify({"location":tweetsWithLoc})); } }); } catch(e) { console.log(e) } // resset array tweetsWithLoc.length = 0; } //send streamed tweets off when the array hits 50 tweets if(streamedTweets.length === 50) { scoreArrayLength = scoreArray.length average = scoreArray.reduce((a, b) => a + b, 0); average = average / scoreArrayLength; average = average.toFixed(4); average = parseFloat(average) //join all the tweets in the array to a single string let trumpSentiment = sentiment(streamedTweets.join()); let words = countThem(trumpSentiment.tokens) // FIRE!! try { wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { client.send(JSON.stringify({"main": {"sentiment": trumpSentiment, "featuredTweet": streamedTweets[19], "pos": pos, "neg": neg, "neu": neu, "average": average, "words":words}})) } }); } catch(e) { console.log(e) } //clear array & start over scoreArrayLength = 0; scoreArray.length = 0; streamedTweets.length = 0; pos = 0 neg = 0 neu = 0 } })
const sentiments = tweetStream.map((tweet) => { return sentiment(tweet.text); });
getSentiment(text){ var score = sentiment(text).score; return Math.tanh(score); }
function score(sentence, callback){ var value = Mood(sentence); //console.log(value.score); var score = value.score; callback(score); }
$('.ProfileTweet-text').toArray().forEach(function(item) { var text = $(item).text(); var results = sentiment(text); console.log(sentimentToSmiley(results), '-', text.replace(/\n/g, ' ')); });
var sentiments = messages.map(function(message){ return { message: message, score: analyzeSentiment(message).score }; })
fs.readFile('books/alice_scrubbed', 'utf-8', (err, data) => { if (err) throw err; textArray = data.split('.'); for (i =0; i < 3; i++) { var blah = ''; var blah2 = ''; var blah3 = ''; //console.log(sentiment(textArray[i])) score[i] = sentiment(textArray[i]).score; alchemy.sentiment(textArray[i], {}, function(err, response) { if (err) throw err; // See http://www.alchemyapi.com/api/ for format of returned object alchemySentiment = response.docSentiment; console.log(JSON.stringify(alchemySentiment)); // Do something with data alchemyScore.push( JSON.stringify(alchemySentiment)); }) if (score[i] % 7 === 0) { blah += 'c'; } else if (score[i] % 7 == 1 || score[i] % 7 == -1) { blah += 'd'; } else if (score[i] % 7 == 2 || score[i] % 7 == -2) { blah += 'e'; } else if (score[i] % 7 == 3 || score[i] % 7 == -3) { blah += 'f'; } else if (score[i] % 7 == 4 || score[i] % 7 == -4) { blah += 'g'; } else if (score[i] % 7 == 5 || score[i] % 7 == -5) { blah += 'a'; } else if (score[i] % 7 == 6 || score[i] % 7 == -6) { blah += 'b'; } else { console.log('wut'); } if (score[i] < -5) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < -4) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < -3) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < -2) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < -1) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < 0) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < 1) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < 2) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < 3) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else if (score[i] < 4) { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } else { blah2 = blah + 4; blah3 = blah + 6; blah += 2; } chord = [blah, blah2, blah3]; music.push(chord); } for (var j = 0; j < music.length; j++ ) { track.note(0, music[j][0], 64); track.noteOn(0, music[j][1]); track.noteOn(0, music[j][2]); } fs.writeFileSync('test.mid', file.toBytes(), 'binary'); });